Skip to content

Commit

Permalink
nrdp_checks: check_proc_fd: Make option processing flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
Máté Eckl committed Jul 26, 2019
1 parent 1181302 commit ea3336a
Showing 1 changed file with 60 additions and 37 deletions.
97 changes: 60 additions & 37 deletions nagios_plugins/check_proc_fd/check_proc_fd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,77 @@
## Created : <2014-12-17>
## Updated: Time-stamp: <2017-09-04 18:54:36>
##-------------------------------------------------------------------
if [ "$1" = "-w" ] && [ "$2" -gt "0" ] && \
[ "$3" = "-c" ] && [ "$4" -gt "0" ]; then
pidPattern=${5?"specify how to get pid"}

if [ "$pidPattern" = "--pidfile" ]; then
pidfile=${6?"pidfile to get pid"}
pid=$(cat "$pidfile")
elif [ "$pidPattern" = "--cmdpattern" ]; then
cmdpattern=${6?"command line pattern to find out pid"}
pid=$(pgrep -a -f "$cmdpattern" | grep -v check_proc_fd.sh | head -n 1 | awk -F' ' '{print $1}')
elif [ "$pidPattern" = "--pid" ]; then
pid=${6?"pid"}
else
echo "ERROR input for pidpattern"
exit 2
fi

if [ -z "$pid" ]; then
echo "ERROR: no related process is found"
exit 2
fi

# Note: nagios need use sudo to run lsof
fdcount=$(sudo lsof -p "$pid" | wc -l)

if [ "$fdcount" -ge "$4" ]; then
echo "CRITICAL: file opened by pid($pid) is $fdcount. It's more than $4|fd=$fdcount"
exit 2
elif [ "$fdcount" -ge "$2" ]; then
echo "WARNING: file opened by pid($pid) is $fdcount. It's more than $2|fd=$fdcount"
exit 1
else
echo "OK: file opened by pid($pid) is $fdcount|fd=$fdcount"
exit 0
fi

else
function print_help {
echo "check_proc_fd v1.0"
echo ""
echo "Usage:"
echo "check_proc_fd.sh -w <warn_MB> -c <criti_MB> <pid_pattern> <pattern_argument>"
echo "check_proc_fd.sh -w <warn_FD> -c <criti_FD> <pid_pattern> <pattern_argument>"
echo ""
echo "Below: If tomcat open more than 1024 file handler, send warning"
echo "check_proc_fd.sh -w 1024 -c 2048 --pidfile /var/run/tomcat7.pid"
echo "check_proc_fd.sh -w 1024 -c 2048 --pid 11325"
echo "check_proc_fd.sh -w 1024 -c 2048 --cmdpattern \"tomcat7.*java.*MaxPermSize\""
echo ""
echo "Copyright (C) 2014 DennyZhang ([email protected])"
}

while [ "$#" -gt 0 ]
do
opt=$1
case $opt in
-w)
warn_fd=$2
shift 2 # Past argument and value
;;
-c)
criti_fd=$2
shift 2 # Past argument and value
;;
--pidfile)
pidfile=$2
pid=$(cat "$pidfile")
shift 2 # Past argument and value
;;
--cmdpattern)
cmdpattern=$2
pid=$(pgrep -a -f "$cmdpattern" | grep -v `basename $0` | head -n 1 | awk -F' ' '{print $1}')
shift 2 # Past argument and value
;;
--pid)
pid=$2
shift 2 # Past argument and value
;;
*)
print_help
exit 3
;;
esac
done

num_re='^[0-9]+$'
if ! [[ "$warn_fd" =~ $num_re ]] || ! [[ "$criti_fd" =~ $num_re ]]
then
echo "ERROR: Warning or Critical level is not a number"
exit 3
fi

if [ -z "$pid" ]; then
echo "ERROR: no related process is found"
exit 2
fi

# Note: nagios need use sudo to run lsof
fdcount=$(sudo lsof -p "$pid" | wc -l)

if [ "$fdcount" -ge "$criti_fd" ]; then
echo "CRITICAL: number of files opened by pid($pid) is $fdcount. It's more than $criti_fd|fd=$fdcount"
exit 2
elif [ "$fdcount" -ge "$warn_fd" ]; then
echo "WARNING: number of files opened by pid($pid) is $fdcount. It's more than $warn_fd|fd=$fdcount"
exit 1
else
echo "OK: number of files opened by pid($pid) is $fdcount|fd=$fdcount"
exit 0
fi
## File - check_proc_fd.sh ends

0 comments on commit ea3336a

Please sign in to comment.