nagios tomcat open files check

here is a quick and dirty little script to see how many open files tomcat is using up.

#!/bin/bash
USER=tomcat
LSOF=`lsof -u $USER | wc -l`
OPEN_F=`ulimit -a | egrep "open files" | awk '{print $4}'`
#PERCDONE=$(( $LSOF / $OPENF * 100 ))
PERCDONE_PRE=$(echo "scale=2;(($LSOF/$OPEN_F) * 100)" |bc)
PERCDONE=`echo $PERCDONE_PRE | cut -d. -f1`

if [ $PERCDONE -lt 84 ]; then
  printf "OK  -- $PERCDONE %% with $LSOF files open\n"
  exit 0
else
  if [ $PERCDONE -ge 85 ] && [ $PERCDONE -le 94 ]; then
    printf "WARN -- $PERCDONE %% with $LSOF files open\n"
    exit 1
  elif [ $PERCDONE -ge 95 ]; then
    printf "CRIT -- $PERCDONE %% with $LSOF files open\n"
    exit 2
  fi
fi
«
»
  • I made a few quick mods to this that make it a bit more flexible. Still very much in beta, but it seems to work ok with minimal testing:

    #!/bin/bash
    # Stolen from http://pissedoffadmins.com/?p=650
    # Modified by Aaron McKinnon

    PROGNAME=`/bin/basename $0`

    print_usage() {
    echo “Usage: $PROGNAME -u username -w -c ”
    echo “Usage: $PROGNAME –help”
    echo “Example: $PROGNAME -u tomcat -w 85 -c 95”
    }

    # Make sure the correct number of command line
    # arguments have been supplied
    if [ $# -lt 3 ]; then
    print_usage
    exit $STATE_UNKNOWN
    fi

    # Grab the command line arguments
    exitstatus=$STATE_WARNING #default
    while test -n “$1”; do
    case “$1” in
    –help)
    print_help
    exit $STATE_OK
    ;;
    -h)
    print_help
    exit $STATE_OK
    ;;
    -u)
    USER=$2
    shift
    ;;
    -w)
    WARN=$2
    shift
    ;;
    -c)
    CRIT=$2
    shift
    ;;
    *)
    echo “Unknown argument: $1”
    print_usage
    exit $STATE_UNKNOWN
    ;;
    esac
    shift
    done

    # Idiot check that the user exists
    id $USER> /dev/null
    RUSER=`echo $?`
    if [ “$RUSER” != “0” ]; then
    echo -e “\nUser does not exist!\n”
    exit
    fi

    # The actual check
    LSOF=`lsof -u $USER | wc -l`
    OPEN_F=`ulimit -a | egrep “open files” | awk ‘{print $4}’`
    PERCDONE_PRE=$(echo “scale=2;(($LSOF/$OPEN_F) * 100)” |bc)
    PERCDONE=`echo $PERCDONE_PRE | cut -d. -f1`

    if [ $PERCDONE -lt $WARN ]; then
    printf “OK — $PERCDONE %% with $LSOF files open\n”
    exit 0
    else
    if [ $PERCDONE -ge $WARN ] && [ $PERCDONE -le $CRIT ]; then
    printf “WARN — $PERCDONE %% with $LSOF files open\n”
    exit 1
    elif [ $PERCDONE -ge $CRIT ]; then
    printf “CRIT — $PERCDONE %% with $LSOF files open\n”
    exit 2
    fi
    fi

    Thanks for the jumping off point!

    -Aaron

  • Nice work.

    I think you has to change the line:

    OPEN_F=`ulimit -a | egrep “open files” | awk ‘{print $4}’`

    to

    OPEN_F=`su -m $USER -c “ulimit -n”`

    su -m nexus -c “ulimit -n”

      • OPEN_F=`su -m $USER -c “ulimit -n”` has to be changed to:

        OPEN_F=`sudo su -m $USER -c “ulimit -n”`

        Don´t forget to edit your sudoers file!

      • OPEN_F=`su -m $USER -c “ulimit -n”` has to be changed to:

        OPEN_F=`sudo su -m $USER -c “ulimit -n”`

        Don´t forget to edit your sudoers file!


Leave a Reply

Your email address will not be published. Required fields are marked *