From 8d04570989d86899699e1c3f9b1b9ae6d2d20e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Gonz=C3=A1lez?= Date: Tue, 24 May 2022 10:59:41 +0200 Subject: [PATCH] Set new mode for manage properly the restarting of snmptrap process --- pandora_server/lib/PandoraFMS/SNMPServer.pm | 31 +++++++++++++-------- pandora_server/util/pandora_manage.pl | 17 ++++++++++- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/SNMPServer.pm b/pandora_server/lib/PandoraFMS/SNMPServer.pm index 492dfdfa60..581c1b441d 100644 --- a/pandora_server/lib/PandoraFMS/SNMPServer.pm +++ b/pandora_server/lib/PandoraFMS/SNMPServer.pm @@ -40,6 +40,7 @@ use PandoraFMS::ProducerConsumerServer; # Inherits from PandoraFMS::ProducerConsumerServer our @ISA = qw(PandoraFMS::ProducerConsumerServer); +our @EXPORT = qw(start_snmptrapd); # Global variables my @TaskQueue :shared; @@ -442,26 +443,29 @@ sub start_snmptrapd ($) { # Manual start of snmptrapd if ($config->{'snmp_trapd'} eq 'manual') { - logger ($config, "No SNMP trap daemon configured. Start snmptrapd manually.", 1); - print_message ($config, " [*] No SNMP trap daemon configured. Start snmptrapd manually.", 1); + my $noSNMPTrap = "No SNMP trap daemon configured. Start snmptrapd manually."; + logger ($config, $noSNMPTrap, 1); + print_message ($config, " [*] $noSNMPTrap", 1); if (! -f $config->{'snmp_logfile'}) { - logger ($config, "SNMP log file " . $config->{'snmp_logfile'} . " not found.", 1); - print_message ($config, " [E] SNMP log file " . $config->{'snmp_logfile'} . " not found.", 1); + my $noLogFile = "SNMP log file " . $config->{'snmp_logfile'} . " not found."; + logger ($config, $noLogFile, 1); + print_message ($config, " [E] $noLogFile", 1); return 1; } - + return 0; } - + if ( -e $pid_file && open (PIDFILE, $pid_file)) { my $pid = + 0; - close PIDFILE; + close PIDFILE; # Check if snmptrapd is running if ($snmptrapd_running = kill (0, $pid)) { - logger ($config, "snmptrapd (pid $pid) is already running, attempting to kill it...", 1); - print_message ($config, "snmptrapd (pid $pid) is already running, attempting to kill it...", 1); + my $alreadyRunning = "snmptrapd (pid $pid) is already running, attempting to kill it..."; + logger ($config, $alreadyRunning, 1); + print_message ($config, " [*] $alreadyRunning ", 1); kill (9, $pid); } } @@ -471,17 +475,20 @@ sub start_snmptrapd ($) { # Select agent-addr field of the PDU or PDU source address for V1 traps my $address_format = ($config->{'snmp_pdu_address'} eq '0' ? '%a' : '%b'); - + my $snmptrapd_args = ' -t -On -n' . $snmp_ignore_authfailure . ' -Lf ' . $config->{'snmp_logfile'} . ' -p ' . $pid_file; $snmptrapd_args .= ' --format1=SNMPv1[**]%4y-%02.2m-%l[**]%02.2h:%02.2j:%02.2k[**]' . $address_format . '[**]%N[**]%w[**]%W[**]%q[**]%v\\\n'; $snmptrapd_args .= ' --format2=SNMPv2[**]%4y-%02.2m-%l[**]%02.2h:%02.2j:%02.2k[**]%b[**]%v\\\n'; if (system ($config->{'snmp_trapd'} . $snmptrapd_args . " >$DEVNULL 2>&1") != 0) { - logger ($config, " [E] Could not start snmptrapd.", 1); - print_message ($config, " [E] Could not start snmptrapd.", 1); + my $showError = "Could not start snmptrapd."; + logger ($config, $showError, 1); + print_message ($config, " [E] $showError ", 1); return 1; } + print_message ($config, " [*] snmptrapd started and running.", 1); + return 0; } diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 26f0b847c7..57a4036875 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -231,6 +231,7 @@ sub help_screen{ print "\nTOOLS:\n\n" unless $param ne ''; help_screen_line('--exec_from_file', ' ', "Execute any CLI option \n\t with macros from CSV file"); help_screen_line('--create_snmp_trap', ' ', "Create a new trap definition. \n\tSeverity 0 (Maintenance), 1(Info) , 2 (Normal), 3 (Warning), 4 (Critical), 5 (Minor) and 6 (Major)"); + help_screen_line('--start_snmptrapd', '[no parameters needed]', "Start the snmptrap process or restart if it is running"); print "\nSETUP:\n\n" unless $param ne ''; help_screen_line('--set_event_storm_protection', '', "Enable (1) or disable (0) event \n\t storm protection"); @@ -279,7 +280,7 @@ sub api_call($$$;$$$$) { my $ua = new LWP::UserAgent; my $url = $pa_config->{"console_api_url"}; my $response = $ua->post($url, $params); - + if ($response->is_success) { $content = $response->decoded_content(); } @@ -1163,6 +1164,16 @@ sub cli_enable_group() { pandora_enable_group ($conf, $dbh, $id_group); } +############################################################################## +# Start snmptrap process. +# Related option: --start_snmptrapd +############################################################################## +sub cli_start_snmptrapd() { + use PandoraFMS::SNMPServer; + print_log "[INFO] Starting snmptrap process. \n"; + PandoraFMS::SNMPServer::start_snmptrapd(\%conf); +} + ############################################################################## # Create an agent. # Related option: --created_agent @@ -7505,6 +7516,10 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 1); cli_enable_group(); } + elsif ($param eq '--start_snmptrapd') { + #param_check($ltotal, 0); + cli_start_snmptrapd(); + } elsif ($param eq '--create_agent') { param_check($ltotal, 8, 4); cli_create_agent();