From f0ff20fda1e33a5f231763bc3fc4ed7fe031e7a3 Mon Sep 17 00:00:00 2001 From: samucarc Date: Wed, 28 Nov 2018 21:38:02 +0100 Subject: [PATCH 1/6] Add commands alert --- pandora_console/include/functions_api.php | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 3cc0e58a67..daada976bd 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -7625,6 +7625,65 @@ function api_set_alert_actions($id, $id2, $other, $trash1) { } } + +function api_set_alert_commands($id, $thrash2, $other, $trash1) { + global $config; + + $command = $other['data'][0]; + $id_group = $other['data'][1]; + $description = $other['data'][2]; + $internal = $other['data'][3]; + $fields_descriptions_decode = io_safe_output(base64_decode($other['data'][4], true)); + $fields_values_decode = io_safe_output(base64_decode($other['data'][5], true)); + + + if (defined ('METACONSOLE')) { + return; + } + + if (!check_acl($config['id_user'], 0, "LW")){ + returnError('forbidden', 'string'); + return; + } + + $name = db_get_value ('id', 'talert_commands', 'name', $id); + $group = db_get_value ('id_grupo', 'tgrupo', 'id_grupo', $id_group); + + if ($name) { + returnError('error_parameter', 'Name already exist'); + return; + } + + if (!$group && $id_group != 0) { + returnError('error_parameter', 'Group does not exist'); + return; + } + + if ($other['type'] == 'string') { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + else if ($other['type'] == 'array') { + + + $values = array('name' => $id,'command' => $command, 'id_group' => $id_group, + 'description' => $description, 'internal' => $internal, 'fields_descriptions' => $fields_descriptions_decode, + 'fields_values' => $fields_values_decode); + + $return = db_process_sql_insert('talert_commands', $values); + + $data['type'] = 'string'; + if ($return === false) { + $data['data'] = 0; + } + else { + $data['data'] = $return; + } + returnData('string', $data); + return; + } +} + function api_set_new_event($trash1, $trash2, $other, $trash3) { $simulate = false; $time = get_system_time(); From dc7176585e33fb8af8377b7770df8a14a395d092 Mon Sep 17 00:00:00 2001 From: samucarc Date: Thu, 29 Nov 2018 16:57:17 +0100 Subject: [PATCH 2/6] new API get_all_alerts_command --- pandora_console/include/functions_api.php | 100 +++++++++++++++++++--- 1 file changed, 90 insertions(+), 10 deletions(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index daada976bd..91024dea22 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -35,6 +35,7 @@ enterprise_include_once ('include/functions_events.php'); enterprise_include_once ('include/functions_agents.php'); enterprise_include_once ('include/functions_modules.php'); enterprise_include_once ('include/functions_clusters.php'); +enterprise_include_once ('include/functions_alerts.php'); /** * Parse the "other" parameter. @@ -4508,6 +4509,45 @@ function api_get_all_alert_templates($thrash1, $thrash2, $other, $thrash3) { } } +function api_get_all_alert_commands($thrash1, $thrash2, $other, $thrash3) { + global $config; + + if (defined ('METACONSOLE')) { + return; + } + + if (!isset($other['data'][0])) + $separator = ';'; // by default + else + $separator = $other['data'][0]; + + if (!check_acl($config["id_user"], 0, "LM")) { + returnError("forbidden", "csv"); + return; + } + + $commands = db_get_all_rows_filter( + 'talert_commands', + array('id_group' => array_keys(users_get_groups(false, "LM"))) + ); + +if ($commands === false) $commands = array (); + + if ($commands !== false) { + $data['type'] = 'array'; + $data['data'] = $commands; + } + + if (!$commands) { + returnError('error_get_all_alert_commands', + __('Error getting all alert commands.')); + } + else { + returnData('csv', $data, $separator); + } +} + + /** * Get an alert tamplate, and print the result like a csv. * @@ -7625,7 +7665,21 @@ function api_set_alert_actions($id, $id2, $other, $trash1) { } } +/** + * Create a new alert command + * @param $id as command name + * other= (optional). Are the following in this order: + * + * + * + * + * + * ... + example: + + *http://localhost/pandora_console/include/api.php?op=set&op2=alert_commands&id=PRUEBA1&other=command|0|Desc|1|des1|val1|des2|val2|des3|val3||val4|des5&other_mode=url_encode_separator_|&apipass=1234&user=admin&pass=pandora +*/ function api_set_alert_commands($id, $thrash2, $other, $trash1) { global $config; @@ -7633,9 +7687,6 @@ function api_set_alert_commands($id, $thrash2, $other, $trash1) { $id_group = $other['data'][1]; $description = $other['data'][2]; $internal = $other['data'][3]; - $fields_descriptions_decode = io_safe_output(base64_decode($other['data'][4], true)); - $fields_values_decode = io_safe_output(base64_decode($other['data'][5], true)); - if (defined ('METACONSOLE')) { return; @@ -7649,13 +7700,23 @@ function api_set_alert_commands($id, $thrash2, $other, $trash1) { $name = db_get_value ('id', 'talert_commands', 'name', $id); $group = db_get_value ('id_grupo', 'tgrupo', 'id_grupo', $id_group); + if ($id == '' || !$id) { + returnError('error_parameter', __('Name cannot be empty.')); + return; + } + + if ($command == '' || !$command) { + returnError('error_parameter', __('Command cannot be empty.')); + return; + } + if ($name) { - returnError('error_parameter', 'Name already exist'); + returnError('error_parameter', __('Name already exist')); return; } if (!$group && $id_group != 0) { - returnError('error_parameter', 'Group does not exist'); + returnError('error_parameter', __('Group does not exist')); return; } @@ -7665,12 +7726,31 @@ function api_set_alert_commands($id, $thrash2, $other, $trash1) { } else if ($other['type'] == 'array') { + $fields_descriptions = array(); + $fields_values = array(); + $max_macro_fields = $config['max_macro_fields'] * 2; + + $values = array(); + for ($i=0;$i<$max_macro_fields; $i++) { + $n = $i + 4; + + if (!$other['data'][$n]) + $other['data'][$n] = ''; + + if ($n%2==0) + $fields_descriptions[] = $other['data'][$n]; + else + $fields_values[] = $other['data'][$n]; + } + + $fields_descriptions_encode = io_json_mb_encode($fields_descriptions); + $fields_values_encode = io_json_mb_encode($fields_values); + + $values = array('id_group' => $id_group, + 'description' => $description, 'internal' => $internal, 'fields_descriptions' => $fields_descriptions_encode, + 'fields_values' => $fields_values_encode); - $values = array('name' => $id,'command' => $command, 'id_group' => $id_group, - 'description' => $description, 'internal' => $internal, 'fields_descriptions' => $fields_descriptions_decode, - 'fields_values' => $fields_values_decode); - - $return = db_process_sql_insert('talert_commands', $values); + $return = alerts_create_alert_command($id, $command, $values); $data['type'] = 'string'; if ($return === false) { From 517cb855476a262a87c0ca90e46647a746861e08 Mon Sep 17 00:00:00 2001 From: samucarc Date: Mon, 3 Dec 2018 17:38:57 +0100 Subject: [PATCH 3/6] Added new features in CLI to create and view alert commands --- pandora_console/include/functions_api.php | 18 ++-- pandora_server/lib/PandoraFMS/Core.pm | 18 ++++ pandora_server/lib/PandoraFMS/DB.pm | 11 +++ pandora_server/util/pandora_manage.pl | 106 +++++++++++++++++++++- 4 files changed, 144 insertions(+), 9 deletions(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 91024dea22..e302c66259 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -7667,14 +7667,14 @@ function api_set_alert_actions($id, $id2, $other, $trash1) { /** * Create a new alert command - * @param $id as command name - * other= (optional). Are the following in this order: + * @param $id as command name (optional) + * other= (mandatory). Are the following in this order: * - * - * - * - * - * ... + * (mandatory) + * (optional) + * (optional) + * (optional) + * ... (optional) example: @@ -7684,7 +7684,9 @@ function api_set_alert_commands($id, $thrash2, $other, $trash1) { global $config; $command = $other['data'][0]; - $id_group = $other['data'][1]; + $id_group = 0; + if ($other['data'][1] != '') + $id_group = $other['data'][1]; $description = $other['data'][2]; $internal = $other['data'][3]; diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index bcb08a23ae..d2045ae89a 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -163,6 +163,7 @@ our @EXPORT = qw( pandora_add_agent_address pandora_audit pandora_create_agent + pandora_create_alert_command pandora_create_group pandora_create_incident pandora_create_module @@ -2681,6 +2682,23 @@ sub pandora_delete_all_template_module_actions ($$) { return db_do ($dbh, 'DELETE FROM talert_template_module_actions WHERE id_alert_template_module = ?', $template_module_id); } +######################################################################## +=head2 C<< pandora_create_alert_command(I<$pa_config>, I<$parameters>, I<$dbh>) >> + +Create a alert command. + +=cut +######################################################################## +sub pandora_create_alert_command ($$$) { + my ($pa_config, $parameters, $dbh) = @_; + + logger($pa_config, "Creating alert command '$parameters->{'name'}'.", 10); + + my $command_id = db_process_insert($dbh, 'id', 'talert_commands', $parameters); + + return $command_id; +} + ######################################################################## =head2 C<< pandora_update_agent_address(I<$pa_config>, I<$agent_id>, I<$address>, I<$dbh>) >> diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index dc0622c98b..20bece94e6 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -62,6 +62,7 @@ our @EXPORT = qw( get_agent_module_id get_alert_template_module_id get_alert_template_name + get_command_id get_db_rows get_db_rows_limit get_db_single_row @@ -209,6 +210,16 @@ sub get_action_id ($$) { return defined ($rc) ? $rc : -1; } +######################################################################## +## Return command ID given the command name. +######################################################################## +sub get_command_id ($$) { + my ($dbh, $command_name) = @_; + + my $rc = get_db_value ($dbh, "SELECT id FROM talert_commands WHERE name = ?", safe_input($command_name)); + return defined ($rc) ? $rc : -1; +} + ######################################################################## ## Return agent ID given the agent name. ######################################################################## diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 0fe2d44eaf..4586d5cc86 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -162,6 +162,8 @@ sub help_screen{ help_screen_line('--enable_alerts', '', 'Enable alerts in all groups (system wide)'); help_screen_line('--create_alert_template', " \n\t [ \n\t \n\t \n\t ]", 'Create alert template'); help_screen_line('--delete_alert_template', '', 'Delete alert template'); + help_screen_line('--create_alert_command', " [ \n\t ", 'Create alert command'); + help_screen_line('--get_alert_commands', "[ \n\t ]", 'Displays all alert commands'); help_screen_line('--update_alert_template', " \n\t ", 'Update a field of an alert template'); help_screen_line('--validate_all_alerts', '', 'Validate all the alerts'); help_screen_line('--create_special_day', " ", 'Create special day'); @@ -3020,6 +3022,100 @@ sub cli_delete_alert_template() { exist_check($result,'alert template',$template_name); } +############################################################################## +# Add alert command. +# Related option: --create_alert_command +############################################################################## + +sub cli_create_alert_command() { + my ($command_name,$command,$group_name,$description,$internal,$fields_descriptions,$fields_values) = @ARGV[2..8]; + + print_log "[INFO] Adding command '$command_name'\n\n"; + + my $command_id = get_command_id($dbh,$command_name); + non_exist_check($command_id,'command',$command_name); + + my $id_group; + + if (! $group_name || $group_name eq "All") { + $id_group = 0; + } + else { + $id_group = get_group_id($dbh,$group_name); + exist_check($id_group,'group',$group_name); + } + + my %parameters; + + $parameters{'name'} = $command_name; + $parameters{'command'} = $command; + $parameters{'id_group'} = $id_group; + $parameters{'description'} = $description; + $parameters{'internal'} = $internal; + $parameters{'fields_descriptions'} = $fields_descriptions; + $parameters{'fields_values'} = $fields_values; + + pandora_create_alert_command ($conf, \%parameters, $dbh); +} + +############################################################################## +# Show all the alert commands (without parameters) or the alert commands with a filter parameters +# Related option: --get_alert_commands +############################################################################## + +sub cli_get_alert_commands() { + my ($command_name, $command, $group_name, $description, $internal) = @ARGV[2..6]; + + my $id_group; + my $condition = ' 1=1 '; + + if($command_name ne '') { + my $name = safe_input ($command_name); + $condition .= " AND name LIKE '%$name%' "; + } + + if($command ne '') { + $condition .= " AND command LIKE '%$command%' "; + } + + if($group_name ne '') { + $id_group = get_group_id($dbh, $group_name); + exist_check($id_group,'group',$group_name); + + $condition .= " AND id_grupo = $id_group "; + } + + if($description ne '') { + $condition .= " AND description LIKE '%$description%' "; + } + + if($internal ne '') { + $condition .= " AND internal = $internal "; + } + + my @alert_command = get_db_rows ($dbh, "SELECT * FROM talert_commands WHERE $condition"); + + if(scalar(@alert_command) == 0) { + print_log "[INFO] No commands found\n\n"; + exit; + } + + my $head_print = 0; + foreach my $commands (@alert_command) { + + if($head_print == 0) { + $head_print = 1; + print "id_command, command_name\n"; + } + print $commands->{'id'}.",".safe_output($commands->{'name'})."\n"; + } + + if($head_print == 0) { + print_log "[INFO] No commands found\n\n"; + } + +} + ############################################################################## # Add profile. # Related option: --add_profile @@ -5885,9 +5981,17 @@ sub pandora_manage_main ($$$) { cli_create_alert_template(); } elsif ($param eq '--delete_alert_template') { - param_check($ltotal, 1); + param_check($ltotal, 7); cli_delete_alert_template(); } + elsif ($param eq '--create_alert_command') { + param_check($ltotal, 7, 2); + cli_create_alert_command(); + } + elsif ($param eq '--get_alert_commands') { + param_check($ltotal, 5, 5); + cli_get_alert_commands(); + } elsif ($param eq '--update_alert_template') { param_check($ltotal, 3); cli_alert_template_update(); From c05e99f37e7bc2ff6274aee5bc091fcc95ad87f2 Mon Sep 17 00:00:00 2001 From: Alejandro Fraguas Date: Tue, 29 Jan 2019 16:07:39 +0100 Subject: [PATCH 4/6] Update pandora_manage.pl Former-commit-id: 451b04e91078b99c8b9f41a35271797c343f357c --- pandora_server/util/pandora_manage.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index c55c6ce495..9cf6c244b8 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -6105,6 +6105,7 @@ sub pandora_manage_main ($$$) { elsif ($param eq '--get_alert_commands') { param_check($ltotal, 5, 5); cli_get_alert_commands(); + } elsif ($param eq '--get_alert_actions') { param_check($ltotal, 3, 3); cli_get_alert_actions(); From eb0b9bd05beec65165ea0f8f002ad0a243ddd135 Mon Sep 17 00:00:00 2001 From: Alejandro Fraguas Date: Tue, 29 Jan 2019 17:16:47 +0100 Subject: [PATCH 5/6] Update pandora_manage.pl Former-commit-id: 4064310d63eb101eb1a13825357aa5c425eae502 --- pandora_server/util/pandora_manage.pl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 9cf6c244b8..b7572da956 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -3175,7 +3175,9 @@ sub cli_get_alert_commands() { if($head_print == 0) { print_log "[INFO] No commands found\n\n"; } +} +############################################################################## # Get alert actions. # Related option: --get_alert_actions ############################################################################## From 3eeaf83ee331d1b25db6ffc4c2af907330c7cbac Mon Sep 17 00:00:00 2001 From: Samuel Carcases Date: Tue, 29 Jan 2019 17:40:25 +0100 Subject: [PATCH 6/6] Update pandora_manage.pl Former-commit-id: 4bc758a4284e96658137fb9580f36163a2031944 --- pandora_server/util/pandora_manage.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index b7572da956..4e88c09c6b 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -3144,7 +3144,7 @@ sub cli_get_alert_commands() { $id_group = get_group_id($dbh, $group_name); exist_check($id_group,'group',$group_name); - $condition .= " AND id_grupo = $id_group "; + $condition .= " AND id_group = $id_group "; } if($description ne '') {