From bc44e5b671235502c63fcf2bf918760e6139a701 Mon Sep 17 00:00:00 2001 From: enriquecd Date: Fri, 16 Dec 2016 10:30:16 +0100 Subject: [PATCH] Add create_web_module and create_policy_web_module functions #4261 --- pandora_server/util/pandora_manage.pl | 254 ++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index c965f18feb..19145f274b 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -127,6 +127,7 @@ sub help_screen{ help_screen_line('--locate_agent', '', 'Search a agent into of nodes of metaconsole. Only Enterprise.'); print "\nMODULES:\n\n" unless $param ne ''; help_screen_line('--create_data_module', " [ \n\t \n\t \n\t \n\t \n\t ]", 'Add data server module to agent'); + help_screen_line('--create_web_module', " [ \n\t \n\t \n\t \n\t \n\t ]", 'Add web server module to agent'); help_screen_line('--create_network_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t ]", 'Add not snmp network module to agent'); help_screen_line('--create_snmp_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Add snmp network module to agent'); @@ -155,6 +156,7 @@ sub help_screen{ help_screen_line('--delete_special_day', '', 'Delete special day'); help_screen_line('--update_special_day', " ", 'Update a field of a special day'); help_screen_line('--create_data_module_from_local_component', ' ', "Create a new data \n\t module from a local component"); + help_screen_line('--create_web_module_from_local_component', ' ', "Create a new web \n\t module from a local component"); help_screen_line('--create_local_component', " [ \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Create local component'); print "\nUSERS:\n\n" unless $param ne ''; @@ -186,11 +188,13 @@ sub help_screen{ help_screen_line('--disable_policy_alerts', '', 'Disable all the alerts of a policy'); help_screen_line('--create_policy', ' '); help_screen_line('--create_policy_data_module', " [ \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Add data server module to policy'); + help_screen_line('--create_policy_web_module', " [ \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Add web server module to policy'); help_screen_line('--create_policy_network_module', " [ \n\t \n\t \n\t \n\t \n\t \n\t ]", "Add not snmp network module to policy"); help_screen_line('--create_policy_snmp_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Add snmp network module to policy'); help_screen_line('--create_policy_plugin_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Add plug-in module to policy'); help_screen_line('--create_policy_data_module_from_local_component', ' '); + help_screen_line('--create_policy_web_module_from_local_component', ' '); help_screen_line('--add_collection_to_policy', " "); help_screen_line('--validate_policy_alerts', '', 'Validate the alerts of a given policy'); help_screen_line('--get_policy_modules', '', 'Get the modules of a policy'); @@ -1332,6 +1336,198 @@ sub cli_create_data_module($) { } } +############################################################################## +# Create web module. +# Related option: --create_web_module +############################################################################## + +sub cli_create_web_module($) { + my $in_policy = shift; + my ($policy_name, $module_name, $module_type, $agent_name, $description, $module_group, + $min,$max,$post_process, $interval, $warning_min, $warning_max, $critical_min, + $critical_max, $history_data, $definition_file, $configuration_data, $warning_str, $critical_str, $enable_unknown_events, + $ff_threshold, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $ff_timeout, + $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions); + + if ($in_policy == 0) { + ($module_name, $module_type, $agent_name, $description, $module_group, + $min,$max,$post_process, $interval, $warning_min, $warning_max, $critical_min, + $critical_max, $history_data, $definition_file, $warning_str, $critical_str, $enable_unknown_events, $ff_threshold, + $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $ff_timeout, + $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions) = @ARGV[2..30]; + } + else { + ($policy_name, $module_name, $module_type, $description, $module_group, + $min,$max,$post_process, $interval, $warning_min, $warning_max, $critical_min, + $critical_max, $history_data, $configuration_data, $warning_str, $critical_str, $enable_unknown_events, $ff_threshold, + $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $ff_timeout, + $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions) = @ARGV[2..31]; + } + + my $module_name_def; + my $module_type_def; + + my $agent_id; + my $policy_id; + + my $disabled_types_event = {}; + if ($enable_unknown_events) { + $disabled_types_event->{'going_unknown'} = 0; + } + else { + $disabled_types_event->{'going_unknown'} = 1; + } + my $disabled_types_event_json = encode_json($disabled_types_event); + + if ($in_policy == 0) { + $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); + non_exist_check($module_exists, 'module name', $module_name); + + #~ print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + } + else { + $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); + exist_check($policy_id,'policy',$policy_name); + + my $policy_module_exist = enterprise_hook('get_policy_module_id',[$dbh, $policy_id, $module_name]); + non_exist_check($policy_module_exist,'policy module',$module_name); + + #~ print_log "[INFO] Adding module '$module_name' to policy '$policy_name'\n\n"; + } + + $module_name_def = $module_name; + $module_type_def = $module_type; + # If the module is local and is not to policy, we add it to the conf file + if (defined($definition_file) && (-e $definition_file) && (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf')){ + open (FILE, $definition_file); + my @file = ; + my $definition = join("", @file); + close (FILE); + + # If the parameter name or type and the definition file name or type + # dont match will be set the file definitions + open (FILE, $definition_file); + while () { + chomp; + my ($key, $val) = split / /; + if ($key eq 'module_name') { + $module_name_def = $val; + } + if ($key eq 'module_type') { + $module_type_def = $val; + } + } + close (FILE); + + open (FILE, $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); + my @file = ; + my $conf_file = join("", @file); + close(FILE); + + open FILE, "> ".$conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'; + print FILE "$conf_file\n$definition"; + close(FILE); + + enterprise_hook('pandora_update_md5_file', [$conf, $agent_name]); + } + + if ($in_policy == 0) { + my $module_exists = get_agent_module_id($dbh, $module_name_def, $agent_id); + non_exist_check($module_exists, 'module name', $module_name_def); + print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + } + else { + my $policy_module_exist = enterprise_hook('get_policy_module_id',[$dbh, $policy_id, $module_name_def]); + non_exist_check($policy_module_exist,'policy module',$module_name_def); + print_log "[INFO] Adding module '$module_name' to policy '$policy_name'\n\n"; + } + + if (defined($definition_file) && $module_type ne $module_type_def) { + $module_type = $module_type_def; + print_log "[INFO] The module type has been forced to '$module_type' by the definition file\n\n"; + } + + if (defined($definition_file) && $module_name ne $module_name_def) { + $module_name = $module_name_def; + print_log "[INFO] The module name has been forced to '$module_name' by the definition file\n\n"; + } + + # The get_module_id has wrong name. Change in future + my $module_type_id = get_module_id($dbh,$module_type); + exist_check($module_type_id,'module type',$module_type); + + if ($module_type !~ m/.?web.?/) { + print_log "[ERROR] '$module_type' is not valid type for web modules. Try with web_data, web_proc, web_content_data or web_content_string types\n\n"; + exit; + } + + my $module_group_id = 0; + + if (defined($module_group)) { + $module_group_id = get_module_group_id($dbh,$module_group); + exist_check($module_group_id,'module group',$module_group); + } + + my %parameters; + + $parameters{'id_tipo_modulo'} = $module_type_id; + + if ($in_policy == 0) { + $parameters{'nombre'} = safe_input($module_name); + $parameters{'id_agente'} = $agent_id; + } + else { + $parameters{'name'} = safe_input($module_name); + $parameters{'id_policy'} = $policy_id; + } + + # Optional parameters + $parameters{'id_module_group'} = $module_group_id unless !defined ($module_group); + $parameters{'min_warning'} = $warning_min unless !defined ($warning_min); + $parameters{'max_warning'} = $warning_max unless !defined ($warning_max); + $parameters{'min_critical'} = $critical_min unless !defined ($critical_min); + $parameters{'max_critical'} = $critical_max unless !defined ($critical_max); + $parameters{'history_data'} = $history_data unless !defined ($history_data); + if ($in_policy == 0) { + $parameters{'descripcion'} = safe_input($description) unless !defined ($description); + $parameters{'id_modulo'} = 7; + } + else { + $parameters{'description'} = safe_input($description) unless !defined ($description); + $parameters{'id_module'} = 7; + $configuration_data !~ s/\\n/\n/g; + $parameters{'configuration_data'} = safe_input($configuration_data); + } + $parameters{'min'} = $min unless !defined ($min); + $parameters{'max'} = $max unless !defined ($max); + $parameters{'post_process'} = $post_process unless !defined ($post_process); + $parameters{'module_interval'} = $interval unless !defined ($interval); + $parameters{'str_warning'} = safe_input($warning_str) unless !defined ($warning_str); + $parameters{'str_critical'} = safe_input($critical_str) unless !defined ($critical_str); + $parameters{'disabled_types_event'} = $disabled_types_event_json; + $parameters{'min_ff_event'} = $ff_threshold unless !defined ($ff_threshold); + $parameters{'each_ff'} = $each_ff unless !defined ($each_ff); + $parameters{'min_ff_event_normal'} = $ff_threshold_normal unless !defined ($ff_threshold_normal); + $parameters{'min_ff_event_warning'} = $ff_threshold_warning unless !defined ($ff_threshold_warning); + $parameters{'min_ff_event_critical'} = $ff_threshold_critical unless !defined ($ff_threshold_critical); + $parameters{'ff_timeout'} = $ff_timeout unless !defined ($ff_timeout); + $parameters{'critical_inverse'} = $critical_inverse unless !defined ($critical_inverse); + $parameters{'warning_inverse'} = $warning_inverse unless !defined ($warning_inverse); + $parameters{'critical_instructions'} = $critical_instructions unless !defined ($critical_instructions); + $parameters{'warning_instructions'} = $warning_instructions unless !defined ($warning_instructions); + $parameters{'unknown_instructions'} = $unknown_instructions unless !defined ($unknown_instructions); + + if ($in_policy == 0) { + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } + else { + enterprise_hook('pandora_create_policy_module_from_hash', [$conf, \%parameters, $dbh]); + } +} + ############################################################################## # Create module group. # Related option: --create_module_group @@ -4572,6 +4768,10 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 30, 24); cli_create_data_module(0); } + elsif ($param eq '--create_web_module') { + param_check($ltotal, 30, 24); + cli_create_web_module(0); + } elsif ($param eq '--create_module_group') { param_check($ltotal, 1, 1); cli_create_module_group(); @@ -4716,6 +4916,10 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 2, 2); cli_create_policy_data_module_from_local_component(); } + elsif ($param eq '--create_policy_web_module_from_local_component') { + param_check($ltotal, 2, 2); + cli_create_policy_web_module_from_local_component(); + } elsif ($param eq '--create_policy') { param_check($ltotal, 3, 2); cli_create_policy(); @@ -4724,6 +4928,10 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 28, 20); cli_create_data_module(1); } + elsif ($param eq '--create_policy_web_module') { + param_check($ltotal, 28, 20); + cli_create_web_module(1); + } elsif ($param eq '--create_policy_network_module') { param_check($ltotal, 30, 20); cli_create_network_module(1); @@ -4870,6 +5078,10 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 2); cli_create_data_module_from_local_component(); } + elsif ($param eq '--create_web_module_from_local_component') { + param_check($ltotal, 2); + cli_create_web_module_from_local_component(); + } elsif ($param eq '--create_local_component') { param_check($ltotal, 35, 33); cli_create_local_component(); @@ -5084,6 +5296,29 @@ sub cli_create_data_module_from_local_component() { #~ pandora_create_module_from_local_component ($conf, $component, $agent_id, $dbh); enterprise_hook('pandora_create_module_from_local_component',[$conf, $component, $agent_id, $dbh]); } +############################################################################## +# Create web module from local component. +# Related option: --create_web_module_from_local_component +############################################################################## + +sub cli_create_web_module_from_local_component() { + my ($agent_name, $component_name) = @ARGV[2..3]; + + my $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $lc_id = pandora_get_local_component_id($dbh, $component_name); + exist_check($lc_id,'local component',$component_name); + + my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); + non_exist_check($module_exists, 'module name', $component_name); + + # Get local component data + my $component = get_db_single_row ($dbh, 'SELECT * FROM tlocal_component WHERE id = ?', $lc_id); + + #~ pandora_create_module_from_local_component ($conf, $component, $agent_id, $dbh); + enterprise_hook('pandora_create_module_from_local_component',[$conf, $component, $agent_id, $dbh]); +} ############################################################################## # Create policy data module from local component. @@ -5104,6 +5339,25 @@ sub cli_create_policy_data_module_from_local_component() { enterprise_hook('pandora_create_policy_data_module_from_local_component',[$conf, $component, $policy_id, $dbh]); } +############################################################################## +# Create policy web module from local component. +# Related option: --create_policy_data_module_from_local_component +############################################################################## +sub cli_create_policy_web_module_from_local_component() { + my ($policy_name, $component_name) = @ARGV[2..3]; + + my $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); + exist_check($policy_id,'policy',$policy_name); + + my $lc_id = pandora_get_local_component_id($dbh, $component_name); + exist_check($lc_id,'local component',$component_name); + + # Get local component data + my $component = get_db_single_row ($dbh, 'SELECT * FROM tlocal_component WHERE id = ?', $lc_id); + + enterprise_hook('pandora_create_policy_web_module_from_local_component',[$conf, $component, $policy_id, $dbh]); +} + ############################################################################## # Create local component. # Related option: --create_local_component