diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:48:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:48:22 +0000 |
commit | 7373ce3d6988706388f136e1c06afd20a3e8d5be (patch) | |
tree | e9ae5af7d102667e5706187646db45de8238e8c4 /plugins-scripts/t | |
parent | Initial commit. (diff) | |
download | monitoring-plugins-upstream.tar.xz monitoring-plugins-upstream.zip |
Adding upstream version 2.3.5.upstream/2.3.5upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | plugins-scripts/t/check_disk_smb.t | 96 | ||||
-rw-r--r-- | plugins-scripts/t/check_file_age.t | 109 | ||||
-rw-r--r-- | plugins-scripts/t/check_ifoperstatus.t | 93 | ||||
-rw-r--r-- | plugins-scripts/t/check_ifstatus.t | 67 | ||||
-rw-r--r-- | plugins-scripts/t/check_log.t | 82 | ||||
-rw-r--r-- | plugins-scripts/t/check_rpc.t | 21 | ||||
-rw-r--r-- | plugins-scripts/t/check_uptime.t | 135 | ||||
-rw-r--r-- | plugins-scripts/t/utils.t | 51 |
8 files changed, 654 insertions, 0 deletions
diff --git a/plugins-scripts/t/check_disk_smb.t b/plugins-scripts/t/check_disk_smb.t new file mode 100644 index 0000000..c2e8bbb --- /dev/null +++ b/plugins-scripts/t/check_disk_smb.t @@ -0,0 +1,96 @@ +#! /usr/bin/perl -w -I .. +# +# test cases for check_disk_smb +# + +use strict; +use Test::More; +use NPTest; + +my $tests = 14; +plan tests => $tests; +my $res; + +my $plugin = "check_disk_smb"; +SKIP: { + skip "$plugin is not created", $tests if ( ! -x $plugin ); + my $auth = ""; + + my $host = getTestParameter("NP_HOST_SMB", "A host providing an SMB Service", + "localhost"); + + my $smb_share = getTestParameter("NP_SMB_SHARE", + "An SMB share name the host provides", + "public"); + + my $smb_share_spc = getTestParameter("NP_SMB_SHARE_SPC", + "An SMB share name containing one or more spaces the host provides", + "pub lic"); + + my $smb_share_deny = getTestParameter("NP_SMB_SHARE_DENY", + "An access denying SMB share name the host provides", + "private"); + + my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", "10.0.0.1" ); + + my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost" ); + my $user = getTestParameter( "NP_SMB_VALID_USER", "A valid smb user", "" ); + my $pass = getTestParameter( "NP_SMB_VALID_USER_PASS", "A valid password for valid smb user", "" ); + $auth .= "-u $user " if ($user); + $auth .= "-p $pass " if ($pass); + + + + $res = NPTest->testCmd( "./$plugin" ); + is( $res->return_code, 3, "No arguments" ); + + $res = NPTest->testCmd( "./$plugin -H fakehostname" ); + is( $res->return_code, 3, "No share specified" ); + + $res = NPTest->testCmd( "./$plugin -H fakehostname -s share -w 100G -c 101G" ); + is( $res->return_code, 3, "warn is less than critical" ); + + SKIP: { + skip "no smb host defined", 10 if ( ! $host ); + + SKIP: { + skip "no share name defined", 2 if ( ! $smb_share ); + $res = NPTest->testCmd( "./$plugin -H $host $auth -s $smb_share -w 2k -c 1k" ); + cmp_ok( $res->return_code, '==', 0, "Exit OK if $smb_share has > 1k free space"); + like($res->output, '/free/i', "String contains the word 'free'"); + + $res = NPTest->testCmd( "./$plugin -H $host $auth -s $smb_share -w 10001G -c 10000G" ); + cmp_ok( $res->return_code, '==', 2, "Exit CRIT if $smb_share has < 10000G free space"); + like($res->output, '/free/i', "String contains the word 'free'"); + + $res = NPTest->testCmd( "./$plugin -H $host $auth -s $smb_share -w 10000G -c 1k" ); + cmp_ok( $res->return_code, '==', 1, "Exit WARN if $smb_share has > 10000G and <1k free space"); + like($res->output, '/free/i', "String contains the word 'free'"); + } + + SKIP: { + skip "no share name containing spaces defined", 2 if ( ! $smb_share_spc ); + $res = NPTest->testCmd( "./$plugin -H $host $auth -s '$smb_share_spc' -w 2k -c 1k" ); + cmp_ok( $res->return_code, '==', 0, "Exit OK if '$smb_share_spc' has > 1k free space"); + like($res->output, '/free/i', "String contains the word 'free'"); + + } + SKIP: { + skip "no share name without permissions ", 2 if ( ! $smb_share_deny ); + $res = NPTest->testCmd( "./$plugin -H $host $auth -s $smb_share_deny -w 2k -c 1k" ); + cmp_ok( $res->return_code, '==', 2, "Exit CRIT if $smb_share_deny has > 1k free space"); + unlike($res->output, '/free/i', "String does not contain the word 'free'"); + + } + } + + SKIP: { + skip "no non responsive host defined", 1 if ( ! $host_nonresponsive ); + $res = NPTest->testCmd( "./$plugin -H $host_nonresponsive -s np_foobar "); + cmp_ok( $res->return_code, '>=', 2, "Exit CRITICAL/UNKNOWN with non responsive host" ); + } + +} diff --git a/plugins-scripts/t/check_file_age.t b/plugins-scripts/t/check_file_age.t new file mode 100644 index 0000000..8b87670 --- /dev/null +++ b/plugins-scripts/t/check_file_age.t @@ -0,0 +1,109 @@ +#!/usr/bin/perl -w -I .. +# +# check_file_age tests +# +# + +use strict; +use Test::More tests => 27; +use NPTest; + +my $successOutput = '/^FILE_AGE OK: /'; +my $warningOutput = '/^FILE_AGE WARNING: /'; +my $criticalOutput = '/^FILE_AGE CRITICAL: /'; +my $unknownOutput = '/^FILE_AGE UNKNOWN: /'; +my $performanceOutput = '/ \| age=[0-9]+s;[0-9:]+;[0-9:]+ size=[0-9]+B;[0-9:]+;[0-9:]+;0$/'; + +my $result; +my $temp_file = "/tmp/check_file_age.tmp"; +my $temp_link = "/tmp/check_file_age.link.tmp"; + +unlink $temp_file, $temp_link; + +$result = NPTest->testCmd("./check_file_age"); +cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); +like ( $result->output, $unknownOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_file_age -f $temp_file"); +cmp_ok( $result->return_code, '==', 2, "File not exists" ); +like ( $result->output, $criticalOutput, "Output for file missing correct" ); + +write_chars(100); +$result = NPTest->testCmd("./check_file_age -f $temp_file"); +cmp_ok( $result->return_code, '==', 0, "File is new enough" ); +like ( $result->output, $successOutput, "Output for success correct" ); + +sleep 2; + +$result = NPTest->testCmd("./check_file_age -f $temp_file -w 1"); +cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" ); +like ( $result->output, $warningOutput, "Output for warning correct" ); + +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1"); +cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" ); +like ( $result->output, $criticalOutput, "Output for critical correct" ); + +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100"); +cmp_ok( $result->return_code, '==', 0, "Checking file size" ); + +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100"); +like( $result->output, $performanceOutput, "Checking for performance Output" ); + +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100"); +like( $result->output, $performanceOutput, "Checking for performance Output from range" ); + +$result = NPTest->testCmd("./check_file_age -f /non/existent --ignore-missing"); +cmp_ok( $result->return_code, '==', 0, "Honours --ignore-missing" ); + +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 101"); +cmp_ok( $result->return_code, '==', 1, "One byte too short" ); + +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 101"); +cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" ); + +SKIP: { + eval 'use Monitoring::Plugin::Range'; + skip "Monitoring::Plugin::Range module require", 9 if $@; + + $result = NPTest->testCmd("./check_file_age -f $temp_file -w 0:1"); + cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old by range" ); + like ( $result->output, $warningOutput, "Output for warning by range correct" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 0:1"); + cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old by range" ); + like ( $result->output, $criticalOutput, "Output for critical by range correct" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 0:1000 -W 0:100"); + cmp_ok( $result->return_code, '==', 0, "Checking file size by range" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 101:"); + cmp_ok( $result->return_code, '==', 1, "One byte too short by range" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 0:99"); + cmp_ok( $result->return_code, '==', 1, "One byte too long by range" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 101:"); + cmp_ok( $result->return_code, '==', 2, "One byte too short by range - critical" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 0:99"); + cmp_ok( $result->return_code, '==', 2, "One byte too long by range - critical" ); +}; + +symlink $temp_file, $temp_link or die "Cannot create symlink"; +$result = NPTest->testCmd("./check_file_age -f $temp_link -c 10"); +cmp_ok( $result->return_code, '==', 0, "Works for symlinks" ); +unlink $temp_link; + +unlink $temp_file; +mkdir $temp_file or die "Cannot create directory"; +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1"); +cmp_ok( $result->return_code, '==', 0, "Works for directories" ); +rmdir $temp_file; + + +sub write_chars { + my $size = shift; + open F, "> $temp_file" or die "Cannot write to $temp_file"; + print F "A" x $size; + close F; +} diff --git a/plugins-scripts/t/check_ifoperstatus.t b/plugins-scripts/t/check_ifoperstatus.t new file mode 100644 index 0000000..2feb364 --- /dev/null +++ b/plugins-scripts/t/check_ifoperstatus.t @@ -0,0 +1,93 @@ +#! /usr/bin/perl -w -I .. +# +# SNMP Test via check_ifoperstatus +# +# + +use strict; +use Test::More; +use NPTest; + +my $tests = 15; +plan tests => $tests; +my $res; + +my $plugin = "check_ifoperstatus"; +SKIP: { + skip "$plugin is not created", $tests if ( ! -x $plugin ); + + my $host_snmp = getTestParameter( "NP_HOST_SNMP", "A host providing an SNMP Service", "localhost"); + + my $snmp_community = getTestParameter( "NP_SNMP_COMMUNITY", + "The SNMP Community string for SNMP Testing", + "public"); + + my ($snmp_interface, $snmp_ifxtable); + if ($host_snmp) { + $snmp_interface = getTestParameter( "NP_SNMP_INTERFACE", "Name of an active network interface on SNMP server", "lo" ); + + $snmp_ifxtable = getTestParameter( "NP_SNMP_IFXTABLE", + "Is IFXTABLE activated in SNMP server (1: yes, 0: no)? snmpwalk -v1 -c $snmp_community $host_snmp ifxtable", + "1" ); + } + + my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", "10.0.0.1" ); + + my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost" ); + + + + $res = NPTest->testCmd( "./$plugin" ); + is( $res->return_code, 3, "No arguments" ); + like( $res->output, '/usage/', "Output contains usage" ); + + $res = NPTest->testCmd( "./$plugin -H fakehostname" ); + is( $res->return_code, 3, "No key/descr specified" ); + like( $res->output, '/Either a valid snmp key/', "Output contains 'Either a valid snmp key'" ); + + $res = NPTest->testCmd( "./$plugin -H fakehost -k 1 -v 3 --seclevel rubbish --secname foobar" ); + is( $res->return_code, 3, "invalid seclevel" ); + like( $res->output, "/Must define a valid security level/", "Output contains 'Must define a valid security level'" ); + + SKIP: { + skip "no snmp host defined", 6 if ( ! $host_snmp ); + + $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -k 1"); + cmp_ok( $res->return_code, '==', 0, "Exit OK for ifindex 1" ); + like($res->output, '/^OK.*Interface.*is up/', "String contains OK Interface is up"); + + SKIP: { + skip "no snmp interface defined", 2 if ( ! $snmp_interface ); + $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -d $snmp_interface"); + cmp_ok( $res->return_code, '==', 0, "Exit OK for ifdescr $snmp_interface" ); + like($res->output, '/^OK.*Interface.*is up/', "String contains OK Interface is up"); + } + + SKIP: { + skip "ifxtable not available", 2 if ( ! $snmp_ifxtable ); + $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -k 1 -n rubbish"); + cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN if interface name doesn't match" ); + like($res->output, '/doesn\'t match snmp value/', "String contains 'doesn't match snmp value'"); + } + + } + + # These checks need a complete command line. An invalid community is used so + # the tests can run on hosts w/o snmp host/community in NPTest.cache. Execution will fail anyway + SKIP: { + skip "no non responsive host defined", 1 if ( ! $host_nonresponsive ); + $res = NPTest->testCmd( "./$plugin -H $host_nonresponsive -C np_foobar -k 1"); + cmp_ok( $res->return_code, '==', 1, "Exit WARNING with non responsive host" ); + } + + SKIP: { + skip "no invalid host defined", 2 if ( ! $hostname_invalid ); + $res = NPTest->testCmd( "./$plugin -H $hostname_invalid -C np_foobar -k 1"); + cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with invalid host" ); + like($res->output, "/Unable to resolve.*$hostname_invalid/", "String matches unable to resolve.*$hostname_invalid"); + } + +} diff --git a/plugins-scripts/t/check_ifstatus.t b/plugins-scripts/t/check_ifstatus.t new file mode 100644 index 0000000..7d0220a --- /dev/null +++ b/plugins-scripts/t/check_ifstatus.t @@ -0,0 +1,67 @@ +#! /usr/bin/perl -w -I .. +# +# SNMP Test via check_ifoperstatus +# +# + +use strict; +use Test::More; +use NPTest; + +my $tests = 9; +plan tests => $tests; +my $res; + +my $plugin = "check_ifstatus"; +SKIP: { + skip "$plugin is not created", $tests if ( ! -x $plugin ); + + my $host_snmp = getTestParameter( "NP_HOST_SNMP", "A host providing an SNMP Service", "localhost"); + + my $snmp_community = getTestParameter( "NP_SNMP_COMMUNITY", + "The SNMP Community string for SNMP Testing", + "public"); + + my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", "10.0.0.1" ); + + my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost" ); + + + $res = NPTest->testCmd( "./$plugin" ); + is( $res->return_code, 3, "No arguments" ); + like( $res->output, '/usage/', "Output contains usage" ); + + $res = NPTest->testCmd( "./$plugin -H fakehost -v 3 --seclevel rubbish --secname foobar" ); + is( $res->return_code, 3, "invalid seclevel" ); + like( $res->output, "/Must define a valid security level/", "Output contains 'Must define a valid security level'" ); + + SKIP: { + skip "no snmp host defined", 2 if ( ! $host_snmp ); + + $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community "); + like($res->output, '/^.*host.*interfaces up/', "String contains host.*interfaces up"); + + $res = NPTest->testCmd( "./$plugin -H $host_snmp -C rubbish"); + cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL for community 'rubbish'" ); + + } + + # These checks need a complete command line. An invalid community is used so + # the tests can run on hosts w/o snmp host/community in NPTest.cache. Execution will fail anyway + SKIP: { + skip "no non responsive host defined", 1 if ( ! $host_nonresponsive ); + $res = NPTest->testCmd( "./$plugin -H $host_nonresponsive -C np_foobar"); + cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL with non responsive host" ); + } + + SKIP: { + skip "no invalid host defined", 2 if ( ! $hostname_invalid ); + $res = NPTest->testCmd( "./$plugin -H $hostname_invalid -C np_foobar"); + cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with invalid host" ); + like($res->output, "/Unable to resolve.*$hostname_invalid/", "String matches unable to resolve.*$hostname_invalid"); + } + +} diff --git a/plugins-scripts/t/check_log.t b/plugins-scripts/t/check_log.t new file mode 100644 index 0000000..b66e0fd --- /dev/null +++ b/plugins-scripts/t/check_log.t @@ -0,0 +1,82 @@ +#!/usr/bin/perl -w -I .. +# +# check_log tests +# +# + +use strict; +use Test::More; +use NPTest; + +my $tests = 18; +plan tests => $tests; + +my $firstTimeOutput ='/^Log check data initialized/'; +my $okOutput = '/^Log check ok - 0 pattern matches found/'; +my $criticalOutput = '/^\(\d+\) < /'; +my $multilineOutput = '/\(3\) <.*\n.*\n.*$/'; +my $unknownOutput = '/^Usage: /'; +my $unknownArgOutput = '/^Unknown argument: /'; +my $bothRegexOutput = '/^Can not use extended and perl regex/'; + +my $result; +my $temp_file = "/tmp/check_log.tmp"; +my $oldlog = "/tmp/oldlog.tmp"; + +open(FH, '>', $temp_file) or die $!; +close(FH); + +$result = NPTest->testCmd("./check_log"); +cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); +like ( $result->output, $unknownOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -f"); +cmp_ok( $result->return_code, '==', 3, "Wrong parameters" ); +like ( $result->output, $unknownArgOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match' -e -p"); +cmp_ok( $result->return_code, '==', 3, "Both regex parameters" ); +like ( $result->output, $bothRegexOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match'"); +cmp_ok( $result->return_code, '==', 0, "First time executing" ); +like ( $result->output, $firstTimeOutput, "Output for first time executing correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This is some text, that should not match\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'No match'"); +cmp_ok( $result->return_code, '==', 0, "No match" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This text should match\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'should match'"); +cmp_ok( $result->return_code, '==', 2, "Pattern match" ); +like ( $result->output, $criticalOutput, "Output for match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This text should not match, because it is excluded\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' --exclude 'because'"); +cmp_ok( $result->return_code, '==', 0, "Exclude a pattern" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "Trying\nwith\nmultiline\nignore me\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Trying\\|with\\|multiline\\|ignore' --exclude 'me' --all"); +cmp_ok( $result->return_code, '==', 2, "Multiline pattern match with --all" ); +like ( $result->output, $multilineOutput, "Output for multiline match correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' -a"); +cmp_ok( $result->return_code, '==', 0, "Non matching --all" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +unlink($oldlog); +unlink($temp_file); diff --git a/plugins-scripts/t/check_rpc.t b/plugins-scripts/t/check_rpc.t new file mode 100644 index 0000000..c58f7bf --- /dev/null +++ b/plugins-scripts/t/check_rpc.t @@ -0,0 +1,21 @@ +#! /usr/bin/perl -w -I .. +# +# Remote Procedure Call (RPC) Tests via check_rpc +# +# + +use strict; +use Test; +use NPTest; + +use vars qw($tests); +BEGIN {$tests = 2; plan tests => $tests} + +my $successOutput = '/^check_rpc/'; + +my $t; + +$t += checkCmd( "./check_rpc -V", 3, $successOutput ); + +exit(0) if defined($Test::Harness::VERSION); +exit($tests - $t); diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t new file mode 100644 index 0000000..6e81db3 --- /dev/null +++ b/plugins-scripts/t/check_uptime.t @@ -0,0 +1,135 @@ +#!/usr/bin/perl -w -I .. +# +# check_uptime tests +# +# + +use strict; +use Test::More tests => 42; +use NPTest; + +my $result; + +$result = NPTest->testCmd( + "./check_uptime" + ); +cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); +like ( $result->output, '/^Usage: check_uptime -w/', "Output for missing parameters correct" ); + +$result = NPTest->testCmd( + "./check_uptime --help" + ); +cmp_ok( $result->return_code, '==', 3, "Help output requested" ); +like ( $result->output, '/ABSOLUTELY NO WARRANTY/', "Output for help correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 5 -c 2" + ); +cmp_ok( $result->return_code, '==', 3, "Warning greater than critical" ); +like ( $result->output, '/^Upper Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); + +$result = NPTest->testCmd( + "./check_uptime -c 1000 -W 100 2>&1" + ); +like ( $result->output, '/^Unknown option: W/', "Output with wrong parameter is correct" ); + +$result = NPTest->testCmd( + "./check_uptime -f -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/Running for \d+/', "Output for the f parameter correct" ); + +$result = NPTest->testCmd( + "./check_uptime -s -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/Running since \d+/', "Output for the s parameter correct" ); + +$result = NPTest->testCmd( + "./check_uptime -d -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/CRITICAL: Uptime is \d+ days/', "Output for the d parameter correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/^CRITICAL: Uptime is \d+ seconds/', "Output for uptime higher than 2 seconds correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 1 -c 9999w" + ); +cmp_ok( $result->return_code, '==', 1, "Uptime lower than 9999 weeks" ); +like ( $result->output, '/^WARNING: Uptime is \d+ seconds/', "Output for uptime lower than 9999 weeks correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 9998w -c 9999w" + ); +cmp_ok( $result->return_code, '==', 0, "Uptime lower than 9998 weeks" ); +like ( $result->output, '/^OK: Uptime is \d+ seconds/', "Output for uptime lower than 9998 weeks correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;6046790400;6047395200;/', "Checking for performance output" ); + +$result = NPTest->testCmd( + "./check_uptime -w 111222d -c 222333d" + ); +cmp_ok( $result->return_code, '==', 0, "Uptime lower than 111222 days" ); +like ( $result->output, '/^OK: Uptime is \d+ seconds/', "Output for uptime lower than 111222 days correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + +# Same as before, hopefully uptime is higher than 2 seconds so no warning +$result = NPTest->testCmd( + "./check_uptime -w 2:111222d -c 1:222333d" + ); +cmp_ok( $result->return_code, '==', 0, "Uptime lower than 111222 days, and higher 2 seconds" ); +like ( $result->output, '/^OK: Uptime is \d+ seconds/', "Output for uptime lower than 111222 days, and higher 2 seconds correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + +# Same as before, now the low warning should trigger +$result = NPTest->testCmd( + "./check_uptime -w 111221d:111222d -c 1:222333d" + ); +cmp_ok( $result->return_code, '==', 1, "Uptime lower than 111221 days raises warning" ); +like ( $result->output, '/^WARNING: Uptime is \d+ seconds/', "Output for uptime lower than 111221 days correct" ); +like ( $result->output, '/Exceeds lower warn threshold/', "Exceeds text correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + +# Same as before, now the low critical should trigger +$result = NPTest->testCmd( + "./check_uptime -w 111221d:111222d -c 111220d:222333d" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime lower than 111220 days raises critical" ); +like ( $result->output, '/^CRITICAL: Uptime is \d+ seconds/', "Output for uptime lower than 111220 days correct" ); +like ( $result->output, '/Exceeds lower crit threshold/', "Exceeds text correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + + +# +# Range values using ":" without two parts ("a:b") is invalid +# Strings without two parts are always considered as upper threshold +# + +$result = NPTest->testCmd( + "./check_uptime -w 2: -c 1:4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper warning .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 2:3 -c 1:" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper critical .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w :3 -c 1:4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper warning .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 2:3 -c :4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper critical .* is not numeric/', "Output for wrong parameter format correct" ); + diff --git a/plugins-scripts/t/utils.t b/plugins-scripts/t/utils.t new file mode 100644 index 0000000..5c23179 --- /dev/null +++ b/plugins-scripts/t/utils.t @@ -0,0 +1,51 @@ +#!/usr/bin/perl -w -I .. +# +# utils.pm tests +# +# +# Run with perl t/utils.t + +use warnings; +use strict; +use Test::More; +use NPTest; + +use lib "."; +use lib ".."; +use utils; + +my $hostname_checks = { + "www.altinity.com" => 1, + "www.888.com" => 1, + "888.com" => 1, + "host-hyphened.com" => 1, + "rubbish" => 1, + "-start.com" => 0, + "nonfqdn-but-endsindot." => 1, + "fqdn.and.endsindot." => 1, + "lots.of.dots.dot.org" => 1, + "endingwithdoubledots.." => 0, + "toomany..dots" => 0, + ".start.with.dot" => 0, + "10.20.30.40" => 1, + "10.20.30.40.50" => 0, + "10.20.30" => 0, + "10.20.30.40." => 1, # This is considered a hostname because of trailing dot. It probably won't exist though... + "888." => 1, # This is because it could be a domain + "host.888." => 1, + "where.did.that.!.come.from." => 0, + "no.underscores_.com" => 0, + "a.somecompany.com" => 1, + "host.a.com" => 1, + }; + +plan tests => ((scalar keys %$hostname_checks) + 4); + +foreach my $h (sort keys %$hostname_checks) { + is (utils::is_hostname($h), $hostname_checks->{$h}, "$h should return ".$hostname_checks->{$h}); +} + +is(utils::is_hostname(), 0, "No parameter errors"); +is(utils::is_hostname(""), 0, "Empty string errors"); +is(utils::is_hostname(0), 0, "0 also errors"); +is(utils::is_hostname(1), 0, "1 also errors"); |