From 42f40c7c2b4ce087878487f5c9f16c905f22b59a Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 30 Apr 2012 10:39:42 +0200 Subject: [PATCH] Added plugin_wrapper.pl. This is a small wrapper to execute plugins, catching any errors, signals, etc. returned by the script. This is primarily meant for Perl, Python, etc. scripts to catch execution errors. --- plugin_wrapper.pl | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 plugin_wrapper.pl diff --git a/plugin_wrapper.pl b/plugin_wrapper.pl new file mode 100755 index 0000000..3db473f --- /dev/null +++ b/plugin_wrapper.pl @@ -0,0 +1,74 @@ +#! /usr/bin/perl + +##################################################################### +# (c) 2012 Sebastian "tokkee" Harl # +# and team(ix) GmbH, Nuernberg, Germany # +# # +# This file is part of "team(ix) Monitoring Plugins" # +# URL: http://oss.teamix.org/projects/monitoringplugins/ # +# # +# This file is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published # +# by the Free Software Foundation, either version 2 of the License, # +# or (at your option) any later version. # +# # +# This file is distributed in the hope that it will be useful, but # +# WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this file. If not, see . # +##################################################################### + +use strict; +use warnings; + +my $p_script = shift; +my @p_args = @ARGV; + +if (! $p_script) { + exit_usage(1); +} + +my $p_stdout; +my $p_pid = open($p_stdout, '-|', "$p_script @p_args 2>&1"); +if (! defined($p_pid)) { + print "CRITICAL: Failed to execute plugin ($p_script): $!\n"; + print "Commandline: $p_script @p_args\n"; + exit 2; +} + +my @p_output = <$p_stdout>; +if (waitpid($p_pid, 0) != $p_pid) { + print "UNKNOWN: Lost track of plugin process\n"; + exit 3; +} +my $p_rc = $?; +my $p_sig = $p_rc & 127; +my $p_cd = $p_rc & 128; # core dumped? +$p_rc >>= 8; +close $p_stdout; + +if ($p_sig || $p_cd) { + print "CRITICAL: Plugin died with signal $p_sig (exit code: $p_rc)" + . ($p_cd ? " (core dumped)" : "") . "\n"; + $p_rc = 2; +} + +if ($p_rc == 255) { + print "CRITICAL: Plugin died with status 255 " + . "(see details for more info)\n"; + print "Commandline: $p_script @p_args\n"; +} + +my $p_output = join('', @p_output); +print $p_output; +exit $p_rc; + +sub exit_usage { + my $status = shift || 0; + print STDERR "Usage: $0 []\n"; + exit $status; +} +