"Fossies" - the Fresh Open Source Software Archive

Member "unix/pandora_agent_exec" (15 Sep 2021, 1744 Bytes) of package /linux/misc/pandorafms_agent_unix-7.0NG.757.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl
    2 ##########################################################################
    3 # pandora_agent_exec
    4 #
    5 # Executes the given command and prints its output to stdout. If the
    6 # execution times out or the command does not exist nothing is printed
    7 # to stdout. This is part of Pandora FMS Plugin server, do not delete!.
    8 #
    9 # Usage: pandora_agent_exec <timeout in seconds> <command>
   10 ##########################################################################
   11 # Copyright (c) 2008-2010 Ramon Novoa, rnovoa@gmail.com
   12 #           (c) 2008-2010 Artica Soluciones Tecnologicas S.L
   13 #
   14 # This program is free software; you can redistribute it and/or
   15 # modify it under the terms of the GNU General Public License
   16 # as published by the Free Software Foundation; version 2.
   17 #
   18 # This program is distributed in the hope that it will be useful,
   19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   21 # GNU General Public License for more details.
   22 # You should have received a copy of the GNU General Public License
   23 # along with this program; if not, write to the Free Software
   24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25 ##########################################################################
   26 
   27 use strict;
   28 use warnings;
   29 
   30 # Check command line parameters 
   31 if ($#ARGV < 1) {
   32     exit 1; 
   33 }
   34 
   35 my @opts = @ARGV;
   36 my $timeout = shift(@opts);
   37 my $command = join(' ', @opts);
   38 my $output = '';
   39 my $ReturnCode = 0;
   40 
   41 # Execute the command
   42 eval {
   43     local $SIG{ALRM} = sub { die "alarm\n" };
   44     alarm $timeout;
   45 
   46     $output = `$command`;
   47     $ReturnCode = ($? >> 8) & 0xff;
   48     alarm 0;
   49 };
   50 
   51 # Timeout
   52 if ($@ eq "alarm\n") {
   53     exit 3;
   54 }
   55 
   56 print $output;
   57 
   58 exit $ReturnCode;