From 56e4f5a6ceeb360bdb5a5f73ec7b2d9a058b6125 Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Tue, 22 Apr 2014 09:22:08 +0000 Subject: [PATCH] 2014-04-22 Sergio Martin * util/pandora_migrate_recon_scripts.pl lib/PandoraFMS/ReconServer.pm: Add compatibility for the new fields system (macros) of the recon scripts to the recon server. Add migrate script from old format to new one git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@9795 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 7 + pandora_server/lib/PandoraFMS/ReconServer.pm | 19 ++- .../util/pandora_migrate_plugins.pl | 0 .../util/pandora_migrate_recon_scripts.pl | 155 ++++++++++++++++++ 4 files changed, 176 insertions(+), 5 deletions(-) mode change 100755 => 100644 pandora_server/util/pandora_migrate_plugins.pl create mode 100644 pandora_server/util/pandora_migrate_recon_scripts.pl diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 787ca1d9a6..bf0c297a03 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,10 @@ +2014-04-22 Sergio Martin + + * util/pandora_migrate_recon_scripts.pl + lib/PandoraFMS/ReconServer.pm: Add compatibility for the new + fields system (macros) of the recon scripts to the recon server. + Add migrate script from old format to new one + 2014-04-04 Ramon Novoa * util/recon_scripts/snmp-recon.pl: Do not load MIB files (DISPLAY-HINTS may diff --git a/pandora_server/lib/PandoraFMS/ReconServer.pm b/pandora_server/lib/PandoraFMS/ReconServer.pm index ba173b8dc8..c079de96f9 100644 --- a/pandora_server/lib/PandoraFMS/ReconServer.pm +++ b/pandora_server/lib/PandoraFMS/ReconServer.pm @@ -26,6 +26,7 @@ use Thread::Semaphore; use IO::Socket::INET; use POSIX qw(strftime ceil); +use JSON qw(decode_json encode_json); # Default lib dir for RPM and DEB packages use lib '/usr/lib/perl5'; @@ -482,13 +483,21 @@ sub exec_recon_script ($$$) { logger($pa_config, 'Executing recon script ' . safe_output($script->{'name'}), 10); my $command = safe_output($script->{'script'}); - my $field1 = safe_output($task->{'field1'}); - my $field2 = safe_output($task->{'field2'}); - my $field3 = safe_output($task->{'field3'}); - my $field4 = safe_output($task->{'field4'}); + + my $macros = safe_output($task->{'macros'}); + my $decoded_macros = decode_json ($macros); + + my $macros_parameters = ''; + + # Add module macros as parameter + if(ref($decoded_macros) eq "HASH") { + while (my ($i, $m) = each (%{$decoded_macros})) { + $macros_parameters = $macros_parameters . ' "' . $m->{"value"} . '"'; + } + } if (-x $command) { - `$command $task->{'id_rt'} $task->{'id_group'} $task->{'create_incident'} $field1 $field2 $field3 $field4`; + `$command $task->{'id_rt'} $task->{'id_group'} $task->{'create_incident'} $macros_parameters`; } else { logger ($pa_config, "Cannot execute recon task command $command."); } diff --git a/pandora_server/util/pandora_migrate_plugins.pl b/pandora_server/util/pandora_migrate_plugins.pl old mode 100755 new mode 100644 diff --git a/pandora_server/util/pandora_migrate_recon_scripts.pl b/pandora_server/util/pandora_migrate_recon_scripts.pl new file mode 100644 index 0000000000..d126692c82 --- /dev/null +++ b/pandora_server/util/pandora_migrate_recon_scripts.pl @@ -0,0 +1,155 @@ +#!/usr/bin/perl + +############################################################################### +# Pandora FMS Plugins migrate tool +############################################################################### +# Copyright (c) 2010 Artica Soluciones Tecnologicas S.L +# +# This program is Free Software, licensed under the terms of GPL License v2 +############################################################################### + +# Includes list +use strict; +use DBI; # DB interface with MySQL +use POSIX qw(strftime); +use POSIX; +use HTML::Entities; # Encode or decode strings with HTML entities +use Data::Dumper; +use JSON qw(encode_json); + +# Default lib dir for RPM and DEB packages +use lib '/usr/lib/perl5'; + +use PandoraFMS::Tools; +use PandoraFMS::DB; +use PandoraFMS::Core; +use PandoraFMS::Config; + +# migrate plugin tool version +my $version = "1.0"; + +# Names of the Description fields on the migrated macros +my $ip_desc = 'Target IP'; +my $port_desc = 'Port'; +my $user_desc = 'Username'; +my $pass_desc = 'Password'; +my $parameters_desc = 'Plug-in Parameters'; + +# Pandora server configuration +my %conf; + +# Read databases credentials +pandora_load_credentials (\%conf); + +# FLUSH in each IO +$| = 0; + +# Connect to the DBs +my $dbh = db_connect ('mysql', $conf{'dbname'}, $conf{'dbhost'}, $conf{'dbport'}, $conf{'dbuser'}, $conf{'dbpass'}); + +# Main +pandora_migrate_recon_main($dbh); + +# Cleanup and exit +db_disconnect ($dbh); +exit; + +############################################################################### +############################################################################### +# GENERAL FUNCTIONS +############################################################################### +############################################################################### + +############################################################################## +# Init screen +############################################################################## +sub pandora_load_credentials ($) { + my $conf = shift; + + $conf->{"verbosity"}=0; # Verbose 1 by default + $conf->{"daemon"}=0; # Daemon 0 by default + $conf->{'PID'}=""; # PID file not exist by default + $conf->{"quiet"}=0; # Daemon 0 by default + + + print "\nPandora FMS Plugins migrate tool $version Copyright (c) 2010 Artica ST\n"; + print "This program is Free Software, licensed under the terms of GPL License v2\n"; + + # Load config file from command line + help_screen () if ($#ARGV < 3); + + $conf{'dbname'} = $ARGV[0]; + $conf{'dbhost'} = $ARGV[1]; + $conf{'dbuser'} = $ARGV[2]; + $conf{'dbpass'} = $ARGV[3]; + $conf{'dbport'} = ($#ARGV >= 4 ? $ARGV[4] : 0); +} + +############################################################################## +# Print a help screen and exit. +############################################################################## +sub help_screen{ + print "\n[ERROR] No valid arguments\n\n"; + + print "Usage: \n\n$0 []\n\n"; + + exit; +} + +############################################################################### +############################################################################### +# MAIN +############################################################################### +############################################################################### + +sub pandora_migrate_recon_main ($) { + my ($dbh) = @_; + + $|++; + + my $migrated_reconscripts = 0; + my $migrated_recontasks = 0; + + print "\n[*] Migrating recon scripts and associated recon tasks.\n\n"; + + my @recon_scripts = get_db_rows ($dbh, "SELECT * FROM trecon_script WHERE macros = '' OR macros IS NULL"); + + my $macros_base; + + for (my $i=1; $i <= 4; $i++) { + $macros_base->{$i}{'macro'} = '_field' . $i . '_'; + $macros_base->{$i}{'desc'} = 'Script field #' . $i; + $macros_base->{$i}{'help'} = ''; + $macros_base->{$i}{'hide'} = ''; + $macros_base->{$i}{'value'} = ''; + } + + my $macros_base_json = encode_json($macros_base); + + foreach my $recon_script (@recon_scripts) { + # Insert macros and parameters in the plugin + db_update ($dbh, "UPDATE trecon_script SET `macros` = '$macros_base_json' WHERE `id_recon_script` = '".$recon_script->{'id_recon_script'}."'"); + $migrated_reconscripts ++; + + # Get the recon tasks created with each recon script + my @recon_tasks = get_db_rows ($dbh, "SELECT * FROM trecon_task WHERE id_recon_script = '".$recon_script->{'id_recon_script'}."' AND (macros = '' OR macros IS NULL)"); + my $macros_rt = $macros_base; + + foreach my $recon_task (@recon_tasks) { + for (my $i=1; $i <= 4; $i++) { + $macros_rt->{$i}{'value'} = $recon_task->{'field' . $i}; + } + + my $macros_rt_json = encode_json($macros_rt); + + db_update ($dbh, "UPDATE trecon_task SET `macros` = '$macros_rt_json' WHERE `id_rt` = '".$recon_task->{'id_rt'}."'"); + $migrated_recontasks ++; + + } + } + + print "\n[*] $migrated_reconscripts recon scripts migrated.\n"; + print "\n[*] $migrated_recontasks recon tasks migrated.\n"; + + exit; +}