Cisco CSS load balance weighted round-robin perl script

For the archives, and since I didn't find anything when I searched for it. I wrote this script to log into our Cisco CSS 11503 and change the weight of each service so that when a machine is in trouble, traffic is directed away from it to the under utilized machines.

Cisco CSS load balance weighted round-robin perl script - download here:

formatting link
#!/usr/bin/perl

use Sys::Hostname; use POSIX qw(setsid); use Net::SNMP; use Net::Telnet;

chdir `/=E2=80=99; umask 0; open STDIN,`/dev/null=E2=80=99; open STDERR,`>/dev/null=E2=80=99; defined(my $pid =3D fork); exit if $pid; setsid;

while(1) { ###Do the below every minute

sleep 60;

###This connects to each host and gets the 1 minute load average via snmp, you need snmp install ###and configured on each host of course. You will need one of ###these for each host behind the ###CSS. This give an example of 2 machines behind the CSS, but of course you can have more by ###duplicating the sections of code below and changing the variables.

my ($session, $error) =3D Net::SNMP->session(

-hostname =3D> shift || `192.168.0.1=E2=80=B2 ,

-community =3D> shift || `public=E2=80=99,

-port =3D> shift || 161 );

if (!defined($session)) { printf(=E2=80=9DERROR: %s.\\n=E2=80=9D, $error); }

my $sysUpTime =3D =E2=80=98.1.3.6.1.4.1.2021.10.1.3.1=E2=80=B2;

my $result =3D $session->get_request(

-varbindlist =3D> [$sysUpTime] );

if (!defined($result)) { printf(=E2=80=9DERROR: %s.\\n=E2=80=9D, $session->error); $session->close; } $sc =3D (=E2=80=9D%s=E2=80=9D, $result->{$sysUpTime}); $session->close;

my ($session, $error) =3D Net::SNMP->session(

-hostname =3D> shift || =E2=80=98192.168.0.2=E2=80=B2,

-community =3D> shift || =E2=80=98public=E2=80=99,

-port =3D> shift || 161 );

if (!defined($session)) { printf(=E2=80=9DERROR: %s.\\n=E2=80=9D, $error); }

my $sysUpTime =3D =E2=80=98.1.3.6.1.4.1.2021.10.1.3.1=E2=80=B2;

my $result =3D $session->get_request(

-varbindlist =3D> [$sysUpTime] );

if (!defined($result)) { printf(=E2=80=9DERROR: %s.\\n=E2=80=9D, $session->error); $session->close; } $sc =3D (=E2=80=9D%s=E2=80=9D, $result->{$sysUpTime}); $session->close;

##This is the load value when your servers get freaky and start malfunctioning.

$loadvalue =3D =E2=80=9C7.00=E2=80=B3;

if ($sc > $loadvalue){ $scweight =3D =E2=80=9C1=E2=80=B3; } if ($sc 10, Prompt =3D> =E2=80=98/ $/=E2=80=99= ); $t->open($host); $t->login(=E2=80=99Username=E2=80=99, =E2=80=98Password=E2=80=99); $t->cmd(=E2=80=9Dconf t=E2=80=9D);

#machine 1 smtp port 25 $t->cmd(=E2=80=9Dservice smtp=E2=80=9D); $t->cmd(=E2=80=9Dweight $scweight=E2=80=9D);

#machine 1ftp port 21 $t->cmd(=E2=80=9Dservice ftp21=E2=80=B3);

#machine 1 http port 80 $t->cmd(=E2=80=9Dservice http=E2=80=9D); $t->cmd(=E2=80=9Dweight $scweight=E2=80=9D);

#machine 1 pop3 port 110 $t->cmd(=E2=80=9Dservice pop3=E2=80=B3); $t->cmd(=E2=80=9Dweight $scweight=E2=80=9D);

#machine 1imap port 143 $t->cmd(=E2=80=9Dservice imap=E2=80=9D); $t->cmd(=E2=80=9Dweight $scweight=E2=80=9D);

#machine 1 https port 443 $t->cmd(=E2=80=9Dservice ssl=E2=80=9D); $t->cmd(=E2=80=9Dweight $scweight=E2=80=9D); }

###Change the CSS for machine 2

open(TI, =E2=80=9C/tmp/ti.status=E2=80=9D); print TI =E2=80=9C$tiweight=E2=80=9D; close TI; ##Start the CSS login and do stuff $host =3D =E2=80=9Ccss.domain.com=E2=80=9D; $login =3D =E2=80=9CUsername=E2=80=9D; $password =3D =E2=80=9CPassword=E2=80=9D; $t =3D new Net::Telnet (Timeout =3D> 10, Prompt =3D> =E2=80=98/ $/=E2=80=99= ); $t->open($host); $t->login(=E2=80=99Username=E2=80=99, =E2=80=98Password=E2=80=99); $t->cmd(=E2=80=9Dconf t=E2=80=9D);

#machine 2 ftp port 20 $t->cmd(=E2=80=9Dservice ftp20=E2=80=B3); $t->cmd(=E2=80=9Dweight $tiweight=E2=80=9D);

#machine 2 smtp port 25 $t->cmd(=E2=80=9Dservice smtp=E2=80=9D); $t->cmd(=E2=80=9Dweight $tiweight=E2=80=9D);

#machine 2 ftp port 21 $t->cmd(=E2=80=9Dservice ftp21=E2=80=B3); $t->cmd(=E2=80=9Dweight $tiweight=E2=80=9D);

#machine 2 http port 80 $t->cmd(=E2=80=9Dservice http=E2=80=9D); $t->cmd(=E2=80=9Dweight $tiweight=E2=80=9D); #machine 2 pop3 port 110 $t->cmd(=E2=80=9Dservice pop3=E2=80=B3); $t->cmd(=E2=80=9Dweight $tiweight=E2=80=9D);

#machine 2 imap port 143 $t->cmd(=E2=80=9Dservice imap=E2=80=9D); $t->cmd(=E2=80=9Dweight $tiweight=E2=80=9D);

#machine 2 https port 443 $t->cmd(=E2=80=9Dservice ssl=E2=80=9D); $t->cmd(=E2=80=9Dweight $tiweight=E2=80=9D);

} }
Reply to
Michael Osten
Loading thread data ...

Cabling-Design.com Forums website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.