4 * A model for generating status filters from livestatus
6 class Old_Status_Model
extends Model
{
8 * Combine a sequence of filter-like parameters
9 * @param $type Probably "and" or "or"
10 * @param $filter Usually array, but can also be string
11 * @returns String representing the filter
13 private function filter_combine($type, $filter) {
14 # No filter, anything goes
18 # Multi-parameter filter, use parentheses galore
19 else if (is_array($filter)) {
20 return '('.implode($filter, ') '.$type.' (').')';
22 # String, I guess? Just return it, I'm sure it's awesome
29 * Livestatus port of the classic ninja bitmask-based filters
31 public function classic_filter($type, $host = false, $hostgroup = false, $servicegroup = false, $hoststatustypes = false, $hostprops = false, $servicestatustypes = false, $serviceprops = false) {
35 $hostfilter = array();
36 $hostgroupfilter = array();
37 $servicefilter = array();
38 $servicegroupfilter = array();
39 if( $host != 'all' and $host != '' ) {
41 if( strpos( $host, '*' ) !== false ) {
42 # convert wildcards into real regexp
43 $searchhost = str_replace('.*', '*', $host);
44 $searchhost = str_replace('*', '.*', $searchhost);
45 /* TODO: validate regex */
46 #$errors++ unless Livestatus::is_valid_regular_expression( $searchhost );
47 $hostfilter[] = "name ~~ \"$searchhost\"";
48 $servicefilter[] = "host.name ~~ \"$searchhost\"";
50 $hostfilter[] = "name = \"$host\"";
51 $servicefilter[] = "host.name = \"$host\"";
54 if ( $hostgroup != 'all' and $hostgroup != '' ) {
55 $hostfilter[] = "in \"$hostgroup\"";
56 $servicefilter[] = "host in \"$hostgroup\"";
57 $hostgroupfilter[] = "name = \"$hostgroup\"";
59 if ( $servicegroup != 'all' and $servicegroup != '' ) {
60 $servicefilter[] = "in \"$servicegroup\"";
61 $servicegroupfilter[] = "name = \"$servicegroup\"";
64 $hostfilter = $this->filter_combine("and", $hostfilter);
65 $hostgroupfilter = $this->filter_combine("or", $hostgroupfilter);
66 $servicefilter = $this->filter_combine("and", $servicefilter);
67 $servicegroupfilter = $this->filter_combine("or", $servicegroupfilter);
69 list( $hostfilter, $servicefilter, $host_statustype_filtervalue, $host_prop_filtervalue, $service_statustype_filtervalue, $service_prop_filtervalue ) = $this->extend_filter( $hostfilter, $servicefilter, $hoststatustypes, $hostprops, $servicestatustypes, $serviceprops );
71 return (array( '[hosts] '.$hostfilter, '[services] '.$servicefilter, '[hostgroups] '.$hostgroupfilter, '[servicegroups] '.$servicegroupfilter ));
74 private function extend_filter($hostfilter, $servicefilter, $hoststatustypes, $hostprops, $servicestatustypes, $serviceprops) {
75 $hostfilterlist = array();
76 $servicefilterlist = array();
78 $hostfilter && $hostfilterlist[] = $hostfilter;
79 $servicefilter && $servicefilterlist[] = $servicefilter;
81 # host statustype filter (up,down,...)
82 list( $hoststatustypes, $host_statustype_filter, $host_statustype_filter_service ) = $this->get_host_statustype_filter($hoststatustypes);
83 $host_statustype_filter && $hostfilterlist[] = $host_statustype_filter;
84 $host_statustype_filter_service && $servicefilterlist[] = $host_statustype_filter_service;
86 # host props filter (downtime, acknowledged...)
87 list( $hostprops, $host_prop_filter, $host_prop_filter_service ) = $this->get_host_prop_filter($hostprops);
88 $host_prop_filter && $hostfilterlist[] = $host_prop_filter;
89 $host_prop_filter_service && $servicefilterlist[] = $host_prop_filter_service;
91 # service statustype filter (ok,warning,...)
92 list( $servicestatustypes, $service_statustype_filter_service ) = $this->get_service_statustype_filter($servicestatustypes);
93 $service_statustype_filter_service && $servicefilterlist[] = $service_statustype_filter_service;
95 # service props filter (downtime, acknowledged...)
96 list( $serviceprops, $service_prop_filter_service ) = $this->get_service_prop_filter($serviceprops);
97 $service_prop_filter_service && $servicefilterlist[] = $service_prop_filter_service;
99 $hostfilter = $this->filter_combine("and", $hostfilterlist);
100 $servicefilter = $this->filter_combine("and", $servicefilterlist);
102 return array( $hostfilter, $servicefilter, $hoststatustypes, $hostprops, $servicestatustypes, $serviceprops );
105 private function get_host_statustype_filter($number) {
106 $hoststatusfilter = array();
107 $servicestatusfilter = array();
112 if( $number & nagstat
::HOST_PENDING
) { # 1 - pending
113 $hoststatusfilter[] = 'has_been_checked = 0';
114 $servicestatusfilter[] = 'host.has_been_checked = 0';
116 if( $number & nagstat
::HOST_UP
) { # 2 - up
117 $hoststatusfilter[] = 'has_been_checked = 1 and state = 0';
118 $servicestatusfilter[] = 'host.has_been_checked = 1 and host.state = 0';
120 if( $number & nagstat
::HOST_DOWN
) { # 4 - down
121 $hoststatusfilter[] = 'has_been_checked = 1 and state = 1';
122 $servicestatusfilter[] = 'host.has_been_checked = 1 and host.state = 1';
124 if( $number & nagstat
::HOST_UNREACHABLE
) { # 8 - unreachable
125 $hoststatusfilter[] = 'has_been_checked = 1 and state = 2';
126 $servicestatusfilter[] = 'host.has_been_checked = 1 and host.state = 2';
129 $hostfilter = $this->filter_combine('or', $hoststatusfilter );
130 $servicefilter = $this->filter_combine('or', $servicestatusfilter );
132 return ( array($number, $hostfilter, $servicefilter ));
136 private function get_host_prop_filter($number) {
137 $host_prop_filter = array();
138 $host_prop_filter_service = array();
143 if( $number & nagstat
::HOST_SCHEDULED_DOWNTIME
) { # 1 - In Scheduled Downtime
144 $host_prop_filter[] = 'scheduled_downtime_depth > 0';
145 $host_prop_filter_service[] = 'host.scheduled_downtime_depth > 0';
147 if( $number & nagstat
::HOST_NO_SCHEDULED_DOWNTIME
) { # 2 - Not In Scheduled Downtime
148 $host_prop_filter[] = 'scheduled_downtime_depth = 0';
149 $host_prop_filter_service[] = 'host.scheduled_downtime_depth = 0';
151 if( $number & nagstat
::HOST_STATE_ACKNOWLEDGED
) { # 4 - Has Been Acknowledged
152 $host_prop_filter[] = 'acknowledged = 1';
153 $host_prop_filter_service[] = 'host.acknowledged = 1';
155 if( $number & nagstat
::HOST_STATE_UNACKNOWLEDGED
) { # 8 - Has Not Been Acknowledged
156 $host_prop_filter[] = 'acknowledged = 0';
157 $host_prop_filter_service[] = 'host.acknowledged = 0';
159 if( $number & nagstat
::HOST_CHECKS_DISABLED
) { # 16 - Checks Disabled
160 $host_prop_filter[] = 'checks_enabled = 0';
161 $host_prop_filter_service[] = 'host.checks_enabled = 0';
163 if( $number & nagstat
::HOST_CHECKS_ENABLED
) { # 32 - Checks Enabled
164 $host_prop_filter[] = 'checks_enabled = 1';
165 $host_prop_filter_service[] = 'host.checks_enabled = 1';
167 if( $number & nagstat
::HOST_EVENT_HANDLER_DISABLED
) { # 64 - Event Handler Disabled
168 $host_prop_filter[] = 'event_handler_enabled = 0';
169 $host_prop_filter_service[] = 'host.event_handler_enabled = 0';
171 if( $number & nagstat
::HOST_EVENT_HANDLER_ENABLED
) { # 128 - Event Handler Enabled
172 $host_prop_filter[] = 'event_handler_enabled = 1';
173 $host_prop_filter_service[] = 'host.event_handler_enabled = 1';
175 if( $number & nagstat
::HOST_FLAP_DETECTION_DISABLED
) { # 256 - Flap Detection Disabled
176 $host_prop_filter[] = 'flap_detection_enabled = 0';
177 $host_prop_filter_service[] = 'host.flap_detection_enabled = 0';
179 if( $number & nagstat
::HOST_FLAP_DETECTION_ENABLED
) { # 512 - Flap Detection Enabled
180 $host_prop_filter[] = 'flap_detection_enabled = 1';
181 $host_prop_filter_service[] = 'host.flap_detection_enabled = 1';
183 if( $number & nagstat
::HOST_IS_FLAPPING
) { # 1024 - Is Flapping
184 $host_prop_filter[] = 'is_flapping = 1';
185 $host_prop_filter_service[] = 'host.is_flapping = 1';
187 if( $number & nagstat
::HOST_IS_NOT_FLAPPING
) { # 2048 - Is Not Flapping
188 $host_prop_filter[] = 'is_flapping = 0';
189 $host_prop_filter_service[] = 'host.is_flapping = 0';
191 if( $number & nagstat
::HOST_NOTIFICATIONS_DISABLED
) { # 4096 - Notifications Disabled
192 $host_prop_filter[] = 'notifications_enabled = 0';
193 $host_prop_filter_service[] = 'host.notifications_enabled = 0';
195 if( $number & nagstat
::HOST_NOTIFICATIONS_ENABLED
) { # 8192 - Notifications Enabled
196 $host_prop_filter[] = 'notifications_enabled = 1';
197 $host_prop_filter_service[] = 'host.notifications_enabled = 1';
199 if( $number & nagstat
::HOST_PASSIVE_CHECKS_DISABLED
) { # 16384 - Passive Checks Disabled
200 $host_prop_filter[] = 'accept_passive_checks = 0';
201 $host_prop_filter_service[] = 'host.accept_passive_checks = 0';
203 if( $number & nagstat
::HOST_PASSIVE_CHECKS_ENABLED
) { # 32768 - Passive Checks Enabled
204 $host_prop_filter[] = 'accept_passive_checks = 1';
205 $host_prop_filter_service[] = 'host.accept_passive_checks = 1';
207 if( $number & nagstat
::HOST_PASSIVE_CHECK
) { # 65536 - Passive Checks
208 $host_prop_filter[] = 'check_type = 1';
209 $host_prop_filter_service[] = 'host.check_type = 1';
211 if( $number & nagstat
::HOST_ACTIVE_CHECK
) { # 131072 - Active Checks
212 $host_prop_filter[] = 'check_type = 0';
213 $host_prop_filter_service[] = 'host.check_type = 0';
215 if( $number & nagstat
::HOST_HARD_STATE
) { # 262144 - In Hard State
216 $host_prop_filter[] = 'state_type = 1';
217 $host_prop_filter_service[] = 'host.state_type = 1';
219 if( $number & nagstat
::HOST_SOFT_STATE
) { # 524288 - In Soft State
220 $host_prop_filter[] = 'state_type = 0';
221 $host_prop_filter_service[] = 'host.state_type = 0';
224 $hostfilter = $this->filter_combine('and', $host_prop_filter );
225 $servicefilter = $this->filter_combine('and', $host_prop_filter_service );
227 return ( array( $number, $hostfilter, $servicefilter ));
231 private function get_service_statustype_filter($number) {
232 $servicestatusfilter = array();
237 if( $number & nagstat
::SERVICE_PENDING
) { # 1 - pending
238 $servicestatusfilter[] = 'has_been_checked = 0';
240 if( $number & nagstat
::SERVICE_OK
) { # 2 - ok
241 $servicestatusfilter[] = 'has_been_checked = 1 and state = 0';
243 if( $number & nagstat
::SERVICE_WARNING
) { # 4 - warning
244 $servicestatusfilter[] = 'has_been_checked = 1 and state = 1';
246 if( $number & nagstat
::SERVICE_UNKNOWN
) { # 8 - unknown
247 $servicestatusfilter[] = 'has_been_checked = 1 and state = 3';
249 if( $number & nagstat
::SERVICE_CRITICAL
) { # 16 - critical
250 $servicestatusfilter[] = 'has_been_checked = 1 and state = 2';
253 $servicefilter = $this->filter_combine('or', $servicestatusfilter );
255 return(array( $number, $servicefilter ));
258 private function get_service_prop_filter($number) {
259 $service_prop_filter = array();
264 if( $number & nagstat
::SERVICE_SCHEDULED_DOWNTIME
) { # 1 - In Scheduled Downtime
265 $service_prop_filter[] = 'scheduled_downtime_depth > 0';
267 if( $number & nagstat
::SERVICE_NO_SCHEDULED_DOWNTIME
) { # 2 - Not In Scheduled Downtime
268 $service_prop_filter[] = 'scheduled_downtime_depth = 0';
270 if( $number & nagstat
::SERVICE_STATE_ACKNOWLEDGED
) { # 4 - Has Been Acknowledged
271 $service_prop_filter[] = 'acknowledged = 1';
273 if( $number & nagstat
::SERVICE_STATE_UNACKNOWLEDGED
) { # 8 - Has Not Been Acknowledged
274 $service_prop_filter[] = 'acknowledged = 0';
276 if( $number & nagstat
::SERVICE_CHECKS_DISABLED
) { # 16 - Checks Disabled
277 $service_prop_filter[] = 'checks_enabled = 0';
279 if( $number & nagstat
::SERVICE_CHECKS_ENABLED
) { # 32 - Checks Enabled
280 $service_prop_filter[] = 'checks_enabled = 1';
282 if( $number & nagstat
::SERVICE_EVENT_HANDLER_DISABLED
) { # 64 - Event Handler Disabled
283 $service_prop_filter[] = 'event_handler_enabled = 0';
285 if( $number & nagstat
::SERVICE_EVENT_HANDLER_ENABLED
) { # 128 - Event Handler Enabled
286 $service_prop_filter[] = 'event_handler_enabled = 1';
288 if( $number & nagstat
::SERVICE_FLAP_DETECTION_ENABLED
) { # 256 - Flap Detection Enabled
289 $service_prop_filter[] = 'flap_detection_enabled = 1';
291 if( $number & nagstat
::SERVICE_FLAP_DETECTION_DISABLED
) { # 512 - Flap Detection Disabled
292 $service_prop_filter[] = 'flap_detection_enabled = 0';
294 if( $number & nagstat
::SERVICE_IS_FLAPPING
) { # 1024 - Is Flapping
295 $service_prop_filter[] = 'is_flapping = 1';
297 if( $number & nagstat
::SERVICE_IS_NOT_FLAPPING
) { # 2048 - Is Not Flapping
298 $service_prop_filter[] = 'is_flapping = 0';
300 if( $number & nagstat
::SERVICE_NOTIFICATIONS_DISABLED
) { # 4096 - Notifications Disabled
301 $service_prop_filter[] = 'notifications_enabled = 0';
303 if( $number & nagstat
::SERVICE_NOTIFICATIONS_ENABLED
) { # 8192 - Notifications Enabled
304 $service_prop_filter[] = 'notifications_enabled = 1';
306 if( $number & nagstat
::SERVICE_PASSIVE_CHECKS_DISABLED
) { # 16384 - Passive Checks Disabled
307 $service_prop_filter[] = 'accept_passive_checks = 0';
309 if( $number & nagstat
::SERVICE_PASSIVE_CHECKS_ENABLED
) { # 32768 - Passive Checks Enabled
310 $service_prop_filter[] = 'accept_passive_checks = 1';
312 if( $number & nagstat
::SERVICE_PASSIVE_CHECK
) { # 65536 - Passive Checks
313 $service_prop_filter[] = 'check_type = 1';
315 if( $number & nagstat
::SERVICE_ACTIVE_CHECK
) { # 131072 - Active Checks
316 $service_prop_filter[] = 'check_type = 0';
318 if( $number & nagstat
::SERVICE_HARD_STATE
) { # 262144 - In Hard State
319 $service_prop_filter[] = 'state_type = 1';
321 if( $number & nagstat
::SERVICE_SOFT_STATE
) { # 524288 - In Soft State
322 $service_prop_filter[] = 'state_type = 0';
325 $servicefilter = $this->filter_combine('and', $service_prop_filter );
327 return (array( $number, $servicefilter ));