3 require_once( dirname(__FILE__
).'/base/baseservice.php' );
6 * Describes a single object from livestatus
8 class Service_Model
extends BaseService_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('host.name', 'description'),
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')
28 * Return the state as text
30 public function get_state_text() {
31 if( !$this->get_has_been_checked() )
33 switch( $this->get_state() ) {
35 case 1: return 'warning';
36 case 2: return 'critical';
37 case 3: return 'unknown';
39 return 'unknown'; // should never happen
43 * Return the name of one of the services groups the object is member of
45 public function get_first_group() {
46 $groups = $this->get_groups();
47 if(isset($groups[0])) return $groups[0];
52 * Returns true if checks are disabled
54 public function get_checks_disabled() {
55 //FIXME: passive as active
56 return !$this->get_active_checks_enabled();
60 * Returns the duration since last state change
62 public function get_duration() {
64 $last_state_change = $this->get_last_state_change();
65 if( $last_state_change == 0 )
67 return $now - $last_state_change;
71 * Get the long plugin output, which is second line and forward
73 * By some reason, nagios escapes this field.
75 public function get_long_plugin_output() {
76 $long_plugin_output = parent
::get_long_plugin_output();
77 return stripcslashes($long_plugin_output);
81 * Returns the number of comments related to the service
83 public function get_comments_count() {
84 return count($this->get_comments());
88 * Return the state type, as text in uppercase
90 public function get_state_type_text() {
91 return $this->get_state_type()?
'hard':'soft';
95 * Get the check type as string (passive/active)
97 public function get_check_type_str() {
98 return $this->get_check_type() ?
'passive' : 'active';
102 * Get a list of custom commands for the service
104 public function get_custom_commands() {
105 return Custom_command_Model
::parse_custom_variables($this->get_custom_variables());
109 * Get if having access to configure the host.
110 * @param $auth op5auth module to use, if not default
112 public function get_config_allowed($auth = false) {
113 if( $auth === false ) {
114 $auth = op5auth
::instance();
116 if(!$auth->authorized_for('configuration_information')) {
119 if($auth->authorized_for('service_edit_all')) {
122 $cts = $this->get_contacts();
123 if(!is_array($cts)) $cts = array();
124 if($auth->authorized_for('service_edit_contact') && in_array($auth->get_user()->username
, $cts)) {
131 * Get configuration url
133 public function get_config_url() {
134 return str_replace(array(
138 urlencode($this->get_host()->get_name()),
139 urlencode($this->get_description())
140 ), Kohana
::config('config.config_url.services'));
145 * Get both address and type of check source
147 * internal function for get_source_node and get_source_type
149 private function get_source() {
150 $check_source = $this->get_check_source();
153 if(preg_match('/^Core Worker ([0-9]+)$/', $check_source, $matches)) {
154 $node = gethostname();
157 if(preg_match('/^Merlin (.*) (.*)$/', $check_source, $matches)) {
161 return array($node, $type);
165 * Get which merlin node handling the check.
167 * This is determined by magic regexp parsing of the check_source field
169 public function get_source_node() {
170 $source = $this->get_source();
175 * Get which merlin node handling the check.
177 * This is determined by magic regexp parsing of the check_source field
179 public function get_source_type() {
180 $source = $this->get_source();
185 * Get the performance data for the object, expressed as an associative array
187 public function get_perf_data() {
188 $perf_data_str = parent
::get_perf_data_raw();
189 return performance_data
::process_performance_data($perf_data_str);