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 | |
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')
-rw-r--r-- | type/Default.class.php | 142 | ||||
-rw-r--r-- | type/GenericIO.class.php | 51 | ||||
-rw-r--r-- | type/GenericStacked.class.php | 59 |
3 files changed, 147 insertions, 105 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 | } |
diff --git a/type/GenericIO.class.php b/type/GenericIO.class.php index e91cb16..ee04316 100644 --- a/type/GenericIO.class.php +++ b/type/GenericIO.class.php | |||
@@ -5,45 +5,54 @@ require_once 'Default.class.php'; | |||
5 | class Type_GenericIO extends Type_Default { | 5 | class Type_GenericIO extends Type_Default { |
6 | 6 | ||
7 | function rrd_gen_graph() { | 7 | function rrd_gen_graph() { |
8 | $filename = $this->get_filename(); | ||
9 | |||
10 | $rrdgraph = $this->rrd_options(); | 8 | $rrdgraph = $this->rrd_options(); |
11 | 9 | ||
10 | $sources = $this->rrd_get_sources(); | ||
11 | |||
12 | if ($this->scale) | 12 | if ($this->scale) |
13 | $raw = '_raw'; | 13 | $raw = '_raw'; |
14 | foreach($this->data_sources as $ds) { | 14 | $i=0; |
15 | $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', $ds, $raw, $filename, $ds); | 15 | foreach ($this->tinstances as $tinstance) { |
16 | $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', $ds, $raw, $filename, $ds); | 16 | foreach ($this->data_sources as $ds) { |
17 | $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', $ds, $raw, $filename, $ds); | 17 | $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', $sources[$i], $raw, $this->files[$tinstance], $ds); |
18 | $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', $sources[$i], $raw, $this->files[$tinstance], $ds); | ||
19 | $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', $sources[$i], $raw, $this->files[$tinstance], $ds); | ||
20 | $i++; | ||
21 | } | ||
18 | } | 22 | } |
19 | if ($this->scale) { | 23 | if ($this->scale) { |
20 | foreach($this->data_sources as $ds) { | 24 | $i=0; |
21 | $rrdgraph[] = sprintf('CDEF:min_%s=min_%s_raw,%s,*', $ds, $ds, $this->scale); | 25 | foreach ($this->tinstances as $tinstance) { |
22 | $rrdgraph[] = sprintf('CDEF:avg_%s=avg_%s_raw,%s,*', $ds, $ds, $this->scale); | 26 | foreach ($this->data_sources as $ds) { |
23 | $rrdgraph[] = sprintf('CDEF:max_%s=max_%s_raw,%s,*', $ds, $ds, $this->scale); | 27 | $rrdgraph[] = sprintf('CDEF:min_%s=min_%1$s_raw,%s,*', $sources[$i], $this->scale); |
28 | $rrdgraph[] = sprintf('CDEF:avg_%s=avg_%1$s_raw,%s,*', $sources[$i], $this->scale); | ||
29 | $rrdgraph[] = sprintf('CDEF:max_%s=max_%1$s_raw,%s,*', $sources[$i], $this->scale); | ||
30 | $i++; | ||
31 | } | ||
24 | } | 32 | } |
25 | } | 33 | } |
26 | 34 | ||
27 | $rrdgraph[] = sprintf('CDEF:overlap=avg_%s,avg_%s,LT,avg_%1$s,avg_%2$s,IF', | 35 | $rrdgraph[] = sprintf('CDEF:overlap=avg_%s,avg_%s,LT,avg_%1$s,avg_%2$s,IF', |
28 | $this->data_sources[0], $this->data_sources[1]); | 36 | $sources[0], $sources[1]); |
29 | 37 | ||
30 | foreach($this->data_sources as $ds) { | 38 | foreach($sources as $source) { |
31 | $rrdgraph[] = sprintf('AREA:avg_%s#%s', $ds, $this->get_faded_color($this->colors[$ds])); | 39 | $rrdgraph[] = sprintf('AREA:avg_%s#%s', $source, $this->get_faded_color($this->colors[$source])); |
32 | } | 40 | } |
33 | 41 | ||
34 | $rrdgraph[] = sprintf('AREA:overlap#%s', | 42 | $rrdgraph[] = sprintf('AREA:overlap#%s', |
35 | $this->get_faded_color( | 43 | $this->get_faded_color( |
36 | $this->get_faded_color($this->colors[$this->data_sources[0]]), | 44 | $this->get_faded_color($this->colors[$sources[0]]), |
37 | $this->get_faded_color($this->colors[$this->data_sources[1]]) | 45 | $this->get_faded_color($this->colors[$sources[1]]) |
38 | ) | 46 | ) |
39 | ); | 47 | ); |
40 | 48 | ||
41 | foreach($this->data_sources as $ds) { | 49 | foreach($sources as $source) { |
42 | $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $ds, $this->colors[$ds], $this->ds_names[$ds]); | 50 | $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; |
43 | $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $ds, $this->rrd_format); | 51 | $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $source, $this->colors[$source], $dsname); |
44 | $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $ds, $this->rrd_format); | 52 | $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $source, $this->rrd_format); |
45 | $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $ds, $this->rrd_format); | 53 | $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $source, $this->rrd_format); |
46 | $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\l\'', $ds, $this->rrd_format); | 54 | $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $source, $this->rrd_format); |
55 | $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\l\'', $source, $this->rrd_format); | ||
47 | } | 56 | } |
48 | 57 | ||
49 | return $rrdgraph; | 58 | return $rrdgraph; |
diff --git a/type/GenericStacked.class.php b/type/GenericStacked.class.php index f024336..7595385 100644 --- a/type/GenericStacked.class.php +++ b/type/GenericStacked.class.php | |||
@@ -3,56 +3,41 @@ | |||
3 | require_once 'Default.class.php'; | 3 | require_once 'Default.class.php'; |
4 | 4 | ||
5 | class Type_GenericStacked extends Type_Default { | 5 | class Type_GenericStacked extends Type_Default { |
6 | 6 | ||
7 | function rrd_gen_graph() { | 7 | function rrd_gen_graph() { |
8 | $rrdgraph = $this->rrd_options(); | 8 | $rrdgraph = $this->rrd_options(); |
9 | 9 | ||
10 | if (is_array($this->args['tinstance'])) | 10 | $sources = $this->rrd_get_sources(); |
11 | if (is_array($this->order)) | ||
12 | $array = array_intersect($this->order, $this->args['tinstance']); | ||
13 | else | ||
14 | $array = $this->args['tinstance']; | ||
15 | else | ||
16 | $array = $this->data_sources; | ||
17 | 11 | ||
18 | $i=0; | 12 | $i=0; |
19 | foreach ($array as $value) { | 13 | foreach ($this->tinstances as $tinstance) { |
20 | if (is_array($this->args['tinstance'])) { | 14 | foreach ($this->data_sources as $ds) { |
21 | $filename = $this->get_filename($value); | 15 | $rrdgraph[] = sprintf('DEF:min_%s=%s:%s:MIN', $sources[$i], $this->files[$tinstance], $ds); |
22 | $ds = $this->data_sources[0]; | 16 | $rrdgraph[] = sprintf('DEF:avg_%s=%s:%s:AVERAGE', $sources[$i], $this->files[$tinstance], $ds); |
23 | } else { | 17 | $rrdgraph[] = sprintf('DEF:max_%s=%s:%s:MAX', $sources[$i], $this->files[$tinstance], $ds); |
24 | $filename = $this->get_filename(); | 18 | $i++; |
25 | $ds = $value; | ||
26 | } | 19 | } |
27 | $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds); | ||
28 | $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds); | ||
29 | $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds); | ||
30 | $i++; | ||
31 | } | 20 | } |
32 | 21 | ||
33 | for ($i=count($array)-1 ; $i>=0 ; $i--) { | 22 | for ($i=count($sources)-1 ; $i>=0 ; $i--) { |
34 | if ($i == (count($array)-1)) | 23 | if ($i == (count($sources)-1)) |
35 | $rrdgraph[] = sprintf('CDEF:cdef%d=avg%d', $i, $i); | 24 | $rrdgraph[] = sprintf('CDEF:area_%s=avg_%1$s', $sources[$i]); |
36 | else | 25 | else |
37 | $rrdgraph[] = sprintf('CDEF:cdef%d=cdef%d,avg%d,+', $i, $i+1, $i); | 26 | $rrdgraph[] = sprintf('CDEF:area_%s=area_%s,avg_%1$s,+', $sources[$i], $sources[$i+1]); |
38 | } | 27 | } |
39 | 28 | ||
40 | $i=0; | 29 | foreach ($sources as $source) { |
41 | foreach ($array as $value) { | 30 | $color = $this->get_faded_color($this->colors[$source]); |
42 | $color = $this->get_faded_color($this->colors[$value]); | 31 | $rrdgraph[] = sprintf('AREA:area_%s#%s', $source, $color); |
43 | $rrdgraph[] = sprintf('AREA:cdef%d#%s', $i, $color); | ||
44 | $i++; | ||
45 | } | 32 | } |
46 | 33 | ||
47 | $i=0; | 34 | foreach ($sources as $source) { |
48 | foreach ($array as $value) { | 35 | $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; |
49 | $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value; | 36 | $rrdgraph[] = sprintf('LINE1:area_%s#%s:\'%s\'', $source, $this->validate_color($this->colors[$source]), $dsname); |
50 | $rrdgraph[] = sprintf('LINE1:cdef%d#%s:\'%s\'', $i, $this->validate_color($this->colors[$value]), $dsname); | 37 | $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $source, $this->rrd_format); |
51 | $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format); | 38 | $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $source, $this->rrd_format); |
52 | $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format); | 39 | $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $source, $this->rrd_format); |
53 | $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format); | 40 | $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\\l\'', $source, $this->rrd_format); |
54 | $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format); | ||
55 | $i++; | ||
56 | } | 41 | } |
57 | 42 | ||
58 | return $rrdgraph; | 43 | return $rrdgraph; |