Merge pull request #2044 from RincewindsHat/fix/fedora-rpm-build
[monitoring-plugins.git] / plugins / t / check_by_ssh.t
blobb6479f1f48ca06d4d9a2da7c397ea3af965087ed
1 #! /usr/bin/perl -w -I ..
3 # check_by_ssh tests
7 use strict;
8 use Test::More;
9 use NPTest;
11 # Required parameters
12 my $ssh_service = getTestParameter("NP_SSH_HOST", "A host providing SSH service", "localhost");
13 my $ssh_key = getTestParameter("NP_SSH_IDENTITY", "A key allowing access to NP_SSH_HOST", "~/.ssh/id_dsa");
14 my $ssh_conf = getTestParameter( "NP_SSH_CONFIGFILE", "A config file with ssh settings", "~/.ssh/config");
17 plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key);
19 plan tests => 42;
21 # Some random check strings/response
22 my @response = ('OK: Everything is fine',
23 'WARNING: Hey, pick me, pick me',
24 'CRITICAL: Shit happens',
25 'UNKNOWN: What can I do for ya',
26 'WOOPS: What did I smoke',
28 my @response_re;
29 my @check;
30 for (@response) {
31 push(@check, "echo $_");
32 my $re_str = $_;
33 $re_str =~ s{(.)} { "\Q$1" }ge;
34 push(@response_re, $re_str);
37 my $result;
39 # expand paths
40 $ssh_key = glob($ssh_key) if $ssh_key;
41 $ssh_conf = glob($ssh_conf) if $ssh_conf;
43 ## Single active checks
45 for (my $i=0; $i<4; $i++) {
46 $result = NPTest->testCmd(
47 "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[$i]; exit $i'"
49 cmp_ok($result->return_code, '==', $i, "Exit with return code $i");
50 is($result->output, $response[$i], "Status text is correct for check $i");
53 $result = NPTest->testCmd(
54 "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 0'"
56 cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
57 is($result->output, 'OK - check_by_ssh: Remote command \'exit 0\' returned status 0', "Status text if command returned none (OK)");
59 $result = NPTest->testCmd(
60 "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 1'"
62 cmp_ok($result->return_code, '==', 1, "Exit with return code 1 (WARNING)");
63 is($result->output, 'WARNING - check_by_ssh: Remote command \'exit 1\' returned status 1', "Status text if command returned none (WARNING)");
65 $result = NPTest->testCmd(
66 "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 2'"
68 cmp_ok($result->return_code, '==', 2, "Exit with return code 2 (CRITICAL)");
69 is($result->output, 'CRITICAL - check_by_ssh: Remote command \'exit 2\' returned status 2', "Status text if command returned none (CRITICAL)");
71 $result = NPTest->testCmd(
72 "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 3'"
74 cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)");
75 is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 3\' returned status 3', "Status text if command returned none (UNKNOWN)");
77 $result = NPTest->testCmd(
78 "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 7'"
80 cmp_ok($result->return_code, '==', 7, "Exit with return code 7 (out of bounds)");
81 is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 7\' returned status 7', "Status text if command returned none (out of bounds)");
83 $result = NPTest->testCmd(
84 "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[4]; exit 8'"
86 cmp_ok($result->return_code, '==', 8, "Exit with return code 8 (out of bounds)");
87 is($result->output, $response[4], "Return proper status text even with unknown status codes");
89 $result = NPTest->testCmd(
90 "./check_by_ssh -i $ssh_key -H $ssh_service -F $ssh_conf -C 'exit 0'"
92 cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
93 is($result->output, 'OK - check_by_ssh: Remote command \'exit 0\' returned status 0', "Status text if command returned none (OK)");
95 # Multiple active checks
96 $result = NPTest->testCmd(
97 "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[1]; sh -c exit\\ 1' -C '$check[0]; sh -c exit\\ 0' -C '$check[3]; sh -c exit\\ 3' -C '$check[2]; sh -c exit\\ 2'"
99 cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK");
100 my @lines = split(/\n/, $result->output);
101 cmp_ok(scalar(@lines), '==', 8, "Correct number of output lines for multiple checks");
102 my %linemap = (
103 '0' => '1',
104 '2' => '0',
105 '4' => '3',
106 '6' => '2',
108 foreach my $line (0, 2, 4, 6) {
109 my $code = $linemap{$line};
110 my $statline = $line+1;
111 is($lines[$line], "$response[$code]", "multiple checks status text is correct for line $line");
112 is($lines[$statline], "STATUS CODE: $code", "multiple check status code is correct for line $line");
115 # Passive checks
116 unlink("/tmp/check_by_ssh.$$");
117 $result = NPTest->testCmd(
118 "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s serv -C '$check[2]; sh -c exit\\ 2' -O /tmp/check_by_ssh.$$"
120 cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
121 open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
122 my @pasv = <PASV>;
123 close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!");
124 cmp_ok(scalar(@pasv), '==', 1, 'One passive result for one check performed');
125 for (0) {
126 if ($pasv[$_]) {
127 like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;' . $response_re[2] . '$/', 'proper result for passive check');
128 } else {
129 fail('proper result for passive check');
132 unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
133 undef @pasv;
135 $result = NPTest->testCmd(
136 "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s c0:c1:c2:c3:c4 -C '$check[0];sh -c exit\\ 0' -C '$check[1];sh -c exit\\ 1' -C '$check[2];sh -c exit\\ 2' -C '$check[3];sh -c exit\\ 3' -C '$check[4];sh -c exit\\ 9' -O /tmp/check_by_ssh.$$"
138 cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
139 open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
140 @pasv = <PASV>;
141 close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!");
142 cmp_ok(scalar(@pasv), '==', 5, 'Five passive result for five checks performed');
143 for (0, 1, 2, 3, 4) {
144 if ($pasv[$_]) {
145 my $ret = $_;
146 $ret = 9 if ($_ == 4);
147 like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $_ . ';' . $ret . ';' . $response_re[$_] . '$/', "proper result for passive check $_");
148 } else {
149 fail("proper result for passive check $_");
152 unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");