check_snmp: Make use of standard threshold functions
[monitoring-plugins.git] / plugins / t / check_snmp.t
blob6cc97b6e9ef9c66a224d6e4993fd134341f9c89e
1 #! /usr/bin/perl -w -I ..
3 # Simple Network Management Protocol (SNMP) Test via check_snmp
7 use strict;
8 use Test::More;
9 use NPTest;
11 my $tests = 44;
12 plan tests => $tests;
13 my $res;
15 SKIP: {
16 skip "check_snmp is not compiled", $tests if ( ! -x "./check_snmp" );
18 my $host_snmp = getTestParameter( "host_snmp", "NP_HOST_SNMP", "localhost",
19 "A host providing an SNMP Service");
21 my $snmp_community = getTestParameter( "snmp_community", "NP_SNMP_COMMUNITY", "public",
22 "The SNMP Community string for SNMP Testing (assumes snmp v1)" );
24 my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1",
25 "The hostname of system not responsive to network requests" );
27 my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost",
28 "An invalid (not known to DNS) hostname" );
30 $res = NPTest->testCmd( "./check_snmp -t 1" );
31 is( $res->return_code, 3, "No host name" );
32 is( $res->output, "No host specified" );
34 $res = NPTest->testCmd( "./check_snmp -H fakehostname" );
35 is( $res->return_code, 3, "No OIDs specified" );
36 is( $res->output, "No OIDs specified" );
38 $res = NPTest->testCmd( "./check_snmp -H fakehost -o oids -P 3 --seclevel=rubbish" );
39 is( $res->return_code, 3, "Invalid seclevel" );
40 like( $res->output, "/check_snmp: Invalid seclevel - rubbish/" );
42 $res = NPTest->testCmd( "./check_snmp -H fakehost -o oids -P 3c" );
43 is( $res->return_code, 3, "Invalid protocol" );
44 like( $res->output, "/check_snmp: Invalid SNMP version - 3c/" );
46 SKIP: {
47 skip "no snmp host defined", 30 if ( ! $host_snmp );
49 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:");
50 cmp_ok( $res->return_code, '==', 0, "Exit OK when querying uptime" );
51 like($res->output, '/^SNMP OK - (\d+)/', "String contains SNMP OK");
52 $res->output =~ /^SNMP OK - (\d+)/;
53 my $value = $1;
54 cmp_ok( $value, ">", 0, "Got a time value" );
55 like($res->perf_output, "/sysUpTime.*$1/", "Got perfdata with value '$1' in it");
57 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysDescr.0");
58 cmp_ok( $res->return_code, '==', 0, "Exit OK when querying sysDescr" );
59 unlike($res->perf_output, '/sysDescr/', "Perfdata doesn't contain string values");
61 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 1:1 -c 1:1");
62 cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrSWRunIndex.1" );
63 like($res->output, '/^SNMP OK - 1\s.*$/', "String fits SNMP OK and output format");
65 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 0 -c 1:");
66 cmp_ok( $res->return_code, '==', 1, "Exit WARNING when querying hrSWRunIndex.1 and warn-th doesn't apply " );
67 like($res->output, '/^SNMP WARNING - \*1\*\s.*$/', "String matches SNMP WARNING and output format");
69 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0");
70 cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL when querying hrSWRunIndex.1 and crit-th doesn't apply" );
71 like($res->output, '/^SNMP CRITICAL - \*1\*\s.*$/', "String matches SNMP CRITICAL and output format");
73 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2 -c 1:2");
74 cmp_ok( $res->return_code, '==', 0, "Checking two OIDs at once" );
75 like($res->output, "/^SNMP OK - 2 1/", "Got two values back" );
76 like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" );
77 like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" );
79 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2,1:2 -c 2:2,2:2");
80 cmp_ok( $res->return_code, '==', 2, "Checking critical threshold is passed if any one value crosses" );
81 like($res->output, "/^SNMP CRITICAL - 2 *1*/", "Got two values back" );
82 like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" );
83 like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" );
85 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w 1:,1: -c 1:,1:");
86 cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrMemorySize and hrSystemProcesses");
87 like($res->output, '/^SNMP OK - \d+ \d+/', "String contains hrMemorySize and hrSystemProcesses");
89 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w \@:0 -c \@0");
90 cmp_ok( $res->return_code, '==', 0, "Exit OK with inside-range thresholds");
91 like($res->output, '/^SNMP OK - 1\s.*$/', "String matches SNMP OK and output format");
93 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3");
94 $res->output =~ m/^SNMP OK - (\d+\.\d{2})\s.*$/;
95 my $lower = $1 - 0.05;
96 my $higher = $1 + 0.05;
97 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3 -w $lower -c $higher");
98 cmp_ok( $res->return_code, '==', 1, "Exit WARNING with fractionnal arcuments");
100 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0,host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w ,:0 -c ,:2");
101 cmp_ok( $res->return_code, '==', 1, "Exit WARNING on 2nd threshold");
102 like($res->output, '/^SNMP WARNING - Timeticks:\s\(\d+\)\s.*,\s.*\s\*1\*\s.*$/', "First OID returned as string, 2nd checked for thresholds");
104 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w '' -c ''");
105 cmp_ok( $res->return_code, '==', 0, "Empty thresholds doesn't crash");
107 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,,1 -c ,,2");
108 cmp_ok( $res->return_code, '==', 0, "Skipping first two thresholds on 2 OID check");
109 like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping first two thresholds, result printed rather than parsed");
111 $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,, -c ,,");
112 cmp_ok( $res->return_code, '==', 0, "Skipping all thresholds");
113 like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping all thresholds, result printed rather than parsed");
117 SKIP: {
118 skip "no non responsive host defined", 2 if ( ! $host_nonresponsive );
119 $res = NPTest->testCmd( "./check_snmp -H $host_nonresponsive -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:");
120 cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" );
121 like($res->output, '/External command error: Timeout: No Response from /', "String matches timeout problem");
124 SKIP: {
125 skip "no non invalid host defined", 2 if ( ! $hostname_invalid );
126 $res = NPTest->testCmd( "./check_snmp -H $hostname_invalid -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:");
127 cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" );
128 like($res->output, '/External command error: .*(nosuchhost|Name or service not known|Unknown host)/', "String matches invalid host");