mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-27 15:54:29 +02:00
Update pandora_exec and pandora_agent_exec.
The previous versions of pandora_exec and pandora_agent_exec did not kill the running process after a timeout.
This commit is contained in:
parent
e1d550142c
commit
396c1e57f0
@ -1,15 +1,8 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
##########################################################################
|
################################################################################
|
||||||
# pandora_agent_exec
|
# pandora_exec - Execute a command with a time limit.
|
||||||
#
|
#
|
||||||
# Executes the given command and prints its output to stdout. If the
|
# Copyright (c) 2008-2023 Artica PFMS S.L.
|
||||||
# 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!.
|
|
||||||
#
|
|
||||||
# Usage: pandora_agent_exec <timeout in seconds> <command>
|
|
||||||
##########################################################################
|
|
||||||
# Copyright (c) 2008-2010 Ramon Novoa, rnovoa@gmail.com
|
|
||||||
# (c) 2008-2010 Artica Soluciones Tecnologicas S.L
|
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or
|
# This program is free software; you can redistribute it and/or
|
||||||
# modify it under the terms of the GNU General Public License
|
# modify it under the terms of the GNU General Public License
|
||||||
@ -22,37 +15,41 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program; if not, write to the Free Software
|
# along with this program; if not, write to the Free Software
|
||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
##########################################################################
|
################################################################################
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use POSIX qw(WEXITSTATUS WIFEXITED);
|
||||||
|
|
||||||
# Check command line parameters
|
# Check command line arguments.
|
||||||
if ($#ARGV < 1) {
|
if ($#ARGV < 1) {
|
||||||
|
print("Usage: $0 <timeout in seconds> <command>\n");
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
my @opts = @ARGV;
|
my @opts = @ARGV;
|
||||||
my $timeout = shift(@opts);
|
my $timeout = shift(@opts);
|
||||||
my $command = join(' ', @opts);
|
my $command = ($0 =~ m/_agent_exec$/) ? # For backward compatibility with pandora_agent.
|
||||||
my $output = '';
|
join(' ', @opts) :
|
||||||
my $ReturnCode = 0;
|
join(' ', map { quotemeta($_) } @opts);
|
||||||
|
|
||||||
# Execute the command
|
# Fork:
|
||||||
eval {
|
# * The child will run the command.
|
||||||
local $SIG{ALRM} = sub { die "alarm\n" };
|
# * The parent will timeout if needed
|
||||||
alarm $timeout;
|
# and exit with the appropriate exit status.
|
||||||
|
my $pid = fork();
|
||||||
$output = `$command`;
|
if ($pid == 0) {
|
||||||
$ReturnCode = ($? >> 8) & 0xff;
|
setpgrp();
|
||||||
alarm 0;
|
exec($command);
|
||||||
};
|
} else {
|
||||||
|
eval {
|
||||||
# Timeout
|
local $SIG{ALRM} = sub { kill(9, -$pid); exit 1; };
|
||||||
if ($@ eq "alarm\n") {
|
alarm $timeout;
|
||||||
exit 3;
|
waitpid($pid, 0);
|
||||||
|
alarm 0;
|
||||||
|
if (WIFEXITED(${^CHILD_ERROR_NATIVE})) {
|
||||||
|
exit WEXITSTATUS(${^CHILD_ERROR_NATIVE});
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
print $output;
|
exit 1;
|
||||||
|
|
||||||
exit $ReturnCode;
|
|
||||||
|
@ -1,15 +1,8 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
##########################################################################
|
################################################################################
|
||||||
# pandora_agent_exec
|
# pandora_exec - Execute a command with a time limit.
|
||||||
#
|
#
|
||||||
# Executes the given command and prints its output to stdout. If the
|
# Copyright (c) 2008-2023 Artica PFMS S.L.
|
||||||
# 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!.
|
|
||||||
#
|
|
||||||
# Usage: pandora_agent_exec <timeout in seconds> <command>
|
|
||||||
##########################################################################
|
|
||||||
# Copyright (c) 2008-2010 Ramon Novoa, rnovoa@gmail.com
|
|
||||||
# (c) 2008-2010 Artica Soluciones Tecnologicas S.L
|
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or
|
# This program is free software; you can redistribute it and/or
|
||||||
# modify it under the terms of the GNU General Public License
|
# modify it under the terms of the GNU General Public License
|
||||||
@ -22,37 +15,41 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program; if not, write to the Free Software
|
# along with this program; if not, write to the Free Software
|
||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
##########################################################################
|
################################################################################
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use POSIX qw(WEXITSTATUS WIFEXITED);
|
||||||
|
|
||||||
# Check command line parameters
|
# Check command line arguments.
|
||||||
if ($#ARGV < 1) {
|
if ($#ARGV < 1) {
|
||||||
|
print("Usage: $0 <timeout in seconds> <command>\n");
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
my @opts = @ARGV;
|
my @opts = @ARGV;
|
||||||
my $timeout = shift(@opts);
|
my $timeout = shift(@opts);
|
||||||
my $command = join(' ', @opts);
|
my $command = ($0 =~ m/_agent_exec$/) ? # For backward compatibility with pandora_agent.
|
||||||
my $output = '';
|
join(' ', @opts) :
|
||||||
my $ReturnCode = 0;
|
join(' ', map { quotemeta($_) } @opts);
|
||||||
|
|
||||||
# Execute the command
|
# Fork:
|
||||||
eval {
|
# * The child will run the command.
|
||||||
local $SIG{ALRM} = sub { die "alarm\n" };
|
# * The parent will timeout if needed
|
||||||
alarm $timeout;
|
# and exit with the appropriate exit status.
|
||||||
|
my $pid = fork();
|
||||||
$output = `$command`;
|
if ($pid == 0) {
|
||||||
$ReturnCode = ($? >> 8) & 0xff;
|
setpgrp();
|
||||||
alarm 0;
|
exec($command);
|
||||||
};
|
} else {
|
||||||
|
eval {
|
||||||
# Timeout
|
local $SIG{ALRM} = sub { kill(9, -$pid); exit 1; };
|
||||||
if ($@ eq "alarm\n") {
|
alarm $timeout;
|
||||||
exit 3;
|
waitpid($pid, 0);
|
||||||
|
alarm 0;
|
||||||
|
if (WIFEXITED(${^CHILD_ERROR_NATIVE})) {
|
||||||
|
exit WEXITSTATUS(${^CHILD_ERROR_NATIVE});
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
print $output;
|
exit 1;
|
||||||
|
|
||||||
exit $ReturnCode;
|
|
||||||
|
@ -1,15 +1,8 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
##########################################################################
|
################################################################################
|
||||||
# pandora_exec
|
# pandora_exec - Execute a command with a time limit.
|
||||||
#
|
#
|
||||||
# Executes the given command and prints its output to stdout. If the
|
# Copyright (c) 2008-2023 Artica PFMS S.L.
|
||||||
# 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!.
|
|
||||||
#
|
|
||||||
# Usage: pandora_exec <timeout in seconds> <command>
|
|
||||||
##########################################################################
|
|
||||||
# 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
|
# This program is free software; you can redistribute it and/or
|
||||||
# modify it under the terms of the GNU General Public License
|
# modify it under the terms of the GNU General Public License
|
||||||
@ -22,41 +15,41 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program; if not, write to the Free Software
|
# along with this program; if not, write to the Free Software
|
||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
##########################################################################
|
################################################################################
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use POSIX qw(WEXITSTATUS WIFEXITED);
|
||||||
|
|
||||||
# Check command line parameters
|
# Check command line arguments.
|
||||||
if ($#ARGV < 1) {
|
if ($#ARGV < 1) {
|
||||||
|
print("Usage: $0 <timeout in seconds> <command>\n");
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
my @opts = @ARGV;
|
my @opts = @ARGV;
|
||||||
my $timeout = shift(@opts);
|
my $timeout = shift(@opts);
|
||||||
my $command;
|
my $command = ($0 =~ m/_agent_exec$/) ? # For backward compatibility with pandora_agent.
|
||||||
foreach my $arg (@opts) {
|
join(' ', @opts) :
|
||||||
$command .= $^O ne 'MSWin32' ? quotemeta ($arg) . ' ' : '"' . $arg . '" ';
|
join(' ', map { quotemeta($_) } @opts);
|
||||||
}
|
|
||||||
chomp ($command);
|
|
||||||
my $output = '';
|
|
||||||
my $ReturnCode = 0;
|
|
||||||
|
|
||||||
# Execute the command
|
# Fork:
|
||||||
eval {
|
# * The child will run the command.
|
||||||
local $SIG{ALRM} = sub { die "alarm\n" };
|
# * The parent will timeout if needed
|
||||||
alarm $timeout;
|
# and exit with the appropriate exit status.
|
||||||
|
my $pid = fork();
|
||||||
$output = `$command`;
|
if ($pid == 0) {
|
||||||
$ReturnCode = ($? >> 8) & 0xff;
|
setpgrp();
|
||||||
alarm 0;
|
exec($command);
|
||||||
};
|
} else {
|
||||||
|
eval {
|
||||||
# Timeout
|
local $SIG{ALRM} = sub { kill(9, -$pid); exit 1; };
|
||||||
if ($@ eq "alarm\n") {
|
alarm $timeout;
|
||||||
exit 3;
|
waitpid($pid, 0);
|
||||||
|
alarm 0;
|
||||||
|
if (WIFEXITED(${^CHILD_ERROR_NATIVE})) {
|
||||||
|
exit WEXITSTATUS(${^CHILD_ERROR_NATIVE});
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
print $output;
|
exit 1;
|
||||||
|
|
||||||
exit $ReturnCode;
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user