diff --git a/hardware/devices/pexip/infinity/managementapi/custom/api.pm b/hardware/devices/pexip/infinity/managementapi/custom/api.pm new file mode 100644 index 000000000..d808d46cb --- /dev/null +++ b/hardware/devices/pexip/infinity/managementapi/custom/api.pm @@ -0,0 +1,224 @@ +# +# Copyright 2020 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::devices::pexip::infinity::managementapi::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use JSON::XS; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => { + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'timeout:s' => { name => 'timeout' }, + 'unknown-http-status:s' => { name => 'unknown_http_status' }, + 'warning-http-status:s' => { name => 'warning_http_status' }, + 'critical-http-status:s' => { name => 'critical_http_status' } + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{http} = centreon::plugins::http->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults {} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; + $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; + $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; + $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; + + if ($self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + if ($self->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if ($self->{api_password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option."); + $self->{output}->option_exit(); + } + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{credentials} = 1; + $self->{option_results}->{basic} = 1; + $self->{option_results}->{username} = $self->{api_username}; + $self->{option_results}->{password} = $self->{api_password}; +} + +sub settings { + my ($self, %options) = @_; + + return if (defined($self->{settings_done})); + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->add_header(key => 'Content-Type', value => 'application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + $self->{settings_done} = 1; +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub get_port { + my ($self, %options) = @_; + + return $self->{port}; +} + +sub request_api { + my ($self, %options) = @_; + + $self->settings(); + + my $results = []; + my ($limit, $offset) = (1000, 0); + my $get_param = defined($options{get_param}) ? $options{get_param} : []; + + while (1) { + my $content = $self->{http}->request( + url_path => $options{endpoint}, + get_param => ['limit=' . $limit, 'offset=' . $offset, @$get_param], + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status} + ); + + if (!defined($content) || $content eq '') { + $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($content); + }; + if ($@ || !defined($decoded->{objects})) { + $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); + $self->{output}->option_exit(); + } + + push @$results, @{$decoded->{objects}}; + if (($limit + $offset) > $decoded->{meta}->{total_count}) { + last; + } + + $offset += $limit; + } + + return $results; +} + +1; + +__END__ + +=head1 NAME + +Pexip Infinity management API + +=head1 REST API OPTIONS + +Pexip Infinity management API + +=over 8 + +=item B<--hostname> + +Pexip manager hostname. + +=item B<--port> + +Port used (Default: 443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +API username. + +=item B<--api-password> + +API password. + +=item B<--timeout> + +Set timeout in seconds (Default: 10). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/hardware/devices/pexip/infinity/managementapi/mode/alarms.pm b/hardware/devices/pexip/infinity/managementapi/mode/alarms.pm new file mode 100644 index 000000000..c26277872 --- /dev/null +++ b/hardware/devices/pexip/infinity/managementapi/mode/alarms.pm @@ -0,0 +1,171 @@ +# +# Copyright 2018 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::devices::pexip::infinity::managementapi::mode::alarms; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::misc; +use centreon::plugins::statefile; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf( + 'alarm [level: %s] [name: %s] [details: %s] %s', + $self->{result_values}->{level}, + $self->{result_values}->{name}, + $self->{result_values}->{details}, + centreon::plugins::misc::change_seconds(value => $self->{result_values}->{opened}) + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { nlabel => 'alerts.problems.current.count', min => 0 }, + group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ] + } + ]; + + $self->{maps_counters}->{alarm} = [ + { + label => 'status', + type => 2, + warning_default => '%{level} =~ /warning|minor/i', + critical_default => '%{level} =~ /critical|major|error/i', + set => { + key_values => [ { name => 'name' }, { name => 'level' }, { name => 'details' }, { name => 'opened' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' }, + 'memory' => { name => 'memory' } + }); + + centreon::plugins::misc::mymodule_load( + output => $self->{output}, module => 'Date::Parse', + error_msg => "Cannot load module 'Date::Parse'." + ); + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (defined($self->{option_results}->{memory})) { + $self->{statefile_cache}->check_options(%options); + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{alarms}->{global} = { alarm => {} }; + my $results = $options{custom}->request_api(endpoint => '/api/admin/status/v1/alarm/'); + + my $last_time; + if (defined($self->{option_results}->{memory})) { + $self->{statefile_cache}->read(statefile => 'cache_pexip_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port(). '_' . $self->{mode}); + $last_time = $self->{statefile_cache}->get(name => 'last_time'); + } + + my ($i, $current_time) = (1, time()); + foreach my $alarm (@$results) { + my $create_time = Date::Parse::str2time($alarm->{time_raised}, 'UTC'); + if (!defined($create_time)) { + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => "Can't Parse date '" . $alarm->{time_raised} . "'" + ); + next; + } + + next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $create_time); + + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $alarm->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $alarm->{name} . "': no matching filter.", debug => 1); + next; + } + + my $diff_time = $current_time - $create_time; + + $self->{alarms}->{global}->{alarm}->{$i} = { + %$alarm, + opened => $diff_time + }; + $i++; + } + + if (defined($self->{option_results}->{memory})) { + $self->{statefile_cache}->write(data => { last_time => $current_time }); + } +} + +1; + +__END__ + +=head1 MODE + +Check alarms. + +=over 8 + +=item B<--filter-name> + +Filter by alert name (can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{level} =~ /warning|minor/i') +Can used special variables like: %{level}, %{details}, %{name} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{level} =~ /critical|major|error/i'). +Can used special variables like: %{level}, %{details}, %{name} + +=item B<--memory> + +Only check new alarms. + +=back + +=cut diff --git a/hardware/devices/pexip/infinity/managementapi/mode/conferences.pm b/hardware/devices/pexip/infinity/managementapi/mode/conferences.pm new file mode 100644 index 000000000..568db8cd4 --- /dev/null +++ b/hardware/devices/pexip/infinity/managementapi/mode/conferences.pm @@ -0,0 +1,196 @@ +# +# Copyright 2018 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::devices::pexip::infinity::managementapi::mode::conferences; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::statefile; +use POSIX; +use Time::HiRes; +use Digest::MD5 qw(md5_hex); + +sub prefix_quality_output { + my ($self, %options) = @_; + + return 'Participants call quality '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'conferences', type => 0, skipped_code => { -10 => 1 } }, + { name => 'participants_quality', type => 0, cb_prefix_output => 'prefix_quality_output', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{conferences} = [ + { label => 'conferences-total', nlabel => 'conferences.total.count', set => { + key_values => [ { name => 'total_conferences' } ], + output_template => 'conferences total: %d', + perfdatas => [ + { template => '%d', min => 0 } + ] + } + }, + { label => 'participants-total', nlabel => 'participants.total.count', set => { + key_values => [ { name => 'total_participants' } ], + output_template => 'participants total: %d', + perfdatas => [ + { template => '%d', min => 0 } + ] + } + }, + ]; + + $self->{maps_counters}->{participants_quality} = []; + foreach (('good', 'ok', 'bad', 'terrible', 'unknown')) { + push @{$self->{maps_counters}->{participants_quality}}, { + label => 'participants-callquality-' . $_, nlabel => 'participants.callquality.' . $_ . '.count', set => { + key_values => [ { name => $_ }, { name => 'total' } ], + output_template => $_ . ': %s', + perfdatas => [ + { template => '%d', min => 0, max => 'total' } + ] + } + }; + } +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => { + }); + + centreon::plugins::misc::mymodule_load( + output => $self->{output}, + module => 'Date::Parse', + error_msg => "Cannot load module 'Date::Parse'." + ); + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{statefile_cache}->check_options(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{statefile_cache}->read( + statefile => 'cache_pexip_' . $options{custom}->{hostname} . '_' . $self->{mode} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) + ); + my $last_conference_endtime = $self->{statefile_cache}->get(name => 'last_conference_endtime'); + if (!defined($last_conference_endtime)) { + $last_conference_endtime = Time::HiRes::time(); + } + + my $date_end = POSIX::strftime('%Y-%m-%dT%H:%M:%S', gmtime($last_conference_endtime)); + $date_end .= sprintf('.%06d', ($last_conference_endtime - int($last_conference_endtime)) * 1000000); + my $conferences = $options{custom}->request_api( + endpoint => '/api/admin/history/v1/conference/', + get_param => ['end_time__gt=' . $date_end] + ); + + $self->{conferences} = { total_participants => 0, total_conferences => 0 }; + $self->{participants_quality} = { total => 0, unknown => 0, ok => 0, good => 0, bad => 0, terrible => 0 }; + + my $conference_ids = {}; + my ($conf_min_starttime, $conf_max_endtime) = (0, 0); + foreach (@$conferences) { + $self->{conferences}->{total_conferences}++; + $conference_ids->{ $_->{id} } = 1; + + my $tmp_time = Date::Parse::str2time($_->{start_time}); + if (!defined($tmp_time)) { + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => "can't parse date '" . $_->{start_time} . "'" + ); + next; + } + $conf_min_starttime = $tmp_time if ($conf_min_starttime == 0 || $conf_min_starttime > $tmp_time); + + $tmp_time = Date::Parse::str2time($_->{end_time}); + if (!defined($tmp_time)) { + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => "can't parse date '" . $_->{end_time} . "'" + ); + next; + } + $conf_max_endtime = $tmp_time if ($conf_max_endtime < $tmp_time); + } + + $self->{statefile_cache}->write(data => { last_conference_endtime => $last_conference_endtime }); + + return if ($conf_min_starttime == 0); + + my $date_end2 = POSIX::strftime('%Y-%m-%dT%H:%M:%S', gmtime($conf_max_endtime + 1)); + $date_end2 .= sprintf('.%06d', ($conf_max_endtime - int($conf_max_endtime)) * 1000000); + my $date_start2 = POSIX::strftime('%Y-%m-%dT%H:%M:%S', gmtime($conf_min_starttime - 1)); + $date_start2 .= sprintf('.%06d', ($conf_min_starttime - int($conf_min_starttime)) * 1000000); + my $participants = $options{custom}->request_api( + endpoint => '/api/admin/history/v1/participant/', + get_param => ['start_time__gte=' . $date_start2, 'end_time__lte=' . $date_end2] + ); + + foreach (@$participants) { + next if ($_->{conference} !~ /conference\/(.*?)\/$/ && !defined($conference_ids->{ $1 })); + $self->{conferences}->{total_participants}++; + $self->{participants_quality}->{total}++; + my ($num, $label) = split(/_/, $_->{call_quality}); + if (defined($self->{participants_quality}->{ lc($label) })) { + $self->{participants_quality}->{ lc($label) }++; + } + } +} + +1; + +__END__ + +=head1 MODE + +Check conferences and participants. + +=over 8 + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'conferences-total', 'participants-total', 'participants-callquality-good', +'participants-callquality-ok', 'participants-callquality-bad', 'participants-callquality-terrible', +'participants-callquality-unknown'. + +=back + +=cut diff --git a/hardware/devices/pexip/infinity/managementapi/plugin.pm b/hardware/devices/pexip/infinity/managementapi/plugin.pm new file mode 100644 index 000000000..ae68ba464 --- /dev/null +++ b/hardware/devices/pexip/infinity/managementapi/plugin.pm @@ -0,0 +1,51 @@ +# +# Copyright 2020 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package hardware::devices::pexip::infinity::managementapi::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ( $class, %options ) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '0.1'; + $self->{modes} = { + 'alarms' => 'hardware::devices::pexip::infinity::managementapi::mode::alarms', + 'conferences' => 'hardware::devices::pexip::infinity::managementapi::mode::conferences' + }; + + $self->{custom_modes}->{api} = 'hardware::devices::pexip::infinity::managementapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Pexip Infinity using management API. +If you use http curl backend (try: --curl-opt="CURLOPT_SSL_VERIFYPEER => 0" --curl-opt="CURLOPT_SSL_VERIFYHOST => 0") + +=cut