]>
Commit | Line | Data |
---|---|---|
1a02d719 ER |
1 | #!/usr/bin/php |
2 | <?php | |
3 | /* vim: set encoding=utf-8: */ | |
4 | /* | |
27254510 | 5 | * Nagios plugin to check Elvis DAM server-status json |
ba2058f9 | 6 | * Copyright (C) 2013 Elan Ruusamäe <glen@delfi.ee> |
1a02d719 | 7 | * |
27254510 ER |
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. | |
1a02d719 | 12 | * |
27254510 ER |
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. | |
1a02d719 | 17 | * |
27254510 ER |
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/>. | |
1a02d719 ER |
20 | */ |
21 | ||
ba2058f9 | 22 | define('PROGNAME', basename(array_shift($argv), '.php')); |
1a02d719 ER |
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: | |
ba2058f9 ER |
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 | |
48c29855 | 40 | -i Invert expression, critical and warning must be below the tresholds |
ba2058f9 ER |
41 | -w The warning range. default '{$opt['w']}' |
42 | -c The critical range. default '{$opt['c']}' | |
43 | -v Enable verbose mode. | |
1a02d719 ER |
44 | "; |
45 | exit(STATE_UNKNOWN); | |
46 | } | |
47 | ||
ebdaa292 ER |
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+)$/', $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 | ||
1a02d719 ER |
80 | $default_opt = array( |
81 | 'u' => '', | |
82 | 'm' => '', | |
83 | 'e' => null, | |
ba2058f9 | 84 | 'v' => null, |
1a02d719 ER |
85 | 'w' => 0, |
86 | 'c' => 0, | |
87 | ); | |
48c29855 ER |
88 | $opt = array_merge($default_opt, getopt("u:e:m:w:c:vi")); |
89 | $invert = isset($opt['i']); | |
ebdaa292 ER |
90 | $verbose = isset($opt['v']); |
91 | $critical = byteConvert($opt['c']); | |
92 | $warning = byteConvert($opt['w']); | |
1a02d719 ER |
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'] .';'; | |
ebdaa292 | 111 | if ($verbose) { |
ba2058f9 ER |
112 | echo "EVAL: $eval\n"; |
113 | } | |
1a02d719 ER |
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); | |
ebdaa292 ER |
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)) { | |
1a02d719 ER |
130 | echo LABEL, ": CRITICAL: {$opt['m']}: $res\n"; |
131 | exit(STATE_CRITICAL); | |
ebdaa292 | 132 | } elseif ((!$invert && $value > $warning) || ($invert && $value < $warning)) { |
1a02d719 ER |
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 | } |