]> git.pld-linux.org Git - packages/nagios-plugin-check_elvis_status.git/blob - check_elvis_status.php
b8422384126e321860dcdd19699d0b9f78a42d8e
[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 /*
49  * based on code from:
50  * http://stackoverflow.com/questions/11807115/php-convert-kb-mb-gb-tb-etc-to-bytes
51  */
52 function byteConvert($input) {
53         if (!preg_match('/^(?P<number>[\d.,]+)\s*(?P<type>\w+B)$/i', $input, $m)) {
54                 return $input;
55         }
56
57         // comma is decimal separator. replace it to computer language
58         $number = str_replace(',', '.', $m['number']);
59
60         switch (strtoupper($m['type'])) {
61         case "B":
62                 $output = $number;
63                 break;
64         case "KB":
65                 $output = $number*1024;
66                 break;
67         case "MB":
68                 $output = $number*1024*1024;
69                 break;
70         case "GB":
71                 $output = $number*1024*1024*1024;
72                 break;
73         case "TB":
74                 $output = $number*1024*1024*1024;
75                 break;
76         }
77         return $output;
78 }
79
80 $default_opt = array(
81         'u' => '',
82         'm' => '',
83         'e' => null,
84         'v' => null,
85         'w' => 0,
86         'c' => 0,
87 );
88 $opt = array_merge($default_opt, getopt("u:e:m:w:c:vi"));
89 $invert = isset($opt['i']);
90 $verbose = isset($opt['v']);
91 $critical = byteConvert($opt['c']);
92 $warning = byteConvert($opt['w']);
93
94 if (empty($opt['u']) || !isset($opt['e'])) {
95         usage();
96 }
97
98 $data = file_get_contents($opt['u']);
99 if ($data === false) {
100         echo "ERROR: Can't fetch '{$opt['u']}'\n";
101         exit(STATE_CRITICAL);
102 }
103
104 $json = json_decode($data);
105 if ($json === null) {
106         echo "ERROR: Can't parse json\n";
107         exit(STATE_CRITICAL);
108 }
109
110 $eval = 'return $json' . $opt['e'] .';';
111 if ($verbose) {
112         echo "EVAL: $eval\n";
113 }
114 $res = eval($eval);
115
116 if ($res === null) {
117         echo LABEL, ": ERROR: {$opt['m']}: Unexpected null\n";
118         exit(STATE_UNKNOWN);
119 } elseif ($res === false) {
120         echo LABEL, ": ERROR: {$opt['m']}: parse error: {$opt['e']}\n";
121         exit(STATE_UNKNOWN);
122 }
123
124 $value = byteConvert($res);
125 if ($verbose) {
126         echo "RES: $res; VALUE: $value; WARNING: $warning; CRITICAL: $critical\n";
127 }
128
129 if ((!$invert && $value > $critical) || ($invert && $value < $critical))  {
130         echo LABEL, ": CRITICAL: {$opt['m']}: $res\n";
131         exit(STATE_CRITICAL);
132 } elseif ((!$invert && $value > $warning) || ($invert && $value < $warning)) {
133         echo LABEL, ": WARNING: {$opt['m']}: $res\n";
134         exit(STATE_WARNING);
135 } else {
136         echo LABEL, ": OK: {$opt['m']}: $res\n";
137         exit(STATE_OK);
138 }
This page took 0.074643 seconds and 2 git commands to generate.