diff --git a/pandora_server/util/n2p.pl b/pandora_server/util/n2p.pl index befdb1cf41..c904a98b1c 100755 --- a/pandora_server/util/n2p.pl +++ b/pandora_server/util/n2p.pl @@ -1109,6 +1109,31 @@ sub get_group_id { return $data[0]; } +################################################################################ +## SUB get_module_group_id +## Get the id of a module_group given its name. +################################################################################ +sub get_module_group_id { + my $name = $_[0]; + my $sth; + my @data; + + # SQL Get group id + $sth = $dbh->prepare ("SELECT id_mg FROM tmodule_group WHERE name = ?") || die ("Error preparing statement: " . $sth->errstr); + + $sth->execute ($name) || die ("Error executing statement: " . $sth->errstr); + + # Group not found + if ($sth->rows == 0) { + return -1; + } + + @data = $sth->fetchrow_array (); + + # Return id + return $data[0]; +} + ################################################################################ ## SUB get_module_id ## Get the id of a module given its name and the id of the host. diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 0abdaa744c..5103b816ef 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -127,7 +127,8 @@ sub help_screen{ help_screen_line('--create_network_module', " \n\t [ \n\t \n\t \n\t ]", 'Add not snmp network module to agent'); help_screen_line('--create_snmp_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t ]", 'Add snmp network module to agent'); help_screen_line('--create_plugin_module', " \n\t [ \n\t \n\t \n\t \n\t ]", 'Add plug-in module to agent'); - help_screen_line('--delete_module', 'Delete module from agent', ' '); + help_screen_line('--create_module_group', ''); + help_screen_line('--delete_module', 'Delete module from agent', ' '); help_screen_line('--data_module', " \n\t []", 'Insert data to module'); help_screen_line('--get_module_data', " []", "\n\t Show the data of a module in the last X seconds (interval) in CSV format"); help_screen_line('--delete_data', '-m | -a | -g ', "Delete historic \n\t data of a module, the modules of an agent or the modules of the agents of a group"); @@ -178,10 +179,13 @@ sub help_screen{ help_screen_line('--add_agent_to_policy', ' ', 'Add an agent to a policy'); help_screen_line('--delete_not_policy_modules', '', 'Delete all modules without policy from configuration file'); help_screen_line('--disable_policy_alerts', '', 'Disable all the alerts of a policy'); + help_screen_line('--create_policy', ' '); help_screen_line('--create_policy_data_module', " [ \n\t \n\t \n\t \n\t \n\t ]", 'Add data server module to policy'); help_screen_line('--create_policy_network_module', " [ \n\t \n\t \n\t \n\t ]", "Add not snmp network module to policy"); help_screen_line('--create_policy_snmp_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t ]", 'Add snmp network module to policy'); help_screen_line('--create_policy_plugin_module', " \n\t [ \n\t \n\t \n\t \n\t ]", 'Add plug-in module to policy'); + help_screen_line('--create_policy_data_module_from_local_component', ' '); + help_screen_line('--add_collection_to_policy', " "); help_screen_line('--validate_policy_alerts', '', 'Validate the alerts of a given policy'); help_screen_line('--get_policy_modules', '', 'Get the modules of a policy'); help_screen_line('--get_policies', '[]', "Get all the policies (without parameters) or \n\tthe policies of a given agent (agent name as parameter)"); @@ -1291,6 +1295,19 @@ sub cli_create_data_module($) { } } +############################################################################## +# Create module group. +# Related option: --create_module_group +############################################################################## +sub cli_create_module_group () { + my $module_group_name = @ARGV[2]; + + my $id_module_group = get_module_group_id($dbh, $module_group_name); + non_exist_check($id_module_group,'group',$module_group_name); + + db_insert ($dbh, 'id_mg', 'INSERT INTO tmodule_group (name) VALUES (?)', safe_input($module_group_name)); +} + ############################################################################## # Create network module from component. # Related option: --create_network_module_from_component @@ -4370,6 +4387,10 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 27, 24); cli_create_data_module(0); } + elsif ($param eq '--create_module_group') { + param_check($ltotal, 1, 1); + cli_create_module_group(); + } elsif ($param eq '--create_network_module') { param_check($ltotal, 26, 20); cli_create_network_module(0); @@ -4490,6 +4511,18 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 4, 1); cli_module_get_data(); } + elsif ($param eq '--add_collection_to_policy') { + param_check($ltotal, 2, 2); + cli_add_collection_to_policy(); + } + elsif ($param eq '--create_policy_data_module_from_local_component') { + param_check($ltotal, 2, 2); + cli_create_policy_data_module_from_local_component(); + } + elsif ($param eq '--create_policy') { + param_check($ltotal, 3, 2); + cli_create_policy(); + } elsif ($param eq '--create_policy_data_module') { param_check($ltotal, 23, 20); cli_create_data_module(1); @@ -4790,6 +4823,43 @@ sub pandora_get_local_component_id($$) { return defined ($lc_id) ? $lc_id : -1; } +############################################################################## +# Create policy +# Related option: --create_policy +############################################################################## +sub cli_create_policy () { + my ($policy_name, $group_name, $description) = @ARGV[2..4]; + + my $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); + + non_exist_check($policy_id,'policy',$policy_name); + + my $id_group = get_group_id($dbh,$group_name); + exist_check($id_group,'group',$group_name); + + my $id = enterprise_hook('create_policy',[$dbh, safe_input($policy_name), safe_input($description), $id_group]); + + return $id; +} + +############################################################################## +# Add collection to a policy +# Related option: --add_collection_to_policy +############################################################################## +sub cli_add_collection_to_policy () { + my ($policy_name, $collection_name) = @ARGV[2..3]; + + my $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); + exist_check($policy_id,'policy',$policy_name); + + my $collection_id = enterprise_hook('get_collection_id',[$dbh, safe_input($collection_name)]); + exist_check($collection_id,'group',$collection_name); + + my $id = enterprise_hook('add_collection_to_policy_db',[$dbh, $policy_id, $collection_id]); + + return $id; +} + ############################################################################## # Create data module from local component. # Related option: --create_data_module_from_local_component @@ -4814,6 +4884,25 @@ sub cli_create_data_module_from_local_component() { enterprise_hook('pandora_create_module_from_local_component',[$conf, $component, $agent_id, $dbh]); } +############################################################################## +# Create policy data module from local component. +# Related option: --create_policy_data_module_from_local_component +############################################################################## +sub cli_create_policy_data_module_from_local_component() { + my ($policy_name, $component_name) = @ARGV[2..3]; + + my $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); + exist_check($policy_id,'policy',$policy_name); + + my $lc_id = pandora_get_local_component_id($dbh, $component_name); + exist_check($lc_id,'local component',$component_name); + + # Get local component data + my $component = get_db_single_row ($dbh, 'SELECT * FROM tlocal_component WHERE id = ?', $lc_id); + + enterprise_hook('pandora_create_policy_data_module_from_local_component',[$conf, $component, $policy_id, $dbh]); +} + ############################################################################## # Create local component. # Related option: --create_local_component