Created
July 26, 2012 11:25
-
-
Save damoun/3181557 to your computer and use it in GitHub Desktop.
A simple abstract class to create a munin plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Field | |
{ | |
protected $_name; | |
protected $_label; | |
protected $_color; | |
protected $_value; | |
public function Field($label, $color = false) | |
{ | |
$this->_name = strtolower($label); | |
$this->_label = $label; | |
$this->_color = $color; | |
$this->_value = 0; | |
} | |
public function setName($name) | |
{ | |
$this->_name = $name; | |
return $this; | |
} | |
public function getName() | |
{ | |
return $this->_name; | |
} | |
public function setLabel($label) | |
{ | |
$this->_name = $name; | |
return $this; | |
} | |
public function getLabel() | |
{ | |
return $this->_label; | |
} | |
public function setColor($color) | |
{ | |
$this->_color = $color; | |
return $this; | |
} | |
public function getColor() | |
{ | |
return $this->_color; | |
} | |
public function setValue($value) | |
{ | |
$this->_value = $value; | |
$this->_isSet = true; | |
return $this; | |
} | |
public function getValue() | |
{ | |
return $this->_value; | |
} | |
} | |
class MuninPluginAbstract | |
{ | |
protected $_graphTitle; | |
protected $_verticalLabel; | |
protected $_fields; | |
protected $_graphCategory; | |
public function config() | |
{ | |
print 'graph_title ' . $this->_graphTitle . "\n"; | |
print 'graph_category ' . $this->_graphCategory . "\n"; | |
print 'graph_vlabel ' . $this->_verticalLabel . "\n"; | |
foreach ($this->_fields as $field) { | |
print $field->getName() . '.label ' . $field->getLabel() . "\n"; | |
if ($field->getColor() !== false) { | |
print $field->getName() . '.colour ' . $field->getColor() . "\n"; | |
} | |
} | |
} | |
public function getValues() | |
{ | |
foreach ($this->_fields as $field) { | |
print $field->getName() . '.value ' . $field->getValue() . "\n"; | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
require_once realpath(dirname(__FILE__) . '/MuninPluginAbstract.php'); | |
class RandomPlugin extends MuninPluginAbstract | |
{ | |
public function RandomPlugin() | |
{ | |
$this->_graphTitle = 'Random Value graph'; | |
$this->_graphCategory = 'Random'; | |
$this->_verticalLabel = 'Number from random'; | |
$this->_fields = array('Random1' => new Field('Random1'), | |
'Random2' => new Field('Random2'), | |
'Random3' => new Field('Random3', 'FF0000') | |
); | |
srand(time()); | |
} | |
public function getValues() | |
{ | |
$this->_fields['Random1']->setValue(rand()); | |
$this->_fields['Random2']->setValue(rand()); | |
$this->_fields['Random3']->setValue(rand()); | |
parent::getValues(); | |
} | |
} | |
$randomPlugin = new RandomPlugin(); | |
if (isset($argv[1]) === true && ($argv[1] == 'config')) { | |
$randomPlugin->config(); | |
} else { | |
$randomPlugin->getValues(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment