"Fossies" - the Fresh Open Source Software Archive

Member "munin-2.0.52/plugins/node.d/asterisk_sipchannels.in" (20 Nov 2019, 4875 Bytes) of package /linux/misc/munin-2.0.52.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #!@@PERL@@ -w
    2 # -*- perl -*-
    3 
    4 =head1 NAME
    5 
    6 asterix_sipchannels - Plugin to monitor number of active SIP
    7 channels by codec used
    8 
    9 =head1 CONFIGURATION
   10 
   11 The following configuration parameters are used by this plugin
   12 
   13  [asterisk_sipchannels]
   14   env.host     - hostname to connect to
   15   env.port     - port number to connect to
   16   env.username - username used for authentication
   17   env.secret   - secret used for authentication
   18   env.codecs   - List of codecs (names)
   19   env.codecsx  - List of codecs (hexadecimal values)
   20 
   21 The values of codecs and codecx must match each other.  Asterisk
   22 returns a hexadecimal value representing the codec used, and the same
   23 list entry is used from "codecs" and "codecsx".
   24 
   25 The "username" and "secret" parameters are mandatory, and have no
   26 defaults.
   27 
   28 =head2 DEFAULT CONFIGURATION
   29 
   30  [asterisk_sipchannels]
   31   env.host 127.0.0.1
   32   env.port 5038
   33   env.codecs gsm ulaw alaw
   34   env.codecsx 0x2 0x4 0x8
   35 
   36 =head1 AUTHOR
   37 
   38 Copyright (C) 2005-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   39 
   40 =head1 LICENSE
   41 
   42 Gnu GPLv2
   43 
   44 =begin comment
   45 
   46 This program is free software; you can redistribute it and/or modify
   47 it under the terms of the GNU General Public License as published by
   48 the Free Software Foundation; version 2 dated June, 1991.
   49 
   50 This program is distributed in the hope that it will be useful, but
   51 WITHOUT ANY WARRANTY; without even the implied warranty of
   52 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   53 General Public License for more details.
   54 
   55 You should have received a copy of the GNU General Public License along
   56 with this program; if not, write to the Free Software Foundation, Inc.,
   57 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   58 
   59 If you improve this script please send your version to my email
   60 address with the copyright notice upgrade with your name.
   61 
   62 =end comment
   63 
   64 =head1 MAGIC MARKERS
   65 
   66  #%# family=contrib
   67 
   68 =cut
   69 
   70 use IO::Socket;
   71 use strict;
   72 
   73 #Codecs g723, gsm, ulaw, alaw, g726, adpcm, slin, lpc10, g729, speex, ilbc
   74 
   75 my @CODECS = exists $ENV{'codecs'} ? split ' ',$ENV{'codecs'} : qw(gsm ulaw alaw);
   76 my @CODECSX = exists $ENV{'codecsx'} ? split ' ',$ENV{'codecsx'} : qw(0x2 0x4 0x8);
   77 
   78 if ($ARGV[0] and $ARGV[0] eq "config")
   79 {
   80     print "graph_title Asterisk sip channels/codecs\n";
   81     print "graph_args --base 1000 -l 0\n";
   82     print "graph_vlabel channels\n";
   83     print "graph_category asterisk\n";
   84     foreach my $codec (@CODECS) {
   85 	if ($codec eq $CODECS[0]) {
   86 	    print "$codec.draw AREA\n";
   87 	}
   88 	else{
   89 	    print "$codec.draw STACK\n";
   90 	}
   91 	print "$codec.label $codec\n";
   92     }
   93     print "other.draw STACK\n";
   94     print "other.label other\n";
   95     print "unknown.draw STACK\n";
   96     print "unknown.label not set\n";
   97     exit 0;
   98 }
   99 
  100 my $host = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
  101 my $port = exists $ENV{'port'} ? $ENV{'port'} : "5038";
  102 
  103 my $username = $ENV{'username'};
  104 my $secret   = $ENV{'secret'};
  105 
  106 my $pop = new IO::Socket::INET (PeerAddr => $host,
  107 				PeerPort => $port,
  108 				Proto => 'tcp');
  109 die "Could not create socket: $!\n" unless $pop;
  110 
  111 ## Read connection message.
  112 my $line = $pop->getline;
  113 die $line unless $line =~ /^Asterisk/;
  114 
  115 ## Send user name.
  116 $pop->print("Action: login\n");
  117 $pop->print("Username: $username\n");
  118 $pop->print("Secret: $secret\n");
  119 $pop->print("Events: off\n");
  120 $pop->print("\n");
  121 
  122 ## Request status of messages.
  123 $pop->print("Action: command\n");
  124 $pop->print("Command: sip show channels\n");
  125 $pop->print("\n");
  126 
  127 #Response: Follows
  128 #Privilege: Command
  129 #Peer           User/ANR    Call ID      Seq (Tx/Rx)  Format         Hold   Last Message
  130 #0 active SIP channels
  131 # -- OR --
  132 #74.218.176.166  (None)    c24a5a230c6  00101/278584006  0x0 (nothing) No    Rx: REGISTER
  133 #74.218.176.166  (None)    508c037f936  00101/07310  0x0 (nothing)    No     Rx: REGISTER
  134 #2 active SIP channels
  135 #--END COMMAND--
  136 
  137 
  138 my @results;
  139 my ($i, $start, $unknown, $other, $fields)=(0,0,0,0,0);
  140 foreach my $codec (@CODECS) {
  141     $results[$i] = 0;
  142     $i++;
  143 }
  144 
  145 while (($line = $pop->getline) and ($line !~ /active SIP channel/o))
  146 {
  147     $i = 0;
  148     if ($start) {
  149 	my @fields = (split ' ', $line);
  150 	if ($fields[4] eq '0x0') {
  151 	    $unknown += 1;
  152 	    next;
  153 	}
  154 	my $found = 0;
  155 	foreach my $codec (@CODECSX) {
  156 	    if ($fields[4] eq "$codec") {
  157 		$results[$i] = $results[$i] + 1;
  158 		$found = 1;
  159 		last;
  160 	    }
  161 	    $i++;
  162 	}
  163 	if (! $found) {
  164 	    $other += 1;
  165 	    print STDERR "SIP other format: $fields[4]\n";
  166 	}
  167     }
  168     $start = 1 if ($line =~ /Peer/o);
  169 }
  170 
  171 $pop->print("Action: logoff\n");
  172 $pop->print("\n");
  173 
  174 ## Exhaust buffer before closing (to avoid polluting Asterisk's logs)
  175 while ($line = $pop->getline) {}
  176 
  177 $i = 0;
  178 foreach my $codec (@CODECS) {
  179     print "$codec.value $results[$i]\n";
  180     $i++;
  181 }
  182 print "other.value $other\n";
  183 print "unknown.value $unknown\n";
  184 #print STDERR "asterisk_codecs other.value $other\n";
  185 #print STDERR "asterisk_codecs unknown.value $unknown\n";
  186 
  187 # vim:syntax=perl