-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_asterisk_peers
146 lines (129 loc) · 3.92 KB
/
check_asterisk_peers
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
#!/usr/bin/perl -w
# simple plugin to check a sip peer/trunk state
# floyd <[email protected]>, <[email protected]>
#
# Extended to support stats by ian <[email protected]>
#
# you will need the Asterisk::Manager module, from http://asterisk.gnuinter.net/
#
# sample usage:
# ./check_asterisk_trunk.pl -h localhost -u admin -s c00lp455w0rd -t sipgate
#
#
# 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
# ----------------
#
# usage:
# ./check_asterisk_trunk.pl -h localhost -u admin-mon -s m3g4p455w0rd -t sipgate
#
#
use Getopt::Long;
use Asterisk::Manager;
use strict;
my $host = "";
my $user = "";
my $secret = "";
my $trunk = "";
our @peers;
GetOptions ('host|h=s'=>\$host, 'user|u=s'=>\$user, 'secret|s=s'=>\$secret, 'trunk|t=s'=>\$trunk);
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->{"ObjectName"} eq $trunk)
{
if ($peer->{"IPaddress"} eq "-none-")
{
print "CRITICAL: Trunk down! IP address is \"-none-\"\n";
exit(2);
}
if ($peer->{"Status"} eq "UNKNOWN")
{
print "CRITICAL: Trunk down! Status is \"UNKNOWN\"\n";
exit(2);
}
if ($peer->{"Status"} eq "UNREACHABLE")
{
print "CRITICAL: Trunk down! Status is \"UNREACHABLE\"\n";
exit(2);
}
my ($latency) = $peer->{"Status"} =~ m/\((\d+) ms\)/;
print "OK: Peer is up: ".$peer->{"ObjectName"}."\t".$peer->{"IPaddress"}."\t".$peer->{"Status"}."|latency=$latency\n";
exit (0);
}
}
print "CRITICAL: No such peer $trunk !\n";
exit(2);
}
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);
}
if (length($trunk)<=0) {
print_usage("Trunk 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 "--trunk -t\t\tSIP trunk to check\n";
print "\n";
print "\n";
}