Skip to content

Instantly share code, notes, and snippets.

@greenmang0
Last active May 23, 2018 22:46
Show Gist options
  • Save greenmang0/5619339 to your computer and use it in GitHub Desktop.
Save greenmang0/5619339 to your computer and use it in GitHub Desktop.
Check percentage of number of file descriptors opened by a process out of allocated file descriptors.
#!/bin/bash
############################################################
# Based On
# http://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/Check-Open-FDs-%28File-Descriptors%29/details
###########################################################
USAGE="$(basename $0) ([-w]<warn %>)([-c]<crit %>)([-p]<port number>)"
THRESHOLD_USAGE="CRITICAL threshold must be greater than WARNING: $(basename $0) $*"
while getopts "hw:c:p:" OPTION; do
case $OPTION in
h)
echo $USAGE
exit 0
;;
w)
WARN=$OPTARG
;;
c)
CRIT=$OPTARG
;;
p)
PORT=$OPTARG
;;
esac
done
# find PID of a process listening on PORT
PID=$(sudo lsof -i:$PORT -sTCP:LISTEN -t)
# count number of FDs opened by above PID
OPEN_FD=$(sudo ls -1 /proc/$PID/fd/ | wc -l)
# max FDs allocated for a process
MAX_FD=$(sudo grep "Max open files" /proc/$PID/limits | awk '{print $4}')
WARN=$[${MAX_FD}/100*${WARN}]
CRIT=$[${MAX_FD}/100*${CRIT}]
OPEN_PERC=$[$OPEN_FD*100/${MAX_FD}]
WARN_PERC=$[$WARN*100/${MAX_FD}]
CRIT_PERC=$[$CRIT*100/${MAX_FD}]
# verify input
[[ $WARN -ge $CRIT ]] && echo -e "\n$THRESHOLD_USAGE\n\nUsage: $USAGE\n" && exit 0
TEXT="$OPEN_FD ($OPEN_PERC%) of $MAX_FD allowed file descriptors open|WARNING = $WARN ($WARN_PERC%), CRITICAL = $CRIT ($CRIT_PERC%)"
[[ $OPEN_FD -le $WARN ]] && echo "OK - $TEXT" && exit 0
[[ $OPEN_FD -gt $WARN ]] && echo "WARNING - $TEXT" && exit 1
[[ $OPEN_FD -gt $CRIT ]] && echo "CRITICAL - $TEXT" && exit 2
@greenmang0
Copy link
Author

README

DO NOT MAKE ANY MISTAKE WHILE EDITING SUDOERS FILE. WRONG SUDOERS CONFIGURATION DISABLES SUDO FUNCTIONALITY

Since nagios-nrpe-server runs as a user nagios, we need to give sudoers privileges to nagios on certain commands. To do that add a file called /etc/sudoers.d/nagios and put following contents in it

# Passwordless sudo functionality for nagios user on specific commands
nagios ALL=NOPASSWD:/usr/bin/lsof,/bin/ls,/bin/grep

And make sure you change the file permissions.

$ chmod 0440 /etc/sudoers.d/nagios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment