From d4213d7a4382f418a2e0eb84a253b5377ea48bd2 Mon Sep 17 00:00:00 2001 From: Dominik Riva Date: Wed, 25 Jan 2017 16:47:31 +0100 Subject: [PATCH 1/5] Using utils.sh from monitoring-plugins.org and added performance data This allows to check against ranges and graph the data. --- README.md | 1 + check_snmp_proc.sh | 110 ++++++++++++++++++++++++++------------------- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 6dd5831..04cffb5 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,5 @@
Print version and license information

This plugin uses the 'snmpwalk' command included with the NET-SNMP package. +
This plugin uses the [utils.sh from monitoring-plugins.org](https://github.com/monitoring-plugins/monitoring-plugins/blob/v2.2/plugins-scripts/utils.sh.in)
This nagios plugins comes with ABSOLUTELY NO WARRANTY. So, enjoy ;) diff --git a/check_snmp_proc.sh b/check_snmp_proc.sh index 3b7de74..48ad864 100755 --- a/check_snmp_proc.sh +++ b/check_snmp_proc.sh @@ -1,13 +1,13 @@ -#!/bin/bash +#!/bin/sh -# Plugin return codes -STATE_OK=0 -STATE_CRITICAL=2 -STATE_WARNING=1 -STATE_UNKNOWN=3 +# boilerplate from check_sensors +PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" +export PATH +PROGNAME=`basename $0` +PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'` +REVISION="1.2" -# Version -VERSION="1.1" +. $PROGPATH/utils.sh # Commands CMD_BASENAME=$(which basename) @@ -15,10 +15,6 @@ CMD_SNMPWALK=$(which snmpwalk) CMD_GREP=$(which grep) CMD_WC=$(which wc) -# Script name -SCRIPTNAME=`$CMD_BASENAME $0` - - #Default variables OID=.1.3.6.1.2.1.25.4.2.1.2 HOST="127.0.0.1" @@ -27,27 +23,23 @@ PROCN="snmpd" STATE=$STATE_UNKNOWN WARNING=0 CRITICAL=0 - +PERFDATA="" +MIN=0 +MAX=100 print_usage() { echo "Usage: ./check_snmp_proc -H 127.0.0.1 -C public -N ssh -w 3 -c 0" - echo " $SCRIPTNAME -H ADDRESS" - echo " $SCRIPTNAME -C STRING" - echo " $SCRIPTNAME -N STRING" - echo " $SCRIPTNAME -w INTEGER" - echo " $SCRIPTNAME -c INTEGER" - echo " $SCRIPTNAME -h" - echo " $SCRIPTNAME -V" -} - -print_version() { - echo $SCRIPTNAME version $VERSION - echo "" - echo "This nagios plugins comes with ABSOLUTELY NO WARRANTY." + echo " $PROGNAME -H ADDRESS" + echo " $PROGNAME -C STRING" + echo " $PROGNAME -N STRING" + echo " $PROGNAME -w INTEGER" + echo " $PROGNAME -c INTEGER" + echo " $PROGNAME -h" + echo " $PROGNAME -V" } print_help() { - print_version + print_revision $PROGNAME $REVISION echo "" print_usage echo "" @@ -63,15 +55,20 @@ print_help() { echo " Warning level of running processes (default: 0)" echo "-c INTEGER" echo " Critical level of running processes (default: 0)" + echo "-m INTEGER" + echo " Minimum for performance data (default: 0)" + echo "-M INTEGER" + echo " Maximum for performance data (default: 100)" echo "-h" echo " Print this help screen" echo "-V" echo " Print version and license information" echo "" echo "This plugin uses the 'snmpwalk' command included with the NET-SNMP package." + echo "This plugin uses the utils.sh from monitoring-plugins.org." } -while getopts H:C:N:w:c:Vh OPT +while getopts H:C:N:w:c:m:M:Vh OPT do case $OPT in H) HOST="$OPTARG" ;; @@ -79,32 +76,55 @@ do N) PROCN="$OPTARG" ;; w) WARNING=$OPTARG ;; c) CRITICAL=$OPTARG ;; + m) MIN=$OPTARG ;; + M) MAX=$OPTARG ;; h) print_help - exit $STATE_UNKNOWN + exit $STATE_OK ;; V) - print_version - exit $STATE_UNKNOWN + print_revision $PROGNAME $REVISION + exit $STATE_OK ;; esac done #Plugin -PROCN=${PROCN:0:15} +#PROCN=${PROCN:0:15} +PROCN=`echo $PROCN | cut -c -15` + +#echo $PROCN + CNT=`$CMD_SNMPWALK -v1 -On -c $COMM $HOST $OID | $CMD_GREP "\"$PROCN\"" | $CMD_WC -l` -if [ $CNT -eq 0 ]; then - STATE=$STATE_CRITICAL - DESCRIPTION="CRITICAL: Process $PROCN does not exist" -elif [ $CNT -le $WARNING ]; then - STATE=$STATE_WARNING - DESCRIPTION="WARNING: Running only $CNT instances of $PROCN" -elif [ $CNT -le $CRITICAL ]; then - STATE=$STATE_CRITICAL - DESCRIPTION="CRITICAL: Running only $CNT instances of $PROCN" + +check_range $CNT $CRITICAL +RET_CRIT=$? +if [ "$RET_CRIT" -eq 2 ]; then + STATE=$STATE_CRITICAL + exit $STATE +fi + +check_range $CNT $WARNING +RET_WARN=$? +if [ "$RET_WARN" -eq 2 ]; then + STATE=$STATE_CRITICAL + exit $STATE +fi + +#echo "Range return is critical $RET_CRIT and warning $RET_WARN" + +PERFDATA="$PROCN=$CNT;$WARNING;$CRITICAL;$MIN;$MAX" + +if [ "$RET_CRIT" -eq 0 ]; then + STATE=$STATE_CRITICAL + DESCRIPTION="PROCESS CRITICAL: Number of $PROCN not in range $CRITICAL" +elif [ "$RET_WARN" -eq 0 ]; then + STATE=$STATE_WARNING + DESCRIPTION="PROCESS WARNING: Number of $PROCN not in range $WARNING" else - STATE=$STATE_OK - DESCRIPTION="OK: Process exist. Running instances: $CNT" - fi -echo $DESCRIPTION + STATE=$STATE_OK + DESCRIPTION="PROCESS OK: Running $CNT instances of $PROCN" +fi + +echo "$DESCRIPTION | $PERFDATA" exit $STATE From 82df2e1991adacab47156cbfe690c1feabcc042e Mon Sep 17 00:00:00 2001 From: Dominik Riva Date: Thu, 26 Jan 2017 11:33:01 +0100 Subject: [PATCH 2/5] Added check for snmpwalk exit codes. Any non zero exit code from snmpwalk will produce a critical exit status of this plugin. --- check_snmp_proc.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/check_snmp_proc.sh b/check_snmp_proc.sh index 48ad864..e0d131b 100755 --- a/check_snmp_proc.sh +++ b/check_snmp_proc.sh @@ -94,8 +94,14 @@ done PROCN=`echo $PROCN | cut -c -15` #echo $PROCN +WLK=`$CMD_SNMPWALK -v1 -On -c $COMM $HOST $OID` +if [ "$?" -ne 0 ]; then + STATE=$STATE_CRITICAL + exit $STATE +fi + -CNT=`$CMD_SNMPWALK -v1 -On -c $COMM $HOST $OID | $CMD_GREP "\"$PROCN\"" | $CMD_WC -l` +CNT=`echo $WLK | $CMD_GREP "\"$PROCN\"" | $CMD_WC -l` check_range $CNT $CRITICAL RET_CRIT=$? From 7313468fb65503da25ccb78422ede0dfe393fe67 Mon Sep 17 00:00:00 2001 From: Dominik Riva Date: Thu, 26 Jan 2017 11:50:54 +0100 Subject: [PATCH 3/5] Updated README.md --- README.md | 169 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 145 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 04cffb5..7349f34 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,145 @@ -

check_snmp_proc.sh

-
This is small Nagios plugin for checking status of processes (or quantity of them) via SNMP
-
Usage: -
./check_snmp_proc -H 127.0.0.1 -C public -N ssh -w 3 -c 0 -
-
Where: -
-H hostname /string/ -
Name or IP address of host (default 127.0.0.1) -
-C OID /string/ -
Community name for the host SNMP agent (default public) -
-N Process name /string/ -
Exact process name (default snmpd) -
-w warning level /integer/ -
Warning level of running processes (default: 0) -
-c critical level /integer/ -
Critical level of running processes (default: 0) -
-h help -
Print this help screen -
-V version number -
Print version and license information
-
-
This plugin uses the 'snmpwalk' command included with the NET-SNMP package. -
This plugin uses the [utils.sh from monitoring-plugins.org](https://github.com/monitoring-plugins/monitoring-plugins/blob/v2.2/plugins-scripts/utils.sh.in) -
This nagios plugins comes with ABSOLUTELY NO WARRANTY. So, enjoy ;) +# check_snmp_proc.sh +## This is small Nagios plugin for checking status of processes (or quantity of them) via SNMP +### Usage: +```./check_snmp_proc -H 127.0.0.1 -C public -N ssh -w 3 -c 0 + +Where: + -H hostname /string/ + Name or IP address of host (default 127.0.0.1) + -C OID /string/ + Community name for the host SNMP agent (default public) + -N Process name /string/ + Exact process name (default snmpd) + -w warning level /integer/ + Warning level of running processes (default: 0) + -c critical level /integer/ + Critical level of running processes (default: 0) + -m minimum level /integer/ + Minimum level for performance data (default: 0) + -M maximum level /integer/ + Maximum level for performance data (default: 100) + -h help + Print this help screen + -V version number + Print version and license information + + This plugin uses the 'snmpwalk' command included with the NET-SNMP package. + This plugin uses the [utils.sh from monitoring-plugins.org](https://github.com/monitoring-plugins/monitoring-plugins/blob/v2.2/plugins-scripts/utils.sh.in) + This nagios plugins comes with ABSOLUTELY NO WARRANTY. So, enjoy ;) +``` +### Icinga2 config +``` +object CheckCommand "snmp_proc" { + import "plugin-check-command" + + command = [ PluginGitDir + "/check_snmp_proc.sh" ] + + arguments = { + "-H" = { + value = "$snmp_proc_host$" + description = "Name or IP address of host (default 127.0.0.1 but overwritten by address)" + required = false + skip_key = false + order = 1 + } + "-C" = { + value = "$snmp_proc_community$" + description = "Community name for the host SNMP agent (default public)" + required = false + skip_key = false + order = 2 + } + "-N" = { + value = "$snmp_proc_name$" + description = "Exact process name (default snmpd)" + required = false + skip_key = false + order = 3 + } + "-w" = { + value = "$snmp_proc_warning$" + description = "Warning level of running processes (default: 0)" + required = false + skip_key = false + order = 4 + } + "-c" = { + value = "$snmp_proc_critical$" + description = "Critical level of running processes (default: 0)" + required = false + skip_key = false + order = 5 + } + "-m" = { + value = "$snmp_proc_min$" + description = "Minimum for performance data (default: 0)" + required = false + skip_key = false + order = 6 + } + "-M" = { + value = "$snmp_proc_max$" + description = "Maximum for performance data (default: 100)" + required = false + skip_key = false + order = 7 + } + } + + vars.snmp_proc_host = "$address$" +} + +``` + + +``` +apply Service for (snmp_proc => config in host.vars.snmp_proc) { + import "generic-service" + + check_command = "snmp_proc" + + display_name = config.display_name + + if (config.groups) { + groups = config.groups + } + + if (config.check_interval) { + check_interval = config.check_interval + } + + if (config.retry_interval) { + retry_interval = config.retry_interval + } + + if (config.max_check_attempts) { + max_check_attempts = config.max_check_attempts + } + + vars += config +} +``` + +``` +object Host "snmphost.test" { + import "generic-host" + display_name = "SNMP Test Host" + address = "127.0.0.1" + + vars.snmp_proc[ "service_name" ] = { + display_name = "Check SNMP service via SNMP process list" + +# snmp_proc_community = "public" + snmp_proc_name = "snmp" + snmp_proc_warning = "40" + snmp_proc_critical = "40" + +# check_interval = 10m +# retry_interval = 10m + max_check_attempts = 1 + } + +} +``` + + From 7cf981554e66ff96fcb442ffcd2b7265edd89395 Mon Sep 17 00:00:00 2001 From: Dominik Riva Date: Thu, 26 Jan 2017 11:52:20 +0100 Subject: [PATCH 4/5] Fix markdown --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7349f34..b1cfdf5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # check_snmp_proc.sh ## This is small Nagios plugin for checking status of processes (or quantity of them) via SNMP ### Usage: -```./check_snmp_proc -H 127.0.0.1 -C public -N ssh -w 3 -c 0 +``` +./check_snmp_proc -H 127.0.0.1 -C public -N ssh -w 3 -c 0 Where: -H hostname /string/ From fdcba1b2576ffe3d1a25e1ceebd4f5110f57c3ec Mon Sep 17 00:00:00 2001 From: Dominik Riva Date: Thu, 26 Jan 2017 13:23:58 +0100 Subject: [PATCH 5/5] Moved mention of utils.sh and link outside of code block. --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b1cfdf5..993b036 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # check_snmp_proc.sh ## This is small Nagios plugin for checking status of processes (or quantity of them) via SNMP + +This plugin uses the [utils.sh from monitoring-plugins.org](https://github.com/monitoring-plugins/monitoring-plugins/blob/v2.2/plugins-scripts/utils.sh.in) to be able to use ranges for warning and critical thresholds. + ### Usage: ``` ./check_snmp_proc -H 127.0.0.1 -C public -N ssh -w 3 -c 0 @@ -25,10 +28,9 @@ Where: Print version and license information This plugin uses the 'snmpwalk' command included with the NET-SNMP package. - This plugin uses the [utils.sh from monitoring-plugins.org](https://github.com/monitoring-plugins/monitoring-plugins/blob/v2.2/plugins-scripts/utils.sh.in) This nagios plugins comes with ABSOLUTELY NO WARRANTY. So, enjoy ;) ``` -### Icinga2 config +### Icinga2 Configuration ``` object CheckCommand "snmp_proc" { import "plugin-check-command"