Jump to content

Obsolete:Lvsmon

From Wikitech
This page contains historical information. It may be outdated or unreliable.

lvsmon was a background script written in PHP to monitor status of apaches and configure an LVS pool accordingly. This has been obsoleted by PyBal.

#!/usr/local/bin/php
<?php
# vim: syn=php

# Configuration
$delay = 5;
$vip = "10.0.5.3";
$testUrl = "http://en.wikipedia.org/w/health-check.php";
$timeout = 20;
$minHosts = 10;


$apachesFile = file( '/home/config/others/usr/local/dsh/node_groups/apaches' );
if ( !$apachesFile ) {
        die( "Could not open apache node list\n" );
}

`ipvsadm -C`;
`ipvsadm -A -t $vip:80`;

$apaches = array();
$names = array();
foreach ( $apachesFile as $line ) {
        $line = trim( $line );
        if ( $line != '' && $line{0} != '#' ) {
                $ip = gethostbyname( $line );
                if ( $ip ) {
                        $apaches[] = $ip;
                        $names[$ip] = $line;
                        `ipvsadm -a -t $vip:80 -r $ip`;
                } else {
                        print "Warning, host not found: $line\n";
                }
        }
}

$minHosts = count( $apaches ) * 0.4;
$downList = array();

print "Monitoring " . count( $apaches ) . " hosts\n";

# Fast scan to start off

foreach ( $apaches as $apache ) {
        $name = $names[$apache];
        print "Checking $name...";
        $down = isDown( $apache, 2 );
        if ( $down ) {
                if ( count($apaches) -  count($downList) > $minHosts ) {
                        print " down\n";
                        `ipvsadm -d -t $vip:80 -r $apache`;
                        $downList[$apache] = true;
                } else {
                        print "Too many hosts down, leaving {$names[$apache]} in rotation\n";
                }
        } else {
                print "\n";
        }
}

# Now do a continuous slow scan

while ( 1 ) {
        foreach ( $apaches as $apache ) {
                $name = $names[$apache];
                print "Checking $name...";
                $down = isDown( $apache, $timeout );
                $wasDown = !empty( $downList[$apache] );
                if ( !$down && $wasDown ) {
                        print " up\n";
                        `ipvsadm -a -t $vip:80 -r $apache`;
                        unset( $downList[$apache] );
                } elseif ( $down && !$wasDown ) {
                        if ( count($apaches) -  count($downList) > $minHosts ) {
                                print " down\n";
                                `ipvsadm -d -t $vip:80 -r $apache`;
                                $downList[$apache] = true;
                        } else {
                                print "Too many hosts down, leaving {$names[$apache]} in rotation\n";
                        }
                } else {
                        print "\n";
                }
        }
        sleep($delay);
}

#--------------------------------------------------------------------------


function isDown( $apache, $timeout ) {
        global $testUrl, $minHosts, $names;

        $c = curl_init( $testUrl );
        curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
        curl_setopt( $c, CURLOPT_PROXY, "$apache:80" );
        ob_start();
        curl_exec( $c );
        $text = ob_get_contents();
        ob_end_clean();
        curl_close( $c );
        if ( $text ) {
                return false;
        } else {
                return true;
        }
}