Re: RFE: RFC1696 mdmMIB support

Chris Adams (cadams@ro.com)
1 Aug 1997 16:17:46 -0500

Chris Adams <cadams@ro.com> wrote:
>David Carmean <dlc@avtel.net> wrote:
>This is a hacked up version of the "pmwho" perl script that Gregory A.
>McLean posted here a little while back. I couldn't think of any more
>info that I could display and still fit in 80 columns! There are some
>assumptions in the column widths as well: usernames <= 10 chars,
>hostnames chopped at 17 chars, idle time <= 20 minutes (our idle
>timeout).

I've received several requests for this, so I cleaned it up a bit and
even put a couple of comments in (but not many)! Here it is. Please
let me know if you make any improvements.

--
Chris Adams - cadams@ro.com
System Administrator - Renaissance Internet Services
I don't speak for anybody but myself - that's enough trouble.

########## CUT HERE ########## #!/usr/local/bin/perl # # Based on pmwho.pl from: # Author : Gregory A. McLean <gregm@randomc.com> # Copyright: Copyright (c) 1997 by Random Communications, Inc. # # Permission to use, copy, modify, and distribute this software # for any purpose without fee is hereby granted, provided that the # above copyright notice and this permission notice appear in all # copies. If you want to distribute this for a fee written # permission must be obtained from the copyright holder. # # THE SOFTWARE IS PROVIDED "AS IS" AND RANDOM COMMUNICATIONS, INC # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO # EVENT SHALL RANDOM COMMUNICATIONS, INC OR ANY OF ITS SUBSIDIES # BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTAL # DAMAGES WHATSOEVER RESULTING FROM THE LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE # OR PERFORMANCE OF THIS SOFTWARE. # # # pmstat.pl # # Chris Adams <cadams@ro.com> # # Queries one (or more) PortMasters via SNMP to get connection information. # Usage: # pmstat.pl [-f] [-u <username>] <host> [<host> ...] # -f run faster (no header or hostname lookups) # -u only show info for logins of this username # # You can specify as many PortMaster hostnames as you want. # # Change $MAXNAMELEN to the longest username you will be showing. # Longer usernames take away from hostname display. # # Change $MYDOMAIN below if you don't want to see your own domain name # in hostname lookups. #

use integer; use SNMP 1.6; use Socket; use Getopt::Std;

$MAXNAMELEN = 12; #$MYDOMAIN = "ro.com"; $MYDOMAIN = "";

getopts ('fu:'); if ($Getopt::Std::opt_u) { $user = $Getopt::Std::opt_u; } else { $user = ""; } if ($Getopt::Std::opt_f) { $fastrun = 1; } else { $fastrun = 0; }

$hostlen = 29 - $MAXNAMELEN; $MAXNAMELEN *= -1; $hostlen *= -1;

