From cd94ef34134d8f06af83ec97119018b4e80346a0 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Sun, 20 Sep 2009 13:36:25 +0200 Subject: initial import of cgp Collectd Graph Panel is a frontend for Collectd written in PHP. The goal of CGP is to provide an easy-to-use frontend for Collectd, starting with page that shows an overview of all the hosts you are managing with Collectd. In this initial import there is support for the plugins that are default enabled in Collectd. The supported plugins are located in the plugin directory. --- type/Default.class.php | 127 ++++++++++++++++++++++++++++++++++++++++++ type/GenericIO.class.php | 59 ++++++++++++++++++++ type/GenericStacked.class.php | 65 +++++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 type/Default.class.php create mode 100644 type/GenericIO.class.php create mode 100644 type/GenericStacked.class.php (limited to 'type') 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 @@ +validate_color($fgc); + if (!is_numeric($percent)) + $percent=0.25; + + $rgb = array('r', 'g', 'b'); + + $fg[r] = hexdec(substr($fgc,0,2)); + $fg[g] = hexdec(substr($fgc,2,2)); + $fg[b] = hexdec(substr($fgc,4,2)); + $bg[r] = hexdec(substr($bgc,0,2)); + $bg[g] = hexdec(substr($bgc,2,2)); + $bg[b] = hexdec(substr($bgc,4,2)); + + foreach ($rgb as $pri) { + $c[$pri] = dechex(round($percent * $fg[$pri]) + ((1.0 - $percent) * $bg[$pri])); + if ($c[$pri] == '0') + $c[$pri] = '00'; + } + + return $c[r].$c[g].$c[b]; + } + + function get_filename($tinstance=NULL) { + if (!is_array($this->args['tinstance']) && $tinstance == NULL) + $tinstance = $this->args['tinstance']; + + $search = array('{host}', '{plugin}', '{pinstance}', '{type}', '{tinstance}'); + $replace = array($this->args['host'], $this->args['plugin'], $this->args['pinstance'], $this->args['type'], $tinstance); + $f = $this->datadir . '/' . str_replace($search, $replace, $this->path_format); + return $f; + } + + function rrd_graph($debug=false) { + $graphdata = $this->rrd_gen_graph(); + + if(!$debug) { + # caching + header("Expires: " . date(DATE_RFC822,strtotime("90 seconds"))); + header("content-type: image/png"); + $graphdata = implode(' ', $graphdata); + echo `$graphdata`; + } else { + print '
';
+			print_r($graphdata);
+			print '
'; + } + } + + function rrd_gen_graph() { + $filename = $this->get_filename(); + + $rrdgraph[] = '/usr/bin/rrdtool graph - -a PNG'; + $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400); + $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175); + $rrdgraph[] = '-l 0'; + $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title); + $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical); + $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400); + + if (is_array($this->args['tinstance'])) + $array = is_array($this->order) ? $this->order : $this->args['tinstance']; + else + $array = $this->data_sources; + + $i=0; + foreach ($array as $value) { + if (is_array($this->args['tinstance'])) { + $filename = $this->get_filename($value); + $ds = $this->data_sources[0]; + } else { + $filename = $this->get_filename(); + $ds = $value; + } + $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds); + $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds); + $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds); + $i++; + } + + if (!is_array($this->args['tinstance'])) { + $rrdgraph[] = sprintf('AREA:max0#%s', $this->get_faded_color($this->colors[$this->data_sources[0]])); + $rrdgraph[] = sprintf('AREA:min0#%s', 'ffffff'); + } + + $i=0; + foreach ($array as $value) { + $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value; + $color = is_array($this->colors) ? $this->colors[$value]: $this->colors; + $rrdgraph[] = sprintf('LINE1:avg%d#%s:\'%s\'', $i, $this->validate_color($color), $dsname); + $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format); + $i++; + } + + return $rrdgraph; + } +} + +?> 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 @@ +get_filename(); + + $rrdgraph[] = '/usr/bin/rrdtool graph - -a PNG'; + $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400); + $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175); + $rrdgraph[] = '-l 0'; + $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title); + $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical); + $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400); + + if ($this->scale) + $raw = '_raw'; + foreach($this->data_sources as $ds) { + $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', $ds, $raw, $filename, $ds); + $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', $ds, $raw, $filename, $ds); + $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', $ds, $raw, $filename, $ds); + } + if ($this->scale) { + foreach($this->data_sources as $ds) { + $rrdgraph[] = sprintf('CDEF:min_%s=min_%s_raw,%s,*', $ds, $ds, $this->scale); + $rrdgraph[] = sprintf('CDEF:avg_%s=avg_%s_raw,%s,*', $ds, $ds, $this->scale); + $rrdgraph[] = sprintf('CDEF:max_%s=max_%s_raw,%s,*', $ds, $ds, $this->scale); + } + } + + $rrdgraph[] = sprintf('CDEF:overlap=avg_%s,avg_%s,LT,avg_%1$s,avg_%2$s,IF', + $this->data_sources[0], $this->data_sources[1]); + + foreach($this->data_sources as $ds) { + $rrdgraph[] = sprintf('AREA:avg_%s#%s', $ds, $this->get_faded_color($this->colors[$ds])); + } + + $rrdgraph[] = sprintf('AREA:overlap#%s', + $this->get_faded_color( + $this->get_faded_color($this->colors[$this->data_sources[0]]), + $this->get_faded_color($this->colors[$this->data_sources[1]]) + ) + ); + + foreach($this->data_sources as $ds) { + $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $ds, $this->colors[$ds], $this->ds_names[$ds]); + $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $ds, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $ds, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $ds, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\l\'', $ds, $this->rrd_format); + } + + return $rrdgraph; + } +} + +?> 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 @@ +width) ? $this->width : 400); + $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175); + $rrdgraph[] = '-l 0'; + $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title); + $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical); + $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400); + + if (is_array($this->args['tinstance'])) + $array = is_array($this->order) ? $this->order : $this->args['tinstance']; + else + $array = $this->data_sources; + + $i=0; + foreach ($array as $value) { + if (is_array($this->args['tinstance'])) { + $filename = $this->get_filename($value); + $ds = $this->data_sources[0]; + } else { + $filename = $this->get_filename(); + $ds = $value; + } + $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds); + $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds); + $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds); + $i++; + } + + for ($i=count($array)-1 ; $i>=0 ; $i--) { + if ($i == (count($array)-1)) + $rrdgraph[] = sprintf('CDEF:cdef%d=avg%d', $i, $i); + else + $rrdgraph[] = sprintf('CDEF:cdef%d=cdef%d,avg%d,+', $i, $i+1, $i); + } + + $i=0; + foreach ($array as $value) { + $color = $this->get_faded_color($this->colors[$value]); + $rrdgraph[] = sprintf('AREA:cdef%d#%s', $i, $color); + $i++; + } + + $i=0; + foreach ($array as $value) { + $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value; + $rrdgraph[] = sprintf('LINE1:cdef%d#%s:\'%s\'', $i, $this->validate_color($this->colors[$value]), $dsname); + $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format); + $i++; + } + + return $rrdgraph; + } +} + +?> -- cgit v1.1