diff --git a/pandora_server/conf/pandora_server.conf.new b/pandora_server/conf/pandora_server.conf.new index 42834d8f2d..b106be1080 100644 --- a/pandora_server/conf/pandora_server.conf.new +++ b/pandora_server/conf/pandora_server.conf.new @@ -482,3 +482,17 @@ claim_back_snmp_modules 1 # interval will become normal. Set to 0 to disable. async_recovery 1 +# Console API credentials. +# Required for some features like the module graphs macros. + +# console_api_url: Api URL (http://localhost/pandora_console/include/api.php by default) +console_api_url http://localhost/pandora_console/include/api.php + +# console_api_pass: Api pass +# console_api_pass 1234 + +# console_user: Console user name (admin by default) +console_user admin + +# console_pass: Console password (pandora by default) +console_pass pandora diff --git a/pandora_server/conf/pandora_server.conf.windows b/pandora_server/conf/pandora_server.conf.windows index 2c1023ab8c..b43edc214e 100644 --- a/pandora_server/conf/pandora_server.conf.windows +++ b/pandora_server/conf/pandora_server.conf.windows @@ -289,3 +289,17 @@ restart_delay 60 # interval will become normal. Set to 0 to disable. async_recovery 1 +# Console API credentials. +# Required for some features like the module graphs macros. + +# console_api_url: Api URL (http://localhost/pandora_console/include/api.php by default) +console_api_url http://localhost/pandora_console/include/api.php + +# console_api_pass: Api pass +# console_api_pass 1234 + +# console_user: Console user name (admin by default) +console_user admin + +# console_pass: Console password (pandora by default) +console_pass pandora diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index c8231eda10..b848470d56 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -343,6 +343,12 @@ sub pandora_load_config { # Auto-recovery of asynchronous modules. $pa_config->{"async_recovery"} = 1; # 5.1SP1 + # Console API connection + $pa_config->{"console_api_url"} = 'http://localhost/pandora_console/include/api.php'; # 6.0 + $pa_config->{"console_api_pass"} = ''; # 6.0 + $pa_config->{"console_user"} = 'admin'; # 6.0 + $pa_config->{"console_pass"} = 'pandora'; # 6.0 + # ------------------------------------------------------------------------- # This values are not stored in .conf files. # This values should be stored in database, not in .conf files! @@ -814,6 +820,18 @@ sub pandora_load_config { elsif ($parametro =~ m/^async_recovery\s+([0-1])/i) { $pa_config->{'async_recovery'}= safe_input($1); } + elsif ($parametro =~ m/^console_api_url\s(.*)/i) { + $pa_config->{'console_api_url'}= safe_input($1); + } + elsif ($parametro =~ m/^console_api_pass\s(.*)/i) { + $pa_config->{'console_api_pass'}= safe_input($1); + } + elsif ($parametro =~ m/^console_user\s(.*)/i) { + $pa_config->{'console_user'}= safe_input($1); + } + elsif ($parametro =~ m/^console_pass\s(.*)/i) { + $pa_config->{'console_pass'}= safe_input($1); + } } # end of loop for parameter # # Set to RDBMS' standard port diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 432b09cfee..a8160fcefb 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -132,6 +132,17 @@ use PandoraFMS::GIS qw(distance_moved); # For Reverse Geocoding use LWP::Simple; +# For api calls +use IO::Socket::INET6; +use LWP::UserAgent; +use HTTP::Request::Common; + +# For IPv6 support in Net::HTTP. +BEGIN { + $Net::HTTP::SOCKET_CLASS = 'IO::Socket::INET6'; + require Net::HTTP; +} + require Exporter; our @ISA = ("Exporter"); @@ -928,18 +939,107 @@ sub pandora_execute_action ($$$$$$$$$;$) { # Email } elsif ($clean_name eq "eMail") { + # Address $field1 = subst_alert_macros ($field1, \%macros, $pa_config, $dbh, $agent, $module); + # Subject $field2 = subst_alert_macros ($field2, \%macros, $pa_config, $dbh, $agent, $module); + # Message $field3 = subst_alert_macros ($field3, \%macros, $pa_config, $dbh, $agent, $module); + + # Check for _module_graph_Xh_ macros + my $module_graph_list = {}; + my $macro_regexp = "_modulegraph_(\\d+)h_"; + + # API connection + my $ua = new LWP::UserAgent; + my $url ||= $pa_config->{"console_api_url"}; + + my $params = {}; + $params->{"apipass"} ||= $pa_config->{"console_api_pass"}; + $params->{"user"} ||= $pa_config->{"console_user"}; + $params->{"pass"} ||= $pa_config->{"console_pass"}; + $params->{"op"} = "get"; + $params->{"op2"} = "module_graph"; + $params->{"id"} = $module->{'id_agente_modulo'}; + + my $subst_func = sub { + my $hours = shift; + my $period = $hours * 3600; # Hours to seconds + $params->{"other"} = $period; + + my $cid = 'module_graph_' . $hours . 'h'; + + if (! exists($module_graph_list->{$cid}) && defined $url) { + # Get the module graph image in base 64 + my $response = $ua->post($url, $params); + + if ($response->is_success) { + $module_graph_list->{$cid} = $response->decoded_content(); + + return ''; + } + } + + return ''; + }; + + # Macro data may contain HTML entities + eval { + no warnings; + local $SIG{__DIE__}; + $field3 =~ s/$macro_regexp/$subst_func->($1)/ige; + }; + + # Default content type + my $content_type = 'text/html; charset="iso-8859-1"'; + + # Check if message has non-ascii chars. + # non-ascii chars should be encoded in UTF-8. + if ($field3 =~ /[^[:ascii:]]/o) { + $field3 = encode("UTF-8", $field3); + $content_type = 'text/html; charset="UTF-8"'; + } + + # Build the mail with attached content + if (keys(%{$module_graph_list}) > 0) { + my $boundary = "====" . time() . "===="; + my $html_content_type = $content_type; + $content_type = 'multipart/related; boundary="'.$boundary.'"'; + $boundary = "--" . $boundary; + + $field3 = $boundary . "\n" + . "Content-Type: " . $html_content_type . "\n" + . "Content-Transfer-Encoding: quoted-printable\n\n" + . $field3 . "\n"; + + + foreach my $cid (keys %{$module_graph_list}) { + my $filename = $cid . ".png"; + + $field3 .= $boundary . "\n" + . "Content-Type: image/png; name=\"" . $filename . "\"\n" + . "Content-Disposition: inline; filename=\"" . $filename . "\"\n" + . "Content-Transfer-Encoding: base64\n" + . "Content-ID: <" . $cid . ">\n" + . "Content-Location: " . $filename . "\n\n" + . $module_graph_list->{$cid} . "\n"; + + delete $module_graph_list->{$cid}; + } + undef %{$module_graph_list}; + + $field3 .= $boundary . "--\n"; + } + if ($pa_config->{"mail_in_separate"} != 0){ foreach my $address (split (',', $field1)) { # Remove blanks $address =~ s/ +//g; - pandora_sendmail ($pa_config, $address, $field2, $field3); + pandora_sendmail ($pa_config, $address, $field2, $field3, $content_type); } } else { - pandora_sendmail ($pa_config, $field1, $field2, $field3); + pandora_sendmail ($pa_config, $field1, $field2, $field3, $content_type); } # Pandora FMS Event diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 8534af25c6..1ad2744b99 100644 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -325,6 +325,7 @@ sub pandora_daemonize { # param2 - Destination email addres # param3 - Email subject # param4 - Email Message body +# param4 - Email content type ######################################################################## sub pandora_sendmail { @@ -333,9 +334,14 @@ sub pandora_sendmail { my $to_address = $_[1]; my $subject = $_[2]; my $message = $_[3]; + my $content_type = $_[4]; $subject = decode_entities ($subject); - $message = decode_entities ($message); + + # If content type is defined, the message will be custom + if (! defined($content_type)) { + $message = decode_entities ($message); + } my %mail = ( To => $to_address, Message => $message, @@ -346,9 +352,13 @@ sub pandora_sendmail { From => $pa_config->{"mta_from"}, ); + if (defined($content_type)) { + $mail{'Content-Type'} = $content_type; + } + # Check if message has non-ascii chars. # non-ascii chars should be encoded in UTF-8. - if ($message =~ /[^[:ascii:]]/o) { + if ($message =~ /[^[:ascii:]]/o && !defined($content_type)) { $mail{Message} = encode("UTF-8", $mail{Message}); $mail{'Content-Type'} = 'text/plain; charset="UTF-8"'; }