Debian Codegraph @

src/apps/voip/asterisk/ami/custom/api.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package apps::voip::asterisk::ami::custom::api;
22
23 use strict;
24 use warnings;
25 use IO::Socket::INET;
26 use IO::Select;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = {};
31 bless $self, $class;
32
33 if (!defined($options{output})) {
34 print "Class Custom: Need to specify 'output' argument.\n";
35 exit 3;
36 }
37 if (!defined($options{options})) {
38 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
39 $options{output}->option_exit();
40 }
41
42 if (!defined($options{noptions})) {
43 $options{options}->add_options(arguments => {
44 'ami-hostname:s@' => { name => 'ami_hostname' },
45 'ami-port:s@' => { name => 'ami_port' },
46 'ami-username:s@' => { name => 'ami_username' },
47 'ami-password:s@' => { name => 'ami_password' },
48 'timeout:s@' => { name => 'timeout' }
49 });
50 }
51 $options{options}->add_help(package => __PACKAGE__, sections => 'AMI API OPTIONS', once => 1);
52
53 $self->{output} = $options{output};
54 $self->{cnx_ami} = undef;
55
56 return $self;
57 }
58
59 sub set_options {
60 my ($self, %options) = @_;
61
62 $self->{option_results} = $options{option_results};
63 }
64
65 sub set_defaults {}
66
67 sub check_options {
68 my ($self, %options) = @_;
69
70 $self->{ami_hostname} = (defined($self->{option_results}->{ami_hostname})) ? shift(@{$self->{option_results}->{ami_hostname}}) : undef;
71 $self->{ami_username} = (defined($self->{option_results}->{ami_username})) ? shift(@{$self->{option_results}->{ami_username}}) : undef;
72 $self->{ami_password} = (defined($self->{option_results}->{ami_password})) ? shift(@{$self->{option_results}->{ami_password}}) : undef;
73 $self->{ami_port} = (defined($self->{option_results}->{ami_port})) ? shift(@{$self->{option_results}->{ami_port}}) : 5038;
74 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10;
75
76 if (!defined($self->{ami_hostname})) {
77 $self->{output}->add_option_msg(short_msg => "Need to specify --ami-hostname option.");
78 $self->{output}->option_exit();
79 }
80 if (!defined($self->{ami_username})) {
81 $self->{output}->add_option_msg(short_msg => "Need to specify --ami-username option.");
82 $self->{output}->option_exit();
83 }
84 if (!defined($self->{ami_password})) {
85 $self->{output}->add_option_msg(short_msg => "Need to specify --ami-password option.");
86 $self->{output}->option_exit();
87 }
88
89 if (!defined($self->{ami_hostname}) ||
90 scalar(@{$self->{option_results}->{ami_hostname}}) == 0) {
91 return 0;
92 }
93
94 return 1;
95 }
96
97 sub get_connect_info {
98 my ($self, %options) = @_;
99
100 return $self->{ami_hostname} . '_' . $self->{ami_port};
101 }
102
103 sub read_ami_protocol_end {
104 my ($self, %options) = @_;
105
106 if (defined($options{response})) {
107 if ($options{response} eq 'Follows') {
108 return 1 if ($options{message} =~ /^--END COMMAND--/ms);
109 } else {
110 if ($options{message} =~ /^Message:\s+Command\s+output\s+follows/i) {
111 return 1 if ($options{message} =~ /\n\n/ms);
112 } elsif ($options{message} =~ /^Message: (.*)(\n)/ms) {
113 return 1;
114 }
115 }
116 }
117
118 return 0;
119 }
120
121 sub read_ami_protocol {
122 my ($self, %options) = @_;
123
124 my $select = IO::Select->new($self->{cnx_ami});
125 # Three types of message:
126 # Response: Error
127 # Message: Authentication failed
128 #
129 # Response: Follows
130 # ...
131 # --END COMMAND--
132 #
133 # Response: Success
134 # Message: Command output follows
135 # output: xxxx
136 # output: xxxx
137 # ...
138 #
139
140 my ($response, $read_msg);
141 my $message = '';
142 while (1) {
143 if (!$select->can_read(10)) {
144 $response = 'Timeout';
145 last;
146 }
147
148 my $status = $self->{cnx_ami}->recv($read_msg, 4096);
149 $read_msg =~ s/\r//msg;
150 if (!defined($response)) {
151 next if ($read_msg !~ /^Response: (.*?)(?:\n)(.*)/ms);
152 ($response, $message) = ($1, $2);
153 } else {
154 $message .= $read_msg;
155 }
156
157 last if ($self->read_ami_protocol_end(response => $response, message => $message));
158 }
159
160 $message =~ s/^Output:\s+//mig;
161 if ($response !~ /Success|Follows/) {
162 $message =~ s/\n+$//msg;
163 $message =~ s/\n/ -- /msg;
164 $self->{output}->add_option_msg(short_msg => "Communication issue [" . $message . "]");
165 $self->{output}->option_exit();
166 }
167
168 $self->{output}->output_add(long_msg => $message, debug => 1);
169 return $message;
170 }
171
172 sub write_ami_protocol {
173 my ($self, %options) = @_;
174
175 $self->{cnx_ami}->send($options{cmd});
176 }
177
178 sub login {
179 my ($self, %options) = @_;
180
181 $self->write_ami_protocol(cmd => "Action:login
182 Username:$self->{ami_username}
183 Secret:$self->{ami_password}
184 Events: off
185
186 ");
187 # don't need to get it. If it comes, it's success :)
188 $self->read_ami_protocol();
189 }
190
191 sub connect {
192 my ($self, %options) = @_;
193
194 $self->{cnx_ami} = IO::Socket::INET->new(
195 PeerAddr => $self->{ami_hostname},
196 PeerPort => $self->{ami_port},
197 Proto => 'tcp',
198 Timeout => $self->{timeout},
199 );
200 if (!defined($self->{cnx_ami})) {
201 $self->{output}->add_option_msg(short_msg => "Can't bind : $@");
202 $self->{output}->option_exit();
203 }
204
205 $self->{cnx_ami}->autoflush(1);
206 $self->login();
207 }
208
209 sub command {
210 my ($self, %options) = @_;
211
212 if (!defined($self->{cnx_ami})) {
213 $self->connect();
214 }
215
216 $self->write_ami_protocol(cmd => "Action:command
217 Command:$options{cmd}
218
219 ");
220 return $self->read_ami_protocol();
221 }
222
223 sub DESTROY {
224 my $self = shift;
225
226 if (defined($self->{cnx_ami})) {
227 $self->{cnx_ami}->close();
228 }
229 }
230
231 1;
232
233 __END__
234
235 =head1 NAME
236
237 Asterisk AMI
238
239 =head1 SYNOPSIS
240
241 Asterisk AMI custom mode
242
243 =head1 AMI API OPTIONS
244
245 =over 8
246
247 =item B<--ami-hostname>
248
249 AMI hostname (required).
250
251 =item B<--ami-port>
252
253 AMI port (default: 5038).
254
255 =item B<--ami-username>
256
257 AMI username.
258
259 =item B<--ami-password>
260
261 AMI password.
262
263 =item B<--timeout>
264
265 Set TCP timeout
266
267 =back
268
269 =head1 DESCRIPTION
270
271 B<custom>.
272
273 =cut