forked from munin-monitoring/munin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasterisk
executable file
·366 lines (303 loc) · 9.58 KB
/
asterisk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/perl -w
=encoding utf8
=head1 NAME
asterisk - Multigraph-capable plugin to monitor Asterisk
=head1 NOTES
This plugin will produce multiple graphs showing:
- total number of active channels (replaces asterisk_channels),
together with breakdown of specific channel types (replaces
asterisk_channelstypes);
- the number of messages in all voicemail boxes (replaces
asterisk_voicemail);
- the number of active MeetMe conferences and users connected to them
(replace asterisk_meetme and asterisk_meetmeusers, respectively);
- the number of active channels for a given codec, for both SIP and
IAX2 channels (replaces asterisk_sipchannels and asterisk_codecs).
=head1 CONFIGURATION
The following configuration parameters are used by this plugin
[asterisk]
env.host - hostname to connect to
env.port - port number to connect to
env.username - username used for authentication
env.secret - secret used for authentication
env.channels - The channel types to look for
env.codecsx - List of codec IDs (hexadecimal values)
env.codecs - List of codecs names, matching codecsx order
The "username" and "secret" parameters are mandatory, and have no
defaults.
=head2 DEFAULT CONFIGURATION
[asterisk]
env.host 127.0.0.1
env.port 5038
env.channels Zap IAX2 SIP
env.codecsx 0x2 0x4 0x8
env.codecs gsm ulaw alaw
=head2 WILDCARD CONFIGURATION
It's possible to use the plugin in a virtual-node capacity, in which
case the host configuration will default to the hostname following the
underscore:
[asterisk_someserver]
env.host someserver
env.port 5038
=head1 AUTHOR
Copyright (C) 2005-2006 Rodolphe Quiedeville <[email protected]>
Copyright (C) 2012 Diego Elio Pettenò <[email protected]>
=head1 LICENSE
GPLv2
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=cut
use strict;
use Munin::Plugin;
use Munin::Plugin::Framework;
use IO::Socket;
sub asterisk_command {
my ($socket, $command) = @_;
my $line, my $reply;
$socket->print("Action: command\nCommand: $command\n\n");
# Response: (Error|Follows|???)
$line = $socket->getline;
if ($line ne "Response: Follows\r\n") {
while ( $line = $socket->getline and $line ne "\r\n" ) {}
return undef;
}
# Privilege: Command
$line = $socket->getline;
# Until we get the --END COMMAND-- marker, it's the command's output.
while ( $line = $socket->getline and $line ne "--END COMMAND--\r\n" ) {
$reply .= $line;
}
# And then wait for the empty line that says we're done
while ( $line = $socket->getline and $line ne "\r\n" ) {}
return $reply;
}
my $plugin = Munin::Plugin::Framework->new;
$0 =~ /asterisk(?:_(.+))$/;
$plugin->{hostname} = $1;
my $peeraddr = $ENV{'host'} || $plugin->{hostname} || '127.0.0.1';
my $peerport = $ENV{'port'} || '5038';
my $username = $ENV{'username'};
my $secret = $ENV{'secret'};
my @CHANNELS = exists $ENV{'channels'} ? split ' ',$ENV{'channels'} : qw(Zap IAX2 SIP);
my @CODECS = exists $ENV{'codecs'} ? split ' ',$ENV{'codecs'} : qw(gsm ulaw alaw);
my @CODECSX = exists $ENV{'codecsx'} ? split ' ',$ENV{'codecsx'} : qw(0x2 0x4 0x8);
my %CODEC_IDS;
@CODEC_IDS{@CODECS} = @CODECSX;
my $line, my $error;
my $socket = new IO::Socket::INET(PeerAddr => $peeraddr,
PeerPort => $peerport,
Proto => 'tcp')
or $error = "Could not create socket: $!";
if ( $socket ) {
# This will consume the "Asterisk Call Manager" welcome line.
$socket->getline;
$socket->print("Action: login\nUsername: $username\nSecret: $secret\nEvents: off\n\n");
my $response_status = $socket->getline;
if ( $response_status ne "Response: Success\r\n" ) {
my $response_message = $socket->getline;
$response_message =~ s/Message: (.*)\r\n/$1/;
$error = "Asterisk authentication error: " . $response_message;
}
while ( $line = $socket->getline and $line ne "\r\n" ) {}
}
my ($active_channels, $messages, @channels_list, $users, $conferences, %codecs);
if ( !$error ) {
my $channels_response = asterisk_command($socket, "core show channels");
#Channel Location State Application(Data)
#Zap/pseudo-198641660 s@frompstn:1 Rsrvd (None)
#Zap/1-1 4@frompstn:1 Up MeetMe(5500)
#2 active channels
#1 active call
$active_channels = $1 if $channels_response =~ /\n([0-9]+) active channels/;
@channels_list = split(/\r\n/, $channels_response);
my $voicemail_response = asterisk_command($socket, "voicemail show users");
#Context Mbox User Zone NewMsg
#default 1234 Example Mailbox 1
#other 1234 Company2 User 0
#2 voicemail users configured.
if ( $voicemail_response and $voicemail_response !~ /no voicemail users/ ) {
$messages = 0;
foreach my $line (split(/\r\n/, $voicemail_response)) {
next unless $line =~ / ([0-9]+)$/;
$messages += $1;
}
}
my $meetme_response = asterisk_command($socket, "meetme list");
#Conf Num Parties Marked Activity Creation
#5500 0001 N/A 00:00:03 Static
#* Total number of MeetMe users: 1
if ( $meetme_response ) {
if ( $meetme_response =~ /No active/ ) {
$users = 0;
$conferences = 0;
} else {
my @meetme_list = split(/\r\n/, $meetme_response);
$users = pop(@meetme_list);
$users =~ s/^Total number of MeetMe users: ([0-9]+)$/$1/;
$conferences = scalar(@meetme_list) -1;
}
}
my $sipchannels_response = asterisk_command($socket, "sip show channels");
#Peer User/ANR Call ID Seq (Tx/Rx) Format
#192.168.1.135 yann 6902112b3e0 00101/00002 g729
#1 active SIP channel(s)
my $iaxchannels_response = asterisk_command($socket, "iax2 show channels");
#Channel Peer Username ID (Lo/Rem) Seq (Tx/Rx) Lag Jitter JitBuf Format
#IAX2/rodolphe@rodolp 10.8.53.6 rodolphe 00003/01287 00006/00004 00000ms 0148ms 0000ms gsm
#1 active IAX channel(s)
if ( $sipchannels_response or $iaxchannels_response ) {
%codecs = ( other => 0, unknown => 0 );
foreach my $codec (@CODECS) {
$codecs{$codec} = 0;
}
# split the channels' listing and drop header and footnotes
my @sipchannels = $sipchannels_response ? split(/\r\n/, $sipchannels_response) : ();
pop(@sipchannels); shift(@sipchannels);
my @iaxchannels = $iaxchannels_response ? split(/\r\n/, $iaxchannels_response) : ();
pop(@iaxchannels); shift(@iaxchannels);
foreach my $sipchan (@sipchannels) {
my $found = 0;
my @fields = split ' ', $sipchan;
if ($fields[4] eq '0x0') {
$codecs{unknown} += 1;
next;
}
foreach my $codec (@CODECS) {
if ($fields[4] eq $CODEC_IDS{$codec}) {
$codecs{$codec} += 1;
$found = 1;
last;
}
}
if (! $found) {
$codecs{other} += 1;
print STDERR "CODEC: SIP other format $fields[4]\n" if $Munin::Plugin::DEBUG;
}
}
foreach my $iaxchan (@iaxchannels) {
my $found = 0;
my @fields = split ' ', $iaxchan;
if ($fields[8] eq '0x0') {
$codecs{unknown} += 1;
next;
}
foreach my $codec (@CODECS) {
if ($fields[8] eq $CODEC_IDS{$codec}) {
$codecs{$codec} += 1;
$found = 1;
last;
}
}
if (! $found) {
$codecs{other} += 1;
print STDERR "CODEC: IAX2 other format: $fields[8]\n" if $Munin::Plugin::DEBUG;
}
}
}
}
# After all the data is fetched we can proceed to process it, the
# connection can be closed as we don't need any more data.
$socket->close() if $socket;
$plugin->{autoconf} = "no ($error)" if $error;
$plugin->add_graphs
(
asterisk_channels =>
{
title => "Asterisk active channels",
args => "--base 1000 -l 0",
vlabel => "channels",
category => "asterisk",
order => "total " . join(' ', @CHANNELS),
fields =>
{
total =>
{
label => "channels",
min => 0,
value => $active_channels,
},
},
},
asterisk_voicemail =>
{
title => "Asterisk voicemail messages",
args => "--base 1000 -l 0",
vlabel => "messages",
category => "asterisk",
fields =>
{
messages =>
{
label => "Total messages",
min => 0,
value => $messages,
},
},
},
asterisk_meetme =>
{
title => "Asterisk meetme statistics",
args => "--base 1000 -l 0",
category => "asterisk",
fields =>
{
users =>
{
label => "Connected Users",
min => 0,
value => $users,
},
conferences =>
{
label => "Active conferences",
min => 0,
value => $conferences,
},
},
},
asterisk_codecs =>
{
title => "Asterisk channels per codec",
args => "--base 1000 -l 0",
vlabel => "channels",
category => "asterisk",
order => join(' ', @CODECS) . " other unknown",
fields =>
{
other =>
{
label => "Other known codecs",
min => 0,
draw => "AREASTACK",
value => $codecs{other},
},
unknown =>
{
label => "Unknown codec",
min => 0,
draw => "AREASTACK",
value => $codecs{unknown},
},
},
},
);
foreach my $channel (@CHANNELS) {
$plugin->{graphs}->{asterisk_channels}->{fields}->{$channel} =
{
draw => "AREASTACK",
label => "$channel channels",
min => 0,
value => @channels_list ? scalar(grep(/^$channel\//, @channels_list)) : undef,
};
}
foreach my $codec (@CODECS) {
$plugin->{graphs}->{asterisk_codecs}->{fields}->{$codec} =
{
label => "$codec channels",
min => 0,
draw => "AREASTACK",
value => $codecs{$codec},
};
}
$plugin->run;