#!/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## IPCsub 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,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,0); $last_check_stamp = time(); } sleep(5);}while (1){ handler();}print time();总结
以上是内存溢出为你收集整理的perl全部内容,希望文章能够帮你解决perl所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)