3 /* vim: set encoding=utf-8: */
5 * Nagios plugin to check Elvis DAM server-status json
6 * Copyright (C) 2013 Elan Ruusamäe <glen@delfi.ee>
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.
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.
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/>.
22 define('PROGNAME', basename(array_shift($argv), '.php'));
23 define('LABEL', strtoupper(str_replace('check_', '', PROGNAME)));
25 // loads from same dir as program
26 require_once 'utils.php';
31 echo "Usage: ", PROGNAME, " [OPTION]...
33 Check Elvis DAM server-status data
34 Example: ", PROGNAME ,"
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.
50 * http://stackoverflow.com/questions/11807115/php-convert-kb-mb-gb-tb-etc-to-bytes
52 function byteConvert($input) {
53 if (!preg_match('/^(?P<number>[\d.,]+)\s*(?P<type>\w+B)$/i', $input, $m)) {
57 // comma is decimal separator. replace it to computer language
58 $number = str_replace(',', '.', $m['number']);
60 switch (strtoupper($m['type'])) {
65 $output = $number*1024;
68 $output = $number*1024*1024;
71 $output = $number*1024*1024*1024;
74 $output = $number*1024*1024*1024;
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']);
94 if (empty($opt['u']) || !isset($opt['e'])) {
98 $data = file_get_contents($opt['u']);
99 if ($data === false) {
100 echo "ERROR: Can't fetch '{$opt['u']}'\n";
101 exit(STATE_CRITICAL);
104 $json = json_decode($data);
105 if ($json === null) {
106 echo "ERROR: Can't parse json\n";
107 exit(STATE_CRITICAL);
110 $eval = 'return $json' . $opt['e'] .';';
112 echo "EVAL: $eval\n";
117 echo LABEL, ": ERROR: {$opt['m']}: Unexpected null\n";
119 } elseif ($res === false) {
120 echo LABEL, ": ERROR: {$opt['m']}: parse error: {$opt['e']}\n";
124 $value = byteConvert($res);
126 echo "RES: $res; VALUE: $value; WARNING: $warning; CRITICAL: $critical\n";
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";
136 echo LABEL, ": OK: {$opt['m']}: $res\n";