forked from lozzd/Nagdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagdash.php
416 lines (360 loc) · 16.8 KB
/
nagdash.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'config.php';
require_once 'utils.php';
require_once 'timeago.php';
if (!function_exists('curl_init')) {
die("ERROR: The PHP curl extension must be installed for Nagdash to function");
}
$nagios_host_status = array(0 => "UP", 1 => "DOWN", 2 => "UNREACHABLE");
$nagios_service_status
= array(0 => "OK", 1 => "WARNING", 2 => "CRITICAL", 3 => "UNKNOWN");
$nagios_host_status_colour
= array(0 => "status_green", 1 => "status_red", 2 => "status_yellow");
$nagios_service_status_colour
= array(0 => "status_green", 1 => "status_yellow", 2 => "status_red", 3 => "status_grey");
$nagios_toggle_status = array(0 => "disabled", 1 => "enabled");
$sort_by_time = ( isset($sort_by_time) && $sort_by_time ) ? true : false;
$errors = array();
$state = array();
$host_summary = array();
$service_summary = array();
$down_hosts = array();
$known_hosts = array();
$known_services = array();
$broken_services = array();
$curl_stats = array();
$api_cols = [
'livestatus' => [
'state' => 'state',
'ack' => 'acknowledged',
'max_attempts' => 'max_check_attempts',
'service_name' => 'description',
'host_name' => 'host_name',
],
'nagios-api' => [
'state' => 'current_state',
'ack' => 'problem_has_been_acknowledged',
'max_attempts' => 'max_attempts',
'service_name' => 'service_name',
'host_name' => 'name',
]
];
function fetch_json($hostname,$port,$protocol,$url) {
global $curl_stats;
$ch = curl_init("$protocol://$hostname:$port$url");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
$info = curl_getinfo($ch);
if (curl_errno($ch)) {
return "<pre>Attempt to hit API failed, sorry. Curl said: " . curl_error($ch) . "</pre>";
} elseif ($info['http_code'] != 200) {
return "<pre>Attempt to hit API failed, sorry. Curl said: HTTP Status {$info['http_code']} </pre>";
} else {
$curl_stats["$hostname:$port"] = curl_getinfo($ch);
}
curl_close($ch);
return json_decode($json, true);
}
function fetch_state_livestatus($hostname, $port, $protocol) {
$state = fetch_json(
$hostname, $port, $protocol,
"/nagios/livestatus/index.php/hosts?" .
"Columns=name,state,acknowledged,last_state_change,downtimes"
);
if (is_string($state)){
return $state;
}
$curl_stats["$hostname:$port"]['objects'] = count($state);
$munge = [];
foreach ($state as $host) {
$host['services'] = [];
$munge[$host['name']] = $host;
}
$state = $munge;
$services = fetch_json(
$hostname, $port, $protocol,
"/nagios/livestatus/index.php/services?" .
"Columns=description,host_name,plugin_output,notifications_enabled," .
"downtimes,scheduled_downtime_depth,state,last_state_change," .
"current_attempt,max_check_attempts,acknowledged"
);
foreach ($services as $service) {
$hostname = $service['host_name'];
if ($state[$hostname]) {
$state[$hostname]['services'][$service['description']] = $service;
}
}
return $state;
}
function fetch_state_nagios_api($hostname, $port, $protocol) {
$response = fetch_json($hostname, $port, $protocol, "/state");
return $response['content'];
}
// Function that does the dirty to connect to the Nagios API
function fetch_state($hostname, $port, $protocol) {
global $api_type;
switch ($api_type) {
case "livestatus":
$state = fetch_state_livestatus($hostname, $port, $protocol);
break;
case "nagios-api":
default:
$state = fetch_state_nagios_api($hostname, $port, $protocol);
break;
}
return $state;
}
// Check to see if the user has a cookie that disables some hosts
$unwanted_hosts = unserialize($_COOKIE['nagdash_unwanted_hosts']);
if (!is_array($unwanted_hosts)) $unwanted_hosts = array();
// Collect the API data from each Nagios host.
foreach ($nagios_hosts as $host) {
// Check if the host has been disabled locally
if (!in_array($host['tag'], $unwanted_hosts)) {
$host_state = fetch_state($host['hostname'], $host['port'], $host['protocol']);
if (is_string($host_state)) {
$errors[] = "Could not connect to API on host {$host['hostname']}, port {$host['port']}: {$host_state}";
} else {
foreach ($host_state as $this_host => $null) {
$host_state[$this_host]['tag'] = $host['tag'];
}
$state += (array) $host_state;
}
}
}
if (isset($mock_state_file)) {
$data = json_decode(file_get_contents($mock_state_file), true);
$state = $data['content'];
}
// Sort the array alphabetically by hostname.
deep_ksort($state);
// At this point, the data collection is completed.
if (count($errors) > 0) {
foreach ($errors as $error) {
echo "<div class='status_red'>{$error}</div>";
}
}
foreach ($state as $hostname => $host_detail) {
// Check if the host matches the filter
if (preg_match("/$filter/", $hostname)) {
// If the host is NOT OK...
if ($host_detail[$api_cols[$api_type]['state']] != 0) {
// Sort the host into the correct array. It's either a known issue or not.
if ( ($host_detail[$api_cols[$api_type]['ack']] > 0) || ($host_detail['scheduled_downtime_depth'] > 0) || ($host_detail['notifications_enabled'] == 0) ) {
$array_name = "known_hosts";
} else {
$array_name = "down_hosts";
}
// Populate the array.
array_push($$array_name, array(
"hostname" => $hostname,
"host_state" => $host_detail[$api_cols[$api_type]['state']],
"duration" => timeago($host_detail['last_state_change'], null, null, false),
"detail" => $host_detail['plugin_output'],
"current_attempt" => $host_detail['current_attempt'],
"max_attempts" => $host_detail['max_attempts'],
"tag" => $host_detail['tag'],
"is_hard" => ($host_detail['current_attempt'] >= $host_detail['max_attempts']) ? true : false,
"is_downtime" => ($host_detail['scheduled_downtime_depth'] > 0) ? true : false,
"is_ack" => ($host_detail[$api_cols[$api_type]['ack']] > 0) ? true : false,
"is_enabled" => ($host_detail['notifications_enabled'] > 0) ? true : false,
));
}
// In any case, increment the overall status counters.
$host_summary[$host_detail[$api_cols[$api_type]['state']]]++;
// Now parse the statuses for this host.
foreach ($host_detail['services'] as $service_name => $service_detail) {
// If the host is OK, AND the service is NOT OK.
if ($service_detail[$api_cols[$api_type]['state']] != 0 && $host_detail[$api_cols[$api_type]['state']] == 0) {
// Sort the service into the correct array. It's either a known issue or not.
if ( ($service_detail[$api_cols[$api_type]['ack']] > 0)
|| ($service_detail['scheduled_downtime_depth'] > 0)
|| ($service_detail['notifications_enabled'] == 0 )
|| ($host_detail['scheduled_downtime_depth'] > 0)
) {
$array_name = "known_services";
} else {
$array_name = "broken_services";
}
$downtime_remaining = null;
$downtimes = array_merge($service_detail['downtimes'], $host_detail['downtimes']);
if ($host_detail['scheduled_downtime_depth'] > 0
|| $service_detail['scheduled_downtime_depth'] > 0
) {
if (count($downtimes) > 0) {
$downtime_info = array_pop($downtimes);
$downtime_remaining = "- ". timeago($downtime_info['end_time'], null, null, false) . " left";
}
}
array_push($$array_name, array(
"hostname" => $hostname,
"service_name" => $service_name,
"service_state" => $service_detail[$api_cols[$api_type]['state']],
"duration" => timeago($service_detail['last_state_change'], null, null, false),
"last_state_change" => $service_detail['last_state_change'],
"detail" => $service_detail['plugin_output'],
"current_attempt" => $service_detail['current_attempt'],
"max_attempts" => $service_detail[$api_cols[$api_type]['max_attempts']],
"tag" => $host_detail['tag'],
"is_hard" => ($service_detail['current_attempt'] >= $service_detail[$api_cols[$api_type]['max_attempts']]) ? true : false,
"is_downtime" => ($service_detail['scheduled_downtime_depth'] > 0 || $host_detail['scheduled_downtime_depth'] > 0) ? true : false,
"downtime_remaining" => $downtime_remaining,
"is_ack" => ($service_detail[$api_cols[$api_type]['ack']] > 0) ? true : false,
"is_enabled" => ($service_detail['notifications_enabled'] > 0) ? true : false,
));
}
if ($host_detail['state'] == 0) {
$service_summary[$service_detail[$api_cols[$api_type]['state']]]++;
}
}
}
}
ksort($host_summary);
ksort($service_summary);
?>
<div id="info-window"><button class="close" onClick='$("#info-window").fadeOut("fast");'>×</button><div id="info-window-text"></div></div>
<div class="frame">
<div class="section">
<div class="header">
<h3>Host status</h3>
<p class="totals"><b>Total:</b> <?php foreach ($host_summary as $state => $count) { echo "<span class='{$nagios_host_status_colour[$state]}'>{$count}</span> "; } ?></p>
</div>
<?php if (count($down_hosts) > 0) { ?>
<table id="broken_hosts" class="widetable">
<tr><th>Hostname</th><th width="150px">State</th><th>Duration</th><th>Attempts</th><th>Detail</th></tr>
<?php
foreach ($down_hosts as $host) {
$controls = build_controls($host['tag'], $host['hostname'], '');
echo "<tr id='host_row' class='{$nagios_host_status_colour[$host['host_state']]}'>";
echo "<td>{$host['hostname']} " . print_tag($host['tag']) . " <span class='controls'>{$controls}</span></td>";
echo "<td><blink>{$nagios_host_status[$host['host_state']]}</blink></td>";
echo "<td>{$host['duration']}</td>";
echo "<td>{$host['current_attempt']}/{$host['max_attempts']}</td>";
echo "<td class=\"desc\">{$host['detail']}</td>";
echo "</tr>";
}
?>
</table>
<?php } else { ?>
<table class="widetable status_green"><tr><td><b>All hosts OK</b></td></tr></table>
<?php
}
if (count($known_hosts) > 0) {
foreach ($known_hosts as $this_host) {
if ($this_host['is_ack']) $status_text = "ack";
if ($this_host['is_downtime']) $status_text = "downtime";
if (!$this_host['is_enabled']) $status_text = "disabled";
$known_host_list[] = "{$this_host['hostname']} " . print_tag($this_host['tag']) . " <span class='known_hosts_desc'>({$status_text} - {$this_host['duration']})</span>";
}
$known_host_list_complete = implode(" • ", $known_host_list);
echo "<table class='widetable known_hosts'><tr><td><b>Known Problem Hosts: </b> {$known_host_list_complete}</td></tr></table>";
}
?>
</div>
</div>
<div class="frame">
<div class="section">
<div class="header">
<h3>Service status</h3>
<p class="totals"><b>Total:</b> <?php foreach($service_summary as $state => $count) { echo "<span class='{$nagios_service_status_colour[$state]}'>{$count}</span> "; } ?></p>
</div>
<?php if (count($broken_services) > 0) { ?>
<table class="widetable" id="broken_services">
<tr><th width="30%">Hostname</th><th width="50%">Service</th><th width="10%">Duration</th><th width="5%">Attempt</th></tr>
<?php
if ($sort_by_time) {
usort($broken_services,'cmp_last_state_change');
}
foreach($broken_services as $service) {
$soft_style = ($service['is_hard']) ? "" : "status_soft";
$blink_tag = ($service['is_hard'] && $enable_blinking) ? "<blink>" : "";
$controls = build_controls($service['tag'], $service['hostname'], $service['service_name']);
echo "<tr>";
echo "<td>{$service['hostname']} " . print_tag($service['tag']) . " <span class='controls'>{$controls}</span></td>";
echo "<td class='bold {$nagios_service_status_colour[$service['service_state']]} {$soft_style}'>{$blink_tag}{$service['service_name']}<span class='detail'>{$service['detail']}</span></td>";
echo "<td>{$service['duration']}</td>";
echo "<td>{$service['current_attempt']}/{$service['max_attempts']}</td>";
echo "</tr>";
}
?>
</table>
<?php } else { ?>
<table class="widetable status_green"><tr><td><b>All services OK</b></td></tr></table>
<?php }
if ($sort_by_time) {
usort($known_services,'cmp_last_state_change');
}
if (count($known_services) > 0) { ?>
<h4>Known Service Problems</h4>
<table class="widetable known_service" id="known_services">
<tr><th width="30%">Hostname</th><th width="37%">Service</th><th width="18%">State</th><th width="10%">Duration</th><th width="5%">Attempt</th></tr>
<?php
foreach($known_services as $service) {
if ($service['is_ack']) $status_text = "ack";
if ($service['is_downtime']) $status_text = "downtime {$service['downtime_remaining']}";
if (!$service['is_enabled']) $status_text = "disabled";
echo "<tr class='known_service'>";
echo "<td>{$service['hostname']} " . print_tag($service['tag']) . "</td>";
echo "<td>{$service['service_name']}</td>";
echo "<td class='{$nagios_service_status_colour[$service['service_state']]}'>{$nagios_service_status[$service['service_state']]} ({$status_text})</td>";
echo "<td>{$service['duration']}</td>";
echo "<td>{$service['current_attempt']}/{$service['max_attempts']}</td>";
echo "</tr>";
}
?>
</table>
<?php } ?>
</div>
</div>
<?php
echo "<!-- nagios-api server status: -->";
foreach ($curl_stats as $server => $server_stats) {
echo "<!-- {$server_stats['url']} returned code {$server_stats['http_code']}, {$server_stats['size_download']} bytes ";
echo "in {$server_stats['total_time']} seconds (first byte: {$server_stats['starttransfer_time']}). JSON parsed {$server_stats['objects']} hosts -->\n";
}
?>
<?php
// Utility function to sort the aggregated array by keys.
function deep_ksort(&$arr) {
ksort($arr);
foreach ($arr as &$a) {
if (is_array($a) && !empty($a)) {
deep_ksort($a);
}
}
}
function cmp_last_state_change($a,$b) {
if ($a['last_state_change'] == $b['last_state_change']) return 0;
return ($a['last_state_change'] > $b['last_state_change']) ? -1 : 1;
}
function build_controls($tag, $host, $service) {
$controls = '<div class="btn-group">';
$controls .= "<a href='#' onClick=\"$.post('do_action.php', {
nag_host: '{$tag}', hostname: '{$host}', service: '{$service}', action: 'ack' }, function(data) { showInfo(data) } ); return false;\" class='btn btn-mini'>
<i class='icon-check'></i> Ack </a>";
if (!isset($service['is_enabled'])) {
$controls .="<a href='#' onClick=\"$.post('do_action.php', {
nag_host: '{$tag}', hostname: '{$host}', service: '{$service}', action: 'disable' }, function(data) { showInfo(data) } ); return false;\" class='btn btn-mini'>
<i class='icon-volume-off'></i> Silence</a>";
} else {
$controls .="<a href='#' onClick=\"$.post('do_action.php', {
nag_host: '{$tag}', hostname: '{$host}', service: '{$service}', action: 'enable' }, function(data) { showInfo(data) } ); return false;\" class='btn btn-mini'>
<i class='icon-volume-up'></i> Unsilence</a>";
}
$controls .= '
<a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#">
<i class="icon-time"></i> Downtime <span class="caret"></span></a>
<ul class="dropdown-menu pull-right">';
$timespans = array("10 minutes" => 10, "30 minutes" => 30, "60 minutes" => 60, "2 hours" => 120, "12 hours" => 720, "1 day" => 1440, "7 days" => 10080);
foreach ($timespans as $name => $minutes) {
$controls .= "<li><a onClick=\"$.post('do_action.php',
{ nag_host: '{$tag}', hostname: '{$host}', service: '{$service}', duration: {$minutes}, action: 'downtime' }, function(data) { showInfo(data) } ); return false;\"
href='#'>{$name}</a></li>";
}
$controls .= "</ul>";
$controls .= "</div>";
return $controls;
}
?>