Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / asterisk_sippeers @ 0fbb34b2

Historique | Voir | Annoter | Télécharger (4,72 ko)

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
Ported to Asterisk 1.6 by cerien.jean@gmail.com
32

    
33
=head1 LICENSE
34

    
35
Gnu GPLv2
36

    
37
=begin comment
38

    
39
This program is free software; you can redistribute it and/or modify
40
it under the terms of the GNU General Public License as published by
41
the Free Software Foundation; version 2 dated June, 1991.
42

    
43
This program is distributed in the hope that it will be useful, but
44
WITHOUT ANY WARRANTY; without even the implied warranty of
45
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
46
General Public License for more details.
47

    
48
You should have received a copy of the GNU General Public License
49
along with this program; if not, write to the Free Software
50
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
51
USA.
52

    
53
If you improve this script please send your version to my email
54
address with the copyright notice upgrade with your name.
55

    
56
=end comment
57

    
58
=head1 MAGIC MARKERS
59

    
60
 #%# family=contrib
61

    
62
=cut
63

    
64
# #################################################################################
65
# Following example from current asterisk 1.4
66
#> sip show peers
67
#Name/username              Host            Dyn Nat ACL Port     Status               
68
#104-RANDALLBUILT/104-RAND  74.218.176.166   D          5060     Unmonitored           
69
#...           
70
#102-ROCKSOLID/102-ROCKSOL  (Unspecified)    D          0        Unmonitored           
71
#101-ROCKSOLID/101-ROCKSOL  (Unspecified)    D   N      0        UNKNOWN              
72
#20 sip peers [Monitored: 0 online, 1 offline Unmonitored: 2 online, 17 offline]
73
# #################################################################################
74

    
75
use strict;
76

    
77
my $ret = undef;
78
if (! eval "require Net::Telnet;")
79
{
80
    $ret = "Net::Telnet not found";
81
}
82

    
83
if ($ARGV[0] and $ARGV[0] eq "config")
84
{
85
    print "graph_title Asterisk sip peers\n";
86
    print "graph_args --base 1000 -l 0\n";
87
    print "graph_order mon moff umon umoff\n";
88
    print "graph_vlabel peers\n";
89
    print "graph_category asterisk\n";
90
    #print "peers.label total\n";
91
    print "mon.draw AREA\n";
92
    print "mon.label monitored online\n";
93
    print "moff.draw STACK\n";
94
    print "moff.label monitored offline\n";
95
    print "umon.draw STACK\n";
96
    print "umon.label unmonitored online\n";
97
    print "umoff.draw STACK\n";
98
    print "umoff.label unmonitored offline\n";
99
    #graph_scale no
100
    #load.warning 10
101
    #load.critical 120
102
    #graph_info The ... describes ....
103
    #load.info Average load for the five minutes.
104
    exit 0;
105
}
106

    
107
my $host = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
108
my $port = exists $ENV{'port'} ? $ENV{'port'} : "5038";
109

    
110
my $username = $ENV{'username'};
111
my $secret   = $ENV{'secret'};
112

    
113
my $pop = new Net::Telnet (Telnetmode => 0);
114
$pop->open(Host => $host,
115
	   Port => $port);
116

    
117
## Read connection message.
118
my $line = $pop->getline;
119
die $line unless $line =~ /^Asterisk/;
120

    
121
## Send user name.
122
$pop->print("Action: login");
123
$pop->print("Username: $username");
124
$pop->print("Secret: $secret");
125
$pop->print("Events: off");
126
$pop->print("");
127

    
128
#Response: Success
129
#Message: Authentication accepted
130

    
131
## Request status of messages.
132
$pop->print("Action: command");
133
$pop->print("Command: sip show peers");
134
$pop->print("");
135

    
136
my ($peers,$monitor_online,$monitor_offline,$unmonitor_online,$unmonitor_offline)=(0,0,0,0,0);
137

    
138
while (($line = $pop->getline) and ($line !~ /END COMMAND/o))
139
{
140
    my @fields = split(' ', $line);
141
    my $count = @fields;
142
    #20 sip peers [Monitored: 0 online, 1 offline Unmonitored: 2 online, 17 offline]
143
    if (($count > 10) and ($fields[1] eq 'sip' and $fields[2] eq 'peers')) {
144
	$peers = $fields[0];
145
	$monitor_online = $fields[4];
146
	$monitor_offline = $fields[6];
147
	$unmonitor_online = $fields[9];
148
	$unmonitor_offline = $fields[11];
149
	#print STDERR "$peers $monitor_online $monitor_offline $unmonitor_online $unmonitor_offline\n";
150
	last;
151
    }
152
}
153

    
154
$pop->print("Action: logoff");
155
$pop->print("");
156

    
157
# purge the last lines to avoid error message on CLI
158
while ($line = $pop->getline)
159
{
160
#	print STDERR " $line \n";
161
}
162
#print "peers.value $peers\n";
163
print "mon.value $monitor_online\n";
164
print "moff.value $monitor_offline\n";
165
print "umon.value $unmonitor_online\n";
166
print "umoff.value $unmonitor_offline\n";
167

    
168
# vim:syntax=perl