From 25f8fbfea071fa23fe3f16d9794f5cedd16d04ed Mon Sep 17 00:00:00 2001 From: qgarnier Date: Fri, 24 Nov 2017 14:24:56 +0100 Subject: [PATCH] Ref #805 --- .../apps/voip/asterisk/ami/custom/api.pm | 278 ++++++++++++++++++ .../voip/asterisk/ami/mode/channelusage.pm | 132 +++++++++ .../voip/asterisk/ami/mode/dahdistatus.pm | 192 ++++++++++++ .../voip/asterisk/ami/mode/sippeersusage.pm | 252 ++++++++++++++++ .../voip/asterisk/{remote => ami}/plugin.pm | 20 +- .../apps/voip/asterisk/remote/lib/ami.pm | 103 ------- .../voip/asterisk/remote/mode/activecalls.pm | 223 -------------- .../voip/asterisk/remote/mode/dahdistatus.pm | 230 --------------- .../asterisk/remote/mode/externalcalls.pm | 213 -------------- .../voip/asterisk/remote/mode/showpeers.pm | 277 ----------------- .../remote-script/asterisk_centreon.conf | 1 - .../remote-script/asterisk_sendcommand.pm | 114 ------- 12 files changed, 863 insertions(+), 1172 deletions(-) create mode 100644 centreon-plugins/apps/voip/asterisk/ami/custom/api.pm create mode 100644 centreon-plugins/apps/voip/asterisk/ami/mode/channelusage.pm create mode 100644 centreon-plugins/apps/voip/asterisk/ami/mode/dahdistatus.pm create mode 100644 centreon-plugins/apps/voip/asterisk/ami/mode/sippeersusage.pm rename centreon-plugins/apps/voip/asterisk/{remote => ami}/plugin.pm (63%) delete mode 100644 centreon-plugins/apps/voip/asterisk/remote/lib/ami.pm delete mode 100644 centreon-plugins/apps/voip/asterisk/remote/mode/activecalls.pm delete mode 100644 centreon-plugins/apps/voip/asterisk/remote/mode/dahdistatus.pm delete mode 100644 centreon-plugins/apps/voip/asterisk/remote/mode/externalcalls.pm delete mode 100644 centreon-plugins/apps/voip/asterisk/remote/mode/showpeers.pm delete mode 100644 centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_centreon.conf delete mode 100755 centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_sendcommand.pm diff --git a/centreon-plugins/apps/voip/asterisk/ami/custom/api.pm b/centreon-plugins/apps/voip/asterisk/ami/custom/api.pm new file mode 100644 index 000000000..0db61b7e2 --- /dev/null +++ b/centreon-plugins/apps/voip/asterisk/ami/custom/api.pm @@ -0,0 +1,278 @@ +# +# Copyright 2017 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 apps::voip::asterisk::ami::custom::api; + +use strict; +use warnings; +use IO::Socket::INET; +use IO::Select; + +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 => + { + "ami-hostname:s@" => { name => 'ami_hostname' }, + "ami-port:s@" => { name => 'ami_port' }, + "ami-username:s@" => { name => 'ami_username' }, + "ami-password:s@" => { name => 'ami_password' }, + "timeout:s@" => { name => 'timeout' }, + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{cnx_ami} = undef; + + return $self; + +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + $self->{ami_hostname} = (defined($self->{option_results}->{ami_hostname})) ? shift(@{$self->{option_results}->{ami_hostname}}) : undef; + $self->{ami_username} = (defined($self->{option_results}->{ami_username})) ? shift(@{$self->{option_results}->{ami_username}}) : undef; + $self->{ami_password} = (defined($self->{option_results}->{ami_password})) ? shift(@{$self->{option_results}->{ami_password}}) : undef; + $self->{ami_port} = (defined($self->{option_results}->{ami_port})) ? shift(@{$self->{option_results}->{ami_port}}) : 5038; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10; + + if (!defined($self->{ami_hostname})) { + $self->{output}->add_option_msg(short_msg => "Need to specify --ami-hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{ami_username})) { + $self->{output}->add_option_msg(short_msg => "Need to specify --ami-username option."); + $self->{output}->option_exit(); + } + if (!defined($self->{ami_password})) { + $self->{output}->add_option_msg(short_msg => "Need to specify --ami-password option."); + $self->{output}->option_exit(); + } + + if (!defined($self->{ami_hostname}) || + scalar(@{$self->{option_results}->{ami_hostname}}) == 0) { + return 0; + } + + return 1; +} + +sub get_connect_info { + my ($self, %options) = @_; + + return $self->{ami_hostname} . '_' . $self->{ami_port}; +} + +sub read_ami_protocol_end { + my ($self, %options) = @_; + + if (defined($options{response})) { + if ($options{response} eq 'Follows') { + return 1 if ($options{message} =~ /^--END COMMAND--/ms); + } else { + return 1 if ($options{message} =~ /^Message: (.*)(\r\n)/ms); + } + } + + return 0; +} + +sub read_ami_protocol { + my ($self, %options) = @_; + + my $select = IO::Select->new($self->{cnx_ami}); + # Two types of message: + # Response: Error + # Message: Authentication failed + # + # Response: Follows + # ... + # --END COMMAND-- + + my ($response, $read_msg); + my $message = ''; + while (1) { + if (!$select->can_read(10)) { + $response = 'Timeout'; + last; + } + + my $status = $self->{cnx_ami}->recv($read_msg, 4096); + if (!defined($response)) { + next if ($read_msg !~ /^Response: (.*?)(?:\r\n|\n)(.*)/ms); + ($response, $message) = ($1, $2); + } else { + $message .= $read_msg; + } + + last if ($self->read_ami_protocol_end(response => $response, message => $message)); + } + + $message =~ s/\r//msg; + if ($response !~ /Success|Follows/) { + $message =~ s/\n+$//msg; + $message =~ s/\n/ -- /msg; + $self->{output}->add_option_msg(short_msg => "Communication issue [" . $message . "]"); + $self->{output}->option_exit(); + } + + $self->{output}->output_add(long_msg => $message, debug => 1); + return $message; +} + +sub write_ami_protocol { + my ($self, %options) = @_; + + $self->{cnx_ami}->send($options{cmd}); +} + +sub login { + my ($self, %options) = @_; + + $self->write_ami_protocol(cmd => "Action:login +Username:$self->{ami_username} +Secret:$self->{ami_password} +Events: off + +"); + # don't need to get it. If it comes, it's success :) + $self->read_ami_protocol(); +} + +sub connect { + my ($self, %options) = @_; + + $self->{cnx_ami} = IO::Socket::INET->new( + PeerAddr => $self->{ami_hostname}, + PeerPort => $self->{ami_port}, + Proto => 'tcp', + Timeout => $self->{timeout}, + ); + if (!defined($self->{cnx_ami})) { + $self->{output}->add_option_msg(short_msg => "Can't bind : $@"); + $self->{output}->option_exit(); + } + + $self->{cnx_ami}->autoflush(1); + $self->login(); +} + +sub command { + my ($self, %options) = @_; + + if (!defined($self->{cnx_ami})) { + $self->connect(); + } + + $self->write_ami_protocol(cmd => "Action:command +Command:$options{cmd} + +"); + return $self->read_ami_protocol(); +} + +sub DESTROY { + my $self = shift; + + if (defined($self->{cnx_ami})) { + $self->{cnx_ami}->close(); + } +} + +1; + +__END__ + +=head1 NAME + +Asterisk AMI + +=head1 SYNOPSIS + +Asterisk AMI custom mode + +=head1 AMI API OPTIONS + +=over 8 + +=item B<--ami-hostname> + +AMI hostname (Required). + +=item B<--ami-port> + +AMI port (Default: 5038). + +=item B<--ami-username> + +AMI username. + +=item B<--ami-password> + +AMI password. + +=item B<--timeout> + +Set TCP timeout + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/centreon-plugins/apps/voip/asterisk/ami/mode/channelusage.pm b/centreon-plugins/apps/voip/asterisk/ami/mode/channelusage.pm new file mode 100644 index 000000000..e3f2b4e2a --- /dev/null +++ b/centreon-plugins/apps/voip/asterisk/ami/mode/channelusage.pm @@ -0,0 +1,132 @@ +# +# Copyright 2017 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 apps::voip::asterisk::ami::mode::channelusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'channels-active', set => { + key_values => [ { name => 'channels_active' } ], + output_template => 'Channels Active: %s', + perfdatas => [ + { label => 'channels_active', value => 'channels_active_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'calls-active', set => { + key_values => [ { name => 'calls_active' } ], + output_template => 'Calls Active: %s', + perfdatas => [ + { label => 'calls_active', value => 'calls_active_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'extcalls-active', set => { + key_values => [ { name => 'extcalls_active' } ], + output_template => 'External Calls Active: %s', + perfdatas => [ + { label => 'extcalls_active', value => 'extcalls_active_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'calls-count', set => { + key_values => [ { name => 'calls_count', diff => 1 } ], + output_template => 'Calls Count: %s', + perfdatas => [ + { label => 'calls_count', value => 'calls_count_absolute', template => '%s', min => 0 }, + ], + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $result = $options{custom}->command(cmd => 'core show channels'); + $self->{global} = { channels_active => 0, calls_active => 0, + calls_count => undef, extcalls_active => 0 }; + + $self->{global}->{channels_active} = $1 + if ($result =~ /^(\d+)\s+active\s+channels/ms); + $self->{global}->{calls_active} = $1 + if ($result =~ /^(\d+)\s+active\s+calls/ms); + $self->{global}->{calls_count} = $1 + if ($result =~ /^(\d+)\s+calls\s+processed/ms); + + my $count = 0; + $count++ while ($result =~ /Outgoing\s+Line/msig); + $self->{global}->{extcalls_active} = $count; + + $self->{cache_name} = "asterisk_" . '_' . $self->{mode} . '_' . $options{custom}->get_connect_info() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check channel usage: active calls, external calls. + +=over 8 + +=item B<--warning-*> + +Threshold warning. +Can be: 'channels-active', 'calls-active', 'extcalls-active', +'calls-count'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'channels-active', 'calls-active', 'extcalls-active', +'calls-count'. + +=back + +=cut diff --git a/centreon-plugins/apps/voip/asterisk/ami/mode/dahdistatus.pm b/centreon-plugins/apps/voip/asterisk/ami/mode/dahdistatus.pm new file mode 100644 index 000000000..9c48d8ae8 --- /dev/null +++ b/centreon-plugins/apps/voip/asterisk/ami/mode/dahdistatus.pm @@ -0,0 +1,192 @@ +# +# Copyright 2017 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 apps::voip::asterisk::ami::mode::dahdistatus; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + my $label = $self->{label}; + $label =~ s/-/_/g; + if (defined($instance_mode->{option_results}->{'critical_' . $label}) && $instance_mode->{option_results}->{'critical_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'critical_' . $label}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{'warning_' . $label}) && $instance_mode->{option_results}->{'warning_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'warning_' . $label}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf('status : %s', $self->{result_values}->{status}); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{description} = $options{new_datas}->{$self->{instance} . '_description'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'dahdi', type => 1, cb_prefix_output => 'prefix_dahdi_output', message_multiple => 'All dahdi lines are ok' }, + ]; + + $self->{maps_counters}->{dahdi} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'description' }, { name => 'status' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-description:s" => { name => 'filter_description' }, + "warning-status:s" => { name => 'warning_status', default => '%{status} =~ /UNCONFIGURED|YEL|BLU/i' }, + "critical-status:s" => { name => 'critical_status', default => '%{status} =~ /RED/i' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; + $self->change_macros(); +} + +sub prefix_dahdi_output { + my ($self, %options) = @_; + + return "Line '" . $options{instance_value}->{description} . "' "; +} + +sub change_macros { + my ($self, %options) = @_; + + foreach (('warning_status', 'critical_status')) { + if (defined($self->{option_results}->{$_})) { + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; + } + } +} + +sub manage_selection { + my ($self, %options) = @_; + + # Status can be: OK, UNCONFIGURED, BLU, YEL, RED, REC (recover), NOP (notopen), UUU + + #Description Alarms IRQ bpviol CRC4 + #Wildcard TDM410P Board 1 OK 0 0 0 + #Wildcard TDM800P Board 2 OK 0 0 0 + + #Description Alarms IRQ bpviol CRC Fra Codi Options LBO + #Wildcard TE131/TE133 Card 0 BLU/RED 0 0 0 CCS HDB3 0 db (CSU)/0-133 feet (DSX-1) + my $result = $options{custom}->command(cmd => 'dahdi show status'); + + $self->{dahdi} = {}; + foreach my $line (split /\n/, $result) { + if ($line =~ /^(.*?)\s+((?:OK|UNCONFIGURED|BLU|YEL|RED|REC|NOP|UUU)[^\s]*)\s+/msg) { + my ($description, $status) = ($1, $2); + if (defined($self->{option_results}->{filter_description}) && $self->{option_results}->{filter_description} ne '' && + $description !~ /$self->{option_results}->{filter_description}/) { + $self->{output}->output_add(long_msg => "skipping '" . $description . "': no matching filter.", debug => 1); + next; + } + + $self->{dahdi}->{$description} = { + description => $description, + status => $status, + }; + } + } + + if (scalar(keys %{$self->{dahdi}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No dahdi lines found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check status of dahdi lines. + +=over 8 + +=item B<--filter-description> + +Filter dahdi description (can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{status} =~ /UNCONFIGURED|YEL|BLU/i'). +Can used special variables like: %{description}, %{status} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} =~ /RED/i'). +Can used special variables like: %{description}, %{status} + +=back + +=cut diff --git a/centreon-plugins/apps/voip/asterisk/ami/mode/sippeersusage.pm b/centreon-plugins/apps/voip/asterisk/ami/mode/sippeersusage.pm new file mode 100644 index 000000000..c926ceeb3 --- /dev/null +++ b/centreon-plugins/apps/voip/asterisk/ami/mode/sippeersusage.pm @@ -0,0 +1,252 @@ +# +# Copyright 2017 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 apps::voip::asterisk::ami::mode::sippeersusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + my $label = $self->{label}; + $label =~ s/-/_/g; + if (defined($instance_mode->{option_results}->{'critical_' . $label}) && $instance_mode->{option_results}->{'critical_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'critical_' . $label}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{'warning_' . $label}) && $instance_mode->{option_results}->{'warning_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'warning_' . $label}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf('status : %s', $self->{result_values}->{status}); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'sip', type => 1, cb_prefix_output => 'prefix_sip_output', message_multiple => 'All SIP peers are ok' }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total-peers', set => { + key_values => [ { name => 'total_peers' } ], + output_template => 'Total Peers: %s', + perfdatas => [ + { label => 'total_peers', value => 'total_peers_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'monitor-online-peers', set => { + key_values => [ { name => 'monitor_online_peers' } ], + output_template => 'Monitor Online Peers: %s', + perfdatas => [ + { label => 'monitor_online_peers', value => 'monitor_online_peers_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'monitor-offline-peers', set => { + key_values => [ { name => 'monitor_offline_peers' } ], + output_template => 'Monitor Offline Peers: %s', + perfdatas => [ + { label => 'monitor_offline_peers', value => 'monitor_offline_peers_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'unmonitor-online-peers', set => { + key_values => [ { name => 'unmonitor_online_peers' } ], + output_template => 'Unmonitor Online Peers: %s', + perfdatas => [ + { label => 'unmonitor_online_peers', value => 'unmonitor_online_peers_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'unmonitor-offline-peers', set => { + key_values => [ { name => 'unmonitor_offline_peers' } ], + output_template => 'Unmonitor Offline Peers: %s', + perfdatas => [ + { label => 'unmonitor_offline_peers', value => 'unmonitor_offline_peers_absolute', template => '%s', min => 0 }, + ], + } + }, + ]; + $self->{maps_counters}->{sip} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'name' }, { name => 'status' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-name:s" => { name => 'filter_name' }, + "warning-status:s" => { name => 'warning_status', default => '%{status} =~ /LAGGED|UNKNOWN/i' }, + "critical-status:s" => { name => 'critical_status', default => '%{status} =~ /UNREACHABLE/i' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; + $self->change_macros(); +} + +sub prefix_sip_output { + my ($self, %options) = @_; + + return "Peer '" . $options{instance_value}->{name} . "' "; +} + +sub change_macros { + my ($self, %options) = @_; + + foreach (('warning_status', 'critical_status')) { + if (defined($self->{option_results}->{$_})) { + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; + } + } +} + +sub manage_selection { + my ($self, %options) = @_; + + # Status can be: UNREACHABLE, LAGGED (%d ms), OK (%d ms), UNKNOWN, Unmonitored + + #Name/username Host Dyn Forcerport Comedia ACL Port Status Description + #02l44k/02l44k 10.9.0.61 D No No 5060 Unmonitored + #0rafkw/0rafkw 10.9.0.28 D No No 5060 Unmonitored + #... + #55 sip peers [Monitored: 0 online, 0 offline Unmonitored: 43 online, 12 offline] + my $result = $options{custom}->command(cmd => 'sip show peers'); + + $self->{sip} = {}; + foreach my $line (split /\n/, $result) { + if ($line =~ /^(.*?)\s+.*(UNREACHABLE|LAGGED|OK|UNKNOWN|Unmonitored)\s/msg) { + my ($name, $status) = ($1, $2); + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $name !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); + next; + } + + $self->{sip}->{$name} = { + name => $name, + status => $status, + }; + } + } + + $self->{global} = { + total_peers => $1, + monitor_online_peers => $2, monitor_offline_peers => $3, + unmonitor_online_peers => $4, unmonitor_offline_peers => $5, + } if ($result =~ /(\d+) sip peers \[Monitored: (\d+) online, (\d+) offline Unmonitored: (\d+) online, (\d+) offline]/msi); + + if (scalar(keys %{$self->{sip}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No sip peers found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check SIP peers usage. + +=over 8 + +=item B<--filter-name> + +Filter sip peer name (can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{status} =~ /LAGGED|UNKNOWN/i'). +Can used special variables like: %{name}, %{status} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} =~ /UNREACHABLE/i'). +Can used special variables like: %{name}, %{status} + +=item B<--warning-*> + +Threshold warning. +Can be: 'total-peers', 'monitor-online-peers', 'monitor-offline-peers', +'unmonitor-online-peers', 'unmonitor-offline-peers'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'total-peers', 'monitor-online-peers', 'monitor-offline-peers', +'unmonitor-online-peers', 'unmonitor-offline-peers'. + +=back + +=cut diff --git a/centreon-plugins/apps/voip/asterisk/remote/plugin.pm b/centreon-plugins/apps/voip/asterisk/ami/plugin.pm similarity index 63% rename from centreon-plugins/apps/voip/asterisk/remote/plugin.pm rename to centreon-plugins/apps/voip/asterisk/ami/plugin.pm index 40a94a11a..543256fa8 100644 --- a/centreon-plugins/apps/voip/asterisk/remote/plugin.pm +++ b/centreon-plugins/apps/voip/asterisk/ami/plugin.pm @@ -18,36 +18,34 @@ # limitations under the License. # -package apps::voip::asterisk::remote::plugin; +package apps::voip::asterisk::ami::plugin; use strict; use warnings; -use base qw(centreon::plugins::script_simple); +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} = '1.1'; + $self->{version} = '1.0'; %{$self->{modes}} = ( - 'showpeers' => 'apps::voip::asterisk::remote::mode::showpeers', - 'dahdistatus' => 'apps::voip::asterisk::remote::mode::dahdistatus', - 'activecalls' => 'apps::voip::asterisk::remote::mode::activecalls', - 'externalcalls' => 'apps::voip::asterisk::remote::mode::externalcalls', - ); + 'channel-usage' => 'apps::voip::asterisk::ami::mode::channelusage', + 'dahdi-status' => 'apps::voip::asterisk::ami::mode::dahdistatus', + 'sip-peers-usage' => 'apps::voip::asterisk::ami::mode::sippeersusage', + ); + $self->{custom_modes}{api} = 'apps::voip::asterisk::ami::custom::api'; return $self; } - 1; __END__ =head1 PLUGIN DESCRIPTION -Check Asterisk through AMI interface (AMI socket; telnet perl module required) +Check Asterisk through AMI interface. =cut diff --git a/centreon-plugins/apps/voip/asterisk/remote/lib/ami.pm b/centreon-plugins/apps/voip/asterisk/remote/lib/ami.pm deleted file mode 100644 index afc346116..000000000 --- a/centreon-plugins/apps/voip/asterisk/remote/lib/ami.pm +++ /dev/null @@ -1,103 +0,0 @@ -# -# Copyright 2017 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 apps::voip::asterisk::remote::lib::ami; - -use strict; -use warnings; -use Net::Telnet; - -my $ami_handle; -my $line; -my @lines; -my @result; - -sub quit { - $ami_handle->print("Action: logoff"); - $ami_handle->print(""); - $ami_handle->close(); -} - -sub connect { - my ($self, %options) = @_; - - my $connection_exit = defined($options{connection_exit}) ? $options{connection_exit} : 'unknown'; - - $ami_handle = new Net::Telnet (Telnetmode => 0, - Timeout => $self->{option_results}->{timeout}, - Errmode => 'return', - ); - - $ami_handle->open(Host => $self->{option_results}->{hostname}, - Port => $self->{option_results}->{port}, - ); - - if ($ami_handle->errmsg) { - $self->{output}->output_add(severity => $connection_exit, - short_msg => 'Unable to connect to AMI: ' . $ami_handle->errmsg); - $self->{output}->display(); - $self->{output}->exit(); - } - - # Check connection message. - $line = $ami_handle->getline; - if ($line !~ /^Asterisk/) { - $self->{output}->output_add(severity => $connection_exit, - short_msg => 'Unable to connect to AMI: ' . $line); - $self->{output}->display(); - $self->{output}->exit(); - } - - # Authentication. - $ami_handle->print("Action: login"); - $ami_handle->print("Username: $self->{option_results}->{username}"); - $ami_handle->print("Secret: $self->{option_results}->{password}"); - $ami_handle->print("Events: off"); - $ami_handle->print(""); - - # Check authentication message (second message). - $line = $ami_handle->getline; - $line = $ami_handle->getline; - if ($line !~ /^Message: Authentication accepted/) { - $self->{output}->output_add(severity => $connection_exit, - short_msg => 'Unable to connect to AMI: ' . $line); - $self->{output}->display(); - $self->{output}->exit(); - } - -} - -sub action { - my ($self) = @_; - - $ami_handle->print("Action: command"); - $ami_handle->print("Command: $self->{asterisk_command}"); - $ami_handle->print(""); - - - my @return; - while (my $line = $ami_handle->getline(Timeout => 1)) { - push(@return,$line); - next if ($line !~ /END COMMAND/o); - } - return @return; -} - -1; diff --git a/centreon-plugins/apps/voip/asterisk/remote/mode/activecalls.pm b/centreon-plugins/apps/voip/asterisk/remote/mode/activecalls.pm deleted file mode 100644 index 9fc86dece..000000000 --- a/centreon-plugins/apps/voip/asterisk/remote/mode/activecalls.pm +++ /dev/null @@ -1,223 +0,0 @@ -# -# Copyright 2017 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 apps::voip::asterisk::remote::mode::activecalls; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::misc; -use apps::voip::asterisk::remote::lib::ami; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '0.2'; - - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', default => 5038 }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "remote:s" => { name => 'remote', default => 'ssh' }, - "ssh-option:s@" => { name => 'ssh_option' }, - "ssh-path:s" => { name => 'ssh_path' }, - "ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "command:s" => { name => 'command', default => 'asterisk_sendcommand.pm' }, - "command-path:s" => { name => 'command_path', default => '/home/centreon/bin' }, - "protocol:s" => { name => 'protocol', }, - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); - $self->{result} = {}; - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the --hostname option"); - $self->{output}->option_exit(); - } - - if ($self->{option_results}->{remote} eq 'ami') - { - if (!defined($self->{option_results}->{username})) { - $self->{output}->add_option_msg(short_msg => "Please set the --username option"); - $self->{output}->option_exit(); - } - - if (!defined($self->{option_results}->{password})) { - $self->{output}->add_option_msg(short_msg => "Please set the --password option"); - $self->{output}->option_exit(); - } - } - - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } -} - -sub manage_selection { - my ($self, %options) = @_; - my @result; - - $self->{asterisk_command} = 'core show channels'; - - if ($self->{option_results}->{remote} eq 'ami') - { - apps::voip::asterisk::remote::lib::ami::connect($self); - @result = apps::voip::asterisk::remote::lib::ami::action($self); - apps::voip::asterisk::remote::lib::ami::quit(); - } - else - { - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => "'".$self->{asterisk_command}."'", - ); - @result = split /\n/, $stdout; - } - - # Compute data - foreach my $line (@result) { - if ($line =~ /^(\d*) active call/) - { - $self->{result}->{activecalls} = {value => $1, status => '1'}; - } - elsif ($line =~ /^Unable to connect .*/) - { - $self->{result}->{activecalls} = {value => $line, status => '0'}; - } - } -} - -sub run { - my ($self, %options) = @_; - - my $msg; - my $old_status = 'ok'; - - $self->manage_selection(); - - # Send formated data to Centreon - if ($self->{result}->{activecalls}->{status} eq '0') - { - $self->{output}->output_add(severity => $self->{result}->{activecalls}->{status}, - short_msg => $self->{result}->{activecalls}->{value}); - $self->{output}->display(); - $self->{output}->exit(); - } - my $exit_code = $self->{perfdata}->threshold_check(value => $self->{result}->{activecalls}->{value}, - threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->perfdata_add(label => 'Active Calls', - value => $self->{result}->{activecalls}->{value}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); - - $self->{output}->output_add(severity => $exit_code, - short_msg => sprintf("Current active calls: %s", $self->{result}->{activecalls}->{value}) - ); - - $self->{output}->display(); - $self->{output}->exit(); -} - -1; - -__END__ - -=head1 MODE - -Show number of current active calls - -=over 8 - -=item B<--warning> - -Threshold warning. - -=item B<--critical> - -Threshold critical. - -=item B<--remote> - -Execute command remotely; can be 'ami' or 'ssh' (default: ssh). - -=item B<--hostname> - -Hostname to query (need --remote option). - -=item B<--port> - -AMI remote port (default: 5038). - -=item B<--username> - -AMI username. - -=item B<--password> - -AMI password. - -=item B<--ssh-option> - -Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52'). - -=item B<--ssh-path> - -Specify ssh command path (default: none) - -=item B<--ssh-command> - -Specify ssh command (default: 'ssh'). Useful to use 'plink'. - -=item B<--timeout> - -Timeout in seconds for the command (Default: 30). - -=item B<--command> - -Command to get information (Default: 'asterisk_sendcommand.pm'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: /home/centreon/bin). - -=back - -=cut diff --git a/centreon-plugins/apps/voip/asterisk/remote/mode/dahdistatus.pm b/centreon-plugins/apps/voip/asterisk/remote/mode/dahdistatus.pm deleted file mode 100644 index 027b5b886..000000000 --- a/centreon-plugins/apps/voip/asterisk/remote/mode/dahdistatus.pm +++ /dev/null @@ -1,230 +0,0 @@ -# -# Copyright 2017 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 apps::voip::asterisk::remote::mode::dahdistatus; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::misc; -use apps::voip::asterisk::remote::lib::ami; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', default => 5038 }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "remote:s" => { name => 'remote', default => 'ssh' }, - "ssh-option:s@" => { name => 'ssh_option' }, - "ssh-path:s" => { name => 'ssh_path' }, - "ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "command:s" => { name => 'command', default => 'asterisk_sendcommand.pm' }, - "command-path:s" => { name => 'command_path', default => '/home/centreon/bin' }, - "filter-name:s" => { name => 'filter_name', }, - }); - $self->{result} = {}; - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the --hostname option"); - $self->{output}->option_exit(); - } - - if ($self->{option_results}->{remote} eq 'ami') - { - if (!defined($self->{option_results}->{username})) { - $self->{output}->add_option_msg(short_msg => "Please set the --username option"); - $self->{output}->option_exit(); - } - - if (!defined($self->{option_results}->{password})) { - $self->{output}->add_option_msg(short_msg => "Please set the --password option"); - $self->{output}->option_exit(); - } - } -} - -sub manage_selection { - my ($self, %options) = @_; - my @result; - - $self->{asterisk_command} = 'dahdi show status'; - - if ($self->{option_results}->{remote} eq 'ami') - { - apps::voip::asterisk::remote::lib::ami::connect($self); - @result = apps::voip::asterisk::remote::lib::ami::action($self); - apps::voip::asterisk::remote::lib::ami::quit(); - } - else - { - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => "'".$self->{asterisk_command}."'", - ); - @result = split /\n/, $stdout; - } - - # Compute data - foreach my $line (@result) { - if ($line =~ /^Description /) - { - next; - } - if ($line =~ /^(.{41})(\w*).*/) - { - my $status; - my ($trunkname, $trunkstatus) = ($1, $2); - $trunkname =~ s/^\s+|\s+$//g; - if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && - $trunkname !~ /$self->{option_results}->{filter_name}/) - { - $self->{output}->output_add(long_msg => "Skipping trunk '" . $trunkname . "': no matching filter name"); - next; - } - if ($trunkstatus eq 'Red' | $trunkstatus eq 'Yel' | $trunkstatus eq 'Blu') - { - $status = 'CRITICAL'; - } - elsif ($trunkstatus eq 'Unconfi') - { - $status = 'WARNING'; - } - $self->{result}->{$trunkname} = {name => $trunkname, status => $status, realstatus => $trunkstatus}; - } - elsif ($line =~ /^Unable to connect .*/) - { - $self->{result}->{$line} = {name => $line, status => 'CRITICAL'}; - } - } -} - -sub run { - my ($self, %options) = @_; - - my $msg; - my $old_status = 'ok'; - - $self->manage_selection(); - - # Send formated data to Centreon - if (scalar keys %{$self->{result}} >= 1) - { - $self->{output}->output_add(severity => 'OK', - short_msg => 'Everything is OK'); - } - else - { - $self->{output}->output_add(severity => 'Unknown', - short_msg => 'Nothing to be monitored'); - } - - foreach my $name (sort(keys %{$self->{result}})) { - if (!$self->{output}->is_status(value => $self->{result}->{$name}->{status}, compare => 'ok', litteral => 1)) - { - $msg = sprintf("Trunk: %s", $self->{result}->{$name}->{name}); - $self->{output}->output_add(severity => $self->{result}->{$name}->{status}, - short_msg => $msg); - } - $self->{output}->output_add(long_msg => sprintf("%s : %s", $self->{result}->{$name}->{name}, $self->{result}->{$name}->{realstatus})); - } - $self->{output}->display(); - $self->{output}->exit(); -} - -1; - -__END__ - -=head1 MODE - -Show status of dahdi lines. - -=over 8 - -=item B<--remote> - -Execute command remotely; can be 'ami' or 'ssh' (default: ssh). - -=item B<--hostname> - -Hostname to query (need --remote option). - -=item B<--port> - -AMI remote port (default: 5038). - -=item B<--username> - -AMI username. - -=item B<--password> - -AMI password. - -=item B<--ssh-option> - -Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52'). - -=item B<--ssh-path> - -Specify ssh command path (default: none) - -=item B<--ssh-command> - -Specify ssh command (default: 'ssh'). Useful to use 'plink'. - -=item B<--timeout> - -Timeout in seconds for the command (Default: 30). - -=item B<--command> - -Command to get information (Default: 'asterisk_sendcommand.pm'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: /home/centreon/bin). - -=item B<--filter-name> - -Filter on trunkname (regexp can be used). - -=back - -=cut \ No newline at end of file diff --git a/centreon-plugins/apps/voip/asterisk/remote/mode/externalcalls.pm b/centreon-plugins/apps/voip/asterisk/remote/mode/externalcalls.pm deleted file mode 100644 index 6144885af..000000000 --- a/centreon-plugins/apps/voip/asterisk/remote/mode/externalcalls.pm +++ /dev/null @@ -1,213 +0,0 @@ -# -# Copyright 2017 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 apps::voip::asterisk::remote::mode::externalcalls; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::misc; -use apps::voip::asterisk::remote::lib::ami; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '0.1'; - - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', default => 5038 }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "remote:s" => { name => 'remote', default => 'ssh' }, - "ssh-option:s@" => { name => 'ssh_option' }, - "ssh-path:s" => { name => 'ssh_path' }, - "ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "command:s" => { name => 'command', default => 'asterisk_sendcommand.pm' }, - "command-path:s" => { name => 'command_path', default => '/home/centreon/bin' }, - "protocol:s" => { name => 'protocol', }, - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); - $self->{result} = {}; - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the --hostname option"); - $self->{output}->option_exit(); - } - - if ($self->{option_results}->{remote} eq 'ami') - { - if (!defined($self->{option_results}->{username})) { - $self->{output}->add_option_msg(short_msg => "Please set the --username option"); - $self->{output}->option_exit(); - } - - if (!defined($self->{option_results}->{password})) { - $self->{output}->add_option_msg(short_msg => "Please set the --password option"); - $self->{output}->option_exit(); - } - } - - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } -} - -sub manage_selection { - my ($self, %options) = @_; - my @result; - - $self->{asterisk_command} = 'core show channels'; - - if ($self->{option_results}->{remote} eq 'ami') - { - apps::voip::asterisk::remote::lib::ami::connect($self); - @result = apps::voip::asterisk::remote::lib::ami::action($self); - apps::voip::asterisk::remote::lib::ami::quit(); - } - else - { - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => "'".$self->{asterisk_command}."'", - ); - @result = split /\n/, $stdout; - } - - # Compute data - $self->{option_results}->{extcallcounter} = '0'; - foreach my $line (@result) { - if ($line =~ /Outgoing Line/m) - { - $self->{option_results}->{extcallcounter}++; - } - } -} - -sub run { - my ($self, %options) = @_; - - my $msg; - my $old_status = 'ok'; - - $self->manage_selection(); - - # Send formated data to Centreon - my $exit_code = $self->{perfdata}->threshold_check(value => $self->{option_results}->{extcallcounter}, - threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->perfdata_add(label => 'External Calls', - value => $self->{option_results}->{extcallcounter}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); - - $self->{output}->output_add(severity => $exit_code, - short_msg => sprintf("Current external calls: %s", $self->{option_results}->{extcallcounter}) - ); - - $self->{output}->display(); - $self->{output}->exit(); -} - -1; - -__END__ - -=head1 MODE - -Show number of current external calls - -=over 8 - -=item B<--warning> - -Threshold warning. - -=item B<--critical> - -Threshold critical. - -=item B<--remote> - -Execute command remotely; can be 'ami' or 'ssh' (default: ssh). - -=item B<--hostname> - -Hostname to query (need --remote option). - -=item B<--port> - -AMI remote port (default: 5038). - -=item B<--username> - -AMI username. - -=item B<--password> - -AMI password. - -=item B<--ssh-option> - -Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52'). - -=item B<--ssh-path> - -Specify ssh command path (default: none) - -=item B<--ssh-command> - -Specify ssh command (default: 'ssh'). Useful to use 'plink'. - -=item B<--timeout> - -Timeout in seconds for the command (Default: 30). - -=item B<--command> - -Command to get information (Default: 'asterisk_sendcommand.pm'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: /home/centreon/bin). - -=back - -=cut diff --git a/centreon-plugins/apps/voip/asterisk/remote/mode/showpeers.pm b/centreon-plugins/apps/voip/asterisk/remote/mode/showpeers.pm deleted file mode 100644 index 6d92077c7..000000000 --- a/centreon-plugins/apps/voip/asterisk/remote/mode/showpeers.pm +++ /dev/null @@ -1,277 +0,0 @@ -# -# Copyright 2017 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 apps::voip::asterisk::remote::mode::showpeers; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::misc; -use apps::voip::asterisk::remote::lib::ami; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', default => 5038 }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "remote:s" => { name => 'remote', default => 'ssh' }, - "ssh-option:s@" => { name => 'ssh_option' }, - "ssh-path:s" => { name => 'ssh_path' }, - "ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "command:s" => { name => 'command', default => 'asterisk_sendcommand.pm' }, - "command-path:s" => { name => 'command_path', default => '/home/centreon/bin' }, - "protocol:s" => { name => 'protocol', }, - "filter-name:s" => { name => 'filter_name', }, - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); - $self->{result} = {}; - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Please set the --hostname option"); - $self->{output}->option_exit(); - } - - if ($self->{option_results}->{remote} eq 'ami') - { - if (!defined($self->{option_results}->{username})) { - $self->{output}->add_option_msg(short_msg => "Please set the --username option"); - $self->{output}->option_exit(); - } - - if (!defined($self->{option_results}->{password})) { - $self->{output}->add_option_msg(short_msg => "Please set the --password option"); - $self->{output}->option_exit(); - } - } - - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } -} - -sub manage_selection { - my ($self, %options) = @_; - my @result; - - if ($self->{option_results}->{protocol} eq 'sip' || $self->{option_results}->{protocol} eq 'SIP') - { - $self->{asterisk_command} = 'sip show peers'; - } - elsif ($self->{option_results}->{protocol} eq 'iax' || $self->{option_results}->{protocol} eq 'IAX') - { - $self->{asterisk_command} = 'iax2 show peers'; - } - - if ($self->{option_results}->{remote} eq 'ami') - { - apps::voip::asterisk::remote::lib::ami::connect($self); - @result = apps::voip::asterisk::remote::lib::ami::action($self); - apps::voip::asterisk::remote::lib::ami::quit(); - } - else - { - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => "'".$self->{asterisk_command}."'", - ); - @result = split /\n/, $stdout; - } - - # Compute data - foreach my $line (@result) { - if ($line =~ /^([\w\-\/]*) *\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} .* (OK) \((.*) (.*)\)/) - { - my ($trunkname, $trunkstatus, $trunkvalue, $trunkunit) = ($1, $2, $3, $4); - - if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && - $trunkname !~ /$self->{option_results}->{filter_name}/) - { - $self->{output}->output_add(long_msg => "Skipping trunk '" . $trunkname . "': no matching filter name"); - next; - } - - $self->{result}->{$trunkname} = {name => $trunkname, status => 'OK', - realstatus => $trunkstatus, - value => $trunkvalue, - unit => $trunkunit}; - } - elsif ($line =~ /^([\w\-\/]*) *\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} .* (Unreachable)/) - { - my ($trunkname, $trunkstatus) = ($1, $2); - $self->{result}->{$trunkname} = {name => $trunkname, status => 'CRITICAL', realstatus => $trunkstatus}; - } - elsif ($line =~ /^Unable to connect .*/) - { - $self->{result}->{$line} = {name => $line, status => 'CRITICAL', realstatus => 'Unreachable'}; - } - } -} - -sub run { - my ($self, %options) = @_; - - my $msg; - my $old_status = 'ok'; - - $self->manage_selection(); - - # Send formated data to Centreon - if (scalar keys %{$self->{result}} >= 1) - { - $self->{output}->output_add(severity => 'OK', - short_msg => 'Everything is OK'); - } - else - { - $self->{output}->output_add(severity => 'Unknown', - short_msg => 'Nothing to be monitored'); - } - - foreach my $name (sort(keys %{$self->{result}})) { - if (defined($self->{result}->{$name}->{value}) && defined($self->{result}->{$name}->{unit})) - { - $self->{result}->{$name}->{status} = $self->{perfdata}->threshold_check(value => $self->{result}->{$name}->{value}, - threshold => [{ label => 'critical', exit_litteral => 'critical' }, - { label => 'warning', exit_litteral => 'warning' }]); - $self->{output}->perfdata_add(label => $self->{result}->{$name}->{name}, - value => $self->{result}->{$name}->{value}.$self->{result}->{$name}->{unit}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); - } - - if (!$self->{output}->is_status(value => $self->{result}->{$name}->{status}, compare => 'ok', litteral => 1)) - { - $msg = sprintf("Trunk: %s", $self->{result}->{$name}->{name}); - $self->{output}->output_add(severity => $self->{result}->{$name}->{status}, - short_msg => $msg); - if ($self->{result}->{$name}->{realstatus} eq 'Unreachable') - { - $self->{output}->output_add(long_msg => sprintf("%s : %s", $self->{result}->{$name}->{name}, $self->{result}->{$name}->{realstatus})); - } - else - { - $self->{output}->output_add(long_msg => sprintf("%s : %s", $self->{result}->{$name}->{name}, $self->{result}->{$name}->{value})); - } - } - } - - $self->{output}->display(); - $self->{output}->exit(); -} - -1; - -__END__ - -=head1 MODE - -Show peers for different protocols. - -=over 8 - -=item B<--warning> - -Threshold warning. - -=item B<--critical> - -Threshold critical. - -=item B<--remote> - -Execute command remotely; can be 'ami' or 'ssh' (default: ssh). - -=item B<--hostname> - -Hostname to query (need --remote option). - -=item B<--port> - -AMI remote port (default: 5038). - -=item B<--username> - -AMI username. - -=item B<--password> - -AMI password. - -=item B<--ssh-option> - -Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52'). - -=item B<--ssh-path> - -Specify ssh command path (default: none) - -=item B<--ssh-command> - -Specify ssh command (default: 'ssh'). Useful to use 'plink'. - -=item B<--timeout> - -Timeout in seconds for the command (Default: 30). - -=item B<--command> - -Command to get information (Default: 'asterisk_sendcommand.pm'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: /home/centreon/bin). - -=item B<--filter-name> - -Filter on trunkname (regexp can be used). - -=item B<--protocol> - -show peer for the choosen protocol (sip or iax). - -=back - -=cut \ No newline at end of file diff --git a/centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_centreon.conf b/centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_centreon.conf deleted file mode 100644 index 0d3576665..000000000 --- a/centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_centreon.conf +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_sendcommand.pm b/centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_sendcommand.pm deleted file mode 100755 index 0b30a7050..000000000 --- a/centreon-plugins/apps/voip/asterisk/remote/remote-script/asterisk_sendcommand.pm +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/perl -w -# -# Copyright (C) 2005 Rodolphe Quiedeville -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; version 2 dated June, -# 1991. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# -# If you improve this script please send your version to my email address -# with the copyright notice upgrade with your name. -# -# -# $Log$ -# Revision 1.0 2015/01/16 11:15 David Sabatie -# Release based on already existing munin script -# -# Parameters mandatory: -# -# username -# secret -# -#%# family=asterisk -#%# capabilities=autoconf - -use strict; -use File::Basename; - -my $ret = undef; -if (! eval "require Net::Telnet;") -{ - $ret = "Net::Telnet not found"; -} - -my $DIRNAME=dirname($0); -my $conffile=$DIRNAME."/asterisk_centreon.conf"; - -my $command; -if ( (defined($ARGV[0])) && ($ARGV[0] ne '') ) -{ - $command = $ARGV[0]; -} -else -{ - print 'No command to send'; - exit; -} - -my $host = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1"; -my $port = exists $ENV{'port'} ? $ENV{'port'} : "5038"; - -#[asterisk_*] -#env.username xivo_centreon_user -#env.secret secretpass - -my ($username, $secret); - -open FILE, $conffile or die $!; -while (my $confline = ) -{ - ($username, $secret) = split(' ', $confline); -} -close(FILE); - -my $pop = new Net::Telnet (Telnetmode => 0); -$pop->open(Host => $host, - Port => $port); - -## Read connection message. -my $line = $pop->getline; -die $line unless $line =~ /^Asterisk/; - -## Send user name. -$pop->print("Action: login"); -$pop->print("Username: $username"); -$pop->print("Secret: $secret"); -$pop->print("Events: off"); -$pop->print(""); - -#Response: Success -#Message: Authentication accepted - -$line = $pop->getline; -$line = $pop->getline; -if ($line !~ /^Message: Authentication accepted/) { - print 'Unable to connect to AMI: ' . $line; - exit; -} - -## Request status of messages. -$pop->print("Action: command"); -$pop->print("Command: ".$command); -$pop->print(""); -$line = $pop->getline; -$line = $pop->getline; -$line = $pop->getline; -while (($line = $pop->getline) and ($line !~ /END COMMAND/o)) -{ - print $line; -} -$pop->print("Action: logoff"); -$pop->print(""); -$pop->close(); - -# vim:syntax=perl