From 0eae3e1f33921152d89c71040c808ac3aa7ae39b Mon Sep 17 00:00:00 2001 From: m-lopez-f Date: Mon, 23 Nov 2015 09:21:35 +0100 Subject: [PATCH] Add to API/CLI new function: Create planned downtimes and items to this planned. Fixed error in creation items. Tiquet: #2972 (cherry picked from commit c4af25cf3455af855ecae0e38aae341a6256456f) --- pandora_console/include/functions_api.php | 116 +++++++++++++++++ .../include/functions_planned_downtimes.php | 120 +++++++++++++++--- pandora_server/util/pandora_manage.pl | 33 +++++ 3 files changed, 248 insertions(+), 21 deletions(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 5d16143c3c..8f0bd67a6e 100755 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -29,6 +29,7 @@ include_once($config['homedir'] . "/include/functions_groups.php"); include_once($config['homedir'] . "/include/functions_network_components.php"); include_once($config['homedir'] . "/include/functions_netflow.php"); include_once($config['homedir'] . "/include/functions_servers.php"); +include_once($config['homedir'] . "/include/functions_planned_downtimes.php"); enterprise_include_once ('include/functions_local_components.php'); enterprise_include_once ('include/functions_events.php'); enterprise_include_once ('include/functions_agents.php'); @@ -3746,6 +3747,121 @@ function api_set_tag($id, $thrash1, $other, $thrash3) { array('type' => 'string', 'data' => $id_tag)); } +/** + * Create a new planned downtime. + * + * @param $id name of planned downtime. + * @param $thrash1 Don't use. + * @param array $other it's array, $other as param is ;;;;; + * ;;;;;;;; + * ;;;;; in this order + * and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_) + * example: + * + * api.php?op=set&op2=planned_downtimes_created&id=pepito&other=testing|1448035508|1448039108|0|1|1|1|1|1|1|1|17:06:00|19:06:00|1|31|quiet|periodically|weekly&other_mode=url_encode_separator_| + * + * @param $thrash3 Don't use. + */ + +function api_set_planned_downtimes_created($id, $thrash1, $other, $thrash3) { + if (defined ('METACONSOLE')) { + return; + } + + $values = array(); + $values['name'] = $id; + $values = array( + 'name' => $id, + 'description' => $other['data'][0], + 'date_from' => $other['data'][1], + 'date_to' => $other['data'][2], + 'id_group' => $other['data'][3], + 'monday' => $other['data'][4], + 'tuesday' => $other['data'][5], + 'wednesday' => $other['data'][6], + 'thursday' => $other['data'][7], + 'friday' => $other['data'][8], + 'saturday' => $other['data'][9], + 'sunday' => $other['data'][10], + 'periodically_time_from' => $other['data'][11], + 'periodically_time_to' => $other['data'][12], + 'periodically_day_from' => $other['data'][13], + 'periodically_day_to' => $other['data'][14], + 'type_downtime' => $other['data'][15], + 'type_execution' => $other['data'][16], + 'type_periodicity' => $other['data'][17] + ); + + $returned = planned_downtimes_created($values); + + if (!$returned['return']) + returnError('error_set_planned_downtime', $returned['message'] ); + else + returnData('string', + array('type' => 'string', 'data' => $returned['return'])); +} + +/** + * Add new items to planned Downtime. + * + * @param $id id of planned downtime. + * @param $thrash1 Don't use. + * @param array $other it's array, $other as param is ; + * in this order + * and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_) + * example: + * + * api.php?op=set&op2=planned_downtimes_additem&id=123&other=1;2;3;4|Status;Unkown_modules&other_mode=url_encode_separator_| + * + * @param $thrash3 Don't use. + */ + +function api_set_planned_downtimes_additem($id, $thrash1, $other, $thrash3) { + if (defined ('METACONSOLE')) { + return; + } + + $total_agents = explode(';',$other['data'][0]); + $agents = $total_agents; + $bad_agents = array(); + $i = 0; + foreach ($total_agents as $agent_id) { + $result_agent = (bool) db_get_value ('id_agente', 'tagente', 'id_agente', $agent_id); + if ( !$result_agent ) { + $bad_agents[] = $agent_id; + unset($agents[$i]); + } + $i++; + } + + if ( isset($other['data'][1]) ) + $name_modules = explode(';',$other['data'][1]); + else + $name_modules = false; + + if ($name_modules) + $all_modules = false; + else + $all_modules = true; + + if ( !empty($agents) ) + $returned = planned_downtimes_add_items($id, $agents, $all_modules, $name_modules); + + if ( empty($agents) ) + returnError('error_set_planned_downtime_additem', "No agents to create planned downtime items"); + else{ + + if ( !empty($returned['bad_modules']) ) + $bad_modules = __("and this modules are doesn't exists or not applicable a this agents: ") . implode(", ",$returned['bad_modules']); + if ( !empty($returned['bad_agents']) ) + $bad_agent = __("and this agents are generate problems: ") . implode(", ", $returned['bad_agents']) ; + if ( !empty($bad_agents) ) + $agents_no_exists = __("and this agents with ids are doesn't exists: ") . implode(", ", $bad_agents) ; + returnData('string', + array('type' => 'string', 'data' => "Successfully created items " . $bad_agent . " " . $bad_modules . " " . $agents_no_exists)); + } +} + /** * Add agent to a policy. And return a message with the result of the operation. * diff --git a/pandora_console/include/functions_planned_downtimes.php b/pandora_console/include/functions_planned_downtimes.php index a819d87aeb..3794def7bc 100644 --- a/pandora_console/include/functions_planned_downtimes.php +++ b/pandora_console/include/functions_planned_downtimes.php @@ -147,7 +147,7 @@ function planned_downtimes_add_items ($downtime_id, $agents, $all_modules = true include_once($config['homedir'] . "/include/functions_modules.php"); - $result = array( + $return = array( 'status' => true, 'bad_agents' => array(), 'bad_modules' => array() @@ -164,41 +164,47 @@ function planned_downtimes_add_items ($downtime_id, $agents, $all_modules = true if (empty($agents)) { $agents = array(); - $result['status'] = false; + $return['status'] = false; } foreach ($agents as $agent_id) { + $values = array( - 'id_downtime' => $downtime_id, - 'id_agent' => $agent_id, - 'all_modules' => $all_modules, - 'id_user' => $config['id_user'] - ); + 'id_downtime' => $downtime_id, + 'id_agent' => $agent_id, + 'all_modules' => $all_modules + ); + $result = db_process_sql_insert('tplanned_downtime_agents', $values); - + if (empty($result)) { - $result['bad_agents'][] = $agent_id; + $return['bad_agents'][] = $agent_id; } else if (!$all_modules) { foreach ($module_names as $module_name) { $module = modules_get_agentmodule_id($module_name, $agent_id); - $module_id = $module["id_agente_modulo"]; + $result = false; + + if ($module) { + + $module_id = $module["id_agente_modulo"]; - $values = array( - 'id_downtime' => $downtime_id, - 'id_agent' => $agent_id, - 'id_agent_module' => $module_id - ); - $result = db_process_sql_insert('tplanned_downtime_modules', $values); - - if (empty($result)) { - $result['bad_modules'][] = $module_id; + $values = array( + 'id_downtime' => $downtime_id, + 'id_agent' => $agent_id, + 'id_agent_module' => $module_id + ); + $result = db_process_sql_insert('tplanned_downtime_modules', $values); + } + + if (!$result) { + $return['bad_modules'][] = $module_name; } } } } - - return $result; + + return $return; } /** @@ -656,4 +662,76 @@ function planned_downtimes_stop ($downtime) { return array('result' => $result, 'message' => $message); } +function planned_downtimes_created ($values) { + global $config; + + $check = (bool) db_get_value ('name', 'tplanned_downtime', 'name', $values['name']); + + $datetime_from = strtotime ($values['once_date_from'] . ' ' . $values['once_time_from']); + $datetime_to = strtotime ($values['once_date_to'] . ' ' . $values['once_time_to']); + $now = time(); + $result = false; + + if ($values['type_execution'] == 'once' && !$config["past_planned_downtimes"] && $values['datetime_from'] < $now) { + return array('return' => false, + 'message' => __('Not created. Error inserting data. Start time must be higher than the current time')); + } + else if ($values['type_execution'] == 'once' && $values['datetime_from'] >= $values['datetime_to']) { + return array('return' => false, + 'message' => __('Not created. Error inserting data') . ". " + . __('The end date must be higher than the start date')); + } + else if ($values['type_execution'] == 'once' && $values['datetime_to'] <= $now) { + return array('return' => false, + 'message' => __('Not created. Error inserting data') . ". " + . __('The end date must be higher than the current time')); + } + else if ($values['type_execution'] == 'periodically' + && (($values['type_periodicity'] == 'weekly' && $values['periodically_time_from'] >= $values['periodically_time_to']) + || ($values['type_periodicity'] == 'monthly' && $values['periodically_day_from'] == + $values['periodically_day_to'] && $values['periodically_time_from'] >= $values['periodically_time_to']))) { + return array('return' => false, + 'message' => __('Not created. Error inserting data') . ". " + . __('The end time must be higher than the start time')); + } + else if ($values['type_execution'] == 'periodically' && + $values['type_periodicity'] == 'monthly' && + $values['periodically_day_from'] > $values['periodically_day_to']) { + return array('return' => false, + 'message' => __('Not created. Error inserting data') . ". " + . __('The end day must be higher than the start day')); + } + else { + if (trim(io_safe_output($values['name'])) != '') { + if (!$check) { + if ($config["dbtype"] == 'oracle') { + $values['periodically_time_from'] = '1970/01/01 ' . $values['periodically_time_from']; + $values['periodically_time_to'] = '1970/01/01 ' . $values['periodically_time_to']; + } + + $result = db_process_sql_insert('tplanned_downtime', $values); + } + else { + return array('return' => false, + 'message' => __('Each planned downtime must have a different name')); + } + } + else { + return array('return' => false, + 'message' => __('Planned downtime must have a name')); + } + + if ($result === false) { + return array('return' => false, + 'message' => __('Could not be created')); + } + else { + return array('return' => $result, + 'message' => __('Successfully created')); + } + } + + return $return; +} + ?> \ No newline at end of file diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index b9d064b884..14950d3c1a 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -109,6 +109,8 @@ sub help_screen{ help_screen_line('--enable_group', '', 'Enable agents from an entire group'); help_screen_line('--create_group', ' [ ]', 'Create an agent group'); help_screen_line('--stop_downtime', '', 'Stop a planned downtime'); + help_screen_line('--create_downtime', " \n\t \n\t ", 'Create a planned downtime'); + help_screen_line('--add_item_planned_downtime', " ", 'Add a items planned downtime'); help_screen_line('--get_agent_group', '', 'Get the group name of an agent'); help_screen_line('--get_agent_modules', '', 'Get the modules of an agent'); help_screen_line('--get_agents', '[ ]', "Get \n\t list of agents with optative filter parameters"); @@ -3362,6 +3364,29 @@ sub cli_policy_add_agent() { } } +sub cli_create_planned_downtime() { + my $name = @ARGV[2]; + my @todo = @ARGV[3..20]; + my $other = join('|', @todo); + + my $result = api_call(\%conf,'set', 'planned_downtimes_created', $name, undef, "$other"); + print $result; +} + +sub cli_add_item_planned_downtime() { + my $id = @ARGV[2]; + my $agent = @ARGV[3]; + my $moduls = @ARGV[4]; + my @agents = split /,/, $agent; + my @modules = split /,/, $moduls; + my $other_agents = join(';', @agents); + my $other_modules = join(';', @modules); + my $other = $other_agents . "|" . $other_modules; + + my $result = api_call(\%conf,'set', 'planned_downtimes_additem', $id, undef, "$other"); + print_log "$result \n\n"; +} + ############################################################################## # Create group # Related option: --create_group @@ -4157,6 +4182,14 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 3); cli_add_tag_to_module(); } + elsif ($param eq '--create_downtime') { + param_check($ltotal, 19); + cli_create_planned_downtime(); + } + elsif ($param eq '--add_item_downtime') { + param_check($ltotal, 3); + cli_add_item_planned_downtime(); + } else { print_log "[ERROR] Invalid option '$param'.\n\n"; $param = '';