From ea8d720267489661ea8bcc5158082aa2708a42a6 Mon Sep 17 00:00:00 2001 From: ramonn Date: Thu, 31 Jul 2008 15:33:44 +0000 Subject: [PATCH] 2008-07-31 Ramon Novoa * util/pandora_exec: Added to repository. Perl script to control command execution timeouts. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@992 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 5 +++ pandora_server/util/pandora_exec | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 pandora_server/util/pandora_exec diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 4bfb1f266d..02dd26be0b 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,8 @@ +2008-07-31 Ramon Novoa + + * util/pandora_exec: Added to repository. Perl script to control + command execution timeouts. + 2008-07-30 Ramon Novoa * lib/PandoraFMS/DB.pm, diff --git a/pandora_server/util/pandora_exec b/pandora_server/util/pandora_exec new file mode 100644 index 0000000000..9b78dea4c3 --- /dev/null +++ b/pandora_server/util/pandora_exec @@ -0,0 +1,62 @@ +#!/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. +# +# Usage: pandora_exec [arguments] +########################################################################## +# 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); +my $command = quotemeta(shift(@opts)); +my $arguments = join(' ', @opts); +my $output = ''; + +# Check that the command exists +if (system("$command >/dev/null 2>&1") == 32512) { + exit 2; +} + +# Execute the command +eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm $timeout; + + $output = `$command $arguments`; + alarm 0; +}; + +# Timeout +if ($@ eq "alarm\n") { + exit 3; +} + +print $output; + +exit 0;