一个监控mysql replication 是否同步,状态,端口,服务的小脚本.仅供参考.

 
  
  1. #!/usr/bin/env perl 
  2. use warnings; 
  3. use strict; 
  4. use DBI; 
  5. use Net::SMTP; 
  6. use Net::SMTP_auth; 
  7. #use MIME::Base64; 
  8. use IO::Socket; 
  9. my $slave_ip = '10.207.10.128'
  10. my $master_ip = '10.207.10.92'
  11. my $monitor_user = 'monitor'
  12. my $monitor_pass = 'redhat'
  13. my $monitor_db = 'test'
  14. my $db_port = '3306'
  15. my @ip_list = ("$master_ip","$slave_ip"); 
  16. my $M_dbh = &get_connect($master_ip); 
  17. my $S_dbh = &get_connect($slave_ip); 
  18. my $M_connect = $M_dbh->ping(); 
  19. my $S_connect = $S_dbh->ping(); 
  20. my @data; 
  21.         foreach my $data (&check_rep_status()){ 
  22.                 push ( @data,$data); 
  23. #my $data = do {local $/;<@data>}; 
  24. my $data=join('\r',@data); 
  25.         foreach my $ip (@ip_list){ 
  26.                         if (! &port_status($ip)){ 
  27.                                         &mail_send($ip,"Mysql_Port: $db_port","$db_port is Downing...!"); 
  28.                         } 
  29.         } 
  30.                 &mail_send($master_ip,"Mysql_Master_Ser","Mysql_Master_Ser is Downing...!") if (! $M_connect); 
  31.                 &mail_send($slave_ip,"Mysql_Slave_Ser","Mysql_Slave_Ser is Downing...!") if (! $S_connect); 
  32.                 &mail_send($slave_ip,"Replication","Replication_Error...!","$data") if (defined (&check_rep_status)); 
  33. sub port_status { 
  34.         my $ip = shift; 
  35.         my $sock = IO::Socket::INET->new(Proto => 'tcp'
  36.                                         PeerAddr => $ip, 
  37.                                         PeerPort => $db_port, 
  38.                                         Timeout => 2); 
  39.         $sock ? return 1 : return 0; 
  40. =this 
  41.         foreach (@ip_list){ 
  42.                 if(check_sql_status($_)){ 
  43.                         print "$_ DB OK\n"
  44.         } 
  45. =cut 
  46. sub get_time { 
  47.         my $time = shift || time (); 
  48.         my ($sec,$min,$hour,$day,$mon,$year,$wday) = localtime($time); 
  49.         $year += 1900; 
  50.         $mon +=1; 
  51.         $min = '0'.$min if length($min) < 2; 
  52.         $sec = '0'.$sec if length($sec) < 2; 
  53.         $mon = '0'.$mon if length($mon) < 2; 
  54.         $day = '0'.$day if length($day) < 2; 
  55.         $hour ='0'.$hour if length($hour) < 2; 
  56.         my $weekday = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$wday]; 
  57.         my $time_now = "$year-$mon-$day $hour:$min:$sec $weekday"
  58.         return $time_now; 
  59. sub get_connect{ 
  60.         my $host = shift; 
  61.         my $dsn = "DBI:mysql:$monitor_db:$host:$db_port"
  62.         my $dbh = DBI->connect($dsn,$monitor_user,$monitor_pass,{RaiseError => 0,PrintError => 0}); 
  63.                 return $dbh; 
  64. sub check_rep_status { 
  65.         my ($error,%result,$data); 
  66.         my $default_seconds_behind_limit = 400; 
  67.         my $sql = "Show Slave Status"
  68.         my $sth=$S_dbh->prepare($sql); 
  69.            $sth->execute(); 
  70.            %result = %{$data}  while ($data = $sth->fetchrow_hashref); 
  71.         if( defined($result{'Slave_IO_Running'}) && $result{'Slave_IO_Running'} ne 'Yes'){ 
  72.                         $error = "1002"
  73.                         return ("Slave_IO_Running=$result{'Slave_IO_Running'}\nSlave_IO_State: $result{'Slave_IO_State'}"); 
  74.                 } 
  75.         if( defined($result{'Slave_SQL_Running'}) && $result{'Slave_SQL_Running'} ne 'Yes' ){ 
  76.                         $error = "1003"
  77.                         return ("Slave_SQL_Running=$result{'Slave_SQL_Running'}\nSlave_IO_State: $result{'Slave_IO_State'}"); 
  78.                 } 
  79.  
  80.         if( defined($result{'Seconds_Behind_Master'}) && $result{'Seconds_Behind_Master'} > $default_seconds_behind_limit ){ 
  81.                         $error = "1004"
  82.                         return ("Seconds Behind Master=$result{'Seconds_Behind_Master'}"); 
  83.                 } 
  84.  
  85.         if( defined($result{'Last_Errno'}) && $result{'Last_Errno'} != 0 ){ 
  86.                         $error = "1005"
  87.                         return ("Last_Errno=$result{'Last_Errno'}"); 
  88.                 } 
  89.         return undef; 
  90. sub mail_send { 
  91.         my $subject = shift; 
  92.         my $subject_ip = shift; 
  93.         my $mail = shift; 
  94.         my $data = shift; 
  95.         my $time_now = &get_time(); 
  96.         my $smtp_mail_host = 'smtp.sinanet.com'
  97.         my $mail_user_from = 'donghui@leju.sina.com.cn'
  98.         my $mail_user_to = 'donghui@leju.sina.com.cn'
  99.         my $mail_user_pass = "P@ssW0rd"
  100.         my $mail_hello = 'mail.sinanet.com'
  101.         my $smtp = Net::SMTP->new( 
  102.                 Host => "$smtp_mail_host"
  103.                 Hello => "$mail_hello"
  104.                 timeout => 40, 
  105.                 Debug => 0,)or die "can not connect mail server!\n"
  106.         $smtp->auth("$mail_user_from","$mail_user_pass")or die "auth faild!\n"
  107.         $smtp->mail("$mail_user_from"); 
  108.         $smtp->to("$mail_user_to"); 
  109.         $smtp->data(); 
  110.         $smtp->datasend("subject:warning: $subject_ip $subject"); 
  111.         $smtp->datasend("From:donghui\@leju.sina.com.cn\n"); 
  112.         $smtp->datasend("Dear ALL:\n"); 
  113.         $smtp->datasend("\t$subject  $subject_ip\n"); 
  114.         $smtp->datasend("\t$mail\n"); 
  115.         $smtp->datasend("\t$data\n\r"); 
  116.         $smtp->datasend("----------------\n"); 
  117.         $smtp->datasend("$time_now\n"); 
  118.         $smtp->dataend(); 
  119.         $smtp->quit(); 
  120.    

端口down掉,主从同步出错,则发送邮件报警...