Writing a Munin Plugin
January 9th, 2010I have to admit something. I’ve become addicted.
One of the things I finally got around to doing while living the quiet life over the christmas holiday was to dive a bit further into Munin – a simple framework for collecting information from your computers and servers and making nice graphs that you can watch while you’re bored.
I’m not going to write a lot about how you can create your own Munin plugin to create your own graphs, as they have a very simple tutorial giving you all the basics about writing Munin plugins themselves. The only thing you need to remember are these two tidbits:
- When Munin first registers your plugin, it runs your script with config as the only argument. This provides Munin with the name of the graph, the labels and names (keys) of the graphs you’re providing values for, information about the axis, etc.
- When Munin runs your script without the config argument, it expects you to give it values for the keys you provided it in the configuration.
You enable and disable plugins by creating symlinks in /etc/munin/plugins (at least under debian / ubuntu), and plugins are usually stored in /usr/share/munin/plugins.
I keep my plugins archived together with the rest of the repository for my web projects, and then either symlink the content into the plugins-directory or create a simple wrapper script that changes the current directory to the location of the script and then invokes it (to make the current working directory be correct).
A very simple bash script that does this – and passes through any parameters given to the script:
-
#!/bin/bash
-
cd <absolute path> && php ./<script name> "$@"
An example of a simple PHP script to provide information to Munin:
-
<?php
-
if ((count($argv) > 1) && ($argv[1] == 'config'))
-
{
-
print("graph_title THE TITLE OF YOUR GRAPH
-
graph_category THE CATEGORY / GROUP OF YOUR GRAPH
-
graph_vlabel Count
-
total.label Total
-
other.label Other
-
");
-
exit();
-
}
-
-
print('total.value ' . get_total_value() . "\n");
-
print('other.value ' . get_other_value() . "\n");
Symlink everything, check that it runs properly when you execute the script from the plugins directory:
mats@xx:/usr/share/munin/plugins$ ./scriptname total.value 37 other.value 13 mats@xx:/usr/share/munin/plugins$
Symlink it into the /etc/munin/plugins directory and reload or restart Munin.
To check that Munin runs your script properly, telnet into the Munin server from an approved host and type “fetch
If stuff doesn’t work and you’re having a hard time finding out why, be sure to check out the munin-node logfile: /var/log/munin/munin-node.log.
As soon as you have the basics down, you’re free to start graphing whatever numeric value you can think of. The most interesting uses are probably something that integrates with your web applications, such as the number of searches, the number of signed up users, the language selection of users, the popularity of certain categories, etc. The possibilities are endless, use your imagination!
And about the addiction: NEED MORE GRAPHS.





