diff options
author | Pim van den Berg | 2009-12-31 11:42:56 +0100 |
---|---|---|
committer | Pim van den Berg | 2009-12-31 13:07:13 +0100 |
commit | 95c70c919837f588389f525fe2cf4aba479e9f91 (patch) | |
tree | ef242a0273458ffcadf8357fd8dd4d64b2fb7105 /type/Default.class.php | |
parent | always show 'on $host' in the rrd title (diff) | |
download | apt-panopticon_cgp-95c70c919837f588389f525fe2cf4aba479e9f91.zip apt-panopticon_cgp-95c70c919837f588389f525fe2cf4aba479e9f91.tar.gz apt-panopticon_cgp-95c70c919837f588389f525fe2cf4aba479e9f91.tar.bz2 apt-panopticon_cgp-95c70c919837f588389f525fe2cf4aba479e9f91.tar.xz |
rewrite of type classes
A constructor is added to the Type_Default class. The constructor will
parse GET values (such as host, plugin, pinstance, type, tinstance,
seconds), create an array of all needed rrd files to generate a graph and
substract identifiers from these rrd files.
Because of the constructor (and related functions) it is not needed to
define an array of tinstances to be grouped and shown in one graph. Also
$obj->args don't have to be defined per plugin. This will result in
smaller plugin files.
The type classes are based on the fact that a plugin has multiple type
instances OR multiple rrd data sources. This is called the source and is
retrieved by rrd_get_sources in each rrd_gen_graph function. Also
variables in function rrd_gen_graph have been renamed to better ones.
Diffstat (limited to 'type/Default.class.php')
-rw-r--r-- | type/Default.class.php | 142 |
1 files changed, 95 insertions, 47 deletions
diff --git a/type/Default.class.php b/type/Default.class.php index 6b1ab5c..0c992e2 100644 --- a/type/Default.class.php +++ b/type/Default.class.php | |||
@@ -5,7 +5,8 @@ | |||
5 | class Type_Default { | 5 | class Type_Default { |
6 | var $datadir; | 6 | var $datadir; |
7 | var $args; | 7 | var $args; |
8 | var $data_sources; | 8 | var $seconds; |
9 | var $data_sources = array('value'); | ||
9 | var $order; | 10 | var $order; |
10 | var $ds_names; | 11 | var $ds_names; |
11 | var $colors; | 12 | var $colors; |
@@ -16,6 +17,29 @@ class Type_Default { | |||
16 | var $width; | 17 | var $width; |
17 | var $heigth; | 18 | var $heigth; |
18 | 19 | ||
20 | var $files; | ||
21 | var $tinstances; | ||
22 | var $identifiers; | ||
23 | |||
24 | function __construct($datadir) { | ||
25 | $this->datadir = $datadir; | ||
26 | $this->parse_get(); | ||
27 | $this->rrd_files(); | ||
28 | $this->identifiers = $this->file2identifier($this->files); | ||
29 | } | ||
30 | |||
31 | # parse $_GET values | ||
32 | function parse_get() { | ||
33 | $this->args = array( | ||
34 | 'host' => $_GET['h'], | ||
35 | 'plugin' => $_GET['p'], | ||
36 | 'pinstance' => $_GET['pi'], | ||
37 | 'type' => $_GET['t'], | ||
38 | 'tinstance' => $_GET['ti'], | ||
39 | ); | ||
40 | $this->seconds = $_GET['s']; | ||
41 | } | ||
42 | |||
19 | function validate_color($color) { | 43 | function validate_color($color) { |
20 | if (!preg_match('/^[0-9a-f]{6}$/', $color)) | 44 | if (!preg_match('/^[0-9a-f]{6}$/', $color)) |
21 | return '000000'; | 45 | return '000000'; |
@@ -46,26 +70,41 @@ class Type_Default { | |||
46 | return $c[r].$c[g].$c[b]; | 70 | return $c[r].$c[g].$c[b]; |
47 | } | 71 | } |
48 | 72 | ||
49 | function identifier($host, $plugin, $pinst, $type, $tinst) { | 73 | function rrd_files() { |
50 | $identifier = sprintf('%s/%s%s%s/%s%s%s', $host, | 74 | $files = $this->get_filenames(); |
51 | $plugin, strlen($pinst) ? '-' : '', $pinst, | ||
52 | $type, strlen($tinst) ? '-' : '', $tinst); | ||
53 | 75 | ||
54 | if (is_file($this->datadir.'/'.$identifier.'.rrd')) | 76 | foreach($files as $filename) { |
55 | return $identifier; | 77 | preg_match("#^$this->datadir/{$this->args['host']}/[\w\d]+-?([\w\d-]+)?/[\w\d]+-?([\w\d-]+)?\.rrd#", $filename, $matches); |
56 | else | 78 | |
57 | return FALSE; | 79 | $this->tinstances[] = $matches[2]; |
80 | $this->files[$matches[2]] = $filename; | ||
81 | } | ||
82 | |||
83 | sort($this->tinstances); | ||
84 | ksort($this->files); | ||
58 | } | 85 | } |
59 | 86 | ||
60 | function get_filename($tinstance=NULL) { | 87 | function get_filenames() { |
61 | if (!is_array($this->args['tinstance']) && $tinstance == NULL) | 88 | $identifier = sprintf('%s/%s%s%s/%s%s%s', $this->args['host'], |
62 | $tinstance = $this->args['tinstance']; | 89 | $this->args['plugin'], strlen($this->args['pinstance']) ? '-' : '', $this->args['pinstance'], |
90 | $this->args['type'], strlen($this->args['tinstance']) ? '-' : '', $this->args['tinstance']); | ||
91 | |||
92 | $wildcard = strlen($this->args['tinstance']) ? '' : '*'; | ||
63 | 93 | ||
64 | $identifier = $this->identifier($this->args['host'], | 94 | $files = glob($this->datadir .'/'. $identifier . $wildcard . '.rrd'); |
65 | $this->args['plugin'], $this->args['pinstance'], | ||
66 | $this->args['type'], $tinstance); | ||
67 | 95 | ||
68 | return $this->datadir.'/'.$identifier.'.rrd'; | 96 | return $files; |
97 | } | ||
98 | |||
99 | function file2identifier($files) { | ||
100 | foreach($files as $key => $file) { | ||
101 | if (is_file($file)) { | ||
102 | $files[$key] = preg_replace("#^$this->datadir/#", '', $files[$key]); | ||
103 | $files[$key] = preg_replace('#\.rrd$#', '', $files[$key]); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | return $files; | ||
69 | } | 108 | } |
70 | 109 | ||
71 | function rrd_graph($debug=false) { | 110 | function rrd_graph($debug=false) { |
@@ -96,48 +135,57 @@ class Type_Default { | |||
96 | return $rrdgraph; | 135 | return $rrdgraph; |
97 | } | 136 | } |
98 | 137 | ||
99 | function rrd_gen_graph() { | 138 | function rrd_get_sources() { |
100 | $filename = $this->get_filename(); | 139 | # is the source spread over multiple files? |
140 | if (is_array($this->files) && count($this->files)>1) { | ||
141 | # and must it be ordered? | ||
142 | if (is_array($this->order)) { | ||
143 | $this->tinstances = array_intersect($this->order, $this->tinstances); | ||
144 | } | ||
145 | # use tinstances as sources | ||
146 | $sources = $this->tinstances; | ||
147 | } | ||
148 | # or one file with multiple data_sources | ||
149 | else { | ||
150 | # use data_sources as sources | ||
151 | $sources = $this->data_sources; | ||
152 | } | ||
153 | return $sources; | ||
154 | } | ||
101 | 155 | ||
156 | function rrd_gen_graph() { | ||
102 | $rrdgraph = $this->rrd_options(); | 157 | $rrdgraph = $this->rrd_options(); |
103 | 158 | ||
104 | if (is_array($this->args['tinstance'])) | 159 | $sources = $this->rrd_get_sources(); |
105 | $array = is_array($this->order) ? $this->order : $this->args['tinstance']; | ||
106 | else | ||
107 | $array = $this->data_sources; | ||
108 | 160 | ||
109 | $i=0; | 161 | $i=0; |
110 | foreach ($array as $value) { | 162 | foreach ($this->tinstances as $tinstance) { |
111 | if (is_array($this->args['tinstance'])) { | 163 | foreach ($this->data_sources as $ds) { |
112 | $filename = $this->get_filename($value); | 164 | $rrdgraph[] = sprintf('DEF:min_%s=%s:%s:MIN', $sources[$i], $this->files[$tinstance], $ds); |
113 | $ds = $this->data_sources[0]; | 165 | $rrdgraph[] = sprintf('DEF:avg_%s=%s:%s:AVERAGE', $sources[$i], $this->files[$tinstance], $ds); |
114 | } else { | 166 | $rrdgraph[] = sprintf('DEF:max_%s=%s:%s:MAX', $sources[$i], $this->files[$tinstance], $ds); |
115 | $filename = $this->get_filename(); | 167 | $i++; |
116 | $ds = $value; | ||
117 | } | 168 | } |
118 | $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds); | ||
119 | $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds); | ||
120 | $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds); | ||
121 | $i++; | ||
122 | } | 169 | } |
123 | 170 | ||
124 | if (!is_array($this->args['tinstance'])) { | 171 | if(count($this->files)<=1) { |
125 | $rrdgraph[] = sprintf('AREA:max0#%s', $this->get_faded_color($this->colors[$this->data_sources[0]])); | 172 | foreach ($sources as $source) { |
126 | $rrdgraph[] = sprintf('AREA:min0#%s', 'ffffff'); | 173 | $rrdgraph[] = sprintf('AREA:max_%s#%s', $source, $this->get_faded_color($this->colors[$source])); |
174 | $rrdgraph[] = sprintf('AREA:min_%s#%s', $source, 'ffffff'); | ||
175 | break; # only 1 area to draw | ||
176 | } | ||
127 | } | 177 | } |
128 | 178 | ||
129 | $i=0; | 179 | foreach ($sources as $source) { |
130 | foreach ($array as $value) { | 180 | $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; |
131 | $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value; | 181 | $color = is_array($this->colors) ? $this->colors[$source]: $this->colors; |
132 | $color = is_array($this->colors) ? $this->colors[$value]: $this->colors; | 182 | $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $source, $this->validate_color($color), $dsname); |
133 | $rrdgraph[] = sprintf('LINE1:avg%d#%s:\'%s\'', $i, $this->validate_color($color), $dsname); | 183 | $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $source, $this->rrd_format); |
134 | $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format); | 184 | $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $source, $this->rrd_format); |
135 | $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format); | 185 | $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $source, $this->rrd_format); |
136 | $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format); | 186 | $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\\l\'', $source, $this->rrd_format); |
137 | $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format); | ||
138 | $i++; | ||
139 | } | 187 | } |
140 | 188 | ||
141 | return $rrdgraph; | 189 | return $rrdgraph; |
142 | } | 190 | } |
143 | } | 191 | } |