#!/usr/bin/perl -w
#
# Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
#
# 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.
#
# Munin's plugin to monitor number of users connected to openvpn server
#
# Usage: copy or link into /etc/munin/plugins
#
# Parameters:
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#
# $Log$
# Revision 1.1  2005/10/11 14:12:19  Rodolphe Quiedeville
#
# Magic markers (optinal - used by munin-config and some installation
# scripts):
#
#%# family=contrib
#%# capabilities=autoconf

use strict;

my $statuslogfile = "/etc/openvpn/openvpn-status.log";
my $users = 0;

if($ARGV[0] and $ARGV[0] eq "autoconf" ) {
    if(-f $statuslogfile) {
	if(-r $statuslogfile) {
	    print "yes\n";
	    exit 0;
	} else {
	    print "no (logfile not readable)\n";
	}
    } else {
	print "no (logfile not found)\n";
    }
    exit 1;
}

if ($ARGV[0] and $ARGV[0] eq "config" ){
    print "graph_title OpenVpn\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_scale yes\n";
    print "graph_vlabel users\n";
    print "graph_category network\n";
    print "graph_info This graph shows the numbers of users connected to openvpn server.\n";
    print "users.label users\n";
    print "users.info The number of users connected to openvpn server\n";
    exit 0;
}

if (-f "$statuslogfile") {
    open(IN, "$statuslogfile") or exit 4;
    my $flagu = 0;
    while(<IN>) {
	if(/^ROUTING TABLE$/) {
	    $flagu = 0;
	}
	if ($flagu) {
	    $users = $users + 1;
	}
	if(/^Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since$/) {
	    $flagu = 1;
	}
    }
    close(IN);
}

print "users.value " . $users."\n";
