aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/type
diff options
context:
space:
mode:
Diffstat (limited to 'type')
-rw-r--r--type/Default.class.php127
-rw-r--r--type/GenericIO.class.php59
-rw-r--r--type/GenericStacked.class.php65
3 files changed, 251 insertions, 0 deletions
diff --git a/type/Default.class.php b/type/Default.class.php
new file mode 100644
index 0000000..8a5dd72
--- /dev/null
+++ b/type/Default.class.php
@@ -0,0 +1,127 @@
1<?php
2
3# Collectd Default type
4
5class Type_Default {
6 var $datadir;
7 var $args;
8 var $data_sources;
9 var $order;
10 var $ds_names;
11 var $colors;
12 var $rrd_title;
13 var $rrd_vertical;
14 var $rrd_format;
15 var $scale;
16 var $width;
17 var $heigth;
18
19 function validate_color($color) {
20 if (!preg_match('/^[0-9a-f]{6}$/', $color))
21 return '000000';
22 else
23 return $color;
24 }
25
26 function get_faded_color($fgc, $bgc='ffffff', $percent=0.25) {
27 $fgc = $this->validate_color($fgc);
28 if (!is_numeric($percent))
29 $percent=0.25;
30
31 $rgb = array('r', 'g', 'b');
32
33 $fg[r] = hexdec(substr($fgc,0,2));
34 $fg[g] = hexdec(substr($fgc,2,2));
35 $fg[b] = hexdec(substr($fgc,4,2));
36 $bg[r] = hexdec(substr($bgc,0,2));
37 $bg[g] = hexdec(substr($bgc,2,2));
38 $bg[b] = hexdec(substr($bgc,4,2));
39
40 foreach ($rgb as $pri) {
41 $c[$pri] = dechex(round($percent * $fg[$pri]) + ((1.0 - $percent) * $bg[$pri]));
42 if ($c[$pri] == '0')
43 $c[$pri] = '00';
44 }
45
46 return $c[r].$c[g].$c[b];
47 }
48
49 function get_filename($tinstance=NULL) {
50 if (!is_array($this->args['tinstance']) && $tinstance == NULL)
51 $tinstance = $this->args['tinstance'];
52
53 $search = array('{host}', '{plugin}', '{pinstance}', '{type}', '{tinstance}');
54 $replace = array($this->args['host'], $this->args['plugin'], $this->args['pinstance'], $this->args['type'], $tinstance);
55 $f = $this->datadir . '/' . str_replace($search, $replace, $this->path_format);
56 return $f;
57 }
58
59 function rrd_graph($debug=false) {
60 $graphdata = $this->rrd_gen_graph();
61
62 if(!$debug) {
63 # caching
64 header("Expires: " . date(DATE_RFC822,strtotime("90 seconds")));
65 header("content-type: image/png");
66 $graphdata = implode(' ', $graphdata);
67 echo `$graphdata`;
68 } else {
69 print '<pre>';
70 print_r($graphdata);
71 print '</pre>';
72 }
73 }
74
75 function rrd_gen_graph() {
76 $filename = $this->get_filename();
77
78 $rrdgraph[] = '/usr/bin/rrdtool graph - -a PNG';
79 $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400);
80 $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175);
81 $rrdgraph[] = '-l 0';
82 $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title);
83 $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical);
84 $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400);
85
86 if (is_array($this->args['tinstance']))
87 $array = is_array($this->order) ? $this->order : $this->args['tinstance'];
88 else
89 $array = $this->data_sources;
90
91 $i=0;
92 foreach ($array as $value) {
93 if (is_array($this->args['tinstance'])) {
94 $filename = $this->get_filename($value);
95 $ds = $this->data_sources[0];
96 } else {
97 $filename = $this->get_filename();
98 $ds = $value;
99 }
100 $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds);
101 $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds);
102 $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds);
103 $i++;
104 }
105
106 if (!is_array($this->args['tinstance'])) {
107 $rrdgraph[] = sprintf('AREA:max0#%s', $this->get_faded_color($this->colors[$this->data_sources[0]]));
108 $rrdgraph[] = sprintf('AREA:min0#%s', 'ffffff');
109 }
110
111 $i=0;
112 foreach ($array as $value) {
113 $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value;
114 $color = is_array($this->colors) ? $this->colors[$value]: $this->colors;
115 $rrdgraph[] = sprintf('LINE1:avg%d#%s:\'%s\'', $i, $this->validate_color($color), $dsname);
116 $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format);
117 $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format);
118 $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format);
119 $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format);
120 $i++;
121 }
122
123 return $rrdgraph;
124 }
125}
126
127?>
diff --git a/type/GenericIO.class.php b/type/GenericIO.class.php
new file mode 100644
index 0000000..573abde
--- /dev/null
+++ b/type/GenericIO.class.php
@@ -0,0 +1,59 @@
1<?php
2
3require_once 'Default.class.php';
4
5class Type_GenericIO extends Type_Default {
6
7 function rrd_gen_graph() {
8 $filename = $this->get_filename();
9
10 $rrdgraph[] = '/usr/bin/rrdtool graph - -a PNG';
11 $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400);
12 $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175);
13 $rrdgraph[] = '-l 0';
14 $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title);
15 $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical);
16 $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400);
17
18 if ($this->scale)
19 $raw = '_raw';
20 foreach($this->data_sources as $ds) {
21 $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', $ds, $raw, $filename, $ds);
22 $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', $ds, $raw, $filename, $ds);
23 $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', $ds, $raw, $filename, $ds);
24 }
25 if ($this->scale) {
26 foreach($this->data_sources as $ds) {
27 $rrdgraph[] = sprintf('CDEF:min_%s=min_%s_raw,%s,*', $ds, $ds, $this->scale);
28 $rrdgraph[] = sprintf('CDEF:avg_%s=avg_%s_raw,%s,*', $ds, $ds, $this->scale);
29 $rrdgraph[] = sprintf('CDEF:max_%s=max_%s_raw,%s,*', $ds, $ds, $this->scale);
30 }
31 }
32
33 $rrdgraph[] = sprintf('CDEF:overlap=avg_%s,avg_%s,LT,avg_%1$s,avg_%2$s,IF',
34 $this->data_sources[0], $this->data_sources[1]);
35
36 foreach($this->data_sources as $ds) {
37 $rrdgraph[] = sprintf('AREA:avg_%s#%s', $ds, $this->get_faded_color($this->colors[$ds]));
38 }
39
40 $rrdgraph[] = sprintf('AREA:overlap#%s',
41 $this->get_faded_color(
42 $this->get_faded_color($this->colors[$this->data_sources[0]]),
43 $this->get_faded_color($this->colors[$this->data_sources[1]])
44 )
45 );
46
47 foreach($this->data_sources as $ds) {
48 $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $ds, $this->colors[$ds], $this->ds_names[$ds]);
49 $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $ds, $this->rrd_format);
50 $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $ds, $this->rrd_format);
51 $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $ds, $this->rrd_format);
52 $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\l\'', $ds, $this->rrd_format);
53 }
54
55 return $rrdgraph;
56 }
57}
58
59?>
diff --git a/type/GenericStacked.class.php b/type/GenericStacked.class.php
new file mode 100644
index 0000000..99f05eb
--- /dev/null
+++ b/type/GenericStacked.class.php
@@ -0,0 +1,65 @@
1<?php
2
3require_once 'Default.class.php';
4
5class Type_GenericStacked extends Type_Default {
6
7 function rrd_gen_graph() {
8 $rrdgraph[] = '/usr/bin/rrdtool graph - -a PNG';
9 $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400);
10 $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175);
11 $rrdgraph[] = '-l 0';
12 $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title);
13 $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical);
14 $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400);
15
16 if (is_array($this->args['tinstance']))
17 $array = is_array($this->order) ? $this->order : $this->args['tinstance'];
18 else
19 $array = $this->data_sources;
20
21 $i=0;
22 foreach ($array as $value) {
23 if (is_array($this->args['tinstance'])) {
24 $filename = $this->get_filename($value);
25 $ds = $this->data_sources[0];
26 } else {
27 $filename = $this->get_filename();
28 $ds = $value;
29 }
30 $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds);
31 $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds);
32 $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds);
33 $i++;
34 }
35
36 for ($i=count($array)-1 ; $i>=0 ; $i--) {
37 if ($i == (count($array)-1))
38 $rrdgraph[] = sprintf('CDEF:cdef%d=avg%d', $i, $i);
39 else
40 $rrdgraph[] = sprintf('CDEF:cdef%d=cdef%d,avg%d,+', $i, $i+1, $i);
41 }
42
43 $i=0;
44 foreach ($array as $value) {
45 $color = $this->get_faded_color($this->colors[$value]);
46 $rrdgraph[] = sprintf('AREA:cdef%d#%s', $i, $color);
47 $i++;
48 }
49
50 $i=0;
51 foreach ($array as $value) {
52 $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value;
53 $rrdgraph[] = sprintf('LINE1:cdef%d#%s:\'%s\'', $i, $this->validate_color($this->colors[$value]), $dsname);
54 $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format);
55 $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format);
56 $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format);
57 $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format);
58 $i++;
59 }
60
61 return $rrdgraph;
62 }
63}
64
65?>