Last active
August 2, 2021 06:01
-
-
Save isitavi/a1e3eae1d10c79535f0d208896a199e7 to your computer and use it in GitHub Desktop.
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 | |
# Command Structure for use this script, | |
# Command: | |
# ./nagios-port-monitoring.sh -p 5999 -w 3 -c 5 | |
# Result: | |
# CRITICAL- 0 Established connection on PORT 5999 | 'Usage'=0;3;5;0;10 | |
# Status Code | |
# STATE_OK=0 | |
# STATE_WARNING=1 | |
# STATE_CRITICAL=2 | |
# STATE_UNKNOWN=3 | |
# STATE_DEPENDENT=4 | |
help() { | |
cat << END | |
Usage : | |
check_port_monitoring.sh -p [Port] -w [VALUE] -c [VALUE] | |
OPTION DESCRIPTION | |
---------------------------------- | |
-h Help | |
-w [VALUE] Warning Threshold | |
-c [VALUE] Critical Threshold | |
-p [Path] Port Number | |
---------------------------------- | |
Note : [VALUE] must be an integer. | |
END | |
} | |
while getopts "p:w:c:" opt | |
do | |
case "$opt" in | |
p ) PORT="$OPTARG" ;; | |
w ) WARN="$OPTARG" ;; | |
c ) CRIT="$OPTARG" ;; | |
*) help ;; | |
esac | |
done | |
# Print helpFunction in case parameters are empty | |
if [ -z "$PORT" ] | |
then | |
echo "Some or all of the parameters are empty"; | |
help | |
fi | |
# Warning threshold must be less than Critical threshold | |
if [ $WARN -ge $CRIT ] | |
then | |
echo "ERROR : Warning threshold must be less than Critical threshold." | |
exit 3 | |
fi | |
# Command | |
result=`netstat -pant | grep :$PORT | egrep -c 'LISTEN|ESTABLISHED'` | |
OUTPUT=`echo $result Established connection on PORT $PORT` | |
WARN_LIMIT_FOR_OK=$(($WARN-1)) | |
# echo $WARN_LIMIT_FOR_OK | |
CRITICAL_LIMIT_FOR_WARN=$(($CRIT-1)) | |
# echo $CRITICAL_LIMIT_FOR_WARN | |
if [[ $result -ge 1 && $result -le $WARN_LIMIT_FOR_OK ]] | |
then | |
STATUS="OK"; | |
EXITSTAT=0; | |
elif [[ $result -ge $WARN && $result -le $CRITICAL_LIMIT_FOR_WARN ]] | |
then | |
STATUS="WARNING"; | |
EXITSTAT=1; | |
elif [[ $result -ge $CRIT ]] | |
then | |
STATUS="CRITICAL"; | |
EXITSTAT=2; | |
else | |
if [ $result -le 0 ] | |
then | |
STATUS="CRITICAL"; | |
EXITSTAT=2; | |
fi | |
fi | |
# Summary Data | |
# echo "'Usage'=${result};$WARN;$CRIT;0;100" && exit $STATE_OK | |
# echo "'Usage'=$STATUS- $OUTPUT;$WARN;$CRIT;0;100" && exit $STATE_OK | |
echo "$STATUS- $OUTPUT | 'Usage'=${result};$WARN;$CRIT;0;100" && exit $EXITSTAT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment