"Fossies" - the Fresh Open Source Software Archive

Member "unix/plugins/pandora_update" (15 Sep 2021, 5265 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 # Update binary tool
    3 # Copyright (c) 2010 Artica Soluciones Tecnologicas S.L.
    4 # Copyright (c) Sancho Lerena <slerena@artica.es>
    5 
    6 use strict;
    7 use warnings;
    8 use POSIX qw(strftime);
    9 use Cwd 'abs_path';
   10 use File::Basename;
   11 use File::Copy;
   12 use FindBin;
   13 use Digest::MD5 qw(md5);
   14 
   15 # Time to wait before the service stops
   16 use constant SERVICE_STOP_WAIT => 5;
   17 
   18 # This tool is intented to be used to update pandora agent binaries using
   19 # the file collection feature. This will work using a module like this:
   20 
   21 # Unix
   22 
   23 # module_begin
   24 # module_name Pandora_Update
   25 # module_type async_string
   26 # module_exec nohup pandora_update fc_1 2> /dev/null && tail -1 nohup.out 2> /dev/null
   27 # module_description Module to check new version of pandora agent and update itself
   28 # module_end
   29 
   30 # Windows:
   31 
   32 # module_begin
   33 # module_name Pandora_Update
   34 # module_type async_string
   35 # module_exec pandora_update.exe fc_1
   36 # module_description Module to check new version of pandora agent and update itself
   37 # module_end
   38 
   39 
   40 # This small function return the current base path (where this tool is stored)
   41 
   42 sub return_basepath () {
   43     return $FindBin::Bin;
   44 }
   45 
   46 # Function to compare two binary files. Return 0 if different 1 if equal, 2 error
   47 
   48 sub compare_twofiles ($$) {
   49 
   50     my $file1 = $_[0];
   51     my $file2 = $_[1];
   52 
   53     my $size1 = -s $file1;
   54     my $size2 = -s $file2;
   55 
   56     # Size matters;
   57 
   58     if ((!defined($size1)) || (!defined($size2))){
   59         return 2;
   60     }
   61 
   62     if ($size1 != $size2){
   63         return 0;
   64     }
   65 
   66     open FILE1, $file1;
   67     binmode FILE1;
   68     my $data1 = join ('', <FILE1>);
   69     close FILE1;
   70     my $hash1 = md5($data1);
   71 
   72     open FILE2, $file2;
   73     binmode FILE2;
   74     my $data2 = join ('', <FILE2>);
   75     close FILE2;
   76     my $hash2 = md5($data2);
   77 
   78     if ($hash1 eq $hash2){
   79         return 1;
   80     }
   81 
   82     return 0;
   83 }
   84 
   85 # Get agent pid
   86 sub get_agent_pid ($$) {
   87     my $daemon = shift;
   88     my $app_path = shift;
   89 
   90     $ENV{'COLUMNS'}=400;
   91     my $os_name=`uname -s`;
   92     my $pid = "";
   93     my $cmd = "";
   94 
   95     if ($os_name eq "HP-UX") {
   96 
   97         $cmd ="ps -ex | grep \"$daemon $app_path\" | grep -v grep | head -1 | awk '{ print \$1 }'";
   98     } 
   99     elsif ($os_name =~ /SunOS/) {
  100         my $cmd_aux="echo \"$daemon $app_path\" | cut -c1-40";
  101         my $truncated_daemon = `$cmd_aux`;
  102 
  103         chop($truncated_daemon);
  104 
  105         my $zone = `/bin/zonename`;
  106 
  107         if ( $zone =~ /global/) {
  108             $cmd ="ps -f -z global | grep \"$truncated_daemon\" | grep -v grep | head -1 | awk '{ print \$2 }'";
  109         } else {
  110             $cmd ="ps -Af | grep \"$truncated_daemon\" | grep -v grep | head -1 | awk '{ print \$2 }'";
  111         }
  112     } else {
  113         $cmd ="ps -Af | grep \"$daemon $app_path\" | grep -v grep | head -1 | awk '{ print \$2 }'";
  114     }
  115 
  116     $pid = `$cmd`;
  117     return $pid;
  118 
  119 }
  120 
  121 # -------------------------------------------------------------------------
  122 
  123 # MAIN CODE starts here
  124 
  125 
  126 # Get the required path (relative to basepath) to find pandora agent binary
  127 
  128 if ($#ARGV < 0) {
  129     print "Usage: $0 <relative path (filecollection id) [custom_path]\n";
  130     print "For example: $0 fc2 /var/opt/PandoraFMS\n\n";
  131     exit 1;
  132 }
  133 
  134 my $fc_path = $ARGV[0];
  135 my $base_path = return_basepath();
  136 my $opt_dir = "";
  137 
  138 if (defined($ARGV[1])) {
  139     $opt_dir = $ARGV[1];
  140 }
  141 
  142 # Setup your particular paths / process settings here
  143 # [SETUP BEGIN] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144 
  145 # Location of binaries 
  146 
  147 # Unix 
  148 my ($running_binary, $updated_binary, $conf_path);
  149 if ($^O eq 'freebsd') {
  150     $running_binary = $opt_dir."/usr/local/bin/pandora_agent";
  151     $updated_binary = $opt_dir."/usr/local/etc/pandora/collections/$fc_path/pandora_agent";
  152     $conf_path = $opt_dir."/usr/local/etc/pandora";
  153 } else {
  154     $running_binary = $opt_dir."/usr/bin/pandora_agent";
  155     $updated_binary = $opt_dir."/etc/pandora/collections/$fc_path/pandora_agent";
  156     $conf_path = $opt_dir."/etc/pandora";
  157 }
  158 
  159 # Windows 
  160 
  161 #my $running_binary = $base_path."/../PandoraAgent.exe";
  162 #my $updated_binary = $base_path."/../collections/$fc_path/PandoraAgent.exe";
  163 
  164 # Location of service start / stop commands
  165 
  166 # Unix style
  167 
  168 my $start_pandora;
  169 my $stop_pandora;
  170 if ($^O eq 'freebsd') {
  171     $start_pandora = $opt_dir."/usr/local/etc/rc.d/pandora_agent start";
  172     $stop_pandora = $opt_dir."/usr/local/etc/rc.d/pandora_agent stop";
  173 } else {
  174     $start_pandora = $opt_dir."/etc/init.d/pandora_agent_daemon start";
  175     $stop_pandora = $opt_dir."/etc/init.d/pandora_agent_daemon stop";
  176 }
  177 
  178 # Windows stuff
  179 
  180 #my $start_pandora = "net start \"Pandora FMS Agent\"";
  181 #my $stop_pandora = "net stop \"Pandora FMS Agent\"";
  182 
  183 # Local log/temp file
  184 
  185 # Unix
  186 my $local_log = "/tmp/pandora_update.log";
  187 
  188 # Windows
  189 #my $local_log = $base_path. "/../pandora_update.log";
  190 
  191 # [SETUP END]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  192 
  193 if (compare_twofiles ($running_binary, $updated_binary) == 0 ){
  194     # Do the update
  195 
  196     # Create the temp file to "acknoledge" a update has been succeed
  197     open (FILE3,">$local_log");
  198     close (FILE3);
  199 
  200     my $ppid = get_agent_pid($running_binary, $conf_path);
  201 
  202     # Kill parent process (a.k.a the Agent)
  203     my $output = `kill -9 $ppid`;
  204     sleep SERVICE_STOP_WAIT;
  205 
  206     # Copy the updated agent
  207     copy($updated_binary, $running_binary) or unlink $local_log;
  208 
  209     # Starting the agent again
  210     my $log = $opt_dir."/var/log/pandora/pandora_agent.log";
  211     $output = `nohup $running_binary $conf_path >/dev/null 2>$log &`
  212     
  213 
  214 } else {
  215     if (-e $local_log){
  216         print "Updated binary from $updated_binary\n";
  217         unlink $local_log;
  218     }
  219 }
  220 
  221 exit;