手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

使用Perl实现系统服务监控和报警

[日期:2009-03-30] 来源:IT168  作者:heiyeluren [字体:大 中 小]

  说明:以下Perl代码在 RHEL 4 + Perl v5.8.6 环境下测试通过

  #!/usr/bin/perl
  use IO::Socket;
  use IO::File;
  use MIME::Base64;

  ##############################
  # Constant define (configure)
  ##############################
  # mail config
  use constant MAIL_ADDR          => ('to'=>'webmaster@example.com', 'from'=>'webmaster@example.com');
  use constant SMTP_INFO          => ('host'=>'smtp.example.com', 'user'=>'webmaster', 'password'=>'pass',
                                      'debug'=>1, 'bufsize'=>1024);
  # common config
  use constant MD5SUM_FILE        => '/tmp/__monitor_md5sum_hash';
  use constant APACHE_LOG_PATH    => '/usr/local/apache2/logs/access';
  # apache
  use constant APACHE_PORT        => 80;
  use constant APACHE_SERVERS     => ('web1.example.com', 'web2.example.com');
  # mysql
  use constant MYSQL_PORT         => 3306;
  use constant MYSQL_SERVERS      => ('db1.example.com', 'db2.example.com');
  # memcache
  use constant MEMCACHE_PORT      => 11211;
  use constant MEMCACHE_SERVERS   => ('cache1.example.com', 'cache2.example.com');
  # search
  use constant SEARCH_PORT        => 8000;
  use constant SEARCH_SERVERS     => ('search1.example.com');


  ##############################
  # Server port is alive check
  ##############################
  sub check_server_alive {
      my($server, $port) = @_;

      $sock = IO::Socket::INET->new(PeerAddr=>$server, PeerPort=>$port, Proto=>'tcp', Timeout=>3);
      if (!$sock){
          return 0;
      }
      $sock->close();
      return 1;
  }

  ##############################
  # Check process is exist
  ##############################
  sub check_process_exist {
      my $proc_name = shift;
      $line = `/bin/ps auxw | /bin/grep $proc_name | /bin/grep -v grep | /usr/bin/wc -l`;
      $line =~ s/^s+|s+$//g;
      if ($line == 0){
          return 0;
      }
      return 1;
  }

  ##############################
  # Check file md5 fingerprint
  ##############################
  sub check_file_md5sum {
      my $io, $line;
      $filename = shift;
      @arr = split(/s/, `/usr/bin/md5sum $filename`);
      $filehash = shift(@arr);
      $io = IO::File->new();
      $io->open(MD5SUM_FILE, O_RDWR);
      if (!($line = $io->getLine())){
          $io->syswrite($filehash);
          $io->close;
          return true;
      }
      if ($line != $filehash){
          $io->truncate(0);
          $io->syswrite($filehash);
          $io->close;
          return true;
      }
      return true;
  }

  ##############################
  # SMTP execute command
  ##############################
  sub smtp_cmd {
      my ($sock, $cmd, $blocking) = @_;
      my %smtpinfo = SMTP_INFO;
      my $buf, $bufsize = $smtpinfo{'bufsize'}, $debug=$smtpinfo{'debug'};

      $sock->syswrite($cmd);
      if ($debug == 1){
          print ">>> $cmd ";
      }
      if ($blocking == 1){
          $sock->sysread($buf, $bufsize);
          if ($debug){
              print "<<< $buf";
          }
      }
  }

  ##############################
  # Send notice mail
  ##############################
  sub send_mail {
      my ($subject, $content) = @_;
      my $sock;
      my %mailaddr = MAIL_ADDR;
      my %smtpinfo = SMTP_INFO;
      my $debug = $smtpinfo{'debug'};

      # Count date time
      ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime(time());
      $datetime = sprintf("%s-%s-%s %s:%s:%s", "20".substr($year,1,2), length($mon)==1?"0$mon":$mon, length($day)==1?"0$day":$day, length($hour)==1?"0$hour":$hour, length($min)==1?"0$min":$min, length($sec)==1?"0$sec":$sec);
      $subject .= "[$datetime]";

      # Connect to SMTP server
      $sock = IO::Socket::INET->new(PeerAddr=>$smtpinfo{'host'}, PeerPort=>25, Proto=>'tcp', Timeout=>10);
      $sock->blocking(1);

      # Send smtp command
      if ($debug == 1){
          print "<<< ". $sock->sysread($buf, $smtpinfo{'bufsize'});
      }
      smtp_cmd($sock, "HELO locahost ", 1);
      smtp_cmd($sock, "AUTH LOGIN ", 1);
      smtp_cmd($sock, encode_base64($smtpinfo{'user'}), 1);
      smtp_cmd($sock, encode_base64($smtpinfo{'password'}), 1);
      smtp_cmd($sock, "MAIL FROM: <". $mailaddr{'from'} ."> ", 1);
      smtp_cmd($sock, "RCPT TO: <". $mailaddr{'to'} ."> ", 1);
      smtp_cmd($sock, "DATA ", 1);
      smtp_cmd($sock, "From: ". $smtpinfo{'from'} ." ", 0);
      smtp_cmd($sock, "To: ". $smtpinfo{'to'} ." ", 0);
      smtp_cmd($sock, "Subject: $subject ", 0);
      smtp_cmd($sock, "$content ", 0);
      smtp_cmd($sock, " . ", 1);
      smtp_cmd($sock, "QUIT ", 0);
      $sock->close();

      return 1;
  }

  ##############################
  # Check server alive main
  ##############################
  sub monitor_main {
      # check apache
      foreach $item (APACHE_SERVERS) {
          if (!check_server_alive($item, APACHE_PORT)) {
              send_mail("$item apache server is down", "$item apache server is down. please timely restoration");
          }
      }
      # check mysql
      foreach $item (MYSQL_SERVERS) {
          if (!check_server_alive($item, MYSQL_PORT)) {
              send_mail("$item mysql server is down", "$item mysql server is down. please timely restoration");
          }
      }
      # check memcache
      foreach $item (MEMCACHE_SERVERS) {
          if (!check_server_alive($item, MEMCACHE_PORT)) {
              send_mail("$item memcache server is down", "$item memcache server is down. please timely restoration");
          }
      }
      # check search
      foreach $item (SEARCH_SERVERS) {
          if (!check_server_alive($item, SEARCH_PORT)) {
              send_mail("$item search server is down", "$item search server is down. please timely restoration");
          }
      }
  }


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

  monitor_main();

linux
相关资讯       RHEL  Perl 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款