1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Helper class for nagios status
6 const CMD_ENABLE_FLAP_DETECTION
= 61; /**< The nagios code for the command to enable flap detection */
7 const CMD_DISABLE_FLAP_DETECTION
= 62; /**< The nagios code for the command to disable flap detection */
9 const DISPLAY_HOSTS
= 0; /**< FIXME: don't know, unused? */
10 const DISPLAY_HOSTGROUPS
= 1; /**< FIXME: don't know, unused? */
11 const DISPLAY_SERVICEGROUPS
= 2; /**< FIXME: don't know, unused? */
13 # These differ from the ones in nagios' cgi's
14 # See comment for service states below for why
15 const HOST_UP
= 1; /**< Nagios host up code as a bit flag */
16 const HOST_DOWN
= 2; /**< Nagios host down code as a bit flag */
17 const HOST_UNREACHABLE
= 4; /**< Nagios host unreachable code as a bit flag */
18 const HOST_PENDING
= 64; /**< Our arbitrary code for not-yet-checked hosts */
19 const HOST_PROBLEM
= 6; /**< DOWN or UNREACHABLE */
20 const HOST_ALL
= 71; /**< All of the above ORed together */
22 const SERVICE_DOWNTIME
= 1; /**< service downtime */
23 const HOST_DOWNTIME
= 2; /**< host downtime */
24 const ANY_DOWNTIME
= 3; /**< host or service downtime */
26 const HOST_SCHEDULED_DOWNTIME
= 1; /**< Code for hosts in scheduled downtime, bit flag */
27 const HOST_NO_SCHEDULED_DOWNTIME
= 2; /**< Code for hosts not in scheduled downtime, bit flag */
28 const HOST_STATE_ACKNOWLEDGED
= 4; /**< Code for hosts in state acknowledged, bit flag */
29 const HOST_STATE_UNACKNOWLEDGED
= 8; /**< Code for hosts not in state acknowledged, bit flag */
30 const HOST_CHECKS_DISABLED
= 16; /**< Code for hosts with disabled checks, bit flag */
31 const HOST_CHECKS_ENABLED
= 32; /**< Code for hosts with enabled checks, bit flag */
32 const HOST_EVENT_HANDLER_DISABLED
= 64; /**< Code for hosts with no enabled event handler, bit flag */
33 const HOST_EVENT_HANDLER_ENABLED
= 128; /**< Code for hosts with an enabled event handler, bit flag */
34 const HOST_FLAP_DETECTION_DISABLED
= 256; /**< Code for hosts with disabled flap detection, bit flag */
35 const HOST_FLAP_DETECTION_ENABLED
= 512; /**< Code for hosts with enabled flap detection, bit flag */
36 const HOST_IS_FLAPPING
= 1024; /**< Code for hosts that are flapping, bit flag */
37 const HOST_IS_NOT_FLAPPING
= 2048; /**< Code for hosts that are not flapping, bit flag */
38 const HOST_NOTIFICATIONS_DISABLED
= 4096; /**< Code for hosts that has disabled notifications, bit flag */
39 const HOST_NOTIFICATIONS_ENABLED
= 8192; /**< Code for hosts with enabled notifications, bit flag */
40 const HOST_PASSIVE_CHECKS_DISABLED
= 16384; /**< Code for hosts with disabled passive checks, bit flag */
41 const HOST_PASSIVE_CHECKS_ENABLED
= 32768; /**< Code for hosts with enabled passive checks, bit flag */
42 const HOST_PASSIVE_CHECK
= 65536; /**< Code for hosts that were last checked by a passive check, bit flag */
43 const HOST_ACTIVE_CHECK
= 131072; /**< Code for hosts that were last checked by an active check, bit flag */
44 const HOST_HARD_STATE
= 262144; /**< Code for hosts in a hard state, bit flag */
45 const HOST_SOFT_STATE
= 524288; /**< Code for hosts in a soft state, bit flag */
47 # These are different from the ones in nagios' cgi's,
48 # because we use bitmasks (actually, we don't, but that's an oracle
49 # implementation detail) in our sql queries, and that
50 # doesn't work unless these consts are sequential and
51 # in the same order as the *real* states are.
52 # We introduce the magic number 64 (1 << 6) for PENDING
53 # instead of keeping special numbers at first.
54 const SERVICE_OK
= 1; /**< Nagios service ok code as a bit flag */
55 const SERVICE_WARNING
= 2; /**< Nagios service warning code as a bit flag */
56 const SERVICE_CRITICAL
= 4; /**< Nagios service critical code as a bit flag */
57 const SERVICE_UNKNOWN
= 8; /**< Nagios service unknown code as a bit flag */
58 const SERVICE_PENDING
= 64; /**< Our arbitrary code for not-yet-checked services */
59 const SERVICE_PROBLEM
= 14; /**< WARNING or CRITICAL or UNKNOWN */
60 const SERVICE_ALL
= 79; /**< All of the above, ORed together */
62 const SERVICE_SCHEDULED_DOWNTIME
= 1; /**< Code for services in scheduled downtime, bit flag */
63 const SERVICE_NO_SCHEDULED_DOWNTIME
= 2; /**< Code for services not in scheduled downtime, bit flag */
64 const SERVICE_STATE_ACKNOWLEDGED
= 4; /**< Code for services in state acknowledged, bit flag */
65 const SERVICE_STATE_UNACKNOWLEDGED
= 8; /**< Code for services not in state acknowledged, bit flag */
66 const SERVICE_CHECKS_DISABLED
= 16; /**< Code for services with disabled checks, bit flag */
67 const SERVICE_CHECKS_ENABLED
= 32; /**< Code for services with enabled checks, bit flag */
68 const SERVICE_EVENT_HANDLER_DISABLED
= 64; /**< Code for services with no enabled event handler, bit flag */
69 const SERVICE_EVENT_HANDLER_ENABLED
= 128; /**< Code for services with an enabled event handler, bit flag */
70 const SERVICE_FLAP_DETECTION_ENABLED
= 256; /**< Code for services with enabled flap detection, bit flag FIXME: This is inverted from the host states */
71 const SERVICE_FLAP_DETECTION_DISABLED
= 512; /**< Code for services with disabled flap detection, bit flag FIXME: This is inverted from the host states */
72 const SERVICE_IS_FLAPPING
= 1024; /**< Code for services that are flapping, bit flag */
73 const SERVICE_IS_NOT_FLAPPING
= 2048; /**< Code for services that are not flapping, bit flag */
74 const SERVICE_NOTIFICATIONS_DISABLED
= 4096; /**< Code for services that has disabled notifications, bit flag */
75 const SERVICE_NOTIFICATIONS_ENABLED
= 8192; /**< Code for services with enabled notifications, bit flag */
76 const SERVICE_PASSIVE_CHECKS_DISABLED
= 16384; /**< Code for services with disabled passive checks, bit flag */
77 const SERVICE_PASSIVE_CHECKS_ENABLED
= 32768; /**< Code for services with enabled passive checks, bit flag */
78 const SERVICE_PASSIVE_CHECK
= 65536; /**< Code for services that were last checked by a passive check, bit flag */
79 const SERVICE_ACTIVE_CHECK
= 131072; /**< Code for services that were last checked by an active check, bit flag */
80 const SERVICE_HARD_STATE
= 262144; /**< Code for services in a hard state, bit flag */
81 const SERVICE_SOFT_STATE
= 524288; /**< Code for services in a soft state, bit flag */
83 const STYLE_OVERVIEW
= 0; /**< FIXME: don't know, unused? */
84 const STYLE_DETAIL
= 1; /**< FIXME: don't know, unused? */
85 const STYLE_SUMMARY
= 2; /**< FIXME: don't know, unused? */
86 const STYLE_GRID
= 3; /**< FIXME: don't know, unused? */
87 const STYLE_HOST_DETAIL
= 4; /**< FIXME: don't know, unused? */
88 /********* HOST CHECK TYPES ***********/
90 const HOST_CHECK_ACTIVE
= 0; /**< Nagios performed the host check */
91 const HOST_CHECK_PASSIVE
= 1; /**< the host check result was submitted by an external source */
93 /******** SERVICE STATE TYPES ********/
94 const SOFT_STATE
= 0; /**< soft state */
95 const HARD_STATE
= 1; /**< hard state */
97 const SORT_ASC
= 'ASC'; /**< Code for when sorting ascending */
98 const SORT_DESC
= 'DESC'; /**< Code for when sorting descending */
100 /********* SCHEDULING QUEUE TYPES *********/
101 const CHECK_OPTION_NONE
= 0; /**< Check was normal */
102 const CHECK_OPTION_FORCE_EXECUTION
= 1; /**< Check was forced */
103 const CHECK_OPTION_FRESHNESS_CHECK
= 2; /**< Check was a freshness check */
104 const CHECK_OPTION_ORPHAN_CHECK
= 4; /**< Check was an orphan check */
106 /********* NOTIFICATION TYPES *********/
108 const NOTIFICATION_ALL
= 0; /**< all service and host notifications */
109 const NOTIFICATION_SERVICE_ALL
= 1; /**< all types of service notifications */
110 const NOTIFICATION_HOST_ALL
= 2; /**< all types of host notifications */
113 const NOTIFICATION_SERVICE_RECOVERY
= 0; /**< Service recovery notification */
114 const NOTIFICATION_SERVICE_WARNING
= 1; /**< Service went warning notification */
115 const NOTIFICATION_SERVICE_UNKNOWN
= 3; /**< Service went unknown notification */
116 const NOTIFICATION_SERVICE_CRITICAL
= 2; /**< Service went critical notification */
119 const NOTIFICATION_HOST_RECOVERY
= 0; /**< Host recovery notification */
120 const NOTIFICATION_HOST_DOWN
= 1; /**< Host went down notification */
121 const NOTIFICATION_HOST_UNREACHABLE
= 2; /**< Host went unreachable notification */
123 // reason type - every uncertain
124 /// Service acknowledgement notification
125 const NOTIFICATION_SERVICE_ACK
= 2; // ? 0, 99?
126 /// Service flapping notification
127 const NOTIFICATION_SERVICE_FLAP
= 3; // ? 0, 99
128 /// Host acknowledgement notification
129 const NOTIFICATION_HOST_ACK
= 2; // ?
130 /// Host flapping notification
131 const NOTIFICATION_HOST_FLAP
= 3; // ?
133 const FIND_HOST
= 1; /**< FIXME: don't know, unused? */
134 const FIND_CONTACT
= 2; /**< FIXME: don't know, unused? */
135 const FIND_SERVICE
= 3; /**< FIXME: don't know, unused? */
137 const MAX_QUERYNAME_LENGTH
= 256; /**< FIXME: don't know, unused? */
140 const HOST_NOTIFICATION
= 0; /**< The notification was for a host */
141 const SERVICE_NOTIFICATION
= 1; /**< The notification was for a service */
145 * Process macros for host- or service objects
147 public static function process_macros($string=false, $obj=false, $objtype=false)
149 if (empty($string) ||
empty($obj)) {
153 # try some heuristics to determine object type
154 if (empty($objtype)) {
155 if (!empty($obj->service_description
)) {
156 $objtype = 'service';
157 } elseif (!empty($obj->address
) ||
!empty($obj->state
)) {
165 '$HOSTNAME$' => 'name',
166 '$HOSTADDRESS$' => 'address',
167 '$HOSTDISPLAYNAME$' => 'display_name',
168 '$HOSTALIAS$' => 'alias',
169 '$HOSTSTATE$' => array(array('UP','DOWN','UNREACHABLE'), 'state'), /* UP/DOWN/UNREACHABLE - callback */
170 '$HOSTSTATEID$' => 'state',
171 '$HOSTSTATETYPE$' => array(array('SOFT','HARD'), 'state_type'), /* HARD/SOFT - callback */
172 '$HOSTATTEMPT$' => 'current_attempt',
173 '$MAXHOSTATTEMPTS$' => 'max_check_attempts',
174 '$HOSTGROUPNAME$' => array('array_first','groups'),
175 '$CURRENT_USER$' => array('current_user')
178 '$HOSTNAME$' => 'host_name',
179 '$HOSTADDRESS$' => 'host_address',
180 '$HOSTDISPLAYNAME$' => 'host_display_name',
181 '$HOSTALIAS$' => 'host_alias',
182 '$HOSTSTATE$' => array(array('UP','DOWN','UNREACHABLE'), 'host_state'), /* UP/DOWN/UNREACHABLE - callback */
183 '$HOSTSTATEID$' => 'host_state',
184 '$HOSTSTATETYPE$' => array(array('SOFT','HARD'), 'host_state_type'), /* HARD/SOFT - callback */
185 '$HOSTATTEMPT$' => 'host_current_attempt',
186 '$MAXHOSTATTEMPTS$' => 'host_max_check_attempts',
187 '$HOSTGROUPNAME$' => array('array_first','host_groups'),
188 '$SERVICEDESC$' => 'description',
189 '$SERVICEDISPLAYNAME$' => 'display_name',
190 '$SERVICEGROUPNAME$' => array('array_first','groups'),
191 '$SERVICESTATE$' => array(array('OK','WARNING','CRITICAL','UNKNOWN'), 'state'),
192 '$CURRENT_USER$' => array('current_user')
194 'hostgroup' => array(
195 '$HOSTGROUPNAME$' => 'name',
196 '$HOSTGROUPALIAS$' => 'alias',
198 'servicegroup' => array(
199 '$SERVICEGROUPNAME$' => 'name',
200 '$SERVICEGROUPALIAS$' => 'alias',
202 # strictly speaking, this is wrong, but the effect is
203 # hopefully negligible and can easily be avoided by
204 # specifying the objtype at the callsite.
206 '$HOSTGROUPNAME$' => 'name',
207 '$HOSTGROUPALIAS$' => 'alias',
208 '$SERVICEGROUPNAME$' => 'name',
209 '$SERVICEGROUPALIAS$' => 'alias',
213 if( !isset( $macros[$objtype] ) ) {
214 return $string; /* No macros defined for object type, no macros can be replaced */
216 $macros = $macros[$objtype];
218 $regexp = '/\$[A-Z_]*\$/';
219 $hits = preg_match_all($regexp, $string, $res);
221 if ($hits > 0 && !empty($res)) {
222 foreach ($res as $matches) {
223 foreach ($matches as $match) {
224 if (array_key_exists($match, $macros)) {
225 $field = $macros[$match];
227 if (is_array($field)) {
228 if( is_array($field[0]) ) {
229 if( isset($obj->{$field[1]}) ) {
230 $string = str_replace($match, $field[0][$obj->{$field[1]}], $string);
233 $func = array_shift( $field );
234 $replace = call_user_func( $func, $obj, $field );
235 if( $replace !== false )
236 $string = str_replace($match, $replace, $string);
239 if (isset($obj->{$field})) {
240 $string = str_replace($match, $obj->{$field}, $string);
253 * Format a Nagios date format string to the
256 * NOTE!!! cal::get_calendar_format has the same thing, without time
258 public static function date_format($nagios_format_name=false)
260 if (empty($nagios_format_name)) {
261 $date_format_id = 'date_format';
262 # try config helper first (includes session check)
263 $nagios_format_name = config
::get('config.'.$date_format_id);
264 if (empty($nagios_format_name)) {
265 # check nagios.cfg file
266 $nagios_config = System_Model
::parse_config_file('nagios.cfg');
267 $nagios_format_name = $nagios_config[$date_format_id];
269 Session
::instance()->set('config.'.$date_format_id, $nagios_format_name);
272 Ninja_setting_Model
::save_page_setting('config.'.$date_format_id, '*', $nagios_format_name);
275 $nagios_format_name = trim($nagios_format_name);
276 if (empty($nagios_format_name)) {
279 $date_format = false;
280 switch (strtolower($nagios_format_name)) {
281 case 'us': # MM/DD/YYYY HH:MM:SS
282 $date_format = 'm/d/Y H:i:s';
284 case 'euro': # DD/MM/YYYY HH:MM:SS
285 $date_format = 'd/m/Y H:i:s';
287 case 'iso8601': # YYYY-MM-DD HH:MM:SS
288 $date_format = 'Y-m-d H:i:s';
290 case 'strict-iso8601': # YYYY-MM-DDTHH:MM:SS
291 $date_format = 'Y-m-d\TH:i:s';
298 * Convert a date format string back to a timestamp
300 * @return false|DateTime
302 public static function timestamp_format($format_str = false, $date_str=false)
304 if (empty($format_str))
305 $format_str = self
::date_format(); # fetch if not set
307 # use now as date if nothing supplied as input FIXME: isn't that extremely anti-useful?
308 $date_str = empty($date_str) ?
date($format_str) : $date_str;
309 $dt = DateTime
::createFromFormat($format_str, $date_str);
313 return $dt->getTimestamp();
318 * Callback to return first of a list field
320 function array_first( $obj, $fields )
322 if( !isset( $obj->{$fields[0]} ) ||
!is_array( $obj->{$fields[0]} ) )
324 $arr = $obj->{$fields[0]};
325 if( count($arr) == 0 ) return '';
330 * Callback to return username of current user
332 function current_user()
334 return Auth
::instance()->get_user()->username
;