Merge branch 'maint/7.0'
[ninja.git] / application / widgets / netw_health / netw_health.php
blobea6ec6cd7974091ae371f33427b3fc0a9d351d48
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Network health widget
5 * @author op5 AB
6 */
7 class Netw_health_Widget extends widget_Base {
8 protected $duplicatable = true;
10 # define warning/critical limit
11 private $health_warning_percentage = 90;
12 private $health_critical_percentage = 75;
13 private $visible_precision = 2;
14 private $host_img = false;
15 private $service_img = false;
16 private $crit_img = '/images/thermcrit.png';
17 private $warn_img = '/images/thermwarn.png';
18 private $ok_img = '/images/thermok.png';
19 private $host_val = false;
20 private $service_val = false;
22 private $netw_health_config = <<<EOC
23 - HOSTS
24 [hosts] scheduled_downtime_depth=0
25 [hosts] state = 0
26 - SERVICE
27 [services] host.scheduled_downtime_depth=0 and scheduled_downtime_depth=0
28 [services] state = 0
29 EOC;
31 public function __construct($model)
33 parent::__construct($model);
35 $this->health_warning_percentage =
36 isset($this->model->setting['health_warning_percentage'])
37 ? $this->model->setting['health_warning_percentage']
38 : $this->health_warning_percentage;
40 $this->health_critical_percentage =
41 isset($this->model->setting['health_critical_percentage'])
42 ? $this->model->setting['health_critical_percentage']
43 : $this->health_critical_percentage;
45 $this->visible_precision =
46 isset($this->model->setting['visible_precision'])
47 ? $this->model->setting['visible_precision']
48 : $this->visible_precision;
49 /* Remove this configuration for now... due to the awfulness of the
50 * configuration interface, this shouldn't be nessecary to support in the
51 * future.
53 * There isn't a feature request with this, so let's remove it...
54 $this->netw_health_config =
55 isset($this->model->setting['netw_health_config'])
56 ? $this->model->setting['netw_health_config']
57 : $this->netw_health_config;
61 public function options()
63 $options = parent::options();
64 $options[] = new option($this->model->name, 'health_warning_percentage', 'Warning Percentage Level', 'input', array(
65 'style' => 'width:20px',
66 'title' => sprintf(_('Default value: %s%%'), 90)), $this->health_warning_percentage);
67 $options[] = new option($this->model->name, 'health_critical_percentage', 'Critical Percentage Level', 'input', array(
68 'style' => 'width:20px',
69 'title' => sprintf(_('Default value: %s%%'), 75)), $this->health_warning_percentage);
70 $options[] = new option($this->model->name, 'visible_precision', 'Precision', 'input', array(
71 'style' => 'width:20px',
72 'title' => sprintf(_('Default value: %d'), 1)), $this->visible_precision);
74 * Because the configuration is to hard for now to support, this is commented out.
76 * Otherwise this can be configured here...
78 // $options[] = new option($this->model->name, 'netw_health_config', 'Configuration', 'textarea', array(), $this->netw_health_config);
80 return $options;
83 public function index()
85 # fetch widget view path
86 $view_path = $this->view_path('view');
88 $health_warning_percentage = $this->health_warning_percentage;
89 $health_critical_percentage = $this->health_critical_percentage;
91 $visible_precision = intval( $this->visible_precision );
92 if( $visible_precision < -1 ) $visible_precision = -1;
93 if( $visible_precision > 10 ) $visible_precision = 10;
96 * Parse configuration text
98 * Sets $bars to an array where each element is a array of three values, containing:
99 * - Name of bar
100 * - Query matching elements defining the set to measure on
101 * - Query matching which elements is included in the percentatage
103 * Both queries needs to work on the same table.
105 $blocks = array();
106 $bar_configs = array();
107 foreach( explode("\n",$this->netw_health_config) as $line ) {
108 $line = trim($line);
109 if( $line == '' )
110 continue;
111 if( $line[0] == '-' ) {
112 if( count($blocks) == 3 ) {
113 $bar_configs[] = $blocks;
115 $blocks = array(trim(substr($line,1)));
116 } else {
117 $blocks[] = $line;
120 if( count($blocks) == 3 ) {
121 $bar_configs[] = $blocks;
124 /* Calculate stats */
125 $bars = array();
126 foreach($bar_configs as $bar) {
127 list($name, $all_query, $sel_query) = $bar;
128 $set_all = ObjectPool_Model::get_by_query($all_query);
129 $set_sel = ObjectPool_Model::get_by_query($sel_query);
130 list($count_all, $count_sel) = $set_all->stats(array($set_all, $set_sel));
131 if( $count_all == 0 ) $count_all = 1;
132 $bars[] = array(
133 'label' => $name,
134 'value' => 100.0*$count_sel/$count_all
138 # set required extra resources
139 require($view_path);