perl

#!/usr/bin/perl -w  
use IO::Socket;
use Net::Ping;  
use Net::SMTP;  
use MIME::Base64;
use IPC::ShareLite;
use Storable qw(freeze thaw);
use IPC::SysV qw(S_IRWXU IPC_CREAT);
use IPC::Semaphore;
use warnings;


## Server Test
## IPC
sub sendmail {  
        my $mailhost = "smtp.163.com"; # the smtp host  
        my $mailfrom = 'ZZZ'; # your email address  
        my $mailto='ZZZ';  
        my $subject=shift ||'Server Down';  
        my $text = shift || "SOS";  
        $smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout =>120, Debug => 1);  
        $smtp->auth('ZZZ','ZZZ');  
 
        $smtp->mail($mailfrom);  
        $smtp->to($mailto);  
        $smtp->data();  
        $smtp->datasend("Content-Type:text/html;charset=utf-8\n");  
        $smtp->datasend("Content-Transfer-Encoding:base64\n");  
        $smtp->datasend("To:=utf-8".encode_base64($mailto,'')."= <$mailto> \n");  
        $smtp->datasend("From:=utf-8".encode_base64($mailfrom,'')."= <$mailfrom> \n");  
        #$smtp->datasend("Subject:".encode_base64($subject,'')."\n");  
        $smtp->datasend("Subject:".$subject."\n");  
        $smtp->datasend("\n");  
        $smtp->datasend(encode_base64($text,'')." \n");  
        $smtp->dataend();  
}

my $gping = Net::Ping->new("icmp");  
my $gtimeout = 5;

sub is_serving{
    my $ret = 0;
    my $host = shift || $_[0];
    my $port = shift || $_[1] || 80;
    
    # check the server host
    unless($gping->ping($host,3)){  
            print "$host is down\n" ;
            #sendmail ($host,"server ".$host." is need your help, please!");
            return $ret;
    }  

    # check the server port
    my $sock = new IO::Socket::INET(PeerAddr=>$host,PeerPort=>$port,Proto=>'tcp',Timeout  =>$gtimeout );
    if($sock) {
        $ret = 1;
        close $sock or die "close: $!";
    } else {
        $ret = 0;
        #sendmail ($host,"server ".$host." is need your help, please!");
        print $host."is down\n";
    }
    
    return $ret;
}


my @prim_list = qw(192.168.1.6 192.168.1.235 192.168.1.124);
my @serv_list = qw();
my $last_check_stamp = 0;

my $share = IPC::ShareLite->new(
        -key     => 1949,
        -create => 'yes',
        -destroy => 'yes'
) or die $!;


my $sem = IPC::Semaphore->new(1564, 1, S_IRWXU|IPC_CREAT) || die "IPC::Semaphore->new: $!\n";

sub handler {

            if ($last_check_stamp == 0) {
                # Write All Server to Share Memory
                my $t = \@prim_list;
                $sem->setval(0,1);
                $share->store(freeze($t));
                $sem->setval(0,0);
                print "Init Shared Memory\n";
            }
            if((time() - $last_check_stamp) > 30){
                # clear all elements
                print "Checking...\n";
                splice(@serv_list);
                foreach $host (@prim_list){  
                        if(is_serving ($host,80)){
                            push(@serv_list,$host);
                            print $host."\n";
                        }
                }
                # Write to Shared Memory
                $t = \@serv_list;
                $sem->setval(0,1);
                $share->store(freeze($t));
                $sem->setval(0,0);
                $last_check_stamp = time();
            }
        sleep(5);
}

while (1){
    handler();
}


print time();




你可能感兴趣的:(perl)