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

# PM3ModemUsage
#
#    Copyright (C) 1999 Carlos Canau and EUnet Portugal 
#
#      changes from usrModemUsage which is:
#
#    Copyright (C) 1998 Jeff Jensen and WebTV Networks, Inc.
#
#    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; either version 2 of the License, or
#    (at your option) any later version.
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.

# Program to return line usage information from a Lucent Portmaster 3
# NAS. Returns total lines used, lines used by modems and ISDN lines.

BEGIN {
    $gInstallRoot = (($0 =~ m:^(.*/):)[0] || "./") . "..";
}

#
# We need these
#
use lib "$gInstallRoot/lib";
use strict;

use common;
use snmpUtils;

#
# Take the hostname and community as arguments
#
my $hostname = $ARGV[0];
my $community = "public";
$community = $ARGV[1] if (defined($ARGV[1]));;

die("usage: $0 host [community-string]\n")
    unless (defined($hostname) && defined($community));

my($UPTableOid) = '.1.3.6.1.4.1.307.3.2.1.1.1.13';
my($IPTableOid) = '.1.3.6.1.4.1.307.3.2.1.1.1.14';
my($modems) = 0;
my($ips) = 0;

my(@UPres) = snmpUtils::walk("$community\@$hostname", $UPTableOid);
my(@IPres) = snmpUtils::walk("$community\@$hostname", $IPTableOid);

# did we get nothing?
if (($#UPres+1 == 0) || ($#IPres+1 == 0)) {
    print "U\n";
    print "U\n";
    print "U\n";
    exit;
}

my($modem0) = 0;
my($row);
foreach $row (@UPres) {
    my($inst, $value) = split(':', $row, 2);

    if ($value) {
	if ($value ne "M0") {
	    $modems++;
	} else {
	    $modem0 = 1; # hack for PM4 (??)
	}
    }
}
$modems += $modem0;

foreach $row (@IPres) {
    my($inst, $value) = split(':', $row, 2);

    if ($value ne "0.0.0.0") {
	$ips++;
    }
}

my($isdn) = $ips;
$isdn -= $modems;
if ($isdn < 0) {
    $isdn = 0;
}

my($linesup) = $modems+$isdn;

print "$linesup\n";
print "$modems\n";
print "$isdn\n";

