| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package notification::sirportly::mode::ticket; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::http; |
| 28 |
use centreon::plugins::statefile; |
| 29 |
use JSON::XS; |
| 30 |
|
| 31 |
sub new { |
| 32 |
my ($class, %options) = @_; |
| 33 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 34 |
bless $self, $class; |
| 35 |
|
| 36 |
$options{options}->add_options(arguments => { |
| 37 |
'hostname:s' => { name => 'hostname', default => 'api.sirportly.com' }, |
| 38 |
'port:s' => { name => 'port', default => 443 }, |
| 39 |
'proto:s' => { name => 'proto', default => 'https' }, |
| 40 |
'api-token:s' => { name => 'api_token' }, |
| 41 |
'api-secret:s' => { name => 'api_secret' }, |
| 42 |
'cache' => { name => 'cache' }, |
| 43 |
'contact-name:s' => { name => 'contact_name', default => 'Centreon Notifications' }, |
| 44 |
'contact-type:s' => { name => 'contact_type', default => 'telephone' }, |
| 45 |
'contact-data:s' => { name => 'contact_data', default => '-' }, |
| 46 |
'brand:s' => { name => 'brand' }, |
| 47 |
'department:s' => { name => 'department' }, |
| 48 |
'submit-user:s' => { name => 'submit_user' }, |
| 49 |
'close-user:s' => { name => 'close_user' }, |
| 50 |
'submit-status:s' => { name => 'submit_status' }, |
| 51 |
'close-status:s' => { name => 'close_status' }, |
| 52 |
'priority-mapping:s' => { name => 'priority_mapping' }, |
| 53 |
'priority:s' => { name => 'priority' }, |
| 54 |
'item-id:s' => { name => 'item_id' }, |
| 55 |
'subject:s' => { name => 'subject' }, |
| 56 |
'message:s' => { name => 'message' }, |
| 57 |
'timeout:s' => { name => 'timeout' }, |
| 58 |
}); |
| 59 |
|
| 60 |
$self->{http} = centreon::plugins::http->new(%options); |
| 61 |
$self->{cache} = centreon::plugins::statefile->new(%options); |
| 62 |
|
| 63 |
return $self; |
| 64 |
} |
| 65 |
|
| 66 |
sub check_options { |
| 67 |
my ($self, %options) = @_; |
| 68 |
|
| 69 |
$self->SUPER::init(%options); |
| 70 |
foreach (('api_token', 'api_secret', 'brand', 'department', 'submit_status', |
| 71 |
'close_status', 'priority', 'item_id', 'subject', 'message')) { |
| 72 |
if (!defined($self->{option_results}->{$_})) { |
| 73 |
my $option = $_; |
| 74 |
$option =~ s/_/-/g; |
| 75 |
$self->{output}->add_option_msg(short_msg => "You need to set --$option option"); |
| 76 |
$self->{output}->option_exit(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$self->{http}->set_options(%{$self->{option_results}}); |
| 81 |
$self->{cache}->check_options(option_results => $self->{option_results}); |
| 82 |
} |
| 83 |
|
| 84 |
sub proceed { |
| 85 |
my ($self, %options) = @_; |
| 86 |
|
| 87 |
# Let's compute a priority mapping hash, with lc keys |
| 88 |
my %priority_mapping; |
| 89 |
if (defined($self->{option_results}->{priority_mapping})) { |
| 90 |
my $i = 0; |
| 91 |
%priority_mapping = map { $i++ % 2 ? $_ : lc } split /[:,]/, $self->{option_results}->{priority_mapping}; |
| 92 |
} |
| 93 |
|
| 94 |
# Let's compute a ticket tag to be able to find the opened ticket later on |
| 95 |
my $tag = $options{item_id}; |
| 96 |
if ($options{priority} =~ /^(UP|DOWN|UNREACHABLE)$/i) { |
| 97 |
$tag = "centreon-hst:$tag"; |
| 98 |
} else { |
| 99 |
$tag = "centreon-svc:$tag"; |
| 100 |
} |
| 101 |
|
| 102 |
# Is there an existing Sirportly ticket for this item ? |
| 103 |
my $post_param = [ |
| 104 |
"spql=SELECT tickets.reference,statuses.name FROM tickets WHERE ticket_tags.tag=\"$tag\" AND statuses.name!=\"$self->{option_results}->{close_status}\" ORDER BY tickets.submitted_at DESC LIMIT 1", |
| 105 |
]; |
| 106 |
my $response = $self->{http}->request(method => 'POST', url_path =>'/api/v2/tickets/spql', post_param => $post_param, warning_status => '', unknown_status => '', critical_status => ''); |
| 107 |
my $ticket_info; |
| 108 |
eval { |
| 109 |
$ticket_info = JSON::XS->new->decode($response)->{results}[0]; |
| 110 |
}; |
| 111 |
|
| 112 |
if ($options{priority} =~ /^(UP|OK)$/i) { # Close the ticket |
| 113 |
if (defined($ticket_info)) { |
| 114 |
$post_param = [ |
| 115 |
"ticket=$ticket_info->[0]", |
| 116 |
"contact=$self->{option_results}->{contact_name}", |
| 117 |
"message=$options{message}", |
| 118 |
]; |
| 119 |
$self->{http}->request(method => 'POST', url_path =>'/api/v2/tickets/post_update', post_param => $post_param, warning_status => '', unknown_status => '', critical_status => ''); |
| 120 |
if ($ticket_info->[1] eq $self->{option_results}->{submit_status}) { |
| 121 |
$post_param = [ |
| 122 |
"ticket=$ticket_info->[0]", |
| 123 |
"user=" . (defined($self->{option_results}->{close_user}) ? $self->{option_results}->{close_user} : ""), |
| 124 |
"status=$self->{option_results}->{close_status}", |
| 125 |
]; |
| 126 |
my $response = $self->{http}->request(method => 'POST', url_path =>'/api/v2/tickets/update', post_param => $post_param, warning_status => '', unknown_status => '', critical_status => ''); |
| 127 |
eval { |
| 128 |
JSON::XS->new->decode($response); |
| 129 |
}; |
| 130 |
if ($@) { # We only check we received a response from Sirportly, thus we don't take the risk to hold the queue/cache processing |
| 131 |
$ticket_info->[0] = "failed"; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} elsif (defined($ticket_info)) { # Update an existing ticket |
| 136 |
$post_param = [ |
| 137 |
"ticket=$ticket_info->[0]", |
| 138 |
"contact=$self->{option_results}->{contact_name}", |
| 139 |
"message=$options{message}", |
| 140 |
]; |
| 141 |
$self->{http}->request(method => 'POST', url_path =>'/api/v2/tickets/post_update', post_param => $post_param, warning_status => '', unknown_status => '', critical_status => ''); |
| 142 |
$post_param = [ |
| 143 |
"ticket=$ticket_info->[0]", |
| 144 |
"priority=" . (defined($priority_mapping{lc($options{priority})}) ? $priority_mapping{lc($options{priority})} : $options{priority}), |
| 145 |
]; |
| 146 |
my $response = $self->{http}->request(method => 'POST', url_path =>'/api/v2/tickets/update', post_param => $post_param, warning_status => '', unknown_status => '', critical_status => ''); |
| 147 |
eval { |
| 148 |
JSON::XS->new->decode($response); |
| 149 |
}; |
| 150 |
if ($@) { # We only check we received a response from Sirportly, thus we don't take the risk to hold the queue/cache processing |
| 151 |
$ticket_info->[0] = 'failed'; |
| 152 |
} |
| 153 |
} else { # Open a new ticket |
| 154 |
$post_param = [ |
| 155 |
"contact_name=$self->{option_results}->{contact_name}", |
| 156 |
"contact_method_type=$self->{option_results}->{contact_type}", |
| 157 |
"contact_method_data=$self->{option_results}->{contact_data}", |
| 158 |
"brand=$self->{option_results}->{brand}", |
| 159 |
"department=$self->{option_results}->{department}", |
| 160 |
"user=" . (defined($self->{option_results}->{submit_user}) ? $self->{option_results}->{submit_user} : ""), |
| 161 |
"status=$self->{option_results}->{submit_status}", |
| 162 |
"priority=" . (defined($priority_mapping{lc($options{priority})}) ? $priority_mapping{lc($options{priority})} : $options{priority}), |
| 163 |
"tag_list=$tag", |
| 164 |
"subject=$options{subject}", |
| 165 |
"message=$options{message}", |
| 166 |
]; |
| 167 |
my $response = $self->{http}->request(method => 'POST', url_path =>'/api/v2/tickets/submit', post_param => $post_param, warning_status => '', unknown_status => '', critical_status => ''); |
| 168 |
my $decoded; |
| 169 |
eval { |
| 170 |
$decoded = JSON::XS->new->decode($response); |
| 171 |
}; |
| 172 |
if ($@ || !defined($decoded->{reference})) { # Here we check Sirportly correctly received the new ticket, as it's the most important step |
| 173 |
$ticket_info->[0] = "failed"; |
| 174 |
} else { |
| 175 |
$ticket_info->[0] = $decoded->{reference}; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
# Return proceeded ticket |
| 180 |
return $ticket_info->[0]; |
| 181 |
} |
| 182 |
|
| 183 |
sub run { |
| 184 |
my ($self, %options) = @_; |
| 185 |
|
| 186 |
# Authentication headers |
| 187 |
$self->{http}->add_header(key => 'X-Auth-Token', value => $self->{option_results}->{api_token}); |
| 188 |
$self->{http}->add_header(key => 'X-Auth-Secret', value => $self->{option_results}->{api_secret}); |
| 189 |
|
| 190 |
# Load cache |
| 191 |
$self->{cache}->read(statefile => 'sirportly_api_' . $self->{option_results}->{api_token}); |
| 192 |
my $cache; |
| 193 |
if (defined($self->{option_results}->{cache})) { |
| 194 |
$cache = $self->{cache}->get(name => 'cache'); |
| 195 |
} else { |
| 196 |
$cache = {}; |
| 197 |
} |
| 198 |
|
| 199 |
# Add current item to cache |
| 200 |
my $cache_index = time(); |
| 201 |
$cache->{$cache_index}->{item_id} = $self->{option_results}->{item_id}; |
| 202 |
$cache->{$cache_index}->{priority} = $self->{option_results}->{priority}; |
| 203 |
$cache->{$cache_index}->{subject} = $self->{option_results}->{subject}; |
| 204 |
$cache->{$cache_index}->{message} = $self->{option_results}->{message}; |
| 205 |
$self->{cache}->write(data => {cache => $cache}); |
| 206 |
|
| 207 |
# Proceed cached notifications |
| 208 |
my $cache_size = keys %{$cache}; |
| 209 |
my $cache_done = 0; |
| 210 |
foreach $cache_index (sort keys %{$cache}) { |
| 211 |
my $ticket_reference = $self->proceed( |
| 212 |
priority => $cache->{$cache_index}->{priority}, |
| 213 |
item_id => $cache->{$cache_index}->{item_id}, |
| 214 |
subject => $cache->{$cache_index}->{subject}, |
| 215 |
message => $cache->{$cache_index}->{message}, |
| 216 |
); |
| 217 |
if (!defined($ticket_reference) || $ticket_reference ne 'failed') { |
| 218 |
delete($cache->{$cache_index}); |
| 219 |
$self->{cache}->write(data => {cache => $cache}); |
| 220 |
$cache_done++; |
| 221 |
} else { |
| 222 |
last; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
# Exit |
| 227 |
$self->{output}->output_add( |
| 228 |
severity => ($cache_size == $cache_done) ? 'OK' : 'CRITICAL', |
| 229 |
short_msg => "Proceeded " . $cache_done . "/" . $cache_size . " notifications" |
| 230 |
); |
| 231 |
$self->{output}->display(force_ignore_perfdata => 1); |
| 232 |
$self->{output}->exit(); |
| 233 |
} |
| 234 |
|
| 235 |
1; |
| 236 |
|
| 237 |
__END__ |
| 238 |
|
| 239 |
=head1 MODE |
| 240 |
|
| 241 |
Open tickets via Sirportly API (https://sirportly.com/docs/api-specification/) |
| 242 |
|
| 243 |
=over 6 |
| 244 |
|
| 245 |
=item B<--hostname> |
| 246 |
|
| 247 |
Hostname of the OVH SMS API (default: 'api.sirportly.com'). |
| 248 |
|
| 249 |
=item B<--port> |
| 250 |
|
| 251 |
Port used by API (default: '443'). |
| 252 |
|
| 253 |
=item B<--proto> |
| 254 |
|
| 255 |
Specify https if needed (default: 'https'). |
| 256 |
|
| 257 |
=item B<--api-token> |
| 258 |
|
| 259 |
Specify the API authentication token. |
| 260 |
|
| 261 |
=item B<--api-secret> |
| 262 |
|
| 263 |
Specify the API authentication secret. |
| 264 |
|
| 265 |
=item B<--cache> |
| 266 |
|
| 267 |
Cache notifications not correctly delivered, to retry at next run. |
| 268 |
|
| 269 |
=item B<--contact-name> |
| 270 |
|
| 271 |
Specify the contact name for this ticket (default: 'Centreon Notifications'). |
| 272 |
|
| 273 |
=item B<--contact-type> |
| 274 |
|
| 275 |
Specify the contact type (email or telephone) of the contact (default: 'telephone'). |
| 276 |
|
| 277 |
=item B<--contact-data> |
| 278 |
|
| 279 |
Specify the email address or telephone number of the contact (default: '-'). |
| 280 |
|
| 281 |
=item B<--brand> |
| 282 |
|
| 283 |
Specify the brand for this ticket. |
| 284 |
|
| 285 |
=item B<--department> |
| 286 |
|
| 287 |
Specify the department for this ticket. |
| 288 |
|
| 289 |
=item B<--submit-user> |
| 290 |
|
| 291 |
Specify the user to assign the ticket to when submitting the ticket. |
| 292 |
|
| 293 |
=item B<--close-user> |
| 294 |
|
| 295 |
Specify the user to assign the ticket to when closing the ticket. |
| 296 |
|
| 297 |
=item B<--submit-status> |
| 298 |
|
| 299 |
Specify the Sirportly status to be used when submitting the ticket. |
| 300 |
|
| 301 |
=item B<--close-status> |
| 302 |
|
| 303 |
Specify the Sirportly status to be used when closing the ticket (only if still in submit status). |
| 304 |
|
| 305 |
=item B<--priority-mapping> |
| 306 |
|
| 307 |
Map the Centreon priorities to Sirportly ones, comma separated (syntax: (down|unreachable|warning|critical|unknown):priority). |
| 308 |
|
| 309 |
=item B<--priority> |
| 310 |
|
| 311 |
Specify the priority for this ticket. |
| 312 |
|
| 313 |
=item B<--item-id> |
| 314 |
|
| 315 |
Specify the host or service ID for this ticket. |
| 316 |
|
| 317 |
=item B<--subject> |
| 318 |
|
| 319 |
Specify the subject for this ticket. |
| 320 |
|
| 321 |
=item B<--message> |
| 322 |
|
| 323 |
Specify the message for this ticket. |
| 324 |
|
| 325 |
=item B<--timeout> |
| 326 |
|
| 327 |
Threshold for HTTP timeout |
| 328 |
|
| 329 |
=back |
| 330 |
|
| 331 |
=cut |