]> git.pld-linux.org Git - packages/nagios-plugin-check_elvis_status.git/blob - check_elvis_status.php
rename plugin with .php ext, add .spec
[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  * @(#) $Id$
22  */
23
24 define('PROGNAME', basename(array_shift($argv)));
25 define('LABEL', strtoupper(str_replace('check_', '', PROGNAME)));
26
27 // loads from same dir as program
28 require_once 'utils.php';
29
30 function usage() {
31         global $default_opt;
32         $opt = $default_opt;
33     echo "Usage: ", PROGNAME, " [OPTION]...
34
35 Check Elvis DAM server-status data
36 Example: ", PROGNAME ,"
37
38 Plugin action specific options:
39   -u                    URL to Elvis DAM /server-status. Sample: http://USERNAME:PASSWORD@HOSTNAME/dam/controller/admin/server-status
40   -m                    Message what you are querying
41   -e                    Expression what to retrieve from json data. this must be valid PHP Expression
42   -w                    The warning range. default '{$opt['w']}'
43   -c                    The critical range. default '{$opt['c']}'
44 ";
45         exit(STATE_UNKNOWN);
46 }
47
48 $default_opt = array(
49         'u' => '',
50         'm' => '',
51         'e' => null,
52         'w' => 0,
53         'c' => 0,
54 );
55 $opt = array_merge($default_opt, getopt("u:e:m:w:c:"));
56
57 if (empty($opt['u']) || !isset($opt['e'])) {
58         usage();
59 }
60
61 $data = file_get_contents($opt['u']);
62 if ($data === false) {
63         echo "ERROR: Can't fetch '{$opt['u']}'\n";
64         exit(STATE_CRITICAL);
65 }
66
67 $json = json_decode($data);
68 if ($json === null) {
69         echo "ERROR: Can't parse json\n";
70         exit(STATE_CRITICAL);
71 }
72
73 $eval = 'return $json' . $opt['e'] .';';
74 $res = eval($eval);
75
76 if ($res === null) {
77         echo LABEL, ": ERROR: {$opt['m']}: Unexpected null\n";
78         exit(STATE_UNKNOWN);
79 } elseif ($res === false) {
80         echo LABEL, ": ERROR: {$opt['m']}: parse error: {$opt['e']}\n";
81         exit(STATE_UNKNOWN);
82 } elseif ($res > $opt['c']) {
83         echo LABEL, ": CRITICAL: {$opt['m']}: $res\n";
84         exit(STATE_CRITICAL);
85 } elseif ($res > $opt['w']) {
86         echo LABEL, ": WARNING: {$opt['m']}: $res\n";
87         exit(STATE_WARNING);
88 } else {
89         echo LABEL, ": OK: {$opt['m']}: $res\n";
90         exit(STATE_OK);
91 }
This page took 0.157333 seconds and 3 git commands to generate.