forked from saz/Naglite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
260 lines (236 loc) · 10.2 KB
/
lib.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
<?php
# vim: set ts=4 sts=4 sw=4 et:
function duration($end) {
$DAY = 86400;
$HOUR = 3600;
$now = time();
$diff = $now - $end;
$days = floor($diff / $DAY);
$hours = floor(($diff % $DAY) / $HOUR);
$minutes = floor((($diff % $DAY) % $HOUR) / 60);
$secs = $diff % 60;
return sprintf("%dd, %02d:%02d:%02d", $days, $hours, $minutes, $secs);
}
function hostsTable($nagios, $hosts) {
echo '<table class="hostsTable" cellspacing="1" cellpadding="1">';
echo '<tr><th width="30%">Host</th><th width="20%">Status</th><th width="20%">Duration</th><th width="30%">Status Information</th></tr>';
foreach($hosts['down'] as $host) {
$state = $nagios["host"][$host["current_state"]];
echo "<tr class='".$state."'>\n";
echo "<td class='hostname'>{$host["host_name"]}</td>\n";
echo "<td class='state'>{$state}</td>\n";
echo "<td class='duration'>".duration($host["last_state_change"])."</td>\n";
echo "<td class='output'>" . htmlspecialchars($host['plugin_output']) . "</td>\n";
echo "</tr>\n";
}
echo '</table>';
}
function servicesTable($nagios, $services, $select = false, $type = false) {
global $maxOutputLength;
print(sprintf("<table class=\"servicesTable\" cellspacing=\"1\" cellpadding=\"1\"><tr class='%s'>\n", ($type ? $type : '')));
// TODO: move percentages to the CSS file
print(
'<th width="20%">Host</th>'.
'<th width="20%">Service</th>'.
'<th width="10%">Status</th>'.
'<th width="15%">Duration</th>'.
'<th width="5%">Attempts</th>'.
'<th width="30%">Plugin Output</th>'."\n"
);
print("</tr>\n");
foreach ($select as $selectedType) {
if (isset($services[$selectedType]) && !empty($services[$selectedType])) {
foreach ($services[$selectedType] as $service) {
$state = $nagios["service"][$service["current_state"]];
if (false === $type) {
$rowType = $state;
} else {
$rowType = $type;
if ("acknowledged" !== $type) {
$state = $type;
}
}
print(sprintf("<tr class='%s'>\n", $rowType));
print(sprintf("<td class='hostname'>%s</td>\n", $service['host_name']));
print(sprintf("<td class='service'>%s</td>\n", $service['service_description']));
print(sprintf("<td class='state'>%s", $state));
if ($service["current_attempt"] < $service["max_attempts"]) {
print(" (Soft)");
}
print("</td>\n");
print(sprintf("<td class='duration'>%s</td>\n", duration($service['last_state_change'])));
print(sprintf("<td class='attempts'>%s/%s</td>\n", $service['current_attempt'], $service['max_attempts']));
$plugin_output = str_split($service['plugin_output'], $maxOutputLength);
$plugin_output = $plugin_output[0] . ((count($plugin_output) > 1) ? '...' : '');
echo '<td class="output">' . htmlspecialchars(strip_tags($plugin_output)) . '</td>' . "\n";
print("</tr>\n");
}
}
}
print("</table>\n");
}
function sectionHeader($section, $counter) {
echo '<div id="' . $section . '" class="section">';
echo '<h2 class="sectionTitle">' . ucfirst($section) . '</h2>';
echo '<div class="stats">';
foreach($counter[$section] as $type => $value) {
if ($value > 0) {
echo '<div class="stat ' . $type . '">' . strtoupper($type) . ': ' . $value . '</div>';
}
}
echo '</div></div>';
}
function parse_status_file($statusFile, $statusFileTimeout) {
// Check if status file is readable
if (!is_readable($statusFile)) {
return array('status' => 'error', 'message' => "Failed to read nagios status from '$statusFile'");
}
// Check if the status file is fresh enough
if ((time() - filemtime($statusFile)) > $statusFileTimeout) {
return array('status' => 'error', 'message' => 'Status File is too old');
}
$nagiosStatusData = file($statusFile);
$in = false;
$type = "unknown";
$status = array();
$host = null;
$lineCount = count($nagiosStatusData);
for($i = 0; $i < $lineCount; $i++) {
if(false === $in) {
$pos = strpos($nagiosStatusData[$i], "{");
if (false !== $pos) {
$in = true;
$type = substr($nagiosStatusData[$i], 0, $pos-1);
if(!empty($status[$type])) {
$arrPos = count($status[$type]);
} else {
$arrPos = 0;
}
continue;
}
} else {
$pos = strpos($nagiosStatusData[$i], "}");
if (false !== $pos) {
$in = false;
$type = "unknown";
$host = null;
continue;
}
// Line with data found
list($key, $value) = explode("=", trim($nagiosStatusData[$i]), 2);
if ("hoststatus" === $type) {
if ("host_name" === $key) {
$host = $value;
}
$status[$type][$host][$key] = $value;
} else {
$status[$type][$arrPos][$key] = $value;
}
}
}
return array('status' => 'ok', 'data' => array('status' => $status));
}
function get_nagios_status($nagios, $statusFile, $statusFileTimeout) {
$nagios_status_data = parse_status_file($statusFile, $statusFileTimeout);
if ($nagios_status_data['status'] != 'ok') {
return $nagios_status_data;
}
$status = $nagios_status_data['data']['status'];
$counter = array(
'hosts' => array(
'ok' => 0,
'down' => 0,
'unreachable' => 0,
'acknowledged' => 0,
'notification' => 0,
'pending' => 0
),
'services' => array(
'ok' => 0,
'warning' => 0,
'critical' => 0,
'unknown' => 0,
'acknowledged' => 0,
'notification' => 0,
'pending' => 0,
)
);
$states = array();
foreach (array_keys($status) as $type) {
switch ($type) {
case "hoststatus":
$hosts = $status[$type];
foreach ($hosts as $host) {
if ((int)$host['scheduled_downtime_depth'] > 0) {
continue;
} else if ($host['problem_has_been_acknowledged'] == '1') {
$counter['hosts']['acknowledged']++;
$states['hosts']['acknowledged'][] = $host['host_name'];
} else if ($host['notifications_enabled'] == 0) {
$counter['hosts']['notification']++;
$states['hosts']['notification'][] = $host['host_name'];
} else if ($host['has_been_checked'] == 0) {
$counter['hosts']['pending']++;
$states['hosts']['pending'][] = $host['host_name'];
} else {
switch ($host['current_state']) {
case $nagios['host']['ok']:
$counter['hosts']['ok']++;
break;
case $nagios['host']['down']:
$counter['hosts']['down']++;
$states['hosts']['down'][] = $host;
break;
case $nagios['host']['unreachable']:
$counter['hosts']['unreachable']++;
$states['hosts']['unreachable'][] = $host['host_name'];
break;
}
}
}
break;
case "servicestatus":
$services = $status[$type];
foreach ($services as $service) {
// Ignore all services if host state is not ok
$state = $status['hoststatus'][$service['host_name']]['current_state'];
if ($nagios['host']['ok'] != $state) {
continue;
}
if ((int)$service['scheduled_downtime_depth'] > 0) {
continue;
} else if ($service['problem_has_been_acknowledged'] == '1') {
$counter['services']['acknowledged']++;
$states['services']['acknowledged'][] = $service;
} else if ($service['notifications_enabled'] == '0') {
$counter['services']['notification']++;
$states['services']['notification'][] = $service;
} else if ($service['has_been_checked'] == '0') {
$counter['services']['pending']++;
$states['services']['pending'][] = $service;
} else {
switch ($service['current_state']) {
case $nagios['service']['ok']:
$counter['services']['ok']++;
break;
case $nagios['service']['warning']:
$counter['services']['warning']++;
$states['services']['warning'][] = $service;
break;
case $nagios['service']['critical']:
$counter['services']['critical']++;
$states['services']['critical'][] = $service;
break;
case $nagios['service']['unknown']:
$counter['services']['unknown']++;
$states['services']['unknown'][] = $service;
break;
}
}
}
break;
}
}
return array('status' => 'ok', 'data' => array('counter' => $counter, 'states' => $states));
}
?>