结合Zabbix做MySQL相关性能参数

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/perl
use Getopt::Long;
use DBI;
use warnings;
use strict;

my $config_file='';
my $db_option='';

#用于保存从数据库得到的数据
my %status;

#Parameters
my $test_para='';
my $rcode='';

#Check Parameters 
if(!@ARGV){
	&Useage;
	exit(123);
}

sub Useage{
     print "Please Use for example:\n";
     print "$0 --config_file=path --db_option=web007\n";
}

#sub Help{
#    print "--dbname=dbname\n";
#    print "--location=location\n";
#    print "--port=port\n";
#    print "--db_user=db_user\n";
#    print "--db_pass=password\n";
#}

#Parse Parameters  解析参数的函数,其中需要指定配置文件的位置、数据库选项
$rcode = GetOptions('config_file=s' => \$config_file,
		    'db_option=s' => \$db_option);
if(!$db_option){
print "please chose db_option\n";
exit(44);
}



#Get MySQL refer Parameters 取MySQL相关参数
my $dbname=&Read_config_file("db_name");
my $location=&Read_config_file("location");
#print "\$location=$location\n";
my $port=&Read_config_file("port");
my $db_user=&Read_config_file("db_user");
my $db_pass=&Read_config_file("db_pass");
my $get_status=&Read_config_file("status");

#Connect MySQL Server ,Get status and push into  array;Get one line everytime
#For example:
#First time @fetch_row=(Aborted_clients,4),so $fetch_row[0]=Aborted_clients,$fetch_row[1]=4
#and the Second time $fetch_row=(Aborted_connects,10),etc.
sub Connect_MySQL{
my $database="DBI:mysql:$dbname:$location:$port";
my $dbh=DBI->connect($database,$db_user,$db_pass) or die "Can not connect MySQL Server". $DBI::errstr;
my $sql="show global status";
my $sth=$dbh->prepare($sql);

$sth->execute() or die "Can not Execute SQL statement";

my @fetch_row;
my @table_name;
while (@fetch_row = $sth->fetchrow_array){
        my @row=@fetch_row;
	#print scalar(@row);
	push(@table_name,\@row);
}

#List 
foreach my $table_name (@table_name){
	my($key,$value)=(@$table_name[0],@$table_name[1]);
	if(($key)&&($value)){
	$status{$key}=$value;
	}
}
}

sub Read_config_file{
#	print "Can Not Find $config_file\n" if(!$config_file);
#	print "Can find $config_file\n";
	open(CONFIG_FILE,"$config_file") or die "Can Not Open $config_file"; 
	my $get_parameters=shift;
	my $return_parameter='';
	#print "\$get_parameters=$get_parameters\n";

	my $read_format_head="\<$db_option\>";
	my $read_format_end="\<\/$db_option\>";
	my @read_file=(<CONFIG_FILE>);
	my $array_count=@read_file;
	for(my $i=0;$i<=($array_count-1);$i++){
		chomp($read_file[$i]);

		print "\$db_option=$db_option\n";


	        my $is_valid_option_qr="\<(.*)\>";
		my @Refer_db_option;
		if($read_file[$i] =~ $is_valid_option_qr){
			push(@Refer_db_option,$1);
		}
		my $return_value = &is_valid_option(@Refer_db_option,$db_option);
		#print "\$return_value=$return_value\n";



#		foreach my $judge_db_option(@Judge_db_option){
#		print "\$judge_db_option=$judge_db_option\n";
#		last if($1 =~ $db_option);
#		}


		next if($read_file[$i] =~ /$read_format_head/);
		last  if($read_file[$i] =~ /$read_format_end/);
		#print "$read_file[$i]\n";
		#$return_parameter=$read_file[$i] if($read_file[$i] =~ /$get_parameters=(.*)/);
		$return_parameter=$1 if($read_file[$i] =~ /$get_parameters=(.*)/);
		#print "\$dbname=$dbname\n";
	}
	return $return_parameter;
}

sub is_valid_option{
	my @Judge_db_option=shift;
	my $get_db_option=shift;

	my $Judge_db_option_count=@Judge_db_option;
	print "\$Judge_db_option_count=$Judge_db_option_count\n";
	print "\$get_db_option=$get_db_option\n";
	#foreach my $judege_db (@Judge_db_option){
	#print "\$judege_db=$judege_db\n";
	#last if($judege_db =~ $get_db_option);
	#}
	#return 0;

}


#Get MySQL status:QPS/Insert/Update etc.
#For example
#If want to get QPS,You should call GET_MySQL_Status(Queries,$Interval_time)
sub GET_MySQL_Status{
	my $MySQL_Status=shift;
	print "\$MySQL_Status=$MySQL_Status\n";
	if(!$MySQL_Status){
	print "please Specify status value on configure file\($config_file\)\n";
	}
	my $Interval_time=shift; 
	$MySQL_Status = 'Queries' if($MySQL_Status eq 'qps');
	#$MySQL_Status = "Com_$MySQL_Status" if ($MySQL_Status);
	
	#Call function Connect_MySQL for connect MySQL Server
	&Connect_MySQL;

	print "\$MySQL_Status=$MySQL_Status\n";
	$Interval_time=2 if (!$Interval_time);
	print "\$Interval_time=$Interval_time\n";
	my $GET_MySQL_Status;
	my $MySQL_Status_old=$status{$MySQL_Status};
	#print "\$status{$MySQL_Status}=$status{$MySQL_Status}\n";

#debug
	#print "\$MySQL_Status_old=";
	#print  sprintf("%0.2f",$MySQL_Status_old)."\n";

	my $Uptime_old=$status{'Uptime'};
	sleep 2;
	my $MySQL_Status_now=$status{$MySQL_Status};

#debug
	#print "\$MySQL_Status_now=";
	#print sprintf("%0.2f",$MySQL_Status_now)."\n";


	my $Uptime_now=$status{'Uptime'};
	if(($MySQL_Status_now)&&($MySQL_Status_old)&&($Uptime_old)&&($Uptime_now)){
		if(($MySQL_Status_now = $MySQL_Status_old) || ($MySQL_Status_now == 0)){
		print "0\n";
		}else{
		$GET_MySQL_Status=($MySQL_Status_now-$MySQL_Status_old)/($Uptime_now-$Uptime_old);
		#print sprintf("%.2f",$GET_MySQL_Status);	
		print "$GET_MySQL_Status\n";	
		}
	}
}
&GET_MySQL_Status($get_status,3);