Merge branch 'maint/7.0'
[ninja.git] / modules / legacy / models / current_status.php
blobb57608ceabaeead83e578df901213219bfb62ca1
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * Retrieves and manipulates current status of hosts (and services?)
5 */
6 class Current_status_Model extends Model
8 private static $instance = false;
10 const HOST_UP = 0; /**< Nagios' host up code */
11 const HOST_DOWN = 1; /**< Nagios' host down code */
12 const HOST_UNREACHABLE = 2; /**< Nagios' host unreachable code */
13 const HOST_PENDING = 6; /**< Our magical "host pending" code for unchecked hosts */
15 const SERVICE_OK = 0; /**< Nagios' service ok code */
16 const SERVICE_WARNING = 1; /**< Nagios' service warning code */
17 const SERVICE_CRITICAL = 2; /**< Nagios' service critical code */
18 const SERVICE_UNKNOWN = 3; /**< Nagios' service unknown code */
19 const SERVICE_PENDING = 6; /**< Our magical "service pending" code for unchecked services */
20 const HOST_CHECK_ACTIVE = 0; /**< Nagios performed the host check */
21 const HOST_CHECK_PASSIVE = 1; /**< the host check result was submitted by an external source */
22 const SERVICE_CHECK_ACTIVE = 0; /**< Nagios performed the service check */
23 const SERVICE_CHECK_PASSIVE = 1; /**< the service check result was submitted by an external source */
25 private $program_data_present = false;
26 private $host_data_present = false;
27 private $service_data_present = false;
28 private $outage_data_present = false;
30 /**
31 * Use this class as a singleton, as it is quite slow
33 * @return A Current_status_Model object
35 public static function instance()
37 if (!self::$instance) {
38 self::$instance = new Current_status_Model();
40 return self::$instance;
43 /**
44 * Fetch current host status from db for current user
45 * @return bool indicating whether query worked
47 public function program_status()
49 if ($this->program_data_present)
50 return $this->ps;
52 $ls = Livestatus::instance();
53 $this->ps = $ls->getProcessInfo(array('auth'=>false));
54 $this->program_data_present = true;
55 return $this->ps;
58 /**
59 * Fetch current host status from db for current user
60 * @return bool indicating whether query worked
62 public function host_status()
64 if ($this->host_data_present)
65 return;
67 $stats = new Stats_Model();
68 $this->hst = $stats->get_stats('host_totals');
69 $this->hst_perf = $stats->get_stats('host_performance', null, $this->ps->program_start);
71 $all = $this->hst->up + $this->hst->down + $this->hst->unreachable;
72 if ($all == 0)
73 $this->percent_host_health = 0.0;
74 else
75 $this->percent_host_health = number_format($this->hst->up/$all*100, 1);
77 $this->host_data_present = true;
78 return;
81 /**
82 * Fetch and calculate status for all services for current user
83 * @return bool indicating whether query worked
85 public function service_status()
87 if ($this->service_data_present)
88 return;
90 $stats = new Stats_Model();
91 $this->svc = $stats->get_stats('service_totals');
92 $this->svc_perf = $stats->get_stats('service_performance', null, $this->ps->program_start);
94 $all = $this->svc->ok + $this->svc->warning + $this->svc->critical + $this->svc->unknown;
95 if ($all == 0)
96 $this->percent_service_health = 0.0;
97 else
98 $this->percent_service_health = number_format($this->svc->ok/$all*100, 1);
100 $this->service_data_present = true;
101 return;
105 * Analyze all status data for hosts and services
106 * Calls
107 * - host_status()
108 * - service_status()
109 * @return bool
111 public function analyze_status_data()
113 $this->program_status();
114 $this->host_status();
115 $this->service_status();
116 return empty($errors);
120 * Translates a given status from db to a readable string
122 * @param $db_status int
123 * @param $db_checked boolean
124 * @param $type string = host
125 * @return string
127 public static function status_text($db_status, $db_checked, $type='host')
129 if (!$db_checked)
130 return 'PENDING';
132 # pending down here doesn't exist anymore, but handle it anyway
133 $host_states = array(
134 self::HOST_UP => 'UP',
135 self::HOST_DOWN => 'DOWN',
136 self::HOST_UNREACHABLE => 'UNREACHABLE',
137 self::HOST_PENDING => 'PENDING'
140 $service_states = array(
141 self::SERVICE_OK => 'OK',
142 self::SERVICE_WARNING => 'WARNING',
143 self::SERVICE_CRITICAL => 'CRITICAL',
144 self::SERVICE_PENDING => 'PENDING',
145 self::SERVICE_UNKNOWN => 'UNKNOWN'
148 $retval = false;
149 switch ($type) {
150 case 'host': case 'hostgroup':
151 if (array_key_exists($db_status, $host_states)) {
152 $retval = $host_states[$db_status];
154 break;
155 case 'service': case 'servicegroup':
156 if (array_key_exists($db_status, $service_states)) {
157 $retval = $service_states[$db_status];
159 break;
161 return $retval;
165 * List available states for host or service
167 * @param $what string 'host' (or 'service')
168 * @return array
170 public function get_available_states($what='host')
172 switch($what) {
173 case 'host':
174 return array(
175 self::HOST_UP => 'UP',
176 self::HOST_DOWN => 'DOWN',
177 self::HOST_UNREACHABLE => 'UNREACHABLE',
178 self::HOST_PENDING => 'PENDING'
180 case 'service':
181 return array(
182 self::SERVICE_OK => 'OK',
183 self::SERVICE_WARNING => 'WARNING',
184 self::SERVICE_CRITICAL => 'CRITICAL',
185 self::SERVICE_PENDING => 'PENDING',
186 self::SERVICE_UNKNOWN => 'UNKNOWN'
188 default:
189 return array();