-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_open_files.sh
executable file
·55 lines (47 loc) · 1.36 KB
/
check_open_files.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
#
# Inspired by: http://pissedoffadmins.com/nagios/nagios-tomcat-open-files-check.html
#
# Author: Gregor Binder
# Mail: [email protected]
SUDO=/bin/sudo
LSOF=/sbin/lsof
AWK=/bin/awk
WC=/bin/wc
ERROR_CODE=-1
if [ -z "$1" ]; then
echo "Usage: $0 username"
echo " username: Username to check for to much open files"
exit $ERROR_CODE
else
USER=$1
fi
function checkExitStatus {
if [ $1 -ne 0 ]; then
echo "!!! command failure !!! $2"
exit -1
fi
}
# check if the username is valid
$LSOF -u $USER &> /dev/null
checkExitStatus $? "Username wrong"
# check if ah PID is available for the user
PID=`ps -u $USER | tail -1 | awk '{print $1}'`
checkExitStatus $? "No PID found"
OPEN_F=`$SUDO cat /proc/$PID/limits | grep "open files" | awk '{print $5}'`
LSOF=`$SUDO $LSOF -u $USER | $WC -l`
PERCDONE_PRE=$(echo "scale=2;(($LSOF/$OPEN_F) * 100)" |bc)
PERCDONE=`echo $PERCDONE_PRE | cut -d. -f1`
if [ $PERCDONE -lt 84 ]; then
ERROR_CODE=0
printf "FILES OK - $PERCDONE %% with $LSOF files open|files=$LSOF;;;\n"
else
if [ $PERCDONE -ge 85 ] && [ $PERCDONE -le 94 ]; then
ERROR_CODE=1
printf "FILES WARN - $PERCDONE %% with $LSOF files open|files=$LSOF;;;\n"
elif [ $PERCDONE -ge 95 ]; then
ERROR_CODE=2
printf "FILES CRIT - $PERCDONE %% with $LSOF files open|files=$LSOF;;;\n"
fi
fi
exit $ERROR_CODE