From c9021513d1328ff94eb1aa20c4f74a39b810ba7b Mon Sep 17 00:00:00 2001 From: qgarnier Date: Tue, 7 Nov 2023 11:42:07 +0100 Subject: [PATCH] enh(email/alert): do not retrieve service graph if no credentials (#4663) Co-authored-by: Colin Gagnaire --- src/notification/email/mode/alert.pm | 111 +++++++++++++++------------ 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/src/notification/email/mode/alert.pm b/src/notification/email/mode/alert.pm index f54ed6ef2..1aba979c8 100644 --- a/src/notification/email/mode/alert.pm +++ b/src/notification/email/mode/alert.pm @@ -129,13 +129,12 @@ sub new { 'service-attempts:s' => { name => 'service_attempts'}, 'max-service-attempts:s' => { name => 'max_service_attempts'}, 'service-duration:s' => { name => 'service_duration' }, + 'centreon-url:s' => { name => 'centreon_url' }, 'centreon-user:s' => { name => 'centreon_user' }, - 'centreon-token:s' => { name => 'centreon_tooken' }, + 'centreon-token:s' => { name => 'centreon_token' }, 'date:s' => { name => 'date' }, 'notif-author:s' => { name => 'notif_author'}, 'notif-comment:s' => { name => 'notif_comment' }, - 'centreon-url:s' => { name => 'centreon_url' }, - 'centreon-token:s' => { name => 'centreon_token' }, 'type:s' => { name => 'type' }, 'timeout:s' => { name => 'timeout', default => 10 } }); @@ -568,23 +567,26 @@ sub service_message { } } - my $content = $self->{http}->request( - hostname => '', - full_url => $self->{option_results}->{centreon_url} . '/centreon/include/views/graphs/generateGraphs/generateImage.php?akey=' . $self->{option_results}->{centreon_token} . '&username=' . $self->{option_results}->{centreon_user} . '&hostname=' . $self->{option_results}->{host_name} . '&service='. $self->{option_results}->{service_description}, - timeout => $self->{option_results}->{timeout}, - unknown_status => '', - warning_status => '', - critical_status => '' - ); - - my $img; - if ($self->{http}->get_code() !~ /200/ || $content =~ /^OK/) { - $img = '

No graph

'; - } elsif ($content =~ /Access denied|Resource not found|Invalid token/) { - $img = '

Cannot retrieve graph: ' . $content . '

'; - } else { - $self->{payload_attachment}->{png} = $content; - $img = '\n"; + my $graph_html; + if ($self->{option_results}->{centreon_user} && $self->{option_results}->{centreon_user} ne '' + && $self->{option_results}->{centreon_token} && $self->{option_results}->{centreon_token} ne '') { + my $content = $self->{http}->request( + hostname => '', + full_url => $self->{option_results}->{centreon_url} . '/centreon/include/views/graphs/generateGraphs/generateImage.php?akey=' . $self->{option_results}->{centreon_token} . '&username=' . $self->{option_results}->{centreon_user} . '&hostname=' . $self->{option_results}->{host_name} . '&service='. $self->{option_results}->{service_description}, + timeout => $self->{option_results}->{timeout}, + unknown_status => '', + warning_status => '', + critical_status => '' + ); + + if ($self->{http}->get_code() !~ /200/ || $content =~ /^OK/) { + $graph_html = '

No graph

'; + } elsif ($content =~ /Access denied|Resource not found|Invalid token/) { + $graph_html = '

Cannot retrieve graph: ' . $content . '

'; + } else { + $self->{payload_attachment}->{graph_png} = $content; + $graph_html = '\n"; + } } my $details = { @@ -835,36 +837,42 @@ sub service_message { if (defined($author_html) && $author_html ne '') { $self->{payload_attachment}->{html_message} .= ' -   - - - '. - $author_html. ' - - - '; +   + + + '. + $author_html. ' + + + '; } + if (defined($comment_html) && $comment_html ne '') { $self->{payload_attachment}->{html_message} .= ' -   - - - '. - $comment_html. ' - - - '; - } - $self->{payload_attachment}->{html_message} .= ' +   + + + '. + $comment_html. ' + + + '; + } + + if (defined($graph_html) && $graph_html ne '') { + $self->{payload_attachment}->{html_message} .= '   - - -

Service Graph:

- '. $img . ' - - - + + +

Service Graph:

+ '. $graph_html . ' + + + '; + } + + $self->{payload_attachment}->{html_message} .= '     @@ -950,8 +958,8 @@ sub run { my $email; - if (defined($self->{payload_attachment}->{png}) && $self->{payload_attachment}->{png} ne '') { - my $img_cid = $self->{option_results}->{host_name} . '_' . $self->{option_results}->{service_description}; + if (defined($self->{payload_attachment}->{graph_png}) && $self->{payload_attachment}->{graph_png} ne '') { + my $graph_png_cid = $self->{option_results}->{host_name} . '_' . $self->{option_results}->{service_description}; $email = Email::MIME->create( header_str => [ @@ -969,7 +977,7 @@ sub run { ), Email::MIME->create( header_str => [ - 'Content-ID' => "<$img_cid>" + 'Content-ID' => "<$graph_png_cid>" ], attributes => { content_type => 'image/png', @@ -977,7 +985,7 @@ sub run { encoding => 'base64', name => $self->{option_results}->{host_name} . ' - ' . $self->{option_results}->{service_description} . '.png' }, - body => $self->{payload_attachment}->{png} + body => $self->{payload_attachment}->{graph_png} ) ] ); @@ -1139,11 +1147,12 @@ Duration of the service status. =item B<--centreon-user> -Username for the Centreon web interface. +Username for the Centreon web interface to retrieve +service's graph (leave empty to not retrieve and display graph). =item B<--centreon-token> -Token for the Centreon web interface (autologin). +Autologin token for the Centreon web interface (if --centreon-user is defined). =item B<--date>