+ -
当前位置:首页 → 问答吧 → Perl 传值问题

Perl 传值问题

时间:2010-10-20

来源:互联网

本帖最后由 mitmax 于 2010-10-20 10:12 编辑

这个是监控Mysql主从的现在只能监控一台,比如我还有很多台,这3个值如果依次传给account.
my $host     = '192.168.1.203','192.168.1.204','192.168.1.205';
my $user     = 'myrepl',myrepl1',myrepl3',;
my $pass     = '123456','123456','123456';

  1. #!/usr/bin/perl -w
  2. # Use mandatory external modules
  3. use strict;
  4. use DBI;
  5. use Net::SMTP_auth;
  6. use Time::HiRes;
  7. use POSIX "strftime";

  8. my $host     = '192.168.1.203';
  9. my $user     = 'myrepl';
  10. my $pass     = '123456';
  11. my $port     = '3306';
  12. my $max_behind_m = 120;
  13. my $check_log = '/var/log/mysql_check_log';

  14. my $mailhost = 'smtp.163.com';
  15. my $mailfrom = '[email protected]';
  16. my @mailto   = '[email protected]';
  17. my $subject  = "mysql_repl_error::$host";
  18. my $text;
  19. my $mailuser   = 'xxxxx';
  20. my $mailpasswd = 'xxxxxx';

  21. # open  log--file
  22. open FH, ">> $check_log" or die $!;

  23. # connect to servers (this)
  24. sub account {
  25.         my ($host, $port, $user, $pass)=@_;
  26. my $this_dbh = &MysqlConnect( $host, $port, $user, $pass );
  27. print FH "ERROR: Can't connect to MySQL (host = $host:$port, user = $user)!"
  28.   if ( !$this_dbh );
  29.         }


  30. # Get slave info
  31. my $slave_status = &MysqlQuery( $this_dbh, "SHOW SLAVE STATUS" );
  32. print FH "ERROR: SQL Query Error: " . $this_dbh->errstr unless ($slave_status);

  33. my $Slave_IO  = $slave_status->{Slave_IO_Running};
  34. my $Slave_SQL = $slave_status->{Slave_SQL_Running};
  35. my $Seconds_Behind_Master = $slave_status->{Seconds_Behind_Master};

  36. print "IO:\t\t $Slave_IO\n";
  37. print "SQL:\t\t $Slave_SQL\n";
  38. print "Behind_Master:\t $Seconds_Behind_Master\n";

  39. # Send to Mail
  40. if ( ( $Slave_IO eq 'Yes' ) and ( $Slave_SQL eq 'Yes' ) and ( $Seconds_Behind_Master < $max_behind_m ) ) {
  41.     print  "mysql replicate is OK" . "\n";
  42. }
  43. else {
  44.     print FH "-" x 80 . ">", "\n";
  45.     my $start_time = &current_time();
  46.     print FH "start at $start_time\n";

  47.     print FH "mysql replicate is Fail!!!!!!!!!!!!" . "\n";
  48.     print FH "IO:\t\t $Slave_IO\n";
  49.     print FH "SQL:\t\t $Slave_SQL\n";
  50.     print FH "Behind_Master:\t $Seconds_Behind_Master\n";

  51.     my $end_time = &current_time();
  52.     print FH "end   at $end_time\n";
  53.     close (FH);

  54.     $text = "$Slave_IO, $Slave_SQL, $Seconds_Behind_Master";
  55.     &SendMail();
  56. }

  57. #-----------------------------------------------------------------
  58. sub MysqlConnect {
  59.     my ( $host, $port, $user, $pass ) = @_;
  60.     my $dsn = "DBI:mysql:host=$host;port=$port";
  61.     return DBI->connect( $dsn, $user, $pass, { PrintError => 0 } );
  62. }

  63. #-----------------------------------------------------------------
  64. sub MysqlQuery {
  65.     my ( $dbh, $query ) = @_;
  66.     my $sth = $dbh->prepare($query);
  67.     my $res = $sth->execute;
  68.     return undef unless ($res);
  69.     my $row = $sth->fetchrow_hashref;
  70.     $sth->finish;
  71.     return $row;
  72. }


  73. #-----------------------------------------------------------------
  74. sub current_time() {
  75.     my $time_now = POSIX::strftime("[%Y-%m-%d %H:%M:%S]", localtime);
  76.     return $time_now;
  77. }


  78. #-----------------------------------------------------------------
  79. sub SendMail() {

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

  83.     foreach my $mailto (@mailto) {
  84.         $smtp->mail($mailfrom);
  85.         $smtp->to($mailto);
  86.         $smtp->data();
  87.         $smtp->datasend("To: $mailto\n");
  88.         $smtp->datasend("From:$mailfrom\n");
  89.         $smtp->datasend("Subject: $subject\n");
  90.         $smtp->datasend("\n");
  91.         $smtp->datasend("$text\n");
  92.         $smtp->dataend();
  93.     }
  94.    
  95.     $smtp->quit;
  96. }
复制代码

作者: mitmax   发布时间: 2010-10-20

看看顶楼提问的智慧。

作者: 兰花仙子   发布时间: 2010-10-20