Monday, September 13, 2010

Writing Collectd Plugin in Python

Python plugin is now available with Collectd 4.9
So in order to write a Python plugin first upgrade Collectd to 4.9
Ubuntu by default comes with 4.81 so first check the version of Collectd.

I will try to explain the architecture of Collectd Plugins.

The Plugin register for callbacks. e.g. read write.
So in case you are interested in writing the information one should implement the write
callback. Most common example is the rrdtool which write the information.

So what happens here is when any other plugin dispatches(there is a dispatch method) information, rrdtool has registered for write callback.
So rrd plugin writes this information.


Most of the times you will end up writing a plugin which reads. That is you are trying to retrieve information from somewhere which you will dispatch and then rrdtool will automatically
fetch this information and write this to the db.

Then you should be able to see the Graphs.




Create a Python File e.g. myplugin.py

import collectd # This class contains all the collectd methods

#== Callback Functions ==#
def configure_callback(conf):
collectd.debug('Configuring Stuff')
for node in conf.children: # here u can iterate over the config parameters
if node.key == 'Host':

def initer():
collectd.debug('initing stuff')

def reader_calback(input_data=None): # in this function ur suppose to read information
collectd.info("##################################\n") # Log
v2 = collectd.Values(type='percent') # Should be one of types.db
v2.host = "SomeHost" # Name of Host to display Graph
v2.plugin='myplugin' # Name of Plugin
v2.type_instance='MyPercent' # This will be the header of Graph
v2.dispatch(values=[random.random() * 100]) # Value to be dispatched


#== Hook Callbacks, Order is important! ==#
collectd.register_config(configure_callback)
collectd.register_init(initer)
collectd.register_read(reader_calback, 30 ) # Here 30 is ur plugins read will be called after 30 seconds



Now lets take a look at the Config


LoadPlugin python

ModulePath "/tmp/myplugin/"
LogTraces true
Interactive false
Import myplugin


Host "Abc"
Host "XYZ"
VM "1" "a"
VM "2" "b"
VM "3" "c"





Note: ModulePath is imp. It should point to the path of ur module
Import should be the name of ur plugin as well as in tag u should specify ur modules name.

No comments:

Post a Comment