This file is indexed.

/usr/share/munin/plugins/asterisk_codecs is in munin-plugins-extra 2.0.33-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/perl -w
# -*- perl -*-

=head1 NAME

asterisk_codecs - Plugin to monitor number of active channels by codec
used

=head1 ABOUT

This plugin uses the asterisk manager API to fetch data.

=head1 CONFIGURATION

The following configuration parameters are used by this plugin

 [asterisk_codecs]
  env.host     - hostname to connect to
  env.port     - port number to connect to
  env.username - username used for authentication
  env.secret   - secret used for authentication
  env.codecs   - List of codecs (names)
  env.codecsx  - List of codecs (hexadecimal values)

The values of codecs and codecx must match each other.  Asterisk
returns a hexadecimal value representing the codec used, and the same
list entry is used from "codecs" and "codecsx".

The "username" and "secret" parameters are mandatory, and have no
defaults.

=head2 DEFAULT CONFIGURATION

 [asterisk_codecs]
  env.host 127.0.0.1
  env.port 5038
  env.codecs gsm ulaw alaw
  env.codecsx 0x2 0x4 0x8

=head1 AUTHOR

Copyright (C) 2005-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>

=head1 LICENSE

Gnu GPLv2

=begin comment

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

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

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

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

=end comment

=head1 MAGIC MARKERS

 #%# family=contrib

=cut

use IO::Socket;
use strict;

my @CODECS = exists $ENV{'codecs'} ? split ' ',$ENV{'codecs'} : qw(gsm ulaw alaw);
my @CODECSX = exists $ENV{'codecsx'} ? split ' ',$ENV{'codecsx'} : qw(0x2 0x4 0x8);

if ($ARGV[0] and $ARGV[0] eq "config")
{
    print "graph_title Asterisk channels/codecs\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel channels\n";
    print "graph_category asterisk\n";
    foreach my $codec (@CODECS) {
	if ($codec eq $CODECS[0]) {
	    print "$codec.draw AREA\n";
	}
	else{
	    print "$codec.draw STACK\n";
	}
	print "$codec.label $codec\n";
    }
    print "other.draw STACK\n";
    print "other.label other\n";
    print "unknown.draw STACK\n";
    print "unknown.label not set\n";
    exit 0;
}

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

my $username = $ENV{'username'};
my $secret   = $ENV{'secret'};

my $pop = new IO::Socket::INET (PeerAddr => $host,
				PeerPort => $port,
				Proto => 'tcp');
die "Could not create socket: $!\n" unless $pop;

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

## Send user name.
$pop->print("Action: login\n");
$pop->print("Username: $username\n");
$pop->print("Secret: $secret\n");
$pop->print("Events: off\n");
$pop->print("\n");

#Response: Success
#Message: Authentication accepted

## Request status of messages.
$pop->print("Action: command\n");
$pop->print("Command: sip show channels\n");
$pop->print("\n");

#Response: Follows
#Peer             User/ANR    Call ID      Seq (Tx/Rx)   Format
#192.168.1.135    yann        6902112b3e0  00101/00002   g729
#1 active SIP channel(s)
#--END COMMAND--

my @results;
my ($i, $start, $unknown, $other)=(0,0,0,0);
foreach my $codec (@CODECS) {
    $results[$i] = 0;
    $i++;
}

my @fields;
while (($line = $pop->getline) and ($line !~ /active SIP channel/o))
{
    #print STDERR "SIP: $line\n";
    $i = 0;
    my $found = 0;
    if ($start) {
	@fields = (split ' ', $line);
        if ($fields[4] eq '0x0') {
            $unknown += 1;
            next;
        }
	foreach my $codec (@CODECSX) {
	    if ($fields[4] eq "$codec") {
		$results[$i] = $results[$i] + 1;
		$found = 1;
		last;
	    }
	    $i++;
	}
	if (! $found) {
            $other += 1;
	    print STDERR "CODEC: SIP other format $fields[4]\n";
	}
    }
    $start = 1 if ($line =~ /Peer/o);
}

## Request status of messages.
$pop->print("Action: command\n");
$pop->print("Command: iax2 show channels\n");
$pop->print("\n");

#Response: Follows
#Channel               Peer             Username    ID (Lo/Rem)  Seq (Tx/Rx)  Lag      Jitter  JitBuf  Format
#IAX2/rodolphe@rodolp  10.8.53.6        rodolphe    00003/01287  00006/00004  00000ms  0148ms  0000ms  gsm
#1 active IAX channel(s)
#--END COMMAND--

$start = 0;
while (($line = $pop->getline) and ($line !~ /active IAX channel/o))
{
    $i = 0;
    my $found = 0;
    if ($start) {
	@fields = (split ' ', $line);
        if ($fields[8] eq '0x0') {
            $unknown += 1;
            next;
        }
	foreach my $codec (@CODECSX) {
	    if ($fields[8] eq "$codec") {
		$results[$i] = $results[$i] + 1;
		$found = 1;
		last;
	    }
	    $i++;
	}
        if (! $found) {
            $other += 1;
            print STDERR "CODEC: IAX2 other format: $fields[8]\n";
        }
    }
    $start = 1 if ($line =~ /Channel/o);
}

# Logoff
$pop->print("Action: logoff\n");
$pop->print("\n");

## Exhaust buffer before closing (to avoid polluting Asterisk's logs)
while ($line = $pop->getline) {}

$i = 0;
foreach my $codec (@CODECS) {
    print "$codec.value $results[$i]\n";
    $i++;
}
print "other.value $other\n";
print "unknown.value $unknown\n";

# vim:syntax=perl