]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/monitoring/plugins/check_command
Add status engine website
[perso/Immae/Config/Nix.git] / modules / private / monitoring / plugins / check_command
1 #!/usr/bin/env perl
2
3 use strict;
4 use Getopt::Std;
5 $| = 1;
6
7 my %opts;
8 getopts('hr:C:c:s:o:', \%opts);
9
10 my $STATE_OK = 0;
11 my $STATE_WARNING = 1;
12 my $STATE_CRITICAL = 2;
13 my $STATE_UNKNOWN = 3;
14
15 if ($opts{'h'} || scalar(%opts) == 0) {
16 &print_help();
17 exit($STATE_OK);
18 }
19
20 my $command = $opts{'c'};
21 if ($command eq '') {
22 print "You must provide a command to check.\n";
23 exit($STATE_UNKNOWN);
24 }
25
26 my $expected_output = $opts{'o'};
27 my $expected_status = $opts{'s'};
28 my $other_command = $opts{'C'};
29
30 if ($other_command eq '' and $expected_status eq '' and $expected_output eq '') {
31 $expected_status = 0;
32 }
33
34 my $cmd = $command . ' 2>&1';
35 my $other_cmd;
36 if ($other_command ne '') {
37 $other_cmd = $other_command . ' 2>&1';
38 }
39
40 my $run_as;
41 if ($opts{'r'}) {
42 $run_as = $opts{'r'};
43 $cmd = "sudo -u $run_as -n $cmd";
44
45 if ($other_command ne '') {
46 $other_cmd = "sudo -u $run_as -n $other_cmd";
47 }
48
49 }
50
51 my $cmd_result = `$cmd`;
52 my $other_cmd_result;
53 if ($other_command ne '') {
54 $other_cmd_result = `$other_cmd`;
55 chomp($other_cmd_result);
56 }
57
58 chomp($cmd_result);
59 if ($cmd_result =~ /sudo/i) {
60 print "$command CRITICAL - No sudo right to run the command | result=1;;;;\n";
61 exit($STATE_UNKNOWN);
62 } elsif ($expected_status ne '') {
63 if ($? != $expected_status) {
64 print "$command CRITICAL - Response status $? | result=1;;;;\n";
65 exit($STATE_CRITICAL);
66 } else {
67 print "$command OK - Response status $? | result=0;;;;\n";
68 exit($STATE_OK);
69 }
70 } elsif ($other_command ne '') {
71 if ($cmd_result ne $other_cmd_result) {
72 print "$command CRITICAL - Expected output not matching other command output | result=1;;;;\n";
73 exit($STATE_CRITICAL);
74 } else {
75 print "$command OK - Expected output matching other command output | result=0;;;;\n";
76 exit($STATE_OK);
77 }
78 } else {
79 if ($cmd_result !~ /$expected_output/) {
80 print "$command CRITICAL - Expected output not matching | result=1;;;;\n";
81 exit($STATE_CRITICAL);
82 } else {
83 print "$command OK - Expected output matching | result=0;;;;\n";
84 exit($STATE_OK);
85 }
86 }
87
88 sub print_help() {
89 print << "EOF";
90 Check whether the given command responds as expected. One of -o -C or -s must be selected.
91
92 Options:
93 -h
94 Print detailed help screen
95
96 -c
97 command to run (required)
98
99 -C
100 other command to compare output
101
102 -r user
103 Run as user via sudo.
104
105 -s
106 status code to check
107
108 -o
109 output to check
110
111 EOF
112 }
113