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 c4af25cf34)
This commit is contained in:
m-lopez-f 2015-11-23 09:21:35 +01:00
parent b8b1d666dc
commit 0eae3e1f33
3 changed files with 248 additions and 21 deletions

View File

@ -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 <description>;<date_from>;<date_to>;<id_group>;<monday>;
* <tuesday>;<wednesday>;<thursday>;<friday>;<saturday>;<sunday>;<periodically_time_from>;<periodically_time_to>;
* <periodically_day_from>;<periodically_day_to>;<type_downtime>;<type_execution>;<type_periodicity>; in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<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 <id_agent1;id_agent2;id_agent3;....id_agentn;>;
* <name_module1;name_module2;name_module3;......name_modulen;> in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<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.
*

View File

@ -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,24 +164,29 @@ 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']
'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);
$result = false;
if ($module) {
$module_id = $module["id_agente_modulo"];
$values = array(
@ -190,15 +195,16 @@ function planned_downtimes_add_items ($downtime_id, $agents, $all_modules = true
'id_agent_module' => $module_id
);
$result = db_process_sql_insert('tplanned_downtime_modules', $values);
}
if (empty($result)) {
$result['bad_modules'][] = $module_id;
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;
}
?>

View File

@ -109,6 +109,8 @@ sub help_screen{
help_screen_line('--enable_group', '<group_name>', 'Enable agents from an entire group');
help_screen_line('--create_group', '<group_name> [<parent_group_name> <icon> <description>]', 'Create an agent group');
help_screen_line('--stop_downtime', '<downtime_name>', 'Stop a planned downtime');
help_screen_line('--create_downtime', "<downtime_name> <description> <date_from> <date_to> <id_group> <monday> <tuesday>\n\t <wednesday> <thursday> <friday> <saturday> <sunday> <periodically_time_from>\n\t <periodically_time_to> <periodically_day_from> <periodically_day_to> <type_downtime> <type_execution> <type_periodicity>", 'Create a planned downtime');
help_screen_line('--add_item_planned_downtime', "<id_downtime> <id_agente1,id_agente2,id_agente3...id_agenteN> <name_module1,name_module2,name_module3...name_moduleN> ", 'Add a items planned downtime');
help_screen_line('--get_agent_group', '<agent_name>', 'Get the group name of an agent');
help_screen_line('--get_agent_modules', '<agent_name>', 'Get the modules of an agent');
help_screen_line('--get_agents', '[<group_name> <os_name> <status> <max_modules> <filter_substring> <policy_name>]', "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 = '';