Merge "Make it possible to sort on simple custom columns"
[ninja.git] / modules / reports / models / reports.php
blob1f18cbc2e49c6809ab68d747a90967ffa3919b12
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * Basic reports model that only exists for legacy reasons.
5 * Only contains crap that should be refactored away.
6 */
7 class Reports_Model extends Model
9 // state codes
10 const STATE_PENDING = -1; /**< Magical state for unchecked objects. In other parts of ninja, 6 is used for this */
11 const STATE_OK = 0; /**< "Everything is fine"-state */
12 const HOST_UP = 0; /**< Host is up */
13 const HOST_DOWN = 1; /**< Host is down */
14 const HOST_UNREACHABLE = 2; /**< Host is unreachable */
15 const HOST_PENDING = -1; /**< Magical state for unchecked hosts. In other parts of ninja, 6 is used for this */
16 const HOST_EXCLUDED = -2; /**< Magical state when a host event falls outside of the specified timeperiod */
17 const HOST_ALL = 7; /**< Bitmask for any non-magical host state */
18 const SERVICE_OK = 0; /**< Service is up */
19 const SERVICE_WARNING = 1; /**< Service is warning */
20 const SERVICE_CRITICAL = 2; /**< Service is critical */
21 const SERVICE_UNKNOWN = 3; /**< Service is unknown */
22 const SERVICE_PENDING = -1; /**< Magical state for unchecked services. In other parts of ninja, 6 is used for this */
23 const SERVICE_EXCLUDED = -2; /**< Magical state when a service event falls outside of the specified timeperiod */
24 const SERVICE_ALL = 15; /**< Bitmask for any non-magical service state */
25 const PROCESS_SHUTDOWN = 103; /**< Nagios code for when it is shut down */
26 const PROCESS_RESTART = 102; /**< Nagios code for when it is restarted - not normally added to report_data, check for stop and start instead */
27 const PROCESS_START = 100; /**< Nagios code for when it is started */
28 const SERVICECHECK = 701; /**< Nagios code for a service check */
29 const HOSTCHECK = 801; /**< Nagios code for a host check */
30 const DOWNTIME_START = 1103; /**< Nagios code for downtime start */
31 const DOWNTIME_STOP = 1104; /**< Nagios code for downtime stop, either because it ended or because it was deleted */
32 const DEBUG = true; /**< Debug bool - can't see this is ever false */
34 /** A map of state ID => state name for hosts. FIXME: one of a gazillion */
35 static public $host_states = array(
36 Reports_Model::HOST_UP => 'up',
37 Reports_Model::HOST_DOWN => 'down',
38 Reports_Model::HOST_UNREACHABLE => 'unreachable',
39 Reports_Model::HOST_PENDING => 'pending',
40 Reports_Model::HOST_EXCLUDED => 'excluded');
42 /** A map of state ID => state name for services. FIXME: one of a gazillion */
43 static public $service_states = array(
44 Reports_Model::SERVICE_OK => 'ok',
45 Reports_Model::SERVICE_WARNING => 'warning',
46 Reports_Model::SERVICE_CRITICAL => 'critical',
47 Reports_Model::SERVICE_UNKNOWN => 'unknown',
48 Reports_Model::SERVICE_PENDING => 'pending',
49 Reports_Model::SERVICE_EXCLUDED => 'excluded');
53 /** The provided options */
54 protected $options = false;
55 /** The timeperiod associated with this report */
56 protected $timeperiod;
57 /** Table to use for the duration of the build */
58 protected $db_table = 'report_data';
60 /**
61 * Constructor
62 * @param $options An instance of Report_options
64 public function __construct(Report_options $options)
66 parent::__construct();
68 if (self::DEBUG === true) {
69 assert_options(ASSERT_ACTIVE, 1);
70 assert_options(ASSERT_WARNING, 0);
71 assert_options(ASSERT_QUIET_EVAL, 0);
72 assert_options(ASSERT_BAIL, 1);
74 # use report helper callback
75 assert_options(ASSERT_CALLBACK, array('reports', 'lib_reports_assert_handler'));
78 $this->options = $options;
79 $this->timeperiod = Old_Timeperiod_Model::instance($options);
80 $this->timeperiod->resolve_timeperiods();
83 /**
84 * Helper method for retrieving a user-friendly representation for nagios codes
85 * Randomly put into the report model, because both "model" and "helper"
86 * contains "e" and "l", so who can keep them apart?
88 * @param $event_type int
89 * @param $object_type string = null (host or service)
90 * @param $short boolean = false (true = key, false = English)
91 * @return string
92 * @throws InvalidArgumentException
94 public static function event_type_to_string($event_type, $object_type = null, $short = false) {
95 $events = array(
96 self::PROCESS_SHUTDOWN => array(
97 'short' => 'monitor_shut_down',
98 'full' => _('Monitor shut down')
100 self::PROCESS_RESTART => array(
101 'short' => 'monitor_restart',
102 'full' => _('Monitor restart')
104 self::PROCESS_START => array(
105 'short' => 'monitor_start',
106 'full' => _('Monitor started')
108 self::SERVICECHECK => array(
109 'short' => 'service_alert',
110 'full' => _('Service alert')
112 self::HOSTCHECK => array(
113 'short' => 'host_alert',
114 'full' => _('Host alert')
116 self::DOWNTIME_START => array(
117 'short' => 'scheduled_downtime_start',
118 'full' => _($object_type . ' has entered a period of scheduled downtime')
120 self::DOWNTIME_STOP => array(
121 'short' => 'scheduled_downtime_stop',
122 'full' => _($object_type . ' has exited a period of scheduled downtime')
125 if(!isset($events[$event_type])) {
126 throw new InvalidArgumentException("Invalid event type '$event_type' in ".__METHOD__.":".__LINE__);
128 return $events[$event_type][$short ? 'short' : 'full'];
132 * Check that we have a valid database installed and usable.
134 public function _self_check()
136 try {
137 # this will result in error if db_name section
138 # isn't set in config/database.php
139 $db = Database::instance();
140 } catch (Kohana_Database_Exception $e) {
141 return false;
143 $table_exists = false;
144 if (isset($db)) {
145 try {
146 $table_exists = $db->table_exists($this->db_table);
147 } catch (Kohana_Database_Exception $e) {
148 return false;
150 } else {
151 return false;
153 return true;