#!/usr/bin/perl
# BSD users will need to change this to /usr/local/bin/perl
###############################################################
# By J.R. Greer jrg@albedo.net
# http://www.albedo.net/pub/portmaster/
#
# (c) 1997,98,99 all rights reserved
#
# Version 0.39
#
# The program is placed under the GNU Public License (GPL)
# You may copy freely or modify the code in anyway as long
# as the above information stays intact.
#
#
# This program requires pmwho by johan Persson, jp@abc.se
# path to PMWHO. Make sure pmwho can be executed
$PMWHO = "/bin/pm";
#
# The path and name to the CGI
$CGI = "http://www.domain.com/cgi-bin/pmrtm";
#
# Define Your portmasters here
@PORTMASTERS = ("pma","pmg","pmd");
#
# PORRT is the Key letter to identify the individual Portmasters
# it must match Exactly the portmaster defined above, Letters
# Can NOT be duplicated and should be capitals.
@PORTT=("A","G","D");
#
#
# define the number of seconds for auto refresh.
$REFRESH="60";
#
# These set when the users idle time changes colour
$WYELLOW=6;
$WRED=20;
#
# These set the Online Time colour change (in Hours)
$OYELLOW=2;
$OORANGE=5;
$OPINK=8;
$ORED=18;
# note over 1 day will be red
# this will cause the online time to flash if over 24 hours
$BLINKONDAY=1;
#
# Show tiles
$TITLE=0;
$SDATE=0;
#
# Show Host IP (lets You fit a lot more on the screen)
$SHIP=0;
#
# Do Multilink Test
$MULTI=1;
# turn on IP security
$SECURITY = 1;
#
# A list of IP addresses allowed to use this program
@IPLIST = (
"10.5.122.3",
"10.5.122.41",
"10.5.122.42",
"10.5.122.43",
"10.5.122.48"
);
# Filter from DNS
$FILTER = "albedo.net";
#
# This sets the number of columns wide to set the display
# this can be set to 2,3,4,5 or 6 @PORTS must have the same number
# of entries per line
$WIDTH=5;
#
@PORTS = (
"A0,A1,A2,A3,A4",
"A5,A6,A7,A8,A9",
"A10,A11,A12,A13,A14",
"A15,A16,A17,A18,A19",
"A20,A21,A22,A24,A25",
"A26,A27,A28,A29,A30",
"A31,A32,A33,A34,A35",
"A36,A37,A38,A39,A40",
"A41,A42,A43,A44,A45",
"A46,D0,D1,D2,D3",
"D4,D5,D6,D7,D8",
"D9,D10,D11,D12,D13",
"D14,D15,D16,D17,D18",
"D19,D20,D21,D22,D23",
"DC0,G1,G2,G3,G4",
"G5,G6,G7,G8,G9",
"G10,G11,G12,G13,G14",
"G15,G16,G17,G18,G19",
"G20,G21,G22,G23,G24",
"G25,G26,G27,G28,G29");
#
$NUMPORTS = 98;
#
$DUPEUSERCGI="http://www.domain.com/cgi-bin/mailbaduser";
#
########################
### This will display a LED style bar-graph of current Bandwidth
### Usage at Your router.
### This data is extracted from an patched MRTG generated file
$USE_VU=0;
$USE_VU2=1;
#
## VU_SCALE is the BYTES per second per VU segment
# 128k=800 256kb=1600 300kb=2000 512kb=3200
# T1=9625
$VU_SCALE=2600;
$VU2_SCALE=9625;
# Path to MRTG HTML doc to extract data
$MRTG_FILE="/httpd/htdocs/local/pm/pm2er.12.cur";
$MRTG_FILE2="/httpd/htdocs/local/pm/cisco1.3.cur";
# Path to the .gif files (with trailing slash)
$VU_GIF="/image/vu/";
#
$VU1_TAG="Microlink";
$VU2_TAG="T1   ";
#
##########################################################################
# Do not Edit below this line unless You really understand PERL
##########################################################################
####### First Print the HTML Header
print "Content-type: text/html\n\n";
if ($SECURITY == 1){
foreach (@IPLIST) {
if ($ENV{'REMOTE_ADDR'} eq $_) {$SECURE = 1;}
}
if ($SECURE == 0) {
print "
404 Not Found\n";
print "\n";
print "\n";
exit;
}
}
print "Portmaster User RTM\n";
print "\n";
####################################################################
## Display VU
if ($USE_VU == 1){
$SCALE=$VU_SCALE;
open (VU ,$MRTG_FILE);
$vu = ;
close (VU);
($curin,$curout) = split(':',$vu);
## print "$curin $curout
";
print "$VU1_TAG ";
$t=$curin;
&dgif;
print "Inbound
";
$t = $curout;
&dgif;
print "Outbound
";
print "
\n";
}
## Display VU
if ($USE_VU2 == 1){
$SCALE=$VU2_SCALE;
open (VU ,$MRTG_FILE2);
$vu = ;
close (VU);
($curin,$curout) = split(':',$vu);
## print "$curin $curout
";
print "$VU2_TAG ";
$t=$curin;
&dgif;
print "Inbound
";
$t = $curout;
&dgif;
print "Outbound
";
print "
\n";
}
####### Now login and get the data from the Portmasters
$pms = @PORTMASTERS;
FPM: for ($i=0;$i<$pms;$i++) {
@list=`$PMWHO $PORTMASTERS[$i]`;
foreach (@list) {
&o1;
$port =~ s/S/$PORTT[$i]/;
$port =~ s/C/$PORTT[$i]C/;
&o2;
}
next FPM;
}
####### Now display the data
print "";
if ($TITLE == 1){
print "";
&headof;
if ($WIDTH == 3){ &headof;}
if ($WIDTH == 4){
&headof;
&headof;
}
if ($WIDTH == 5){
&headof;
&headof;
&headof;
}
if ($WIDTH == 6){
&headof;
&headof;
&headof;
&headof;
}
&headof;
print "
";
}
foreach (@PORTS){
if ($WIDTH == 2){
($L1,$L2) = split(/,/,$_);
█
}
if ($WIDTH == 3){
($L1,$L2,$L3) = split(/,/,$_);
█
}
if ($WIDTH == 4){
($L1,$L2,$L3,$L4) = split(/,/,$_);
█
}
if ($WIDTH == 5){
($L1,$L2,$L3,$L4,$L5) = split(/,/,$_);
█
}
if ($WIDTH == 6){
($L1,$L2,$L3,$L4,$L5,$L6) = split(/,/,$_);
█
}
}
print "
";
print "| ";
$T = int(($use*100)/$NUMPORTS);
print "Use $T % | ";
print " |
";
####### Finish the HTML page
print "";
exit;
###############################
# Print one line of the table
sub block {
print "";
$TL1 = $L1;
&plint;
$TL1 = $L2;
&plint;
if ($WIDTH==3){
$TL1 = $L3;
&plint;
}
if ($WIDTH==4){
$TL1 = $L3;
&plint;
$TL1 = $L4;
&plint;
}
if ($WIDTH==5){
$TL1 = $L3;
&plint;
$TL1 = $L4;
&plint;
$TL1 = $L5;
&plint;
}
if ($WIDTH==6){
$TL1 = $L3;
&plint;
$TL1 = $L4;
&plint;
$TL1 = $L5;
&plint;
$TL1 = $L6;
&plint;
}
print "
\n";
}
#######################################
### Extract portmaster data
### There is probably a better way to do this :)
sub o1 {
$t = "$_";
$t =~ s/\s//;
$t =~ s/\n//;
$t =~ s/\r//;
$t =~ s/^(.)(.*)/$1/;
next if ($t eq "-" );
next if ($t eq "P" );
$_ =~ s/\s\s\s\s/\040/g;
$_ =~ s/\s\s\s/\040/g;
$_ =~ s/\s\s/\040/g;
$_ =~ s/\s\s/\040/g;
($port, $user, $host, $x, $x1 ,$x2, $start ,$on) = split(/ /, $_);
if ($X{$user}) {
# Chezzy patch to show mulilink users
if ($Y{$user} eq $host){
print "Multilink User : $user\n"
}
else {
print "Dup User : $user \n"
}
}
}
sub o2 {
$t = "$user";
$t =~ s/\s//;
$t =~ s/\n//;
$t =~ s/\r//;
$t =~ s/^(.)(.*)/$1/;
next if ($t eq "-" );
$PUSER{$port} = $user;
$PHOST{$port} = $host;
$PSTART{$port} = $start;
$PON{$port} = $on;
$X{$user}=1;
$Y{$user}=$host;
}
##########
# Print one segment of the title row
sub headof {
print "Sx | User | ";
if ($SHIP == 1) { print "Host | ";}
print "On | I | | ";
}
###########
# Print one port
sub plint {
### Print Port ###############
print "";
if ($PUSER{$TL1} eq "") {
print "";
}
else {
print "";
$use=$use+1;
}
### Print Username ############
print "$TL1 | $PUSER{$TL1} | ";
if ($SHIP == 1) {
print "$PHOST{$TL1} | ";
}
### Print online Time
$FT="";
$FF="";
$PT=$PSTART{$TL1};
if ($PT =~ ":"){
$PT =~ y/:/\./;
if ($PT >= $OYELLOW){
$FT="";
$FF="";
}
if ($PT >= $OORANGE){
$FT="";
$FF="";
}
if ($PT >= $OPINK){
$FT="";
$FF="";
}
if ($PT >= $ORED){
$FT="";
$FF="";
}
}
if ($PT =~ "days"){
if ($BLINKONDAY ==1){
$FT="";
}
else{
$FT="";
$FF="";
}
}
print "$FT$PSTART{$TL1}$FF | ";
### Print Idle Time
if ($PON{$TL1} <= ($WYELLOW-1)) {
print "";
}
if ($PON{$TL1} >= $WYELLOW) {
if ($PON{$TL1} >= $WRED) {
print "";
}
else {
print "";
}
}
print "$PON{$TL1} | | \n";
}
#############################
#
sub dgif {
if ($t >= ($SCALE*22)) {$gif="22m.gif";}
elsif ($t >= ($SCALE*21)) {$gif="21m.gif";}
elsif ($t >= ($SCALE*20)) {$gif="20m.gif";}
elsif ($t >= ($SCALE*19)) {$gif="19m.gif";}
elsif ($t >= ($SCALE*18)) {$gif="18m.gif";}
elsif ($t >= ($SCALE*17)) {$gif="17m.gif";}
elsif ($t >= ($SCALE*16)) {$gif="16m.gif";}
elsif ($t >= ($SCALE*15)) {$gif="15m.gif";}
elsif ($t >= ($SCALE*14)) {$gif="14m.gif";}
elsif ($t >= ($SCALE*13)) {$gif="13m.gif";}
elsif ($t >= ($SCALE*12)) {$gif="12m.gif";}
elsif ($t >= ($SCALE*11)) {$gif="11m.gif";}
elsif ($t >= ($SCALE*10)) {$gif="10m.gif";}
elsif ($t >= ($SCALE*9)) {$gif="9m.gif";}
elsif ($t >= ($SCALE*8)) {$gif="8m.gif";}
elsif ($t >= ($SCALE*7)) {$gif="7m.gif";}
elsif ($t >= ($SCALE*6)) {$gif="6m.gif";}
elsif ($t >= ($SCALE*5)) {$gif="5m.gif";}
elsif ($t >= ($SCALE*4)) {$gif="4m.gif";}
elsif ($t >= ($SCALE*3)) {$gif="3m.gif";}
elsif ($t >= ($SCALE*2)) {$gif="2m.gif";}
elsif ($t >= $SCALE) {$gif="1m.gif";}
else {$gif="0m.gif";}
}
#####################################################################
## receive & Decode the html data
sub cgi_receive {
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
}
else {
$incoming = $ENV{'QUERY_STRING'}; # this should never happen
}
@pairs = split(/&/, $incoming);
foreach (@pairs) {
($name, $value) = split(/=/, $_);
$name =~ tr/+/ /;
$value =~ tr/+/ /;
$name =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
$value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
$value =~ s/;/$$/g; # Strip out semicolons
$value =~ s/&(\S{1,6})$$/&\1;/g;
$value =~ s/$$/ /g;
$value =~ s/\|/ /g;
$value =~ s/^!/ /g;
#$value =~ s/[\/ ;\[\]<\>&\t]/_/g; #Sanitize!
next if ($value eq "");
if ($name =~ /^assign-dynamic/) {
$name = $value;
$value = "on";
}
$FINPUT{$name} .= ", " if ($FINPUT{$name});
$FINPUT{$name} .= $value;
}
}