2010-06-18 14:36:19 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
##########################################################################
|
|
|
|
# pandora_exec
|
|
|
|
#
|
|
|
|
# Executes the given command and prints its output to stdout. If the
|
|
|
|
# execution times out or the command does not exist nothing is printed
|
|
|
|
# to stdout. This is part of Pandora FMS Plugin server, do not delete!.
|
|
|
|
#
|
2010-07-08 17:04:43 +02:00
|
|
|
# Usage: pandora_exec <timeout in seconds> <command>
|
2010-06-18 14:36:19 +02:00
|
|
|
##########################################################################
|
|
|
|
# Copyright (c) 2008 Ramon Novoa, rnovoa@gmail.com
|
|
|
|
# (c) 2008 Artica Soluciones Tecnologicas S.L
|
|
|
|
#
|
|
|
|
# This program 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; version 2.
|
|
|
|
#
|
|
|
|
# This program 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 program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
# Check command line parameters
|
|
|
|
if ($#ARGV < 1) {
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @opts = @ARGV;
|
|
|
|
my $timeout = shift(@opts);
|
2010-07-08 17:04:43 +02:00
|
|
|
my $command = join(' ', @opts);
|
2010-06-18 14:36:19 +02:00
|
|
|
my $output = '';
|
|
|
|
my $ReturnCode = 0;
|
|
|
|
|
|
|
|
# Execute the command
|
|
|
|
eval {
|
|
|
|
local $SIG{ALRM} = sub { die "alarm\n" };
|
|
|
|
alarm $timeout;
|
|
|
|
|
2010-07-08 17:04:43 +02:00
|
|
|
$output = `$command`;
|
2010-06-18 14:36:19 +02:00
|
|
|
$ReturnCode = ($? >> 8) & 0xff;
|
|
|
|
alarm 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Timeout
|
|
|
|
if ($@ eq "alarm\n") {
|
|
|
|
exit 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
print $output;
|
|
|
|
|
|
|
|
exit $ReturnCode;
|