| [ < ] | [ > ] | [ << ] | [ 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:
EMT plugins are typically divided between a command that is used to collect stats and the code in EMT that defines what is being collected from those commands. A good example plugin that ships with EMT is the emt_mysql plugin. This gathers stats from MySQL such as types of queries being executed, innodb buffer pool, transacation log, and row stats. The standalone command for this is emt_mysql. It's command line arguments are similar to the mysql command line client.
/opt/emt/bin/emt_mysql -t 60 -h :/var/lib/mysql/mysql.sock -u emt_user -p emt_pass
emt_mysql will connect to mysql as user emt_user with password emt_pass and collect stats for 60 seconds. The output is a csv string which I've shortened here because the full output is about 3900 characters.
mysql_com_rollback=0,mysql_opened_tables=0,mysql_bytes_received=29488,mysql_bytes_sent=409284,mysql_created_tmp_disk_tables=0,mysql_created_tmp_files=0
This isn't very useful for human consumption but it's great for EMT because EMT has built in parsing for commands that output CSV. Now we need to teach EMT how to execute this command and parse the data from it.
Let's start with the defining the command object. The source code for the class that handles 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 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'];
}
}
After the command class has been defined some field objects must be created. A field object defines the structure of a single value gathered by EMT such as number of select queries executed or bytes received by MySQL. 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.
These field classes extend csv_field which defines a parse function that EMT uses to parse the output of a command. If your command does not output CSV format you will need to defined a parse method per field or have your field objects extend a class that defines a parse function for your command. Examples of this can be found in ‘emt/commands/iostat.php’ and ‘emt/commands/vmstat.php’.
The most common type of field is a field with sub fields. Let's take a look at the mysql_com field which is responsible for keeping track of all the different Com_* status variables in MySQL. MySQL gives us a Com status variable for each different kind command such as select, delete, and commit. In the case of MySQL the list of commands is pretty standard but since they're sub fields EMT will automatically track new commands as they're created. The emt_mysql command returns keys like mysql_com.select, mysql_com.delete, and mysql_com.commit for each of the commands it detects from show global status during runtime.
class mysql_com_field extends csv_field
{
var $namespace = 'mysql';
var $name = 'mysql_com';
var $fname = 'Com';
var $description = 'MySQL Com';
var $type = 'incr';
var $precsision = 0;
var $command = 'emt_mysql';
var $multi_value = TRUE;
}
The last step is to create a configuration file for your plugin. Configuration files serve two purposes the first is to pass parameters to your commands, the second is to tell EMT which fields you want gathered. Just because a field is defined in a php file doesn't mean EMT will start collecting it. It also must be defined in the configuration file. This allows users to deploy all of their EMT plugins and pick which fields and thus which commands should be executed per machine. This is especially important because EMT ships with every plugin, they're enabled by copying configuration files into the emt.d dir.
This is the first part of the emt_mysql.cnf configuration file.
[emt_mysql] host=:/var/lib/mysql/mysql.sock user=emt_user password=emt_pass [emt_gather] field=mysql_bytes_received field=mysql_bytes_sent field=mysql_com field=mysql_open_tables field=mysql_opened_tables field=mysql_threads_connected field=mysql_threads_running field=mysql_slow_queries
I left out some of the fields for brevity. In order for a field to be acknowledged by EMT it must be in a configuration file under an [emt_gather] heading.
The first section of the configuration file will be parsed and set to ->config in the command object. In the command object example above you can see it referencing $this->config['host'] and others. The file name of the configuration file doesn't matter. EMT parses them as a global configuration and will pass the appropriately named section to your command.
One the command file is in ‘plugins/commands/’, the field file is in ‘plugins/fields/’ and the configuration file is in extra_config_dir (typically /etc/emt.d/) EMT will begin executing your plugins. You can see any errors it found in /var/log/messages.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |