Cisco CSS load balance weighted round-robin perl script

Discussion in 'Cisco' started by Michael Osten, Feb 14, 2007.

  1. 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: http://www.bleepyou.com/wordpress/?p=12

    #!/usr/bin/perl

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

    chdir `/’;
    umask 0;
    open STDIN,`/dev/null’;
    open STDERR,`>/dev/null’;
    defined(my $pid = 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) = Net::SNMP->session(
    -hostname => shift || `192.168.0.1′ ,
    -community => shift || `public’,
    -port => shift || 161
    );

    if (!defined($session)) {
    printf(â€ERROR: %s.\nâ€, $error);
    }

    my $sysUpTime = ‘.1.3.6.1.4.1.2021.10.1.3.1′;

    my $result = $session->get_request(
    -varbindlist => [$sysUpTime]
    );

    if (!defined($result)) {
    printf(â€ERROR: %s.\nâ€, $session->error);
    $session->close;
    }
    $sc = (â€%sâ€, $result->{$sysUpTime});
    $session->close;

    my ($session, $error) = Net::SNMP->session(
    -hostname => shift || ‘192.168.0.2′,
    -community => shift || ‘public’,
    -port => shift || 161
    );

    if (!defined($session)) {
    printf(â€ERROR: %s.\nâ€, $error);
    }

    my $sysUpTime = ‘.1.3.6.1.4.1.2021.10.1.3.1′;

    my $result = $session->get_request(
    -varbindlist => [$sysUpTime]
    );

    if (!defined($result)) {
    printf(â€ERROR: %s.\nâ€, $session->error);
    $session->close;
    }
    $sc = (â€%sâ€, $result->{$sysUpTime});
    $session->close;

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

    $loadvalue = “7.00″;

    if ($sc > $loadvalue){
    $scweight = “1″;
    }
    if ($sc <= $loadvalue){
    $scweight = “10″;
    }

    ###This is where it logs into the CSS and sets the weight for the
    service for this machine.

    open(SC, “</tmp/sc.statusâ€);
    chomp($line = <SC>);
    close SC;
    if ($line eq $scweight){
    print “SC: Do Nothing\nâ€;
    }
    if ($line ne $scweight){
    print “SC: Do Something\nâ€;
    open(SC, “>/tmp/sc.statusâ€);
    print SC “$scweightâ€;
    close SC;
    ###Start the CSS login and do stuff
    $host = “css.domain.comâ€;
    $login = “Usernameâ€;
    $password = “Passwordâ€;
    #
    $t = new Net::Telnet (Timeout => 10, Prompt => ‘/ $/’);
    $t->open($host);
    $t->login(’Username’, ‘Password’);
    $t->cmd(â€conf tâ€);

    #machine 1 smtp port 25
    $t->cmd(â€service smtpâ€);
    $t->cmd(â€weight $scweightâ€);

    #machine 1ftp port 21
    $t->cmd(â€service ftp21″);

    #machine 1 http port 80
    $t->cmd(â€service httpâ€);
    $t->cmd(â€weight $scweightâ€);

    #machine 1 pop3 port 110
    $t->cmd(â€service pop3″);
    $t->cmd(â€weight $scweightâ€);

    #machine 1imap port 143
    $t->cmd(â€service imapâ€);
    $t->cmd(â€weight $scweightâ€);

    #machine 1 https port 443
    $t->cmd(â€service sslâ€);
    $t->cmd(â€weight $scweightâ€);
    }

    ###Change the CSS for machine 2

    open(TI, “</tmp/ti.statusâ€);
    chomp($line = <TI>);
    close TI;
    if ($line eq $tiweight){
    print “TI: Do Nothing\nâ€;
    }
    if ($line ne $tiweight){
    print “TI: Do Something\nâ€;
    open(TI, “>/tmp/ti.statusâ€);
    print TI “$tiweightâ€;
    close TI;
    ##Start the CSS login and do stuff
    $host = “css.domain.comâ€;
    $login = “Usernameâ€;
    $password = “Passwordâ€;
    $t = new Net::Telnet (Timeout => 10, Prompt => ‘/ $/’);
    $t->open($host);
    $t->login(’Username’, ‘Password’);
    $t->cmd(â€conf tâ€);

    #machine 2 ftp port 20
    $t->cmd(â€service ftp20″);
    $t->cmd(â€weight $tiweightâ€);

    #machine 2 smtp port 25
    $t->cmd(â€service smtpâ€);
    $t->cmd(â€weight $tiweightâ€);

    #machine 2 ftp port 21
    $t->cmd(â€service ftp21″);
    $t->cmd(â€weight $tiweightâ€);

    #machine 2 http port 80
    $t->cmd(â€service httpâ€);
    $t->cmd(â€weight $tiweightâ€);
    #machine 2 pop3 port 110
    $t->cmd(â€service pop3″);
    $t->cmd(â€weight $tiweightâ€);

    #machine 2 imap port 143
    $t->cmd(â€service imapâ€);
    $t->cmd(â€weight $tiweightâ€);

    #machine 2 https port 443
    $t->cmd(â€service sslâ€);
    $t->cmd(â€weight $tiweightâ€);

    }

    }
    Michael Osten, Feb 14, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff
    Replies:
    1
    Views:
    1,605
    dmcknigh
    Nov 16, 2003
  2. hplim
    Replies:
    0
    Views:
    405
    hplim
    Aug 2, 2007
  3. business one way

    Is it a Perl program or a Perl script?

    business one way, Jan 5, 2008, in forum: Digital Photography
    Replies:
    0
    Views:
    688
    business one way
    Jan 5, 2008
  4. short circuit

    charging battery "round robin" style

    short circuit, Jun 28, 2009, in forum: Computer Support
    Replies:
    6
    Views:
    495
    Whiskers
    Jun 29, 2009
  5. tonionio

    VHDL Round Robin Arbiter help

    tonionio, Jun 6, 2012, in forum: Software
    Replies:
    0
    Views:
    1,558
    tonionio
    Jun 6, 2012
Loading...

Share This Page