-
Notifications
You must be signed in to change notification settings - Fork 13
/
statusJson.php
357 lines (305 loc) · 12.2 KB
/
statusJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
// statusJson.php
//
// Script to parse Nagios status.dat and present it as JSON, to allow inclusion
// of status info in a web page served from a separate machine.
//
// This code is just a modification of Jason Antman's statusXML.php available at
// https://github.com/jantman/php-nagios-xml
//
// +----------------------------------------------------------------------+
// | Copyright (c) 2013 Christian Lizell. |
// | |
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation; either version 3 of the License, or |
// | (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// +----------------------------------------------------------------------+
// Change this accordingly
$statusFile = "/opt/local/var/nagios/status.dat";
$nag_version = getFileVersion($statusFile);
$created_ts = 0;
$debug = false;
if ($nag_version == 4) {
$data = getData4($statusFile);
} else if ($nag_version == 3) {
$data = getData3($statusFile);
} else {
$data = getData2($statusFile);
}
$hosts = $data['hosts'];
$services = $data['services'];
$program = "";
if (array_key_exists("program", $data)) {
$program = $data['program'];
}
outputJson($hosts, $services, $program);
function outputJson($hosts, $services, $program)
{
// begin outputting XML
header("Content-type: application/json");
echo "{" . "\n";
// program status
if ($program != "") {
echo ' "programStatus": {' . "\n";
foreach ($program as $key => $val) {
echo ' "' . jsonString($key) . '": "' . jsonString($val) . '"' . (isLast($program, $key) ? '' : ',') . "\n";
}
unset($key, $val);
echo ' },' . "\n";
}
// hosts
echo ' "hosts": {' . "\n";
foreach ($hosts as $hostName => $hostArray) {
echo ' "' . jsonString($hostName) . '": {' . "\n";
foreach ($hostArray as $key => $val) {
echo ' "' . jsonString($key) . '": "' . jsonString($val) . '"' . (isLast($hostArray, $key) ? '' : ',') . "\n";
}
unset($key, $val);
echo ' }' . (isLast($hosts, $hostName) ? '' : ',') . "\n";
}
unset($hostName, $hostArray);
echo ' },' . "\n";
// loop through the services
echo ' "services": {' . "\n";
foreach ($services as $hostName => $service) {
echo ' "' . jsonString($hostName) . '": {' . "\n";
foreach ($service as $serviceDesc => $serviceArray) {
echo ' "' . jsonString($serviceDesc) . '": {' . "\n";
foreach ($serviceArray as $key => $val) {
echo ' "' . jsonString($key) . '": "' . jsonString($val) . '"' . (isLast($serviceArray, $key) ? '' : ',') . "\n";
}
unset($key, $val);
echo ' }' . (isLast($service, $serviceDesc) ? '' : ',') . "\n";
}
echo ' }' . (isLast($services, $hostName) ? '' : ',') . "\n";
unset($serviceDesc, $serviceArray);
}
unset($hostName, $service);
echo ' }' . "\n";
echo "}";
}
// Determines if the given key is last in the given array
function isLast($array, $key)
{
end($array);
return ($key === key($array));
}
// replace reserved characters in json
function jsonString($s)
{
$s = str_replace('\\', '\\\\', $s);
$s = str_replace('"', '\"', $s);
$s = str_replace("\t", '\t', $s);
$s = str_replace("\n", " ", $s);
$s = str_replace("\r", " ", $s);
return $s;
}
// figure out what version the file is
function getFileVersion($statusFile)
{
global $created_ts;
$version = 2;
$fh = fopen($statusFile, 'r');
$inInfo = false;
while ($line = fgets($fh)) {
if (trim($line) == "info {") {
$inInfo = true;
} elseif (trim($line) == "}") {
$inInfo = false;
break;
} elseif ($inInfo) {
$vals = explode("=", $line);
if (trim($vals[0]) == "created") {
$created = $vals[1];
} elseif (trim($vals[0]) == "version") {
$version = substr($vals[1], 0, 1);
}
}
}
return $version;
}
// parse nagios2 status.dat
function getData2($statusFile)
{
// the keys to get from host status:
$host_keys = array('host_name', 'has_been_checked', 'check_execution_time', 'check_latency', 'check_type', 'current_state', 'current_attempt', 'state_type', 'last_state_change', 'last_time_up', 'last_time_down', 'last_time_unreachable', 'last_notification', 'next_notification', 'no_more_notifications', 'current_notification_number', 'notifications_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'active_checks_enabled', 'passive_checks_enabled', 'last_update');
// keys to get from service status:
$service_keys = array('host_name', 'service_description', 'has_been_checked', 'check_execution_time', 'check_latency', 'current_state', 'state_type', 'last_state_change', 'last_time_ok', 'last_time_warning', 'last_time_unknown', 'last_time_critical', 'plugin_output', 'last_check', 'notifications_enabled', 'active_checks_enabled', 'passive_checks_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'last_update', 'is_flapping');
# open the file
$fh = fopen($statusFile, 'r');
# variables to keep state
$inSection = false;
$sectionType = "";
$lineNum = 0;
$sectionData = array();
$hostStatus = array();
$serviceStatus = array();
#variables for total hosts and services
$typeTotals = array();
# loop through the file
while ($line = fgets($fh)) {
$lineNum++; // increment counter of line number, mainly for debugging
$line = trim($line); // strip whitespace
if ($line == "") {
continue;
} // ignore blank line
if (substr($line, 0, 1) == "#") {
continue;
} // ignore comment
// ok, now we need to deal with the sections
if (!$inSection) {
// we're not currently in a section, but are looking to start one
if (strstr($line, " ") && (substr($line, -1) == "{")) // space and ending with {, so it's a section header
{
$sectionType = substr($line, 0, strpos($line, " ")); // first word on line is type
$inSection = true;
// we're now in a section
$sectionData = array();
// increment the counter for this sectionType
if (isset($typeTotals[$sectionType])) {
$typeTotals[$sectionType] = $typeTotals[$sectionType] + 1;
} else {
$typeTotals[$sectionType] = 1;
}
}
}
if ($inSection && $line == "}") // closing a section
{
if ($sectionType == "service") {
$serviceStatus[$sectionData['host_name']][$sectionData['service_description']] = $sectionData;
}
if ($sectionType == "host") {
$hostStatus[$sectionData["host_name"]] = $sectionData;
}
$inSection = false;
$sectionType = "";
continue;
} else {
// we're currently in a section, and this line is part of it
$lineKey = substr($line, 0, strpos($line, "="));
$lineVal = substr($line, strpos($line, "=") + 1);
// add to the array as appropriate
if ($sectionType == "service") {
if (in_array($lineKey, $service_keys)) {
$sectionData[$lineKey] = $lineVal;
}
} elseif ($sectionType == "host") {
if (in_array($lineKey, $host_keys)) {
$sectionData[$lineKey] = $lineVal;
}
}
// else continue on, ignore this section, don't save anything
}
}
fclose($fh);
$retArray = array("hosts" => $hostStatus, "services" => $serviceStatus);
return $retArray;
}
// parse nagios3 status.dat
function getData3($statusFile)
{
global $debug;
# open the file
$fh = fopen($statusFile, 'r');
# variables to keep state
$inSection = false;
$sectionType = "";
$lineNum = 0;
$sectionData = array();
$hostStatus = array();
$serviceStatus = array();
$programStatus = array();
#variables for total hosts and services
$typeTotals = array();
# loop through the file
while ($line = fgets($fh)) {
$lineNum++; // increment counter of line number, mainly for debugging
$line = trim($line); // strip whitespace
if ($line == "") {
continue;
} // ignore blank line
if (substr($line, 0, 1) == "#") {
continue;
} // ignore comment
// ok, now we need to deal with the sections
if (!$inSection) {
// we're not currently in a section, but are looking to start one
if (substr($line, strlen($line) - 1, 1) == "{") // space and ending with {, so it's a section header
{
$sectionType = substr($line, 0, strpos($line, " ")); // first word on line is type
$inSection = true;
// we're now in a section
$sectionData = array();
// increment the counter for this sectionType
if (isset($typeTotals[$sectionType])) {
$typeTotals[$sectionType] = $typeTotals[$sectionType] + 1;
} else {
$typeTotals[$sectionType] = 1;
}
}
} elseif ($inSection && trim($line) == "}") // closing a section
{
if ($sectionType == "servicestatus") {
$serviceStatus[$sectionData['host_name']][$sectionData['service_description']] = $sectionData;
} elseif ($sectionType == "hoststatus") {
$hostStatus[$sectionData["host_name"]] = $sectionData;
} elseif ($sectionType == "programstatus") {
$programStatus = $sectionData;
}
$inSection = false;
$sectionType = "";
continue;
} else {
// we're currently in a section, and this line is part of it
$lineKey = substr($line, 0, strpos($line, "="));
$lineVal = substr($line, strpos($line, "=") + 1);
// add to the array as appropriate
if ($sectionType == "servicestatus" || $sectionType == "hoststatus" || $sectionType == "programstatus") {
if ($debug) {
echo "LINE " . $lineNum . ": lineKey=" . $lineKey . "= lineVal=" . $lineVal . "=\n";
}
$sectionData[$lineKey] = $lineVal;
}
// else continue on, ignore this section, don't save anything
}
}
fclose($fh);
$retArray = array("hosts" => $hostStatus, "services" => $serviceStatus, "program" => $programStatus);
return $retArray;
}
// parse nagios4 status.dat
function getData4($statusFile)
{
// For now just re-use the nagios3 parsing
return getData3($statusFile);
}
// this formats the age of a check in seconds into a nice textual description
function ageString($seconds)
{
$age = "";
if ($seconds > 86400) {
$days = (int)($seconds / 86400);
$seconds = $seconds - ($days * 86400);
$age .= $days . " days ";
}
if ($seconds > 3600) {
$hours = (int)($seconds / 3600);
$seconds = $seconds - ($hours * 3600);
$age .= $hours . " hours ";
}
if ($seconds > 60) {
$minutes = (int)($seconds / 60);
$seconds = $seconds - ($minutes * 60);
$age .= $minutes . " minutes ";
}
$age .= $seconds . " seconds ";
return $age;
}
?>