-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_asterisk_peercount
119 lines (104 loc) · 2.74 KB
/
check_asterisk_peercount
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
#!/usr/bin/perl -w
# simple plugin to count connected peers
# ian <[email protected]>
#
# you will need the Asterisk::Manager module, from http://asterisk.gnuinter.net/
#
# You can also use a separate asterisk manager user for the checks, add this to /etc/asterisk/manager.conf
# ----------------
# [admin-mon]
# secret = m3g4p455w0rd
# deny=0.0.0.0/0.0.0.0
# permit=127.0.0.1/255.255.255.0
# read = command,system,verbose
# write = command,system
# ----------------
#
#
#
use Getopt::Long;
use Asterisk::Manager;
use strict;
my $host = "";
my $user = "";
my $secret = "";
my $online = 0;
our @peers;
GetOptions ('host|h=s'=>\$host, 'user|u=s'=>\$user, 'secret|s=s'=>\$secret);
if (check_vars()) {
exit(-1);
}
my $astman = new Asterisk::Manager;
$astman->user($user);
$astman->secret($secret);
$astman->host($host);
$astman->debug(1);
$astman->connect || die $astman->error . "\n";
$astman->setcallback('PeerEntry', \&sippeers_callback);
$astman->setcallback('PeerlistComplete', \&peerlist_complete);
$astman->setcallback('DEFAULT', \&default_callback);
$astman->sendcommand(Action => 'SIPpeers');
$astman->sendcommand(Action => 'Logoff');
$astman->eventloop;
$astman->disconnect;
sub sippeers_callback {
my %stuff = @_;
push @peers, \%stuff;
}
#sample result:
#Dynamic: no
#IPport: 5060
#Event: PeerEntry
#Status: OK (334 ms)
#Natsupport: no
#RealtimeDevice: no
#ObjectName: voip.domain.com
#ACL: no
#IPaddress: 1.2.3.4
#VideoSupport: no
#Channeltype: SIP
#ChanObjectType: peer
sub peerlist_complete {
foreach my $peer (@peers) {
if ($peer->{"IPaddress"} eq "-none-")
{
next;
}
if ($peer->{"Status"} eq "UNKNOWN")
{
next;
}
$online++;
}
print "OK - $online/" . scalar @peers . " peers online|peers=$online";
exit(0);
}
sub default_callback {
print "--------------------------------------------------\n";
}
sub check_vars {
if (length($host)<=0) {
print_usage("Host variable missing");
return(3);
}
if (length($user)<=0) {
print_usage("User variable missing");
return(3);
}
if (length($secret)<=0) {
print_usage("Secret variable missing");
return(3);
}
}
sub print_usage {
my ($msg)=@_;
if ($msg) {
print "Error: $msg\n";
}
print "\n";
print "--host -h\t\tHost to connect to\n";
print "--user -u\t\tUser to connect as\n";
print "--secret -s\t\tSecret for user\n";
print "\n";
print "\n";
}