From ce395c9752d1f6ed3a9f53b03d6e616e90dd90a5 Mon Sep 17 00:00:00 2001 From: Nicolas Hohm Date: Wed, 16 Jul 2014 19:45:59 +0200 Subject: [PATCH] Implement basic functionality --- lib/MuninPlugin/Data.php | 43 ++++++++++++++++++ lib/MuninPlugin/Datasource.php | 48 ++++++++++++++++++++ lib/MuninPlugin/DatasourceSet.php | 25 +++++++++++ lib/MuninPlugin/Graph.php | 74 +++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 lib/MuninPlugin/Data.php create mode 100644 lib/MuninPlugin/Datasource.php create mode 100644 lib/MuninPlugin/DatasourceSet.php create mode 100644 lib/MuninPlugin/Graph.php diff --git a/lib/MuninPlugin/Data.php b/lib/MuninPlugin/Data.php new file mode 100644 index 0000000..63024a7 --- /dev/null +++ b/lib/MuninPlugin/Data.php @@ -0,0 +1,43 @@ + + */ +abstract class Data { + + public function __call($name, $arguments) + { + $var_name = strtolower(substr($name, 3)); + + if (strpos($name, 'get') === 0) { + return $this->$var_name; + } elseif (strpos($name, 'set') === 0) { + $this->$var_name = $arguments[0]; + return $this; + } else { + throw new \Exception('Unkown method '.$name, 1); + } + } + + protected function getObjectVars(array $blacklist) + { + $objectVars = get_object_vars($this); + + foreach (array_keys($objectVars) as $key) { + + if (in_array($key, $blacklist)) { + unset($objectVars[$key]); + } + + } + + return $objectVars; + } + + protected function getObjectVarsNotNull(array $blacklist) { + return array_filter($this->getObjectVars($blacklist)); + } + +} diff --git a/lib/MuninPlugin/Datasource.php b/lib/MuninPlugin/Datasource.php new file mode 100644 index 0000000..cce8f01 --- /dev/null +++ b/lib/MuninPlugin/Datasource.php @@ -0,0 +1,48 @@ + + */ +class Datasource extends Data { + + protected $label; + protected $cdef; + protected $draw; + protected $graph; + protected $info; + protected $extinfo; + protected $max; + protected $min; + protected $negative; + protected $type; + protected $warning; + protected $critical; + protected $colour; + protected $sum; + protected $stack; + protected $line; + protected $oldname; + protected $value; + + protected $name; + protected $blacklist = array('blacklist', 'name', 'value'); + + public function __construct($name) { + $this->name = $name; + } + + public function getConfig() + { + $output = []; + $objectVars = $this->getObjectVarsNotNull($this->blacklist); + + foreach ($objectVars as $key => $value) { + $output[] = $this->name.'.'.$key.' '.$value; + } + + return implode(PHP_EOL, $output); + } + +} diff --git a/lib/MuninPlugin/DatasourceSet.php b/lib/MuninPlugin/DatasourceSet.php new file mode 100644 index 0000000..85c8b81 --- /dev/null +++ b/lib/MuninPlugin/DatasourceSet.php @@ -0,0 +1,25 @@ + + */ +class DatasourceSet extends \ArrayObject { + + public function getValues() + { + $output = []; + + foreach ($this as $datasource) { + $output[] = $datasource->getName().'.value '.$datasource->getValue(); + } + + return implode(PHP_EOL, $output); + } + + public function append(Datasource $datasource) { + parent::append($datasource); + } + +} diff --git a/lib/MuninPlugin/Graph.php b/lib/MuninPlugin/Graph.php new file mode 100644 index 0000000..bcd958b --- /dev/null +++ b/lib/MuninPlugin/Graph.php @@ -0,0 +1,74 @@ + + */ +class Graph extends Data { + + protected $title; + protected $createArgs; + protected $graphArgs; + protected $category; + protected $info; + protected $order; + protected $vlabel; + protected $total; + protected $scale; + protected $graph; + protected $host_name; + protected $update; + protected $period; + protected $vtitle; + protected $service_order; + protected $width; + protected $height; + protected $printf; + + protected $datasourceSet; + protected $blacklist = array('blacklist', 'datasourceSet'); + + public function __construct($title) + { + $this->title = $title; + $this->datasourceSet = new DatasourceSet(); + } + + public function getConfig() + { + $output = [$this->getGraphConfig()]; + + foreach ($this->datasourceSet as $datasource) { + $output[] = $datasource->getConfig(); + } + + return implode(PHP_EOL, $output); + } + + protected function getGraphConfig() + { + $output = []; + $objectVars = $this->getObjectVarsNotNull($this->blacklist); + + foreach ($objectVars as $key => $value) { + + $output[] = 'graph_'.$key.' '.$value; + + } + + return implode(PHP_EOL, $output); + } + + public function getDatasourceSet() + { + return $this->datasourceSet; + } + + public function setDatasourceSet(DatasourceSet $datasourceSet) + { + $this->datasourceSet = $datasourceSet; + return $this; + } + +}