Load & Connections

{code}
#!/usr/bin/perl

use strict;
use warnings;

my @net = ();
my @active_con = ();
my $db_con = 0;
my %clients = ();
my $local = 0;
my $non_local = 0;


@net = `netstat -an`;

# clean up the raw imput to leave us with just the established
# connections to tcp addresses (this leaves out listening
# local ports and waiting connections)

foreach (@net) {
my ($proto,$bytes1,$bytes2,$from,$to,$status) = split " ";
if ($proto eq "tcp" && $status eq "ESTABLISHED") {
push @active_con, $to;
}
}

# add up the connetions to the db and toss the rest into
# a hash so the duplicates arn't counted, as one client
# (web browser) will ofen have several connetions to
# the server

foreach (@active_con) {
if (/132.235.8.154/) { # The orda database
$db_con ++;
}
else {
($_) = split ":"; # get rid of the port
$clients{$_} = (); # store in a hash to get rid of any duplicates
}
}

# Now that we have a list of non duplicates, lets scan through it
# and count the local verses non-local ones
foreach (keys %clients) {
if (/^132.235/) { # if the client is on OU's network
$local++;
}
else {
$non_local++;
}
}

# Grab the time and date
my ($Second, $Minute, $Hour, $Day, $Month, $Year) = localtime(time);
$Month++;


my $time = "$Month/$Day/$Year - $Hour:$Minute:$Second";

$time = localtime(time);


#Grab the load average too
open IN, "</proc/loadavg";
my $load = <IN>;
($load) = split " ", $load;
$load = $load * 100;
$load = "$load \%";
close IN;


# Output to the end of a text file
open OUT, ">>/usr/share/tomcat/webapps/sys/netload.txt";
print OUT $time . "\t";
print OUT @active_con . "\t";
print OUT (keys %clients) . "\t";
print OUT $local . "\t";
print OUT $non_local . "\t";
print OUT $db_con . "\t";
print OUT $load . "\t";
print OUT "\n";
close OUT;


## Now lets add a 'most recent' by tailing that big 'ol file we've been creating
open OUT, ">/usr/share/tomcat/webapps/sys/recent.txt";
print OUT `tail -40 /usr/share/tomcat/webapps/sys/netload.txt | tac`;
close OUT;
{code}
Comments