List alert actions in nodes with CLI and API

Former-commit-id: 11e9a8137a2a37b081fcbac2ee50cac27c2c208b
This commit is contained in:
samucarc 2018-12-14 17:20:46 +01:00
parent f2f88579fa
commit 632d67095b
2 changed files with 95 additions and 0 deletions

View File

@ -4559,6 +4559,62 @@ function api_get_alert_template($id_template, $thrash1, $other, $thrash3) {
}
}
/**
* List of alert actions.
*
* @param array $other it's array, $other as param is <action_name>;<separator_data> and separator (pass in param
* othermode as othermode=url_encode_separator_<separator>)
* @param $returnType (csv, string or json).
*
* example:
*
* api.php?op=get&op2=alert_actions&apipass=1234&user=admin&pass=pandora&other=Create|;&other_mode=url_encode_separator_|&return_type=json
*
*/
function api_get_alert_actions($thrash1, $thrash2, $other, $returnType) {
global $config;
if (!check_acl($config['id_user'], 0, "LM")) {
returnError('forbidden', 'string');
return;
}
if (!isset($other['data'][0]))
$other['data'][1] = '';
if (!isset($other['data'][1]))
$separator = ';'; //by default
else
$separator = $other['data'][1];
$action_name = $other['data'][0];
$filter = array();
if (!is_user_admin($config['id_user']))
$filter['talert_actions.id_group'] = array_keys(users_get_groups(false, "LM"));
$filter['talert_actions.name'] = "%$action_name%";
$actions = db_get_all_rows_filter (
'talert_actions INNER JOIN talert_commands ON talert_actions.id_alert_command = talert_commands.id',
$filter,
'talert_actions.id, talert_actions.name'
);
if ($actions === false)
$actions = array ();
if ($actions !== false) {
$data['type'] = 'array';
$data['data'] = $actions;
}
if (!$actions) {
returnError('error_get_alert_actions',
__('Error getting alert actions.'));
}
else {
returnData($returnType, $data, $separator);
}
}
/**
* Get module groups, and print all the result like a csv.
*

View File

@ -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', "<template_name> <condition_type_serialized>\n\t <time_from> <time_to> [<description> <group_name> <field1> <field2> \n\t <field3> <priority> <default_action> <days> <time_threshold> <min_alerts> \n\t <max_alerts> <alert_recovery> <field2_recovery> <field3_recovery> \n\t <condition_type_separator>]", 'Create alert template');
help_screen_line('--delete_alert_template', '<template_name>', 'Delete alert template');
help_screen_line('--get_alert_actions', '[<action_name> <separator> <return_type>]', 'get all alert actions');
help_screen_line('--get_alert_actions_meta', '[<server_name> <action_name> <separator> <return_type>]', 'get all alert actions in nodes');
help_screen_line('--update_alert_template', "<template_name> <field_to_change> \n\t <new_value>", 'Update a field of an alert template');
help_screen_line('--validate_all_alerts', '', 'Validate all the alerts');
help_screen_line('--create_special_day', "<special_day> <same_day> <description> <group>", 'Create special day');
@ -3028,6 +3030,35 @@ sub cli_delete_alert_template() {
exist_check($result,'alert template',$template_name);
}
##############################################################################
# Get alert actions.
# Related option: --get_alert_actions
##############################################################################
sub cli_get_alert_actions() {
my ($action_name,$separator,$return_type) = @ARGV[2..4];
if ($return_type eq '') {
$return_type = 'csv';
}
my $result = api_call(\%conf,'get', 'alert_actions', undef, undef, "$action_name|$separator",$return_type);
print "$result \n\n ";
}
##############################################################################
# Get alert actions in nodes.
# Related option: --get_alert_actions_meta
##############################################################################
sub cli_get_alert_actions_meta() {
my ($server_name,$action_name,$separator,$return_type) = @ARGV[2..5];
if ($return_type eq '') {
$return_type = 'csv';
}
my $result = api_call(\%conf,'get', 'alert_actions_meta', undef, undef, "$server_name|$action_name|$separator",$return_type);
print "$result \n\n ";
}
##############################################################################
# Add profile.
# Related option: --add_profile
@ -5896,6 +5927,14 @@ sub pandora_manage_main ($$$) {
param_check($ltotal, 1);
cli_delete_alert_template();
}
elsif ($param eq '--get_alert_actions') {
param_check($ltotal, 3, 3);
cli_get_alert_actions();
}
elsif ($param eq '--get_alert_actions_meta') {
param_check($ltotal, 4, 4);
cli_get_alert_actions_meta();
}
elsif ($param eq '--update_alert_template') {
param_check($ltotal, 3);
cli_alert_template_update();