3 require_once( dirname(__FILE__
).'/base/basehost.php' );
6 * Describes a single object from livestatus
8 class Host_Model
extends BaseHost_Model
{
10 * An array containing the custom column dependencies
12 static public $rewrite_columns = array(
13 'state_text' => array('state','has_been_checked'),
14 'first_group' => array('groups'),
15 'checks_disabled' => array('active_checks_enabled'),
16 'duration' => array('last_state_change'),
17 'comments_count' => array('comments'),
18 'config_url' => array('name'),
19 'state_type_text' => array('state_type'),
20 'check_type_str' => array('check_type'),
21 'config_allowed' => array('contacts'),
22 'source_node' => array('check_source'),
23 'source_type' => array('check_source'),
24 'perf_data' => array('perf_data_raw')
30 public function get_state_text() {
31 if( !$this->get_has_been_checked() )
33 switch( $this->get_state() ) {
35 case 1: return 'down';
36 case 2: return 'unreachable';
38 return 'unknown'; // should never happen
42 * get if checks are disabled
44 public function get_checks_disabled() {
45 //FIXME: passive as active
46 return !$this->get_active_checks_enabled();
50 * Get the first host group of the host group memberships
52 public function get_first_group() {
53 $groups = $this->get_groups();
54 if(isset($groups[0])) return $groups[0];
59 * Get the long plugin output, which is second line and forward
61 * By some reason, nagios escapes this field.
63 public function get_long_plugin_output() {
64 $long_plugin_output = parent
::get_long_plugin_output();
65 return stripcslashes($long_plugin_output);
71 public function get_duration() {
73 $last_state_change = $this->get_last_state_change();
74 if( $last_state_change == 0 )
76 return $now - $last_state_change;
80 * get the number of comments associated to the host
82 public function get_comments_count() {
83 return count($this->get_comments());
87 * Return the state type, as text in uppercase
89 public function get_state_type_text() {
90 return $this->get_state_type()?
'hard':'soft';
94 * Get check type, as a string ("active" or "passive")
96 public function get_check_type_str() {
97 return $this->get_check_type() ?
'passive' : 'active';
101 * Get a list of custom commands for the host
103 public function get_custom_commands() {
104 return Custom_command_Model
::parse_custom_variables($this->get_custom_variables());
108 * Get if having access to configure the host.
109 * @param $auth op5auth module to use, if not default
111 public function get_config_allowed($auth = false) {
112 if( $auth === false ) {
113 $auth = op5auth
::instance();
115 if(!$auth->authorized_for('configuration_information')) {
118 if($auth->authorized_for('host_edit_all')) {
121 $cts = $this->get_contacts();
122 if(!is_array($cts)) $cts = array();
123 if($auth->authorized_for('host_edit_contact') && in_array($auth->get_user()->username
, $cts)) {
130 * Get configuration url
132 public function get_config_url() {
133 return str_replace(array(
136 urlencode($this->get_name())
137 ), Kohana
::config('config.config_url.hosts'));
142 * Get both address and type of check source
144 * internal function for get_source_node and get_source_type
146 private function get_source() {
147 $check_source = $this->get_check_source();
150 if(preg_match('/^Core Worker ([0-9]+)$/', $check_source, $matches)) {
151 $node = gethostname();
154 if(preg_match('/^Merlin (.*) (.*)$/', $check_source, $matches)) {
158 return array($node, $type);
162 * Get which merlin node handling the check.
164 * This is determined by magic regexp parsing of the check_source field
166 public function get_source_node() {
167 $source = $this->get_source();
172 * Get which merlin node handling the check.
174 * This is determined by magic regexp parsing of the check_source field
176 public function get_source_type() {
177 $source = $this->get_source();
182 * Get the performance data for the object, expressed as an associative array
184 public function get_perf_data() {
185 $perf_data_str = parent
::get_perf_data_raw();
186 return performance_data
::process_performance_data($perf_data_str);