centreon-plugins/apps/voip/asterisk/remote/lib/ami.pm

104 lines
3.0 KiB
Perl
Raw Normal View History

2015-01-09 16:04:46 +01:00
#
2016-01-20 19:09:18 +01:00
# Copyright 2016 Centreon (http://www.centreon.com/)
2015-01-09 16:04:46 +01:00
#
2015-07-21 11:51:02 +02:00
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
2015-01-09 16:04:46 +01:00
#
2015-07-21 11:51:02 +02:00
# 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
2015-01-09 16:04:46 +01:00
#
2015-07-21 11:51:02 +02:00
# http://www.apache.org/licenses/LICENSE-2.0
2015-01-09 16:04:46 +01:00
#
2015-07-21 11:51:02 +02:00
# 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.
2015-01-09 16:04:46 +01:00
#
package apps::voip::asterisk::remote::lib::ami;
2015-01-09 16:04:46 +01:00
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},
2015-04-02 09:46:15 +02:00
Errmode => 'return',
2015-01-09 16:04:46 +01:00
);
$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}");
2015-01-09 16:04:46 +01:00
$ami_handle->print("");
2015-04-02 09:46:15 +02:00
2015-01-09 16:04:46 +01:00
my @return;
while (my $line = $ami_handle->getline(Timeout => 1)) {
push(@return,$line);
next if ($line !~ /END COMMAND/o);
2015-04-02 09:46:15 +02:00
}
2015-01-09 16:04:46 +01:00
return @return;
}
1;