]> git.pld-linux.org Git - packages/nagios-plugin-check_elvis_status.git/blob - check_elvis_status.php
formatting cosmetics
[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) 2012 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)));
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   -w                    The warning range. default '{$opt['w']}'
41   -c                    The critical range. default '{$opt['c']}'
42 ";
43         exit(STATE_UNKNOWN);
44 }
45
46 $default_opt = array(
47         'u' => '',
48         'm' => '',
49         'e' => null,
50         'w' => 0,
51         'c' => 0,
52 );
53 $opt = array_merge($default_opt, getopt("u:e:m:w:c:"));
54
55 if (empty($opt['u']) || !isset($opt['e'])) {
56         usage();
57 }
58
59 $data = file_get_contents($opt['u']);
60 if ($data === false) {
61         echo "ERROR: Can't fetch '{$opt['u']}'\n";
62         exit(STATE_CRITICAL);
63 }
64
65 $json = json_decode($data);
66 if ($json === null) {
67         echo "ERROR: Can't parse json\n";
68         exit(STATE_CRITICAL);
69 }
70
71 $eval = 'return $json' . $opt['e'] .';';
72 $res = eval($eval);
73
74 if ($res === null) {
75         echo LABEL, ": ERROR: {$opt['m']}: Unexpected null\n";
76         exit(STATE_UNKNOWN);
77 } elseif ($res === false) {
78         echo LABEL, ": ERROR: {$opt['m']}: parse error: {$opt['e']}\n";
79         exit(STATE_UNKNOWN);
80 } elseif ($res > $opt['c']) {
81         echo LABEL, ": CRITICAL: {$opt['m']}: $res\n";
82         exit(STATE_CRITICAL);
83 } elseif ($res > $opt['w']) {
84         echo LABEL, ": WARNING: {$opt['m']}: $res\n";
85         exit(STATE_WARNING);
86 } else {
87         echo LABEL, ": OK: {$opt['m']}: $res\n";
88         exit(STATE_OK);
89 }
This page took 0.13795 seconds and 3 git commands to generate.