From 7c39f3680ecfb74b60a94744b25b7aa3e8d4e705 Mon Sep 17 00:00:00 2001 From: Jose Gonzalez Date: Tue, 22 Oct 2019 11:54:27 +0200 Subject: [PATCH] Added feature sample_agent --- .../FreeBSD/pandora_server.conf.new | 9 ++ pandora_server/NetBSD/pandora_server.conf.new | 9 ++ pandora_server/bin/pandora_server | 8 ++ pandora_server/conf/pandora_server.conf.new | 9 ++ .../conf/pandora_server.conf.windows | 9 ++ pandora_server/lib/PandoraFMS/Config.pm | 14 ++- pandora_server/lib/PandoraFMS/Core.pm | 87 ++++++++++++++++++- 7 files changed, 143 insertions(+), 2 deletions(-) diff --git a/pandora_server/FreeBSD/pandora_server.conf.new b/pandora_server/FreeBSD/pandora_server.conf.new index 93d9abbc75..01ff859fa5 100644 --- a/pandora_server/FreeBSD/pandora_server.conf.new +++ b/pandora_server/FreeBSD/pandora_server.conf.new @@ -348,6 +348,15 @@ self_monitoring 1 # Self monitoring interval (in seconds). self_monitoring_interval 300 +# Pandora Sample Agent. If enabled, every 10 minutes, this embedded agent +# will make sample data. Disabled by default. + +sample_agent 0 + +# Pandora Sample Agent interval (in seconds). + +sample_agent_interval 600 + # Update parent from the agent xml #update_parent 1 diff --git a/pandora_server/NetBSD/pandora_server.conf.new b/pandora_server/NetBSD/pandora_server.conf.new index 5f38a678da..66743aee36 100644 --- a/pandora_server/NetBSD/pandora_server.conf.new +++ b/pandora_server/NetBSD/pandora_server.conf.new @@ -340,6 +340,15 @@ restart_delay 60 self_monitoring 1 +# Pandora Sample Agent. If enabled, every 10 minutes, this embedded agent +# will make sample data. Disabled by default. + +sample_agent 0 + +# Pandora Sample Agent interval (in seconds). + +sample_agent_interval 600 + # Update parent from the agent xml #update_parent 1 diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index a367eb7bca..d91b523da6 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -382,6 +382,14 @@ sub pandora_server_tasks ($) { pandora_self_monitoring ($pa_config, $dbh); } + # Pandora sample agent + if (defined($pa_config->{"sample_agent"}) + && $pa_config->{"sample_agent"} == 1 + && !is_metaconsole($pa_config) + && $counter % $pa_config->{"sample_agent_interval"} == 0) { + pandora_sample_agent ($pa_config); + } + # Avoid counter overflow if ($counter >= ~0){ $counter = 0; diff --git a/pandora_server/conf/pandora_server.conf.new b/pandora_server/conf/pandora_server.conf.new index 3aa61de07f..7722f22962 100644 --- a/pandora_server/conf/pandora_server.conf.new +++ b/pandora_server/conf/pandora_server.conf.new @@ -376,6 +376,15 @@ self_monitoring 1 # Self monitoring interval (in seconds). self_monitoring_interval 300 +# Pandora Sample Agent. If enabled, every 10 minutes, this embedded agent +# will make sample data. Disabled by default. + +sample_agent 0 + +# Pandora Sample Agent interval (in seconds). + +sample_agent_interval 600 + # Update parent from the agent xml #update_parent 1 diff --git a/pandora_server/conf/pandora_server.conf.windows b/pandora_server/conf/pandora_server.conf.windows index c7c5db64b4..a7232e459c 100644 --- a/pandora_server/conf/pandora_server.conf.windows +++ b/pandora_server/conf/pandora_server.conf.windows @@ -330,6 +330,15 @@ restart_delay 60 # Self monitoring interval (in seconds). #self_monitoring_interval 300 +# Pandora Sample Agent. If enabled, every 10 minutes, this embedded agent +# will make sample data. Disabled by default. + +sample_agent 0 + +# Pandora Sample Agent interval (in seconds). + +sample_agent_interval 600 + # Update parent from the agent xml #update_parent 1 diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index bedb8b7e70..7b5d6496c2 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -79,7 +79,7 @@ sub help_screen { sub pandora_init { my $pa_config = $_[0]; my $init_string = $_[1]; - print "\n$init_string $pandora_version Build $pandora_build Copyright (c) 2004-2018 " . pandora_get_initial_copyright_notice() . "\n"; + print "\n$init_string $pandora_version Build $pandora_build Copyright (c) 2004-20".substr($pandora_build,0,2)." " . pandora_get_initial_copyright_notice() . "\n"; print "This program is OpenSource, licensed under the terms of GPL License version 2.\n"; print "You can download latest versions and documentation at official web page.\n\n"; @@ -397,6 +397,12 @@ sub pandora_load_config { # Self monitoring interval $pa_config->{'self_monitoring_interval'} = 300; # 5.1SP1 + # Sample Agent + $pa_config->{'sample_agent'} = 0; + + # Sample agent interval + $pa_config->{'sample_agent_interval'} = 600; + # Process XML data files as a stack $pa_config->{"dataserver_lifo"} = 0; # 5.0 @@ -946,6 +952,12 @@ sub pandora_load_config { elsif ($parametro =~ m/^self_monitoring_interval\s+([0-9]*)/i) { $pa_config->{'self_monitoring_interval'} = clean_blank($1); } + elsif ($parametro =~ m/^sample_agent\s+([0-1])/i) { + $pa_config->{'sample_agent'} = clean_blank($1); + } + elsif ($parametro =~ m/^sample_agent_interval\s+([0-9]*)/i) { + $pa_config->{'sample_agent_interval'} = clean_blank($1); + } elsif ($parametro =~ m/^update_parent\s+([0-1])/i) { $pa_config->{'update_parent'} = clean_blank($1); } diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index ae67d5ca70..280bdb2158 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -100,6 +100,8 @@ Exported Functions: =item * C +=item * C + =back =head1 METHODS @@ -122,6 +124,7 @@ use threads::shared; use JSON qw(decode_json encode_json); use MIME::Base64; use Text::ParseWords; +use Math::Trig; # Math functions # Debugging #use Data::Dumper; @@ -247,6 +250,7 @@ our @EXPORT = qw( pandora_group_statistics pandora_server_statistics pandora_self_monitoring + pandora_sample_agent pandora_process_policy_queue subst_alert_macros subst_column_macros @@ -5114,7 +5118,7 @@ sub pandora_self_monitoring ($$) { my $timezone_offset = 0; # PENDING (TODO) ! my $utimestamp = time (); my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime()); - + logger($pa_config, "Self monitoring activated.", 1); my $xml_output = ""; $xml_output = "{'version'} . "' agent_name='".$pa_config->{'servername'} . "' agent_alias='".$pa_config->{'servername'} . "' interval='".$pa_config->{"self_monitoring_interval"}."' timestamp='".$timestamp."' >"; @@ -5235,6 +5239,87 @@ sub pandora_self_monitoring ($$) { print XMLFILE $xml_output; close (XMLFILE); } +########################################################################## +=head2 C<< xml_module_template (I<$module_name>, I<$module_type>, I<$module_data>) >> + +Module template for sample agent + +=cut +########################################################################## +sub xml_module_template ($$$) { + my ($module_name, $module_type, $module_data) = @_; + my $output = "\n"; + + $module_name = "" if $module_name =~ /[\s+.]+/; + $module_data = "" if $module_data =~ /[\s+.]+/; + + $output .= "\t".$module_name."\n"; + $output .= "\t".$module_type."\n"; + $output .= "\t".$module_data."\n"; + $output .= "\n"; + + return $output; +} +########################################################################## +=head2 C<< pandora_sample_agent (I<$pa_config>) >> + +Pandora agent for make sample data + +=cut +########################################################################## +sub pandora_sample_agent ($) { + + my ($pa_config) = @_; + logger($pa_config, "Sample agent activated.", 1); + my $utimestamp = time (); + my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime()); + + # First line + my $xml_output = "{'version'} . "' agent_name='".$pa_config->{'servername'} . "' agent_alias='".$pa_config->{'servername'} . "' interval='".$pa_config->{"self_monitoring_interval"}."' timestamp='".$timestamp."' >"; + # Boolean_True -> generic_proc siempre a 1 + $xml_output .= xml_module_template ("Siempre a uno", "generic_proc","1"); + # Boolean_MostlyTrue -> generic_proc un 80% de veces a 1 + my $sample_boolean_mostly_true = 1; + $sample_boolean_mostly_true = 0 if rand(9) > 7; + $xml_output .= xml_module_template ("Boolean mostly true", "generic_proc",$sample_boolean_mostly_true); + # Boolean_MostlyFalse -> generic_proc un 80% de veces a 1 + my $sample_boolean_mostly_false = 0; + $sample_boolean_mostly_false = 1 if rand(9) > 7; + $xml_output .= xml_module_template ("Siempre a uno", "generic_proc", $sample_boolean_mostly_false); + # Boolean_False -> generic_proc siempre a 0 + $xml_output .= xml_module_template ("Siempre_false", "generic_proc","0"); + # Data_Series_Scatter -> Valores random de 0 a 100 + $xml_output .= xml_module_template ("Random_integer_values", "generic_data",int(rand(100))); + # Data_Series_Curve -> Valores en curvas senoidales o similares de 0 a 100 + # (utilizar funcion matemática que use timestamp actual, para no tener que + # guardar valores anteriores) + my $b = 1; + my $sample_serie_curve = 1 + cos(deg2rad($b)); + $b = $b + rand(20)/10; + $b = 0 if ($b > 180); + $sample_serie_curve = $sample_serie_curve * $b * 10; + $sample_serie_curve =~ s/\,/\./g; + $xml_output .= xml_module_template ("Random text", "generic_string", $sample_serie_curve); + # Text -> generic_string > Con una cadena de texto aleatorio de + # 10 caracteres, idealmente con espacios y caracteres ascii + # basicos (letras). + # String with 10 random characters + my $sample_random_text = ""; + my @characters = ('a'..'z','A'..'Z'); + for (1...10){ + $sample_random_text .= $characters[int(rand(@characters))]; + } + $xml_output .= xml_module_template ("Random text", "generic_string", $sample_random_text); + # End of xml + $xml_output .= ""; + # File definition + my $filename = $pa_config->{"incomingdir"}."/".$pa_config->{'servername'}.".sample.".$utimestamp.".data"; + # Opening, Writing and closing of XML + open (my $xmlfile, ">", $filename) or die "[FATAL] Could not open sample XML file for deploying monitorization at '$filename'"; + print $xmlfile $xml_output; + close ($xmlfile); + +} ########################################################################## =head2 C<< set_master (I<$pa_config>, I<$dbh>) >>