初学perl 写了个lvs监控的脚本 请各位纠错 谢谢
duketang
|
1#
duketang 发表于 2005-11-14 14:24
初学perl 写了个lvs监控的脚本 请各位纠错 谢谢
主要是为了监控realserver 还有 master_dr slave_dr的状态
这两天学perl 弄了这么一个东西 请大家帮忙看看什么地方可以改好些 因为刚学 所以用到perl的定义包 想练练手 像hidden forwa....等东西 还是在正常环境下启动时加上 master dr 在cluster.pl中 把SERVER_TYPE改成master slave dr 则改成slave cluster.conf
[Copy to clipboard] [ - ]
CODE:
##############
# Director # ############## vhost_ip=192.168.0.200 master_dr=192.168.0.201 slave_dr=192.168.0.202 ############## # RealServer # ############## port=80 realserver=192.168.0.201 realserver=192.168.0.202 realserver=*** port=21 realserver=*** cluster.pl
[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl
use strict; use IO::Socket; use CONF; use Net::Ping; my %DR_CONF = (); my @SERVER_CONF = (); my $SERVER_TYPE = "slave"; my $master_status = "ON"; my $chkofping; my @REAL_STATUS = (); my $SERVICE_STATUS = "START"; %DR_CONF = CONF::GET(); @SERVER_CONF = CONF::GET_GROUP(); @REAL_STATUS = $CONF::REAL_SERVER_STATUS; $chkofping = Net::Ping->new(); my $OFF_COUNT=0; my $FOR_COUNT; my $GO_COUNT=0; print "Vhost ip:".$DR_CONF{vhost_ip}."n"; print "Master dr :".$DR_CONF{master_dr}."n"; print "Slave dr :".$DR_CONF{slave_dr}."n"; while(my $q=1 < 2) { if($SERVER_TYPE eq "slave") { if($chkofping->ping($DR_CONF{master_dr})){ if($master_status eq "OFF") { system("ifconfig eth1 down"); system("ifconfig lo:0 ".$DR_CONF{vhost_ip}." netmask 255.255.255.255 broadcast"); system("route add -host ".$DR_CONF{vhost_ip}." dev lo:0"); system("echo 1 > /proc/sys/net/ipv4/conf/all/hidden"); system("echo 1 > /proc/sys/net/ipv4/conf/lo/hidden"); } $master_status = "ON"; $OFF_COUNT=0; }else{ $OFF_COUNT++; if($OFF_COUNT == 3) { system("ifconfig lo:0 down"); system("ifconfig eth1 ".$DR_CONF{vhost_ip}." netmask 255.255.255.255 broadcast"); system("route -vF del ".$DR_CONF{vhost_ip}." dev lo:0"); system("echo 0 > /proc/sys/net/ipv4/conf/all/hidden"); system("echo 0 > /proc/sys/net/ipv4/conf/lo/hidden"); $master_status = "OFF"; } } print "OFF COUNT:".$OFF_COUNT."n"; } if(($master_status eq "OFF" and $SERVER_TYPE eq "slave") || $SERVER_TYPE eq "master") { my $i=1; if($SERVICE_STATUS eq "START") { system("ipvsadm -C"); while($i < scalar(@SERVER_CONF)) { system("ipvsadm -A -t ".$DR_CONF{vhost_ip}.":".$SERVER_CONF[$i][0]." -s wlc"); my $run_count=1; while($run_count <= (scalar(@{$SERVER_CONF[$i]})-1)) { system("ipvsadm -a -t ".$DR_CONF{vhost_ip}.":".$SERVER_CONF[$i][0]." -r ".$SERVER_CONF[$i][$run_count][0]." -g"); $run_count++; } $i++; } $i = 1; $SERVICE_STATUS="RUNING"; } while($i < scalar(@SERVER_CONF)) { sleep 1; print "Server Group port :".$SERVER_CONF[$i][0]."n"; print "Server Num :".(scalar(@{$SERVER_CONF[$i]})-1)."n"; my $server_while=1; while($server_while <= (scalar(@{$SERVER_CONF[$i]})-1)) { my $socket_test; if($socket_test=IO::Socket::INET->new(PeerAddr=>$SERVER_CONF[$i][$server_while][0],PeerPort=>$SERVER_CONF[$i][0],Proto=>"tcp",Type=>SOCK_STREAM)) { if($SERVER_CONF[$i][$server_while][1] eq "OFF") { system("ipvsadm -a -t ".$DR_CONF{vhost_ip}.":".$SERVER_CONF[$i][0]." -r ".$SERVER_CONF[$i][$server_while][0]." -g"); } $SERVER_CONF[$i][$server_while][1] = "ON"; }else{ if($SERVER_CONF[$i][$server_while][1] eq "ON") { system("ipvsadm -d -t ".$DR_CONF{vhost_ip}.":".$SERVER_CONF[$i][0]." -r ".$SERVER_CONF[$i][$server_while][0]); } $SERVER_CONF[$i][$server_while][1] = "OFF"; } close($socket_test); print "Real Server status:".$SERVER_CONF[$i][$server_while][1]." IP addres:".$SERVER_CONF[$i][$server_while][0]."n"; $server_while++; } $i++; } }else{ sleep 1; system("service ipvsadm stop"); $SERVICE_STATUS="START"; } } CONF.pm
[Copy to clipboard] [ - ]
CODE:
package CONF;
BEGIN{ } sub GET { my $CONF_FILE="cluster.conf"; my $DATA; my $CONF; if(open(CONF_FILE,$CONF_FILE)) { my $DATA; my $CONF; while($DATA = <CONF_FILE>) { if($DATA !~ /#/) { split(/=/,$DATA); if($_[0] eq "vhost_ip") { $CONF{vhost_ip} = $_[1]; chomp($CONF{vhost_ip}); }elsif($_[0] eq "master_dr"){ $CONF{master_dr} = $_[1]; chomp($CONF{master_dr}); }elsif($_[0] eq "slave_dr"){ $CONF{slave_dr} = $_[1]; chomp($CONF{slave_dr}); } } } close(CONF_FILE); }else{ print "ERRORtCan't load $CONF_FILE file!n"; } return %CONF; } sub GET_GROUP { my $CONF_FILE="cluster.conf"; my $DATA; if(open(CONF_FILE,$CONF_FILE)) { my $DATA; my $SERVER_CONF; my $SERVER_GROUP = 1; my $SERVER_COUNT = 1; while($DATA = <CONF_FILE>) { if($DATA !~ /#/) { split(/=/,$DATA); if($_[0] eq "port"){ $SERVER_CONF[$SERVER_GROUP][0] = $_[1]; chomp($SERVER_CONF[$SERVER_GROUP][0]); $SERVER_GROUP++; $SERVER_COUNT = 1; }elsif($_[0] eq "realserver"){ $SERVER_CONF[$SERVER_GROUP-1][$SERVER_COUNT][0] = $_[1]; $SERVER_CONF[$SERVER_GROUP-1][$SERVER_COUNT][1] = "ON"; chomp($SERVER_CONF[$SERVER_GROUP-1][$SERVER_COUNT][0]); chomp($SERVER_CONF[$SERVER_GROUP-1][$SERVER_COUNT][1]); $SERVER_COUNT++; $REAL_SERVER_COUNT++; } } } close(CONF_FILE); }else{ print "ERRORtCan't load $CONF_FILE file!n"; } return @SERVER_CONF; } return 1; END{ } |