12 This script allows you to use plugins that were written for Munin with
13 collectd's C<exec-plugin>. Since the data models of Munin and collectd are
14 quite different rewriting the plugins should be preferred over using this
15 transition layer. Having more than one "data source" for one "data set" doesn't
16 work with this script, for example.
20 use Sys::Hostname ('hostname');
21 use File::Basename ('basename');
22 use Config::General ('ParseConfig');
23 use Regexp::Common ('number');
25 our $ConfigFile = '/etc/exec-munin.conf';
38 This script reads it's configuration from F</etc/exec-munin.conf>. The
39 configuration is read using C<Config::General> which understands a Apache-like
40 config syntax, so it's very similar to the F<collectd.conf> syntax, too.
42 Here's a short sample config:
45 AddType voltage-out out
47 Script /usr/lib/munin/plugins/nut
49 The options have the following semantic (i.E<nbsp>e. meaning):
53 =item B<AddType> I<to> I<from> [I<from> ...]
55 collectd uses B<types> to specify how data is structured. In Munin all data is
56 structured the same way, so some way of telling collectd how to handle the data
57 is needed. This option translates the so called "field names" of Munin to the
58 "types" of collectd. If more than one field are of the same type, e.E<nbsp>g.
59 the C<nut> plugin above provides C<in> and C<out> which are both voltages, you
60 can use a hyphen to add a "type instance" to the type.
62 For a list of already defined "types" look at the F<types.db> file in
63 collectd's library path, e.E<nbsp>g. F</usr/lib/collectd/>.
65 =item B<Interval> I<Seconds>
67 Sets the interval in which the plugins are executed. This doesn't need to match
68 the interval setting of the collectd daemon. Usually, you want to execute the
69 Munin plugins much less often, e.E<nbsp>g. every 300 seconds versus every 10
72 =item B<Script> I<File>
74 Adds a script to the list of scripts to be executed once per I<Interval>
81 sub handle_config_addtype
85 for (my $i = 0; $i < @$list; $i++)
87 my ($to, @from) = split (' ', $list->[$i]);
88 for (my $j = 0; $j < @from; $j++)
90 $TypeMap->{$from[$j]} = $to;
93 } # handle_config_addtype
95 sub handle_config_script
99 for (my $i = 0; $i < @$scripts; $i++)
101 my $script = $scripts->[$i];
104 print STDERR "Script `$script' doesn't exist.\n";
108 print STDERR "Script `$script' exists but is not executable.\n";
112 push (@$Scripts, $script);
115 } # handle_config_script
121 if (defined ($config->{'addtype'}))
123 if (ref ($config->{'addtype'}) eq 'ARRAY')
125 handle_config_addtype ($config->{'addtype'});
127 elsif (ref ($config->{'addtype'}) eq '')
129 handle_config_addtype ([$config->{'addtype'}]);
133 print STDERR "Cannot handle ref type '"
134 . ref ($config->{'addtype'}) . "' for option 'AddType'.\n";
138 if (defined ($config->{'script'}))
140 if (ref ($config->{'script'}) eq 'ARRAY')
142 handle_config_script ($config->{'script'});
144 elsif (ref ($config->{'script'}) eq '')
146 handle_config_addtype ([$config->{'script'}]);
150 print STDERR "Cannot handle ref type '"
151 . ref ($config->{'script'}) . "' for option 'Script'.\n";
155 if (defined ($config->{'interval'})
156 && (ref ($config->{'interval'}) eq ''))
158 my $num = int ($config->{'interval'});
164 } # handle_config }}}
172 my $host = hostname () || 'localhost';
173 if (!open ($fh, '-|', $script))
175 print STDERR "Cannot execute $script: $!";
179 $pinst = basename ($script);
181 while (my $line = <$fh>)
184 if ($line =~ m#^([^\.\-/]+)\.value\s+($RE{num}{real})#)
188 my $type = (defined ($TypeMap->{$field})) ? $TypeMap->{$field} : $field;
190 print "$host/munin-$pinst/$type interval=$Interval $time:$value\n";
202 my %config = ParseConfig (-ConfigFile => $ConfigFile,
204 -LowerCaseNames => 1);
205 handle_config (\%config);
210 $next_run = $last_run + $Interval;
217 while ((my $timeleft = ($next_run - time ())) > 0)
226 This script requires the following Perl modules to be installed:
230 =item C<Config::General>
232 =item C<Regexp::Common>
238 L<http://munin.projects.linpro.no/>,
239 L<http://collectd.org/>,
244 Florian octo Forster E<lt>octo at verplant.orgE<gt>
248 # vim: set sw=2 sts=2 ts=8 fdm=marker :