#!/usr/bin/perl -w
# -*- perl -*-

=head1 NAME

asterix_meetmeusers - Plugin to monitor number of active users in all
the conferences

=head1 CONFIGURATION

The following configuration parameters are used by this plugin

 [asterisk_meetmeusers]
  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

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

=head2 DEFAULT CONFIGURATION

 [asterisk_meetmeusers]
  env.host 127.0.0.1
  env.port 5038

=head1 AUTHOR

Copyright (C) 2005 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 strict;

my $ret = undef;
if (! eval "require Net::Telnet;")
{
    $ret = "Net::Telnet not found";
}

if ($ARGV[0] and $ARGV[0] eq "config")
{
    print "graph_title Asterisk conference's users\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_category asterisk\n";
    print "graph_vlabel users\n";
    print "users.draw AREA\n";
    print "users.label users\n";
    print "users.info users on active conference\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 Net::Telnet (Telnetmode => 0);
$pop->open(Host => $host,
	   Port => $port);

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

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

#Response: Success
#Message: Authentication accepted

## Request status of messages.
$pop->print("Action: command");
$pop->print("Command: meetme");
$pop->print("");

my $nb = 0;

#Response: Follows
#Privilege: Command
#No active MeetMe conferences.
# OR
#Total number of MeetMe users: 1

while (($line = $pop->getline) and ($line !~ /END COMMAND/o))
{
    $nb = $1 if $line =~ /MeetMe users:\s(\d)$/;
}

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

print "users.value $nb\n";

# vim:syntax=perl
