aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/type/Default.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'type/Default.class.php')
-rw-r--r--type/Default.class.php127
1 files changed, 127 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?>