Last active
May 23, 2018 22:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
DO NOT MAKE ANY MISTAKE WHILE EDITING SUDOERS FILE. WRONG SUDOERS CONFIGURATION DISABLES SUDO FUNCTIONALITY
Since
nagios-nrpe-server
runs as a usernagios
, we need to givesudoers
privileges tonagios
on certain commands. To do that add a file called/etc/sudoers.d/nagios
and put following contents in itAnd make sure you change the file permissions.