]> jfr.im git - munin-plugins.git/blob - asterisk_sippeers
support for a 2nd unbound plugin
[munin-plugins.git] / asterisk_sippeers
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3
4 =head1 NAME
5
6 asterix_sippeers - Plugin to monitor number of sip peers registered
7
8 =head1 CONFIGURATION
9
10 The following configuration parameters are used by this plugin
11
12 [asterisk_sippeers]
13 env.host - hostname to connect to
14 env.port - port number to connect to
15 env.username - username used for authentication
16 env.secret - secret used for authentication
17
18 The "username" and "secret" parameters are mandatory, and have no
19 defaults.
20
21 =head2 DEFAULT CONFIGURATION
22
23 [asterisk_sippeers]
24 env.host 127.0.0.1
25 env.port 5038
26
27 =head1 AUTHOR
28
29 Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
30
31 =head1 LICENSE
32
33 Gnu GPLv2
34
35 =begin comment
36
37 This program is free software; you can redistribute it and/or modify
38 it under the terms of the GNU General Public License as published by
39 the Free Software Foundation; version 2 dated June, 1991.
40
41 This program is distributed in the hope that it will be useful, but
42 WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 General Public License for more details.
45
46 You should have received a copy of the GNU General Public License along
47 with this program; if not, write to the Free Software Foundation, Inc.,
48 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49
50 If you improve this script please send your version to my email
51 address with the copyright notice upgrade with your name.
52
53 =end comment
54
55 =head1 MAGIC MARKERS
56
57 #%# family=contrib
58
59 =cut
60
61 # #################################################################################
62 # Following example from current asterisk 1.4
63 #> sip show peers
64 #Name/username Host Dyn Nat ACL Port Status
65 #104-RANDALLBUILT/104-RAND 74.218.176.166 D 5060 Unmonitored
66 #...
67 #102-ROCKSOLID/102-ROCKSOL (Unspecified) D 0 Unmonitored
68 #101-ROCKSOLID/101-ROCKSOL (Unspecified) D N 0 UNKNOWN
69 #20 sip peers [Monitored: 0 online, 1 offline Unmonitored: 2 online, 17 offline]
70 # #################################################################################
71
72 use IO::Socket;
73 use strict;
74
75 if ($ARGV[0] and $ARGV[0] eq "config")
76 {
77 print "graph_title Asterisk sip peers\n";
78 print "graph_args --base 1000 -l 0\n";
79 print "graph_order mon moff umon umoff\n";
80 print "graph_vlabel peers\n";
81 print "graph_category asterisk\n";
82 #print "peers.label total\n";
83 print "mon.draw AREA\n";
84 print "mon.label monitored online\n";
85 print "moff.draw STACK\n";
86 print "moff.label monitored offline\n";
87 print "umon.draw STACK\n";
88 print "umon.label unmonitored online\n";
89 print "umoff.draw STACK\n";
90 print "umoff.label unmonitored offline\n";
91 #graph_scale no
92 #load.warning 10
93 #load.critical 120
94 #graph_info The ... describes ....
95 #load.info Average load for the five minutes.
96 exit 0;
97 }
98
99 my $host = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
100 my $port = exists $ENV{'port'} ? $ENV{'port'} : "5038";
101
102 my $username = $ENV{'username'};
103 my $secret = $ENV{'secret'};
104
105 my $pop = new IO::Socket::INET (PeerAddr => $host,
106 PeerPort => $port,
107 Proto => 'tcp');
108 die "Could not create socket: $!\n" unless $pop;
109
110 ## Read connection message.
111 my $line = $pop->getline;
112 die $line unless $line =~ /^Asterisk/;
113
114 ## Send user name.
115 $pop->print("Action: login\n");
116 $pop->print("Username: $username\n");
117 $pop->print("Secret: $secret\n");
118 $pop->print("Events: off\n");
119 $pop->print("\n");
120
121 while ($line = $pop->getline)
122 {
123 last if $line eq "\r\n";
124 }
125 #Response: Success
126 #Message: Authentication accepted
127
128 ## Request status of messages.
129 $pop->print("Action: command\n");
130 $pop->print("Command: sip show peers\n");
131 $pop->print("\n");
132
133 my ($peers,$monitor_online,$monitor_offline,$unmonitor_online,$unmonitor_offline)=(0,0,0,0,0);
134
135 while (($line = $pop->getline) and ($line ne "\r\n"))
136 {
137 $line =~ s/^Output: //;
138 my @fields = split(' ', $line);
139 my $count = @fields;
140 #20 sip peers [Monitored: 0 online, 1 offline Unmonitored: 2 online, 17 offline]
141 if (($count > 10) and ($fields[1] eq 'sip' and $fields[2] eq 'peers')) {
142 $peers = $fields[0];
143 $monitor_online = $fields[4];
144 $monitor_offline = $fields[6];
145 $unmonitor_online = $fields[9];
146 $unmonitor_offline = $fields[11];
147 #print STDERR "$peers $monitor_online $monitor_offline $unmonitor_online $unmonitor_offline\n";
148 last;
149 }
150 }
151
152 $pop->print("Action: logoff\n");
153 $pop->print("\n");
154
155 ## Exhaust buffer before closing (to avoid polluting Asterisk's logs)
156 while ($line = $pop->getline) {}
157
158 #print "peers.value $peers\n";
159 print "mon.value $monitor_online\n";
160 print "moff.value $monitor_offline\n";
161 print "umon.value $unmonitor_online\n";
162 print "umoff.value $unmonitor_offline\n";
163
164 # vim:syntax=perl