enh(email/alert): do not retrieve service graph if no credentials (#4663)

Co-authored-by: Colin Gagnaire <gagnaire.colin@gmail.com>
This commit is contained in:
qgarnier 2023-11-07 11:42:07 +01:00 committed by GitHub
parent 1f85378b5c
commit c9021513d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -129,13 +129,12 @@ sub new {
'service-attempts:s' => { name => 'service_attempts'}, 'service-attempts:s' => { name => 'service_attempts'},
'max-service-attempts:s' => { name => 'max_service_attempts'}, 'max-service-attempts:s' => { name => 'max_service_attempts'},
'service-duration:s' => { name => 'service_duration' }, 'service-duration:s' => { name => 'service_duration' },
'centreon-url:s' => { name => 'centreon_url' },
'centreon-user:s' => { name => 'centreon_user' }, 'centreon-user:s' => { name => 'centreon_user' },
'centreon-token:s' => { name => 'centreon_tooken' }, 'centreon-token:s' => { name => 'centreon_token' },
'date:s' => { name => 'date' }, 'date:s' => { name => 'date' },
'notif-author:s' => { name => 'notif_author'}, 'notif-author:s' => { name => 'notif_author'},
'notif-comment:s' => { name => 'notif_comment' }, 'notif-comment:s' => { name => 'notif_comment' },
'centreon-url:s' => { name => 'centreon_url' },
'centreon-token:s' => { name => 'centreon_token' },
'type:s' => { name => 'type' }, 'type:s' => { name => 'type' },
'timeout:s' => { name => 'timeout', default => 10 } 'timeout:s' => { name => 'timeout', default => 10 }
}); });
@ -568,23 +567,26 @@ sub service_message {
} }
} }
my $content = $self->{http}->request( my $graph_html;
hostname => '', if ($self->{option_results}->{centreon_user} && $self->{option_results}->{centreon_user} ne ''
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}, && $self->{option_results}->{centreon_token} && $self->{option_results}->{centreon_token} ne '') {
timeout => $self->{option_results}->{timeout}, my $content = $self->{http}->request(
unknown_status => '', hostname => '',
warning_status => '', 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},
critical_status => '' timeout => $self->{option_results}->{timeout},
); unknown_status => '',
warning_status => '',
my $img; critical_status => ''
if ($self->{http}->get_code() !~ /200/ || $content =~ /^OK/) { );
$img = '<h2 style="font-family: CoconPro-BoldCond, Open Sans, Verdana, sans-serif; margin:0; font-size:20px; padding-left:5%;">No graph</h2>';
} elsif ($content =~ /Access denied|Resource not found|Invalid token/) { if ($self->{http}->get_code() !~ /200/ || $content =~ /^OK/) {
$img = '<h2 style="font-family: CoconPro-BoldCond, Open Sans, Verdana, sans-serif; margin:0; font-size:20px; padding-left:5%;">Cannot retrieve graph: ' . $content . '</h2>'; $graph_html = '<h2 style="font-family: CoconPro-BoldCond, Open Sans, Verdana, sans-serif; margin:0; font-size:20px; padding-left:5%;">No graph</h2>';
} else { } elsif ($content =~ /Access denied|Resource not found|Invalid token/) {
$self->{payload_attachment}->{png} = $content; $graph_html = '<h2 style="font-family: CoconPro-BoldCond, Open Sans, Verdana, sans-serif; margin:0; font-size:20px; padding-left:5%;">Cannot retrieve graph: ' . $content . '</h2>';
$img = '<img src="cid:' . $self->{option_results}->{host_name} . '_' . $self->{option_results}->{service_description} . "\" style=\"display:block; width:98%; height:auto;margin:0 10px 0 10px;\">\n"; } else {
$self->{payload_attachment}->{graph_png} = $content;
$graph_html = '<img src="cid:' . $self->{option_results}->{host_name} . '_' . $self->{option_results}->{service_description} . "\" style=\"display:block; width:98%; height:auto;margin:0 10px 0 10px;\">\n";
}
} }
my $details = { my $details = {
@ -835,36 +837,42 @@ sub service_message {
if (defined($author_html) && $author_html ne '') { if (defined($author_html) && $author_html ne '') {
$self->{payload_attachment}->{html_message} .= ' $self->{payload_attachment}->{html_message} .= '
<td style="font-size:9px;vertical-align:top;">&nbsp;</td> <td style="font-size:9px;vertical-align:top;">&nbsp;</td>
<tbody> <tbody>
<tr> <tr>
<td width="98%" style="vertical-align:middle;font-size:14px;width:98%;margin:0 10px 0 10px;">'. <td width="98%" style="vertical-align:middle;font-size:14px;width:98%;margin:0 10px 0 10px;">'.
$author_html. ' $author_html. '
</td> </td>
</tr> </tr>
</tbody>'; </tbody>';
} }
if (defined($comment_html) && $comment_html ne '') { if (defined($comment_html) && $comment_html ne '') {
$self->{payload_attachment}->{html_message} .= ' $self->{payload_attachment}->{html_message} .= '
<td style="font-size:9px;vertical-align:top;">&nbsp;</td> <td style="font-size:9px;vertical-align:top;">&nbsp;</td>
<tbody> <tbody>
<tr> <tr>
<td width="98%" style="vertical-align:middle;font-size:14px;width:98%;margin:0 10px 0 10px;">'. <td width="98%" style="vertical-align:middle;font-size:14px;width:98%;margin:0 10px 0 10px;">'.
$comment_html. ' $comment_html. '
</td> </td>
</tr> </tr>
</tbody>'; </tbody>';
} }
$self->{payload_attachment}->{html_message} .= '
if (defined($graph_html) && $graph_html ne '') {
$self->{payload_attachment}->{html_message} .= '
<tbody><tr><td style="font-size:9px;vertical-align:top;">&nbsp;</td></tr></tbody> <tbody><tr><td style="font-size:9px;vertical-align:top;">&nbsp;</td></tr></tbody>
<tbody> <tbody>
<tr> <tr>
<td width="98%" style="vertical-align:middle;font-size:14px;width:98%;margin:0 10px 0 10px;"> <td width="98%" style="vertical-align:middle;font-size:14px;width:98%;margin:0 10px 0 10px;">
<h4 style="font-family: CoconPro-BoldCond, Open Sans, Verdana, sans-serif; margin:0; font-size:15px; color:#b0b0b0; padding-left:3%;text-decoration:underline;">Service Graph:</h4> <h4 style="font-family: CoconPro-BoldCond, Open Sans, Verdana, sans-serif; margin:0; font-size:15px; color:#b0b0b0; padding-left:3%;text-decoration:underline;">Service Graph:</h4>
'. $img . ' '. $graph_html . '
</td> </td>
</tr> </tr>
</tbody> </tbody>';
}
$self->{payload_attachment}->{html_message} .= '
<tbody><tr><td style="font-size:9px;vertical-align:top;">&nbsp;</td> <tbody><tr><td style="font-size:9px;vertical-align:top;">&nbsp;</td>
</tr></tbody> </tr></tbody>
<tbody><tr><td style="font-size:16px;vertical-align:top;">&nbsp;</td> <tbody><tr><td style="font-size:16px;vertical-align:top;">&nbsp;</td>
@ -950,8 +958,8 @@ sub run {
my $email; my $email;
if (defined($self->{payload_attachment}->{png}) && $self->{payload_attachment}->{png} ne '') { if (defined($self->{payload_attachment}->{graph_png}) && $self->{payload_attachment}->{graph_png} ne '') {
my $img_cid = $self->{option_results}->{host_name} . '_' . $self->{option_results}->{service_description}; my $graph_png_cid = $self->{option_results}->{host_name} . '_' . $self->{option_results}->{service_description};
$email = Email::MIME->create( $email = Email::MIME->create(
header_str => [ header_str => [
@ -969,7 +977,7 @@ sub run {
), ),
Email::MIME->create( Email::MIME->create(
header_str => [ header_str => [
'Content-ID' => "<$img_cid>" 'Content-ID' => "<$graph_png_cid>"
], ],
attributes => { attributes => {
content_type => 'image/png', content_type => 'image/png',
@ -977,7 +985,7 @@ sub run {
encoding => 'base64', encoding => 'base64',
name => $self->{option_results}->{host_name} . ' - ' . $self->{option_results}->{service_description} . '.png' 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> =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> =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> =item B<--date>