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';
27 our $Interval = defined ($ENV{'COLLECTD_INTERVAL'}) ? (0 + $ENV{'COLLECTD_INTERVAL'}) : 300;
28 our $Hostname = defined ($ENV{'COLLECTD_HOSTNAME'}) ? $ENV{'COLLECTD_HOSTNAME'} : '';
38 This script reads it's configuration from F</etc/exec-nagios.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 <Script /usr/lib/nagios/check_tcp>
46 Arguments -H alice -p 22
49 <Script /usr/lib/nagios/check_dns>
54 The options have the following semantic (i.E<nbsp>e. meaning):
58 =item B<Interval> I<Seconds>
60 Sets the interval in which the plugins are executed. This doesn't need to match
61 the interval setting of the collectd daemon. Usually, you want to execute the
62 Nagios plugins much less often, e.E<nbsp>g. every 300 seconds versus every 10
65 =item E<lt>B<Script> I<File>E<gt>
67 Adds a script to the list of scripts to be executed once per I<Interval>
68 seconds. You can use the following optional arguments to specify the operation
73 =item B<Arguments> I<Arguments>
75 Pass the arguments I<Arguments> to the script. This is often needed with Nagios
76 plugins, because much of the logic is implemented in the plugins, not in the
77 daemon. If you need to specify a warning and/or critical range here, please
78 consider using collectd's own threshold mechanism, which is by far the more
79 elegant solution than this transition layer.
83 If the plugin provides "performance data" the performance data is dispatched to
84 collectd with this type. If no type is configured the data is ignored. Please
85 note that this is limited to types that take exactly one value, such as the
86 type C<delay> in the example above. If you need more complex performance data,
87 rewrite the plugin as a collectd plugin (or at least port it do run directly
88 with the C<exec-plugin>).
96 sub handle_config_addtype
100 for (my $i = 0; $i < @$list; $i++)
102 my ($to, @from) = split (' ', $list->[$i]);
103 for (my $j = 0; $j < @from; $j++)
105 $TypeMap->{$from[$j]} = $to;
108 } # handle_config_addtype
110 sub handle_config_script
117 my $opts = $scripts->{$script};
121 print STDERR "Script `$script' doesn't exist.\n";
125 print STDERR "Script `$script' exists but is not executable.\n";
129 if (ref ($opts) eq 'ARRAY')
134 $opt->{'script'} = $script;
135 push (@$Scripts, $opt);
140 $opts->{'script'} = $script;
141 push (@$Scripts, $opts);
144 } # for (keys %$scripts)
145 } # handle_config_script
151 if (defined ($config->{'addtype'}))
153 if (ref ($config->{'addtype'}) eq 'ARRAY')
155 handle_config_addtype ($config->{'addtype'});
157 elsif (ref ($config->{'addtype'}) eq '')
159 handle_config_addtype ([$config->{'addtype'}]);
163 print STDERR "Cannot handle ref type '"
164 . ref ($config->{'addtype'}) . "' for option 'AddType'.\n";
168 if (defined ($config->{'script'}))
170 if (ref ($config->{'script'}) eq 'HASH')
172 handle_config_script ($config->{'script'});
176 print STDERR "Cannot handle ref type '"
177 . ref ($config->{'script'}) . "' for option 'Script'.\n";
181 if (defined ($config->{'interval'})
182 && (ref ($config->{'interval'}) eq ''))
184 my $num = int ($config->{'interval'});
190 } # handle_config }}}
202 if (($unit =~ m/^mb(yte)?$/i) || ($unit eq 'M'))
204 return ($value * 1000000);
206 elsif ($unit =~ m/^k(b(yte)?)?$/i)
208 return ($value * 1000);
214 sub sanitize_instance
223 $inst =~ s/[^A-Za-z_-]/_/g;
231 sub handle_performance_data
239 my $ident = "$host/$plugin-$pinst/$type-$tinst";
245 if ($line =~ m/^([^=]+)=($RE{num}{real})([^;]*)/)
247 $tinst = sanitize_instance ($1);
248 $value = scale_value ($2, $3);
257 print qq(PUTVAL "$ident" interval=$Interval ${time}:$value\n);
267 my $host = $Hostname || hostname () || 'localhost';
272 my @longserviceoutput;
274 my $script_name = $script->{'script'};
276 if ($script->{'arguments'})
278 @args = split (' ', $script->{'arguments'});
281 if (!open ($fh, '-|', $script_name, @args))
283 print STDERR "Cannot execute $script_name: $!";
287 $pinst = sanitize_instance (basename ($script_name));
289 # Parse the output of the plugin. The format is seriously fucked up, because
290 # it got extended way beyond what it could handle.
291 while (my $line = <$fh>)
298 ($serviceoutput, $perfdata) = split (m/\s*\|\s*/, $line, 2);
302 push (@serviceperfdata, split (' ', $perfdata));
311 ($longoutput, $perfdata) = split (m/\s*\|\s*/, $line, 2);
313 push (@longserviceoutput, $longoutput);
317 push (@serviceperfdata, split (' ', $perfdata));
323 push (@serviceperfdata, split (' ', $line));
328 # Save the exit status of the check in $state
345 my $type = $script->{'type'} || 'nagios_check';
347 print "PUTNOTIF time=$time severity=$state host=$host plugin=nagios "
348 . "plugin_instance=$pinst type=$type message=$serviceoutput\n";
351 if ($script->{'type'})
353 for (@serviceperfdata)
355 handle_performance_data ($host, 'nagios', $pinst, $script->{'type'},
366 my %config = ParseConfig (-ConfigFile => $ConfigFile,
368 -LowerCaseNames => 1);
369 handle_config (\%config);
374 $next_run = $last_run + $Interval;
381 while ((my $timeleft = ($next_run - time ())) > 0)
390 This script requires the following Perl modules to be installed:
394 =item C<Config::General>
396 =item C<Regexp::Common>
402 L<http://www.nagios.org/>,
403 L<http://nagiosplugins.org/>,
404 L<http://collectd.org/>,
409 Florian octo Forster E<lt>octo at verplant.orgE<gt>
413 # vim: set sw=2 sts=2 ts=8 fdm=marker :