12 This script allows you to use plugins that were written for Nagios with
13 collectd's C<exec-plugin>. If the plugin checks some kind of threshold, please
14 consider configuring the threshold using collectd's own facilities instead of
15 using this transition layer.
19 use Sys::Hostname ('hostname');
20 use File::Basename ('basename');
21 use Config::General ('ParseConfig');
22 use Regexp::Common ('number');
24 our $ConfigFile = '/etc/exec-nagios.conf';
37 This script reads it's configuration from F</etc/exec-nagios.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 <Script /usr/lib/nagios/check_tcp>
45 Arguments -H alice -p 22
48 <Script /usr/lib/nagios/check_dns>
53 The options have the following semantic (i.E<nbsp>e. meaning):
57 =item B<Interval> I<Seconds>
59 Sets the interval in which the plugins are executed. This doesn't need to match
60 the interval setting of the collectd daemon. Usually, you want to execute the
61 Nagios plugins much less often, e.E<nbsp>g. every 300 seconds versus every 10
64 =item E<lt>B<Script> I<File>E<gt>
66 Adds a script to the list of scripts to be executed once per I<Interval>
67 seconds. You can use the following optional arguments to specify the operation
72 =item B<Arguments> I<Arguments>
74 Pass the arguments I<Arguments> to the script. This is often needed with Nagios
75 plugins, because much of the logic is implemented in the plugins, not in the
76 daemon. If you need to specify a warning and/or critical range here, please
77 consider using collectd's own threshold mechanism, which is by far the more
78 elegant solution than this transition layer.
82 If the plugin provides "performance data" the performance data is dispatched to
83 collectd with this type. If no type is configured the data is ignored. Please
84 note that this is limited to types that take exactly one value, such as the
85 type C<delay> in the example above. If you need more complex performance data,
86 rewrite the plugin as a collectd plugin (or at least port it do run directly
87 with the C<exec-plugin>).
93 sub handle_config_addtype
97 for (my $i = 0; $i < @$list; $i++)
99 my ($to, @from) = split (' ', $list->[$i]);
100 for (my $j = 0; $j < @from; $j++)
102 $TypeMap->{$from[$j]} = $to;
105 } # handle_config_addtype
107 sub handle_config_script
114 my $opts = $scripts->{$script};
118 print STDERR "Script `$script' doesn't exist.\n";
122 print STDERR "Script `$script' exists but is not executable.\n";
126 $opts->{'script'} = $script;
127 push (@$Scripts, $opts);
129 } # for (keys %$scripts)
130 } # handle_config_script
136 if (defined ($config->{'addtype'}))
138 if (ref ($config->{'addtype'}) eq 'ARRAY')
140 handle_config_addtype ($config->{'addtype'});
142 elsif (ref ($config->{'addtype'}) eq '')
144 handle_config_addtype ([$config->{'addtype'}]);
148 print STDERR "Cannot handle ref type '"
149 . ref ($config->{'addtype'}) . "' for option 'AddType'.\n";
153 if (defined ($config->{'script'}))
155 if (ref ($config->{'script'}) eq 'HASH')
157 handle_config_script ($config->{'script'});
161 print STDERR "Cannot handle ref type '"
162 . ref ($config->{'script'}) . "' for option 'Script'.\n";
166 if (defined ($config->{'interval'})
167 && (ref ($config->{'interval'}) eq ''))
169 my $num = int ($config->{'interval'});
175 } # handle_config }}}
187 if (($unit =~ m/^mb(yte)?$/i) || ($unit eq 'M'))
189 return ($value * 1000000);
191 elsif ($unit =~ m/^k(b(yte)?)?$/i)
193 return ($value * 1000);
199 sub sanitize_instance
208 $inst =~ s/[^A-Za-z_-]/_/g;
216 sub handle_performance_data
229 if ($line =~ m/^([^=]+)=($RE{num}{real})([^;]*)/)
231 $tinst = sanitize_instance ($1);
232 $value = scale_value ($2, $3);
239 print "PUTVAL $host/$plugin-$pinst/$type-$tinst interval=$Interval ${time}:$value\n";
249 my $host = hostname () || 'localhost';
254 my @longserviceoutput;
256 my $script_name = $script->{'script'};
258 if ($script->{'arguments'})
260 @args = split (' ', $script->{'arguments'});
263 if (!open ($fh, '-|', $script_name, @args))
265 print STDERR "Cannot execute $script_name: $!";
269 $pinst = sanitize_instance (basename ($script_name));
271 # Parse the output of the plugin. The format is seriously fucked up, because
272 # it got extended way beyond what it could handle.
273 while (my $line = <$fh>)
280 ($serviceoutput, $perfdata) = split (m/\s*\|\s*/, $line, 2);
284 push (@serviceperfdata, split (' ', $perfdata));
293 ($longoutput, $perfdata) = split (m/\s*\|\s*/, $line, 2);
295 push (@longserviceoutput, $longoutput);
299 push (@serviceperfdata, split (' ', $perfdata));
305 push (@serviceperfdata, split (' ', $line));
310 # Save the exit status of the check in $state
327 my $type = $script->{'type'} || 'nagios_check';
329 print "PUTNOTIF time=$time severity=$state host=$host plugin=nagios "
330 . "plugin_instance=$pinst type=$type message=$serviceoutput\n";
333 if ($script->{'type'})
335 for (@serviceperfdata)
337 handle_performance_data ($host, 'nagios', $pinst, $script->{'type'},
348 my %config = ParseConfig (-ConfigFile => $ConfigFile,
350 -LowerCaseNames => 1);
351 handle_config (\%config);
356 $next_run = $last_run + $Interval;
363 while ((my $timeleft = ($next_run - time ())) > 0)
372 This script requires the following Perl modules to be installed:
376 =item C<Config::General>
378 =item C<Regexp::Common>
384 L<http://www.nagios.org/>,
385 L<http://nagiosplugins.org/>,
386 L<http://collectd.org/>,
391 Florian octo Forster E<lt>octo at verplant.orgE<gt>
395 # vim: set sw=2 sts=2 ts=8 fdm=marker :