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