if (! $fastrun) { # Print the header printf "%${MAXNAMELEN}s %${hostlen}s %11s %5s %-11s %-10s %-5s\n", "", "", "Connect ", "Idle", " Speed", "Protocol/", " Reneg/"; printf "%${MAXNAMELEN}s %${hostlen}s %11s %5s %-11s %-10s %-5s\n", "Username", "Hostname", "Time/Date ", "Time", " In/Out ", "Compress", " Retrn"; print "---------------------------------------------------------------", "----------------"; print "\n";

# Escape domain name $MYDOMAIN =~ s/^([^\.])/.$1/; $MYDOMAIN = quotemeta ($MYDOMAIN); }

sethostent (1); while ($ARGV[0]) { pmstat ($ARGV[0], $user, $MYDOMAIN, $MAXNAMELEN, $hostlen); shift; } endhostent ();

sub pmstat { my ($pm, $muser, $mydomain, $namelen, $hostlen) = @_; my ($sess, $host, $vars, @ret); my ($suser, $sip, $sportstat, $sstarted, $sidle, $sinspeed, $soutspeed); my ($scompress, $sproto, $sreneg, $sretr, $stype); my ($stat, $user, $hostname, $start, $idle, $type, $in, $out, $comp); my ($proto, $reneg, $retr, $con_info);

$sess = new SNMP::Session (DestHost => $pm, Community => 'public'); $host = $sess->{DestAddr};

$vars = new SNMP::VarList ( ['livingstonSerialUser','1'], ['livingstonSerialIpAddress', '1'], ['livingstonSerialPortStatus', '1'], ['livingstonSerialStarted', '1'], ['livingstonSerialIdle', '1'], ['livingstonSerialInSpeed', '1'], ['livingstonSerialOutSpeed', '1'], ['livingstonSerialModemCompression', '1'], ['livingstonSerialModemProtocol', '1'], ['livingstonSerialModemRenegotiates', '1'], ['livingstonSerialModemRetrains', '1'], ['livingstonSerialPhysType', '1'] );

@ret = $sess->get ($vars);

while ((@{$vars})[0]->[0] eq "livingstonSerialUser") { ($suser, $sip, $sportstat, $sstarted, $sidle, $sinspeed, $soutspeed, $scompress, $sproto, $sreneg, $sretr, $stype) = @{$vars};

$stat = $sportstat->[2]; if ($stat != 3) { @ret = $sess->getnext($vars); next; }

$user = $suser->[2]; if (($muser ne "") && ($user ne $muser)) { @ret = $sess->getnext($vars); next; } $hostname = host_lookup ($sip->[2], $hostlen, $mydomain); $start = convert_date ($sstarted->[2]); $idle = convert_time ($sidle->[2]); $type = $stype->[2]; $in = $sinspeed->[2]; $out = $soutspeed->[2]; $comp = ("-", "None", "v42bis", "MNP5", "STAC")[$scompress->[2]]; $proto = ("-", "None", "LAPM", "MNP")[$sproto->[2]]; $reneg = $sreneg->[2]; $retr = $sretr->[2];

if ($type != 5) { $con_info = sprintf " %5s %6s", $in, $comp; } else { $con_info = sprintf "%5s/%-5s %4s/%-6s %2s/%-2s", $in, $out, $proto, $comp, $reneg, $retr; }

printf "%${MAXNAMELEN}s %${hostlen}s %11s %5s %s\n", $user, $hostname, $start, $idle, $con_info;

@ret = $sess->getnext($vars); } }

sub convert_time { my ($time) = @_; my ($seconds, $minutes, $hours, $days);

$seconds = $time / 100; $minutes = $seconds / 60; $hours = $minutes / 60; $days = $hours / 24; $seconds %= 60; $minutes %=60; $hours %=24;

if ($minutes == 0) { return ($seconds); } elsif ($hours == 0) { return (sprintf "%d:%02d", $minutes, $seconds); } elsif ($hours < 10) { return (sprintf "%dh:%02d", $hours, $minutes); } elsif ($days == 0) { return (sprintf "%dh", $hours); } elsif ($days < 10) { return (sprintf "%dd:%02d", $days, $hours); } else { return (sprintf "%dd", $days); } }

sub convert_date { my ($time) = @_; my ($start, $sec, $min, $hour, $mday, $mon);

$start = time () - $time / 100; ($sec, $min, $hour, $mday, $mon) = localtime ($start); return (sprintf "%2d/%-2d %2d:%02d", $mon + 1, $mday, $hour, $min); }

sub host_lookup { my ($ip, $hostlen, $mydomain) = @_; my ($iaddr, $host);

if ($fastrun) { return (join ('.', unpack ("C4", $ip))); } $iaddr = inet_aton ($ip); $host = gethostbyaddr ($ip, AF_INET); if (! $host) { $host = join ('.', unpack ("C4", $ip)); } $host =~ s/$mydomain//; return (substr ($host, 0, (-1 * $hostlen))); } ########## CUT HERE ##########

-- 
Chris Adams - cadams@ro.com
System Administrator - Renaissance Internet Services
I don't speak for anybody but myself - that's enough trouble.