Perl 监控Mysql主从问题.
时间:2010-10-19
来源:互联网
本人想用perl来监控Mysql主从的,便从网上copy下来一些代码,稍稍的修改下,发现报错错误是,
Can't call method "prepare" on an undefined value at duplicate.pl line 39. 查了半天还是差不出来。望高手出马.
复制代码
Can't call method "prepare" on an undefined value at duplicate.pl line 39. 查了半天还是差不出来。望高手出马.
- #!/usr/bin/perl
- # Use mandatory external modules
- use strict;
- use warnings;
- use DBI;
- use Time::HiRes;
- use POSIX "strftime";
- my $host = '192.168.1.240';
- my $user = 'dodo';
- my $pass = 'dodo';
- my $port = '3306';
- my $max_behind_m = 120;
- my $check_log = '/var/log/mysql_check_log';
-
- # open log--file
- open (FH, ">> $check_log") or die $!;
-
- # connect to servers (this)
- my $this_dbh = &MysqlConnect( $host, $port, $user, $pass );
-
- # Get slave info
- my $slave_status = &MysqlQuery( $this_dbh, 'show slave status' );
- print FH "ERROR: SQL Query Error: " . $this_dbh->errstr unless ($slave_status);
- my $Slave_IO = $slave_status->{Slave_IO_Running};
- my $Slave_SQL = $slave_status->{Slave_SQL_Running};
- my $Seconds_Behind_Master = $slave_status->{Seconds_Behind_Master};
- print "IO:\t\t $Slave_IO\n";
- print "SQL:\t\t $Slave_SQL\n";
- print "Behind_Master:\t $Seconds_Behind_Master\n";
-
- sub MysqlConnect {
- my ( $host, $port, $user, $pass ) = @_;
- my $dsn = "DBI:mysql:host=$host;port=$port";
- return DBI->connect( $dsn, $user, $pass, { PrintError => 0 } );
- }
-
- sub MysqlQuery {
- my ( $dbh, $query ) = @_;
- my $sth = $dbh->prepare($query);
- my $res = $sth->execute;
- return undef unless ($res);
- my $row = $sth->fetchrow_hashref;
- $sth->finish;
- return $row;
- }
-
- #-----------------------------------------------------------------
- sub ExecuteQuery {
- my ( $dbh, $query ) = @_;
- my $sth = $dbh->prepare($query);
- return $sth->execute;
- }
-
- #-----------------------------------------------------------------
- sub current_time {
- my $time_now = POSIX::strftime( "[%Y-%m-%d %H:%M:%S]", localtime );
- return $time_now;
- }
作者: mitmax 发布时间: 2010-10-19
19. my $this_dbh = &MysqlConnect( $host, $port, $user, $pass );
这里连接失败返回 undef 了应该,你应该检查这个错误的
这里连接失败返回 undef 了应该,你应该检查这个错误的
作者: zhlong8 发布时间: 2010-10-19
Thank 是连接数据库问题

作者: mitmax 发布时间: 2010-10-19
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28