From dbf8d36e7a16a669ff4291ddda101d5315a903eb Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 29 Sep 2020 11:16:08 +0200 Subject: [PATCH] add cisco callmanager sxml --- .../cisco/callmanager/sxml/custom/xmlapi.pm | 276 ++++++++++++++++++ network/cisco/callmanager/sxml/mode/alerts.pm | 243 +++++++++++++++ network/cisco/callmanager/sxml/plugin.pm | 49 ++++ 3 files changed, 568 insertions(+) create mode 100644 network/cisco/callmanager/sxml/custom/xmlapi.pm create mode 100644 network/cisco/callmanager/sxml/mode/alerts.pm create mode 100644 network/cisco/callmanager/sxml/plugin.pm diff --git a/network/cisco/callmanager/sxml/custom/xmlapi.pm b/network/cisco/callmanager/sxml/custom/xmlapi.pm new file mode 100644 index 000000000..ca861ae3b --- /dev/null +++ b/network/cisco/callmanager/sxml/custom/xmlapi.pm @@ -0,0 +1,276 @@ +# +# 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 network::cisco::callmanager::sxml::custom::xmlapi; + +use strict; +use warnings; +use centreon::plugins::http; +use XML::LibXML::Simple; + +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 => { + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + '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 => 'XMLAPI 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} : 8443; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $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->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 20; + $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}->{username} = $self->{api_username}; + $self->{option_results}->{password} = $self->{api_password}; + $self->{option_results}->{credentials} = 1; + $self->{option_results}->{basic} = 1; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub get_port { + my ($self, %options) = @_; + + return $self->{port}; +} + +sub get_one_file { + my ($self, %options) = @_; + + $self->settings(); + my $data = < + + + +$options{filename} + + + +END_FILE + + my $content = $self->{http}->request( + method => 'POST', + url_path => '/logcollectionservice/services/DimeGetFileService', + header => [ + 'SOAPAction: http://schemas.cisco.com/ast/soap/action/#LogCollectionPort#GetOneFile', + 'Content-type: text/xml' + ], + query_form_post => $data, + unknown_status => '(%{http_code} < 200 or %{http_code} >= 300) and %{http_code} != 500', + warning_status => '', + critical_status => '' + ); + + if ($self->{http}->get_code() == 500) { + my $xml_result; + eval { + $SIG{__WARN__} = sub {}; + $xml_result = XMLin($content, KeyAttr => []); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@"); + $self->{output}->option_exit(); + } + + if (!defined($xml_result->{'soapenv:Body'}->{'soapenv:Fault'})) { + $self->{output}->add_option_msg(short_msg => 'soap response issue'); + $self->{output}->option_exit(); + } + + return undef; + } + + if ($content !~ /(Time Stamp,Alert Type.*?)\r?\n\r?\n/msi) { + $self->{output}->add_option_msg(short_msg => 'soap response issue'); + $self->{output}->option_exit(); + } + + return $1; +} + +sub perfmon_collect_counter_data { + my ($self, %options) = @_; + + $self->settings(); + + # example object value: Cisco CallManager + my $data = < + + + +$self->{hostname} +$options{object} + + + +END_FILE + + my $content = $self->{http}->request( + method => 'POST', + url_path => '/perfmonservice/services/PerfmonPort', + header => [ + 'SOAPAction: http://schemas.cisco.com/ast/soap/action/#PerfmonPort#perfmonCollectCounterData', + 'Content-type: text/xml' + ], + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status} + ); + + my $xml_result; + eval { + $SIG{__WARN__} = sub {}; + $xml_result = XMLin($content, ForceArray => $options{force_array}, KeyAttr => []); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@"); + $self->{output}->option_exit(); + } + + return $xml_result; +} + +1; + +__END__ + +=head1 NAME + +Cisco VCS XML API + +=head1 SYNOPSIS + +Cisco VCS XML API + +=head1 XMLAPI OPTIONS + +=over 8 + +=item B<--hostname> + +API hostname. + +=item B<--port> + +API port (Default: 8443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +Set API username + +=item B<--api-password> + +Set API password + +=item B<--timeout> + +Set HTTP timeout + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/network/cisco/callmanager/sxml/mode/alerts.pm b/network/cisco/callmanager/sxml/mode/alerts.pm new file mode 100644 index 000000000..969534a21 --- /dev/null +++ b/network/cisco/callmanager/sxml/mode/alerts.pm @@ -0,0 +1,243 @@ +# +# 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 network::cisco::callmanager::sxml::mode::alerts; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::statefile; +use DateTime; +use Digest::MD5 qw(md5_hex); + +my $map_severity = { + 0 => 'emergency', + 1 => 'alert', + 2 => 'critical', + 3 => 'error', + 4 => 'warning', + 5 => 'notice', + 6 => 'informational', + 7 => 'debugging' +}; + +sub prefix_output_global { + my ($self, %options) = @_; + + return 'Alerts '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total', nlabel => 'alerts.total.count', set => { + key_values => [ { name => 'total' } ], + output_template => 'total: %d', + perfdatas => [ + { template => '%d', min => 0 } + ] + } + } + ]; + + foreach (values %$map_severity) { + push @{$self->{maps_counters}->{global}}, { + label => 'severity-' . $_, nlabel => 'alerts.severity.' . $_ . '.count', set => { + key_values => [ { name => $_ }, { name => 'total' } ], + output_template => $_ . ': %d', + 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; + + $options{options}->add_options(arguments => { + 'filter-alert-name:s' => { name => 'filter_alert_name' }, + 'display-alerts' => { name => 'display_alerts' } + }); + + $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 init_seekfile { + my ($self, %options) = @_; + + $self->{seek_infos} = $self->{statefile_cache}->get(name => 'seek_infos'); + if (!defined($self->{seek_infos})) { + $self->{seek_infos} = { last_timestamp => time(), files => {} }; + } +} + +sub get_list_files { + my ($self, %options) = @_; + + my $list_files = []; + my $current_timestamp = time(); + my $last_timestamp = $self->{seek_infos}->{last_timestamp} - 86400; + + # format: AlertLog_MM_DD_YYYY_hh_mm.csv + for (; $last_timestamp <= ($current_timestamp + 86400); $last_timestamp += 86400) { + my $dt = DateTime->from_epoch(epoch => $last_timestamp); + push @$list_files, sprintf('/var/log/active/cm/log/amc/AlertLog/AlertLog_%02d_%02d_%d_00_0', $dt->month(), $dt->day(), $dt->year()); + } + + $self->{seek_infos}->{last_timestamp} = $current_timestamp; + return $list_files; +} + +sub get_file { + my ($self, %options) = @_; + + if (defined($options{suffix})) { + my $content = $options{custom}->get_one_file( + filename => $options{filename} . $options{suffix} + ); + if (defined($content)) { + return ($options{suffix}, $content); + } + + return undef; + } + + # AlertLog file can finish by _00.csv or _01.csv + foreach (('0.csv', '1.csv')) { + my $content = $options{custom}->get_one_file( + filename => $options{filename} . $_ + ); + if (defined($content)) { + return ($_, $content); + } + } + + return undef; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{global} = { total => 0 }; + foreach (values %$map_severity) { + $self->{global}->{$_} = 0; + } + + $self->{statefile_cache}->read( + statefile => 'cisco_cucm_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . + (defined($self->{option_results}->{filter_alert_name}) ? md5_hex($self->{option_results}->{filter_alert_name}) : md5_hex('all')) + ); + + $self->init_seekfile(); + my $list_files = $self->get_list_files(); + for (my $i = 0; defined($list_files->[$i]); $i++) { + next if (defined($self->{seek_infos}->{files}->{ $list_files->[$i] }) && + defined($list_files->[$i + 1]) && defined($self->{seek_infos}->{files}->{ $list_files->[$i + 1] })); + + my ($suffix, $content) = $self->get_file( + custom => $options{custom}, + filename => $list_files->[$i], + suffix => defined($self->{seek_infos}->{files}->{ $list_files->[$i] }) ? $self->{seek_infos}->{files}->{ $list_files->[$i] }->{suffix} : undef, + ); + next if (!defined($suffix)); + + $self->{seek_infos}->{files}->{ $list_files->[$i] } = { line => 1 } + if (!defined($self->{seek_infos}->{files}->{ $list_files->[$i] })); + $self->{seek_infos}->{files}->{ $list_files->[$i] }->{suffix} = $suffix; + + my $j = 0; + foreach my $line (split /\n/, $content) { + $j++; + next if ($j <= $self->{seek_infos}->{files}->{ $list_files->[$i] }->{line}); + + # Time Stamp,Alert Type,Alert Name,Alert Message,Monitored Object Name,Severity,PollValue,Action,Node ID,Group ID + my @fields = split(/,/, $line); + + next if (defined($self->{option_results}->{filter_alert_name}) && $self->{option_results}->{filter_alert_name} ne '' + && $fields[2] !~ /$self->{option_results}->{filter_alert_name}/); + + $self->{global}->{ $map_severity->{ $fields[5] } }++; + $self->{global}->{total}++; + + if (defined($self->{option_results}->{display_alerts})) { + $self->{output}->output_add( + long_msg => sprintf( + 'alert [name: %s] [severity: %s] [date: %s]: %s', + $fields[2], + $map_severity->{ $fields[5] }, + scalar(localtime($fields[0] / 1000)), + $fields[3] + ) + ); + } + } + + $self->{seek_infos}->{files}->{ $list_files->[$i] }->{line} = $j; + } + + $self->{statefile_cache}->write(data => { seek_infos => $self->{seek_infos} }); +} + +1; + +__END__ + +=head1 MODE + +Check alerts. + +=over 8 + +=item B<--filter-alert-name> + +Filter alerts by name (Can use regexp). + +=item B<--display-alerts + +Display alerts in verbose output. + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'total', +'severity-informational', 'severity-error', 'severity-debugging', 'severity-critical', +'severity-alert', 'severity-warning', 'severity-emergency', 'severity-notice'. + +=back + +=cut diff --git a/network/cisco/callmanager/sxml/plugin.pm b/network/cisco/callmanager/sxml/plugin.pm new file mode 100644 index 000000000..623644049 --- /dev/null +++ b/network/cisco/callmanager/sxml/plugin.pm @@ -0,0 +1,49 @@ +# +# 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 network::cisco::callmanager::sxml::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} = { + 'alerts' => 'network::cisco::callmanager::sxml::mode::alerts' + }; + + $self->{custom_modes}->{api} = 'network::cisco::callmanager::sxml::custom::xmlapi'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Cisco Unified Communications Manager (CUCM) using Serviceability XML APIs. + +=cut