2009
11.20

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
The following two tabs change content below.

Latest posts by cesar (see all)

6 comments so far

Add Your Comment
  1. 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”

    • ignore the last line – please.

      • 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!

  2. 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

    • very cool. i like what you did there.