Merge branch '2519_Listart_y_crear_comandos_de_alerta' into 'develop'
Add commands alert See merge request artica/pandorafms!2025 Former-commit-id: 340201221d0219a0c67e1e34eb6421b7c6d1782d
This commit is contained in:
commit
11456bcfbb
|
@ -37,6 +37,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.
|
||||
|
@ -4514,6 +4515,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.
|
||||
*
|
||||
|
@ -7841,6 +7881,108 @@ function api_set_module_group_synch($thrash1, $thrash2, $other, $thrash4) {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new alert command
|
||||
* @param $id as command name (optional)
|
||||
* other=<serialized_parameters> (mandatory). Are the following in this order:
|
||||
* <name>
|
||||
* <command> (mandatory)
|
||||
* <id_group> (optional)
|
||||
* <description> (optional)
|
||||
* <internal> (optional)
|
||||
* <field_description_1><field_value_1><field_description_2><field_value_2>...<field_description_n><field_value_n> (optional)
|
||||
|
||||
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;
|
||||
|
||||
$command = $other['data'][0];
|
||||
$id_group = 0;
|
||||
if ($other['data'][1] != '')
|
||||
$id_group = $other['data'][1];
|
||||
$description = $other['data'][2];
|
||||
$internal = $other['data'][3];
|
||||
|
||||
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 ($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'));
|
||||
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') {
|
||||
|
||||
$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);
|
||||
|
||||
$return = alerts_create_alert_command($id, $command, $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();
|
||||
|
|
|
@ -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>) >>
|
||||
|
||||
|
|
|
@ -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.
|
||||
########################################################################
|
||||
|
|
|
@ -164,6 +164,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('--create_alert_command', "<command_name> <comand> [<id_group> <description> \n\t <internal> <fields_descriptions> <fields_values>", 'Create alert command');
|
||||
help_screen_line('--get_alert_commands', "[<command_name> <comand> <id_group> <description> \n\t <internal>]", 'Displays all alert commands');
|
||||
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');
|
||||
|
@ -3083,6 +3085,99 @@ 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_group = $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";
|
||||
}
|
||||
}
|
||||
|
||||
##############################################################################
|
||||
# Get alert actions.
|
||||
# Related option: --get_alert_actions
|
||||
|
@ -6019,9 +6114,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 '--get_alert_actions') {
|
||||
param_check($ltotal, 3, 3);
|
||||
cli_get_alert_actions();
|
||||
|
|
Loading…
Reference in New Issue