]> git.pld-linux.org Git - packages/nagios-plugin-check_elvis_status.git/blob - check_elvis_status.php
add -i option to invert warning and critical checks
[packages/nagios-plugin-check_elvis_status.git] / check_elvis_status.php
1 #!/usr/bin/php
2 <?php
3 /* vim: set encoding=utf-8: */
4 /*
5  * Nagios plugin to check Elvis DAM server-status json
6  * Copyright (C) 2013 Elan Ruusamäe <glen@delfi.ee>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 define('PROGNAME', basename(array_shift($argv), '.php'));
23 define('LABEL', strtoupper(str_replace('check_', '', PROGNAME)));
24
25 // loads from same dir as program
26 require_once 'utils.php';
27
28 function usage() {
29         global $default_opt;
30         $opt = $default_opt;
31     echo "Usage: ", PROGNAME, " [OPTION]...
32
33 Check Elvis DAM server-status data
34 Example: ", PROGNAME ,"
35
36 Plugin action specific options:
37   -u    URL to Elvis DAM /server-status. Sample: http://USERNAME:PASSWORD@HOSTNAME/dam/controller/admin/server-status
38   -m    Message what you are querying
39   -e    Expression what to retrieve from json data. this must be valid PHP Expression
40   -i    Invert expression, critical and warning must be below the tresholds
41   -w    The warning range. default '{$opt['w']}'
42   -c    The critical range. default '{$opt['c']}'
43   -v    Enable verbose mode.
44 ";
45         exit(STATE_UNKNOWN);
46 }
47
48 $default_opt = array(
49         'u' => '',
50         'm' => '',
51         'e' => null,
52         'v' => null,
53         'w' => 0,
54         'c' => 0,
55 );
56 $opt = array_merge($default_opt, getopt("u:e:m:w:c:vi"));
57 $invert = isset($opt['i']);
58
59 if (empty($opt['u']) || !isset($opt['e'])) {
60         usage();
61 }
62
63 $data = file_get_contents($opt['u']);
64 if ($data === false) {
65         echo "ERROR: Can't fetch '{$opt['u']}'\n";
66         exit(STATE_CRITICAL);
67 }
68
69 $json = json_decode($data);
70 if ($json === null) {
71         echo "ERROR: Can't parse json\n";
72         exit(STATE_CRITICAL);
73 }
74
75 $eval = 'return $json' . $opt['e'] .';';
76 if (isset($opt['v'])) {
77         echo "EVAL: $eval\n";
78 }
79 $res = eval($eval);
80
81 if ($res === null) {
82         echo LABEL, ": ERROR: {$opt['m']}: Unexpected null\n";
83         exit(STATE_UNKNOWN);
84 } elseif ($res === false) {
85         echo LABEL, ": ERROR: {$opt['m']}: parse error: {$opt['e']}\n";
86         exit(STATE_UNKNOWN);
87 } elseif ((!$invert && $res > $opt['c']) || ($invert && $res < $opt['c']))  {
88         echo LABEL, ": CRITICAL: {$opt['m']}: $res\n";
89         exit(STATE_CRITICAL);
90 } elseif ((!$invert && $res > $opt['w']) || ($invert && $res < $opt['w'])) {
91         echo LABEL, ": WARNING: {$opt['m']}: $res\n";
92         exit(STATE_WARNING);
93 } else {
94         echo LABEL, ": OK: {$opt['m']}: $res\n";
95         exit(STATE_OK);
96 }
This page took 0.203963 seconds and 3 git commands to generate.