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';
37 This script reads it's configuration from F</etc/exec-munin.conf>. The
38 configuration is read using C<Config::General> which understands a Apache-like
39 config syntax, so it's very similar to the F<collectd.conf> syntax, too.
41 Here's a short sample config:
44 AddType voltage-out out
46 Script /usr/lib/munin/plugins/nut
48 The options have the following semantic (i.E<nbsp>e. meaning):
52 =item B<AddType> I<to> I<from> [I<from> ...]
54 collectd uses B<types> to specify how data is structured. In Munin all data is
55 structured the same way, so some way of telling collectd how to handle the data
56 is needed. This option translates the so called "field names" of Munin to the
57 "types" of collectd. If more than one field are of the same type, e.E<nbsp>g.
58 the C<nut> plugin above provides C<in> and C<out> which are both voltages, you
59 can use a hyphen to add a "type instance" to the type.
61 For a list of already defined "types" look at the F<types.db> file in
62 collectd's shared data directory, e.E<nbsp>g. F</usr/share/collectd/>.
64 =item B<Interval> I<Seconds>
66 Sets the interval in which the plugins are executed. This doesn't need to match
67 the interval setting of the collectd daemon. Usually, you want to execute the
68 Munin plugins much less often, e.E<nbsp>g. every 300 seconds versus every 10
71 =item B<Script> I<File>
73 Adds a script to the list of scripts to be executed once per I<Interval>
80 sub handle_config_addtype
84 for (my $i = 0; $i < @$list; $i++)
86 my ($to, @from) = split (' ', $list->[$i]);
87 for (my $j = 0; $j < @from; $j++)
89 $TypeMap->{$from[$j]} = $to;
92 } # handle_config_addtype
94 sub handle_config_script
98 for (my $i = 0; $i < @$scripts; $i++)
100 my $script = $scripts->[$i];
103 print STDERR "Script `$script' doesn't exist.\n";
107 print STDERR "Script `$script' exists but is not executable.\n";
111 push (@$Scripts, $script);
114 } # handle_config_script
120 if (defined ($config->{'addtype'}))
122 if (ref ($config->{'addtype'}) eq 'ARRAY')
124 handle_config_addtype ($config->{'addtype'});
126 elsif (ref ($config->{'addtype'}) eq '')
128 handle_config_addtype ([$config->{'addtype'}]);
132 print STDERR "Cannot handle ref type '"
133 . ref ($config->{'addtype'}) . "' for option 'AddType'.\n";
137 if (defined ($config->{'script'}))
139 if (ref ($config->{'script'}) eq 'ARRAY')
141 handle_config_script ($config->{'script'});
143 elsif (ref ($config->{'script'}) eq '')
145 handle_config_script ([$config->{'script'}]);
149 print STDERR "Cannot handle ref type '"
150 . ref ($config->{'script'}) . "' for option 'Script'.\n";
154 if (defined ($config->{'interval'})
155 && (ref ($config->{'interval'}) eq ''))
157 my $num = int ($config->{'interval'});
163 } # handle_config }}}
171 my $host = hostname () || 'localhost';
172 if (!open ($fh, '-|', $script))
174 print STDERR "Cannot execute $script: $!";
178 $pinst = basename ($script);
180 while (my $line = <$fh>)
183 if ($line =~ m#^([^\.\-/]+)\.value\s+($RE{num}{real})#)
187 my $type = (defined ($TypeMap->{$field})) ? $TypeMap->{$field} : $field;
189 print "$host/munin-$pinst/$type interval=$Interval $time:$value\n";
201 my %config = ParseConfig (-ConfigFile => $ConfigFile,
203 -LowerCaseNames => 1);
204 handle_config (\%config);
209 $next_run = $last_run + $Interval;
216 while ((my $timeleft = ($next_run - time ())) > 0)
225 This script requires the following Perl modules to be installed:
229 =item C<Config::General>
231 =item C<Regexp::Common>
237 L<http://munin.projects.linpro.no/>,
238 L<http://collectd.org/>,
243 Florian octo Forster E<lt>octo at verplant.orgE<gt>
247 # vim: set sw=2 sts=2 ts=8 fdm=marker :