-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincludes.php
More file actions
229 lines (177 loc) · 7.89 KB
/
includes.php
File metadata and controls
229 lines (177 loc) · 7.89 KB
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
<?php
function formatInterval($interval) {
$return = array();
if ($interval->m > 1 ) {
array_push($return, $interval->format('%m Months'));
} elseif ($interval->m == 1) {
array_push($return, $interval->format('%m Month'));
}
if ($interval->d > 1 ) {
array_push($return, $interval->format('%d Days'));
} elseif ($interval->d == 1) {
array_push($return, $interval->format('%d Day'));
}
if ($interval->h > 1 ) {
array_push($return, $interval->format('%h Hours'));
} elseif ($interval->h == 1) {
array_push($return, $interval->format('%h Hour'));
}
if ($interval->i > 1 ) {
array_push($return, $interval->format('%i Minutes'));
} elseif ($interval->i == 1) {
array_push($return, $interval->format('%i Minute'));
}
if ($interval->s > 1 ) {
array_push($return, $interval->format('%s Seconds'));
} elseif ($interval->s == 1) {
array_push($return, $interval->format('%s Second'));
}
return join(", ", array_slice($return, 0,2));
}
function statusToClass($status) {
if ($status == 2) {
$class = "noc_critical";
} elseif ($status == 1 || $status == 3) {
$class = "noc_warning";
} else {
$class = "noc_ok";
}
return $class;
}
function hostStatusToClass($status) {
if ($status == 0) {
$class = "noc_ok";
} else {
$class = "noc_critical";
}
return $class;
}
function queryServicesRow($query) {
// limit what the user can see
$args = array(
"sql" => $query,
"objectauthfields" => array(
"nagios_servicestatus.service_object_id",
),
"objectauthperms" => P_READ,
);
$query = limit_sql_by_authorized_object_ids($args);
$result = mysql_query($query);
$row = mysql_fetch_array($result);
return $row;
}
function queryServicesAll($query,$order_by) {
// limit what the user can see
$args = array(
"sql" => $query,
"objectauthfields" => array(
"nagios_servicestatus.service_object_id",
),
"objectauthperms" => P_READ,
);
$query = limit_sql_by_authorized_object_ids($args);
$result = mysql_query($query . $order_by);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
array_push($rows, $row);
}
return $rows;
}
function getServicesStatus() {
$results = array();
$select_agg = "SELECT count(*) as total, max(nagios_servicestatus.current_state) as status ";
$select_rows = "SELECT obj1.name1 AS host_name, obj1.name2 AS service_name, nagios_servicestatus.current_state as status, nagios_servicestatus.output, nagios_servicestatus.last_hard_state_change,nagios_servicestatus.last_time_ok, nagios_servicestatus.last_check, nagios_servicestatus.problem_has_been_acknowledged,
nagios_hosts.address as ha, nagios_hosts.alias as host_description, nagios_hoststatus.problem_has_been_acknowledged AS hack, nagios_services.service_id as sid ,
nagios_servicestatus.servicestatus_id as ssid,
nagios_hosts.host_object_id AS hid ";
$base_joins = "
FROM nagios_servicestatus
LEFT JOIN nagios_objects AS obj1 ON nagios_servicestatus.service_object_id=obj1.object_id
LEFT JOIN nagios_services ON nagios_servicestatus.service_object_id=nagios_services.service_object_id
LEFT JOIN nagios_hosts ON nagios_services.host_object_id=nagios_hosts.host_object_id
LEFT JOIN nagios_hoststatus ON nagios_hosts.host_object_id=nagios_hoststatus.host_object_id
";
$base_where = " WHERE nagios_hoststatus.problem_has_been_acknowledged='0' AND nagios_hoststatus.last_hard_state='0' AND nagios_hoststatus.current_state='0' AND nagios_hoststatus.scheduled_downtime_depth='0' ";
$all_where = " WHERE 1=1 ";
$up_query = $base_where . " AND nagios_servicestatus.scheduled_downtime_depth='0' AND nagios_hoststatus.scheduled_downtime_depth='0' AND nagios_servicestatus.problem_has_been_acknowledged='0' AND nagios_servicestatus.current_state = '0' ";
$down_query = $base_where . " AND nagios_servicestatus.scheduled_downtime_depth='0' AND nagios_hoststatus.scheduled_downtime_depth='0' AND nagios_servicestatus.problem_has_been_acknowledged='0' AND nagios_servicestatus.current_state != '0' ";
$ack_query = $base_where . " AND nagios_servicestatus.scheduled_downtime_depth='0' AND nagios_hoststatus.scheduled_downtime_depth='0' AND nagios_servicestatus.problem_has_been_acknowledged='1' AND nagios_servicestatus.current_state != '0' ";
$sch_query = $base_where . " AND nagios_servicestatus.scheduled_downtime_depth!='0' AND nagios_hoststatus.scheduled_downtime_depth='0' AND nagios_servicestatus.problem_has_been_acknowledged='1' AND nagios_servicestatus.current_state != '0' ";
$order_by = " ORDER BY nagios_servicestatus.last_time_ok DESC";
$row = queryServicesRow($select_agg . $base_joins . $down_query);
$results['down'] = $row[0];
$results['status'] = $row[1];
$row = queryServicesRow($select_agg . $base_joins . $ack_query);
$results['ack'] = $row[0];
$row = queryServicesRow($select_agg . $base_joins . " WHERE 1=1 ");
$results['total'] = $row[0];
$results['down_pct'] = round($results['down'] / $results['total'] * 100, 2);
$results['up'] = $results['total'] - $results['down'];
$results['up_pct'] = round($results['up'] / $results['total'] * 100, 2);
//$results['down_services'] = queryServicesAll($select_rows . $base_joins . $base_where . $down_query);
$results['down_services'] = queryServicesAll($select_rows . $base_joins . $down_query, $order_by);
return $results;
}
function queryHostRow($query) {
$args = array(
"sql" => $query,
"objectauthfields" => array(
"nagios_hoststatus.host_object_id",
),
"objectauthperms" => P_READ,
);
$query = limit_sql_by_authorized_object_ids($args);
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
return $row;
}
function queryHostsAll($query, $order_by) {
// limit what the user can see
$args = array(
"sql" => $query,
"objectauthfields" => array(
"nagios_hoststatus.host_object_id",
),
"objectauthperms" => P_READ,
);
$query = limit_sql_by_authorized_object_ids($args);
$result = mysql_query($query . $order_by);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
array_push($rows, $row);
}
return $rows;
}
function getHostsStatus() {
$results = array();
$select_agg = "SELECT count(*) as total ";
$select_rows = "SELECT obj1.name1 AS host_name, nagios_hosts.alias, nagios_hosts.address, current_state as status, problem_has_been_acknowledged , nagios_hoststatus.last_check,nagios_hoststatus.last_time_up ";
$base_joins = " FROM nagios_hoststatus
LEFT JOIN nagios_objects AS obj1 ON nagios_hoststatus.host_object_id=obj1.object_id
LEFT JOIN nagios_hosts ON nagios_hoststatus.host_object_id=nagios_hosts.host_object_id
";
$query_down = " WHERE scheduled_downtime_depth='0' AND problem_has_been_acknowledged = '0' AND current_state!='0'";
$query_ack = " WHERE scheduled_downtime_depth='0' AND problem_has_been_acknowledged != '0' AND current_state!='0'";
$query_warn = " WHERE scheduled_downtime_depth='0' AND problem_has_been_acknowledged != '0' AND (current_state = '1' or current_state = '3')";
$query_exclude_down_parents = " AND (SELECT count(1) FROM nagios_host_parenthosts ph JOIN nagios_hoststatus ON nagios_hoststatus.host_object_id = ph.parent_host_object_id
WHERE ph.host_id = nagios_hosts.host_id
AND scheduled_downtime_depth='0'
AND problem_has_been_acknowledged='0'
AND current_state!='0'
) < 1";
$query_all = " WHERE 1=1";
$order_by = " ORDER BY nagios_hoststatus.last_time_up DESC";
$row = queryHostRow($select_agg . $base_joins . $query_all);
$results['total'] = $row['total'];
$row = queryHostRow($select_agg . $base_joins . $query_ack);
$results['ack'] = $row['total'];
$row = queryHostRow($select_agg . $base_joins . $query_down, $order_by);
$results['down'] = $row['total'];
if ($results['down'] == 0) {
$results['status'] == 0;
} else {
$results['status'] = 2;
}
$results['down_hosts'] = queryHostsAll($select_rows . $base_joins . $query_down . $query_exclude_down_parents);
return $results;
}