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 if (ref ($opts) eq 'ARRAY')
131 $opt->{'script'} = $script;
132 push (@$Scripts, $opt);
137 $opts->{'script'} = $script;
138 push (@$Scripts, $opts);
141 } # for (keys %$scripts)
142 } # handle_config_script
148 if (defined ($config->{'addtype'}))
150 if (ref ($config->{'addtype'}) eq 'ARRAY')
152 handle_config_addtype ($config->{'addtype'});
154 elsif (ref ($config->{'addtype'}) eq '')
156 handle_config_addtype ([$config->{'addtype'}]);
160 print STDERR "Cannot handle ref type '"
161 . ref ($config->{'addtype'}) . "' for option 'AddType'.\n";
165 if (defined ($config->{'script'}))
167 if (ref ($config->{'script'}) eq 'HASH')
169 handle_config_script ($config->{'script'});
173 print STDERR "Cannot handle ref type '"
174 . ref ($config->{'script'}) . "' for option 'Script'.\n";
178 if (defined ($config->{'interval'})
179 && (ref ($config->{'interval'}) eq ''))
181 my $num = int ($config->{'interval'});
187 } # handle_config }}}
199 if (($unit =~ m/^mb(yte)?$/i) || ($unit eq 'M'))
201 return ($value * 1000000);
203 elsif ($unit =~ m/^k(b(yte)?)?$/i)
205 return ($value * 1000);
211 sub sanitize_instance
220 $inst =~ s/[^A-Za-z_-]/_/g;
228 sub handle_performance_data
241 if ($line =~ m/^([^=]+)=($RE{num}{real})([^;]*)/)
243 $tinst = sanitize_instance ($1);
244 $value = scale_value ($2, $3);
251 print "PUTVAL $host/$plugin-$pinst/$type-$tinst interval=$Interval ${time}:$value\n";
261 my $host = hostname () || 'localhost';
266 my @longserviceoutput;
268 my $script_name = $script->{'script'};
270 if ($script->{'arguments'})
272 @args = split (' ', $script->{'arguments'});
275 if (!open ($fh, '-|', $script_name, @args))
277 print STDERR "Cannot execute $script_name: $!";
281 $pinst = sanitize_instance (basename ($script_name));
283 # Parse the output of the plugin. The format is seriously fucked up, because
284 # it got extended way beyond what it could handle.
285 while (my $line = <$fh>)
292 ($serviceoutput, $perfdata) = split (m/\s*\|\s*/, $line, 2);
296 push (@serviceperfdata, split (' ', $perfdata));
305 ($longoutput, $perfdata) = split (m/\s*\|\s*/, $line, 2);
307 push (@longserviceoutput, $longoutput);
311 push (@serviceperfdata, split (' ', $perfdata));
317 push (@serviceperfdata, split (' ', $line));
322 # Save the exit status of the check in $state
339 my $type = $script->{'type'} || 'nagios_check';
341 print "PUTNOTIF time=$time severity=$state host=$host plugin=nagios "
342 . "plugin_instance=$pinst type=$type message=$serviceoutput\n";
345 if ($script->{'type'})
347 for (@serviceperfdata)
349 handle_performance_data ($host, 'nagios', $pinst, $script->{'type'},
360 my %config = ParseConfig (-ConfigFile => $ConfigFile,
362 -LowerCaseNames => 1);
363 handle_config (\%config);
368 $next_run = $last_run + $Interval;
375 while ((my $timeleft = ($next_run - time ())) > 0)
384 This script requires the following Perl modules to be installed:
388 =item C<Config::General>
390 =item C<Regexp::Common>
396 L<http://www.nagios.org/>,
397 L<http://nagiosplugins.org/>,
398 L<http://collectd.org/>,
403 Florian octo Forster E<lt>octo at verplant.orgE<gt>
407 # vim: set sw=2 sts=2 ts=8 fdm=marker :