Skip to content

Instantly share code, notes, and snippets.

@jijojv
Forked from drmalex07/spam-rrd-graph-example.sh
Created September 16, 2016 15:47
Show Gist options
  • Save jijojv/1f4bbabd6db56984e5caccb8d62f04b6 to your computer and use it in GitHub Desktop.
Save jijojv/1f4bbabd6db56984e5caccb8d62f04b6 to your computer and use it in GitHub Desktop.
Example Python plugin for collectd. #collectd #collectd-plugin
#!/bin/bash
# Navigate to where the RRD database is stored
cd /var/lib/collectd/rrd/localhost/spam/
# Graph values for the last 10 minutes (600s)
rrdtool graph /tmp/gauge-foo.svg -a SVG -s -600s -e -0s 'DEF:val=gauge-foo.rrd:value:AVERAGE' 'LINE1:val#ff0000:Value'
'''
Example collectd plugin written in Python.
For details on plugin's architecture, see: man 5 collectd-python
'''
import collectd
PLUGIN_NAME = 'spam'
INTERVAL = 5 # seconds
_v = 0
_foo = None
collectd.info('spam: Loading Python plugin:' + PLUGIN_NAME)
def configure(configobj):
'''Configure this plugin based on collectd.conf parts.
Example configuration:
<LoadPlugin python>
Globals true
</LoadPlugin>
...
<Plugin python>
ModulePath "/usr/local/lib/collectd/python/"
LogTraces true
Interactive false
Import "spam"
<Module spam>
spam "wonderfull"
foo "Baz" "Bee"
</Module>
</Plugin>
'''
global _foo
collectd.info('spam: Configure with: key=%s, children=%r' % (
configobj.key, configobj.children))
config = {c.key: c.values for c in configobj.children}
collectd.info('spam: Configured with %r' % (config))
# Set a module-global based on external configuration
_foo = next(iter(config.get('foo', ['Foo'])))
def read(data=None):
'''Read another data value and dispatch it to collectd
Here, we emulate reading a new value using random().
'''
from random import random
global _v
global _foo
collectd.info('spam(foo=%s): Reading data (data=%r)' % (_foo, data))
vl = collectd.Values(type='gauge', type_instance='foo')
vl.plugin = PLUGIN_NAME
_v = (_v + int(random() * 3)) % 15
vl.values = [_v]
collectd.info('spam(foo=%s): Read values: %r' % (_foo, vl.values))
vl.dispatch()
def write(vl, data=None):
'''Prepare value before writing to RRD database'''
collectd.info('Writing data (vl=%r, data=%r)' % (vl, data))
for v in vl.values:
collectd.debug("spam: Writing %s (%s): %f" % (vl.plugin, vl.type, v))
#
# Register our callbacks to collectd
#
collectd.register_config(configure)
collectd.register_read(read, INTERVAL)
#collectd.register_write(write)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment