Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2115218
  • 博文数量: 227
  • 博客积分: 10521
  • 博客等级: 上将
  • 技术积分: 3452
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-20 14:59
个人简介

低调做人,高调做事!

文章分类

全部博文(227)

文章存档

2013年(4)

2012年(8)

2011年(16)

2010年(24)

2009年(92)

2008年(83)

分类: LINUX

2010-06-24 16:50:56

#!/usr/bin/perl

use IO::Socket;
use Net::SMTP_auth;
use strict;



# mail
use constant MAIL_PORTS   => ('25','110');
use constant MAIL_SERVERS => ('10.10.10.10');



my $mailhost = 'smtp.163.com';
my $mailfrom = 'test@163.com';
my @mailto   = '139xxxxxxx@139.com';
my $subject  = '';

my $user   = 'test';
my $passwd = 'test';





##############################
# Server port is alive check
##############################
sub check_server_alive {
    my ( $server, $port ) = @_;
    my $sock = IO::Socket::INET->new(
        PeerAddr => $server,
        PeerPort => $port,
        Proto    => 'tcp',
        Timeout  => 6
    );
    if ( !$sock ) {
        return 0;
    }
    $sock->close();
    return 1;
}

##############################
# Send notice mail
##############################
sub SendMail() {

    my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 1 )
      or die "Error.\n";
    $smtp->auth( 'LOGIN', $user, $passwd );

    foreach my $mailto (@mailto) {
        $smtp->mail($mailfrom);
        $smtp->to($mailto);
        $smtp->data();
        $smtp->datasend("To: $mailto\n");
        $smtp->datasend("From:$mailfrom\n");
        $smtp->datasend("Subject: $subject\n");
        $smtp->datasend("\n");
        $smtp->dataend();
    }

    $smtp->quit;
}


##############################
# Check server alive main
##############################
sub monitor_main {


    # check mail
    foreach my $s_tem (MAIL_SERVERS) {
        foreach my $p_tem (MAIL_PORTS) {

            if ( !&check_server_alive( $s_tem, $p_tem ) ) {


            print  "$s_tem mail server $p_tem port is fail!" . "\n";
            $subject = "$s_tem mail server $p_tem port is fail";
            &SendMail();
            }
        }
    }


}





##############################
# Main running
##############################

&monitor_main();
阅读(2261) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

wiliiwin2010-06-28 09:49:01

呵呵 不错 socket也用上了 学习了