9 $host, $username, $password, $verbose, $help, $command, $mode,
10 $ipaddr, $respaddr, $sendto, $msg, $recvfrom,
11 $version, $response, $message, $line,
27 my $iax_maxlen = 1024;
29 my $iax_src_call = "8000"; #8000 most siginificant bit is IAX packet type full ... required for a poke etc...
30 my $iax_dst_call = "0000";
31 my $iax_timestamp = "00000000";
32 my $iax_outbound_seq = "00";
33 my $iax_inbound_seq = "00";
34 my $iax_type = "06"; #IAX_Control
46 print "WARNING: $s\n";
60 print "UNKNOWN: $s\n";
66 unless ($s =~ m/Help:/) {
67 $s = "Error: (".$s.")" or $s = 'Unknown';
69 print "$s\n" unless ($help);
70 print "Syntax: $0 -m mgr -h <host> -u <username> -p <password> [-cwv]\n";
71 print "Syntax: $0 -m iax -h <host> [-v]\n";
72 print "* --host -h Host\n";
73 print "* --mode -m Mode - eithr 'mgr' or 'iax'\n";
74 print " --username -u Username\n";
75 print " --password -p Password\n";
76 print " --port -P n Port (if not using $mgr_port for manager or $iax_port for IAX)\n";
77 print " --warning xxx=n Return warning if > n channels of type xxx.\n";
78 print " --critical xxx=n Return critical if > n channels of type xxx.\n";
79 print " --verbose -v Verbose\n";
80 print " --help -h This help\n";
84 Getopt
::Long
::Configure
('bundling');
86 ("p=s" => \
$password, "password=s" => \
$password,
87 "u=s" => \
$username, "username=s" => \
$username,
88 "h=s" => \
$host, "host=s" => \
$host,
89 "P=i" => \
$port, "port=i" => \
$port,
90 "H" => \
$help, "help" => \
$help,
91 "v" => \
$verbose, "verbose" => \
$verbose,
92 "m=s" => \
$mode, "mode=s" => \
$mode,
93 "critical=s" => \
$critical, "warning=s" => \
$warning);
95 syntax
("Help:") if ($help);
96 syntax
("Missing host") unless (defined($host));
97 syntax
("Missing mode") unless (defined($mode));
98 if ($mode =~ /^iax$/i) {
99 print "Running in IAX mode\n" if ($verbose);
101 } elsif ($mode =~ /^mgr$/i) {
102 print "Running in Manager mode\n" if ($verbose);
105 syntax
("Unknown mode $mode")
108 ##############################################################################
112 syntax
("Missing username") unless (defined($username));
113 syntax
("Missing password") unless (defined($password));
114 if (defined($warning)) {
115 foreach $s (split(/,/, $warning)) {
116 syntax
("Warning value given, $s, is invalid")
117 unless ($s =~ /^(\w+)=(\d+)$/);
119 print "Clear to give WARNING after $2 connections on $1\n" if ($verbose);
122 if (defined($critical)) {
123 foreach $s (split(/,/, $critical)) {
124 syntax
("Critical value given, $s, is invalid")
125 unless ($s =~ /^(\w+)=(\d+)$/);
127 print "Clear to give CRITICAL after $2 connections on $1\n" if ($verbose);
131 print "Connecting to $host:$port\n" if ($verbose);
132 unless ($sock = IO
::Socket
::INET
->new(PeerAddr
=> $host, PeerPort
=> $port, Proto
=> 'tcp')) {
133 print("Could not connect to asterisk server ".$host.":".$port."\n");
136 print "Connected to $host:$port\n" if ($verbose);
138 print $version if ($verbose);
140 print $sock "Action: Login\r\nUsername: $username\r\nSecret: $password\r\nEvents: off\r\n\r\n";
141 print "Action: Login\r\nUsername: $username\r\nSecret: $password\r\n\r\n" if ($verbose);
145 print $response.$message if ($verbose);
146 print $s if ($verbose);
148 exit(1) unless ($response =~ m/^Response:\s+(.*)$/i);
149 exit(1) unless ($1 =~ m/Success/i);
151 print $sock "Action: Status\r\n\r\n";
152 print "Action: Status\r\n\r\n" if ($verbose);
156 print $response.$message if ($verbose);
158 &unknown
("Unknown answer $response (wanted Response: something)") unless ($response =~ m/^Response:\s+(.*)$/i);
159 &unknown
("$response didn't say Success") unless ($1 =~ m/Success/i);
160 &unknown
("Unknown answer $response (wanted Message: something)") unless ($message =~ m/^Message:\s+(.*)$/i);
161 &unknown
("didn't understand message $message") unless ($1 =~ m/Channel status will follow/i);
164 while (($stop == 0) && ($line = <$sock>)) {
165 print "$line" if ($verbose);
166 if ($line =~ m/Channel:\s+(\w+)\//) {
168 print "Found $1 channel\n" if ($verbose);
170 if ($line =~ m/Event:\s*StatusComplete/i) {
176 print $sock "Action: Logoff\r\n\r\n";
179 foreach $key (keys %channels) {
180 $s .= " " . $key . " (" . $channels{$key} . ")";
183 foreach $key (keys %critval) {
184 print "key = $key\n" if ($verbose);
185 if (defined($channels{$key}) && ($channels{$key} > $critval{$key})) {
187 $cause .= $channels{$key} . " $key channels detected. ";
192 foreach $key (keys %warnval) {
193 print "key = $key\n" if ($verbose);
194 if (defined($channels{$key}) && ($channels{$key} > $warnval{$key})) {
196 $cause .= $channels{$key} . " $key channels detected. ";
201 if ($exitcode == 0) {
203 } elsif ($exitcode == 1) {
205 } elsif ($exitcode == 2) {
207 } elsif ($exitcode > 2) {
211 $cause .= " Channels:$s";
218 print "\n" if ($verbose);
221 } elsif ($runmode == 1) {
224 socket(PING
, PF_INET
, SOCK_DGRAM
, getprotobyname("udp"));
226 $msg = pack "H24", $iax_src_call . $iax_dst_call . $iax_timestamp .
227 $iax_outbound_seq . $iax_inbound_seq . $iax_type . $iax_type;
229 $ipaddr = inet_aton
($host);
230 $sendto = sockaddr_in
($port,$ipaddr);
232 send(PING
, $msg, 0, $sendto) == length($msg) or die "cannot send to $host : $port : $!\n";
235 local $SIG{ALRM
} = sub { die("alarm time out"); };
239 $recvfrom = recv(PING
, $msg, $iax_maxlen, 0) or die "recv: $!";
240 ($port, $ipaddr) = sockaddr_in
($recvfrom);
241 $respaddr = inet_ntoa
($ipaddr);
243 # print "Response from $respaddr : $port\n";
249 if ($iax_answer == 1) {
254 &ok
("Got $iax_answer $reply");
256 &error
("Got no reply");