| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
For an example of how plugins are created let's take a look at the mysql plugin. The mysql plugin consists of five parts:
Let's start with the defining the command object. The source code for the two classes that handle the emt_mysql command is located in ‘emt_mysql.php’ in ‘emt/plugins/commands/’. The emt_mysql class is responsible for defining the shell command that EMT will execute. The emt_mysql_parse class contains the parsing logic for taking the output of the emt_mysql shell command and creating field objects out of it. The emt_mysql command can be replaced by any other CLI program for your own plugins. Configration parameters from the ‘.cnf’ file for your plugin are parsed and set in the config object.
class emt_mysql extends gather_general
{
function command()
{
$this->cmd = INSTALL_PATH . 'bin/emt_mysql ' .
"-t " . $this->gather_time . " " .
"-h " . $this->config['host'] . " " .
"-u " . $this->config['user'] . " " .
"-p " . $this->config['password'];
}
}
class emt_mysql_parse extends field
{
function parse($str)
{
$fields = preg_split('/,\s*/', $str);
foreach ($fields as $field)
{
list($key, $value) = split('=', $field);
if ($key == $this->name)
return parent::parse($value);
}
}
}
This example class parses the emt_mysql program output (which is already in CSV format) into individual values. Calling parent::parse on a value will tell the parent field class to create an field object out of the value.
After the command and parse classes have been defined, some field objects must be created. A field object defines the structure of a single value gathered by EMT. Looking at ‘mysql.php’ in ‘emt/plugins/fields/’, there are several field classes defined for the emt_mysql plugin. Each value gathered by EMT needs a field class associated with it. This gives information about decimal precision, namespace, and description which are used by emt_view and will eventually used by an associated graphing application.
IMPORTANT! Do not forget to register your fields with the register_field function. This adds the field name to a global array that EMT uses to discover which fields have been created. EMT will not gather data for a field if the field is not registered.
The last step is to create a configuration file for your plugin.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |