Load Balancer Server Monitor (perl/CGI)

#!/usr/bin/perl
use strict;
print "Content-type: text/htmlnn";
print "<HEAD>n<TITLE>Load Balancer Status</TITLE>n</HEAD>n<BODY>n";
print "<style type="text/css"> np.normal {color: white; background-color: green;} np.warning {color: black; background-color: yellow;} np.danger {color: white; background-color: red;}np#total {font:bold;}</STYLE>n";

# use module
use XML::Simple;
use Data::Dumper;
use LWP::UserAgent;
use Scalar::Util 'reftype';

# Poll Origin server for list of active edge servers

my $lb_ua = new LWP::UserAgent;
my $lb_url = "http://wms.rezonline.org:1935/loadbalancer?serverInfoXML";
my $lb_req = new HTTP::Request GET => $lb_url;
my $lb_res = $lb_ua->request($lb_req);
my $lb_xml = new XML::Simple;
my $lb_data = $lb_xml->XMLin($lb_res->content);
my @lb_ip;
my %connections;
my %status;
my $total=0;
my $running=0;
my $capacity=250;

# Dump raw XML (for debugging)
# print Dumper($lb_data);

# check reference type:
# if HASH, only single server is running, and XML returns without an array wrapping single element
# if ARRAY, multiple servers, each one inside an array element.

my $reference = reftype $lb_data->{LoadBalancerServer};        # Check to see how to handle this...

if ( $reference eq 'ARRAY' )
{
for my $lb_server (@{$lb_data->{LoadBalancerServer}})
{
push @lb_ip, $lb_server->{redirect};
$connections{$lb_server->{redirect}} = $lb_server->{connectCount};
$status{$lb_server->{redirect}} = $lb_server->{status};
}
}

elsif ( $reference eq 'HASH' )
{
if ($lb_data->{LoadBalancerServer}->{status} eq "RUNNING")
{
push @lb_ip, $lb_data->{LoadBalancerServer}->{redirect};
$connections{$lb_data->{LoadBalancerServer}->{redirect}} = $lb_data->{LoadBalancerServer}->{connectCount};
$status{$lb_data->{LoadBalancerServer}->{redirect}} = $lb_data->{LoadBalancerServer}->{status};

}
}

else {
print "unknown reference type - abortingn";
}

my $arraysize = @lb_ip;
my $style;
foreach (@lb_ip) {

my $load = ($connections{$_} / $capacity)*100;
if ($load > 85) {
$style = "danger";
}
elsif ( $load >75) {
$style = "warning";
}
else    {
$style = "normal";
}

if ($status{$_} eq "RUNNING") {
if ($_ eq "50.19.92.63") { print "<I>"; }
print "<P class="$style">Repeater $_ is $status{$_} with $connections{$_} connections ($load %)</P>n";
if ($_ eq "50.19.92.63") { print "</I>"; }
$total += $connections{$_};
$running ++;
}
}
my $load = ($total / ($running * $capacity)) * 100;

if ($load > 85) {
$style = "danger";
}
elsif ( $load >75) {
$style = "warning";
}
else    {
$style = "normal";
}

print "<P id="total" class="$style">Total of $total connections on $running active servers ($load %)</P>n";

print "</BODY>n</HTML>";