mkdir /etc/zabbix/scripts/

vim mysql.sh

chmod +x mysql.sh

客户端编辑配置文件

1
2
3
4
[root@master zabbix] # vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.status[*], /etc/zabbix/scripts/mysql .sh $1
 
UserParameter=mysql. ping ,mysqladmin -uzabbix -p123 -hlocalhost  ping  grep  -c alive

1) 监控mysql主从状态(8分)

1
2
3
4
5
6
7
8
9
slave_conn= "/usr/local/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql.sock2"
check_slave(){<br>         #结果是2 否则。。
         ${slave_conn} -e  "show slave status\G;"  2> /dev/null  | egrep  "Running|Yes"  | head   -n2| awk  -F  ':'  '{print $2}' | wc  -l
}
case  $1  in
         slave)
         check_slave
         ;;
esac


2.1) mysql  bandwidth(带宽/流量/吞吐量)

 mysqladmin -uroot -p123456 -S /tmp/mysql.sock1 extended-status|grep -w "Bytes_sent"|awk  '{print $4}'     -->发送

 mysqladmin -uroot -p123456 -S /tmp/mysql.sock1 extended-status|grep -w "Bytes_received"|awk  '{print $4}'   -->接收

2.2) mysql operations(操作)

1
Com_begin(){
1
#每秒开始的操作<br>mysqladmin extended-status|grep -w "Com_begin"|awk '{print $4}'<br>}<br>Com_commit(){<br>#每秒提交的操作
1
2
3
4
5
6
mysqladmin extended-status| grep  -w  "Com_commit" | awk  '{print $4}' <br>}<br>Com_delete() {
mysqladmin extended-status |  awk  '/<Com_delete>/{print $4}' <br><br> #mysqladmin extended-status|grep -w "Com_delete"|awk '{print $4}'
}
Com_insert() {
mysqladmin extended-status |  awk  '/<Com_insert>/{print $4}'
}<br>Com_rollback(){
1
2
3
4
5
6
7
8
mysqladmin extended-status| grep  -w  "Com_rollback" | awk  '{print $4}' <br>}
Com_update() {
mysqladmin extended-status |  awk  '/<Com_update>/{print $4}'
}
Com_select() {
mysqladmin extended-status |  awk  '/<Com_select>/{print $4}'
}
  

3) 监控mysql的qps和tps(8分)

QPS(Questions Per second:):每秒查询处理量,表示每秒能处理多少次请求,这里是指是Mysql每秒处理查询数,同时适用于InnoDB和MysqlSAM引擎

如何计算得到呢并计算QPS呢?

很简单,通过"msyqladmin status" 就是先获取到Questions和uptime对应的数值,随后通过Questions/Uptime即可获取

question=mysqladmin status |awk '{print $6}

uptime= mysqladmin status |awk '{print $2}'

QPS=question/uptime

 浮点数可以用:
小数点后保留两位
echo "scale=2;$question/$uptime"|bc

TPS(Transactions Per Second)

每秒处理事务数,简单的来说就是数据库传输事务处理个数,这是指单台数据库服务器在单位时间内处理的事务的个数。 ,支持事务的存储引擎如InnoDB等特性指标

基于com_commit和com_rollback相加并除以uptim计算出TPS

rollback=mysqladmin extended-status | awk '/\<Com_rollback\>/{print $4}'
commit=mysqladmin extended-status | awk '/\<Com_commit\>/{print $4}'

tps=(com_rollback+com_commit)/uptime

awk

  • BEGIN 模式:是指 awk 将在读取任何输入行之前立即执行 BEGIN 中指定的动作。
  • END 模式:是指 awk 将在它正式退出前执行 END 中指定的动作。

printf

%s %c %d %f都是格式替代符

(-表示左对齐,没有则表示右对齐)

%-4.2f 指格式化为小数,其中.2指保留2位小数。

1
2
3
4
5
6
7
8
9
10
11
qps)
     ${mysqladmin_check} status|awk '{print $6/$2}'
     ;;
tps)
     uptime=`${mysqladmin_check} status|awk '{print $2}'`
     rollback=`${mysqladmin_check} extended-status|grep -w "Com_rollback" |awk '{print $4}'`
     commit=`${mysqladmin_check} extended-status|grep -w "Com_commit" |awk '{print $4}'`
     count=$[$rollback+$commit]
     ##chmod 777 /tmp/re.txt
     echo "$count $uptime" >/tmp/re.txt
     cat /tmp/re.txt|awk '{print $1/$2}'

添加监控项  注意 信息类型是 浮点数

4) 监控数据库表的大小(8分)信息类型=浮点数

mysql 函数

sum 求和

round 四舍五入

concat 用于将多个字符串连接成一个字符串

 字节转换成M(1024/1204)

把 information_schema 看作是一个数据库,确切说是信息数据库。

其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权 限等。

1
2
3
4
5
6
7
DB_size() {
        mysql -D information_schema -e  "select concat(round(sum(data_length/1024/1024),2)) as data from tables where table_schema='zabbix'"  | awk  'NR==2'
}
 
Table_size() {
      mysql -Dinformation_schema -e  "select concat(round(sum(data_length/1024/1024),2)) as data from tables where table_schema='zabbix' and table_name='items'"  | awk  'NR==2'
}

 完整脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
uptime=` /usr/local/mysql/bin/mysqladmin   -uroot -p123456  -S  /tmp/mysql .sock1 status 2> /dev/null  | awk   '{print  $2}' `
Mysqladmin= "/usr/local/mysql/bin/mysqladmin  -uroot -p123456  -S /tmp/mysql.sock1"
Mysql= "/usr/local/mysql/bin/mysql  -uroot -p123456  -S /tmp/mysql.sock1"
##mysql主从状态
slave(){
     /usr/local/mysql/bin/mysql  -uroot -p123456 -S  /tmp/mysql .sock2 -e  "show slave status\G"  2> /dev/null  | egrep  "Running|Yes" | head  -n2| awk  -F  ':'  '{print $2}' | wc  -l
}
##mysql 流量
Bytes_sent(){
     $Mysqladmin extended-status 2> /dev/null  | grep  -w  "Bytes_sent" | awk   '{print $4}'
}
Bytes_received(){
      $Mysqladmin extended-status 2> /dev/null  | grep  -w  "Bytes_received" | awk   '{print $4}'
}
####################
 
##mysql的操作信息##
Com_insert(){
     $Mysqladmin  extended-status 2> /dev/null  | grep  -w  "Com_insert" | awk  '{print $4}'
}
Com_update(){
     $Mysqladmin  extended-status 2> /dev/null  | grep  -w  "Com_update" | awk  '{print $4}'
}
Com_select(){
     $Mysqladmin  extended-status 2> /dev/null  | grep  -w  "Com_select" | awk  '{print $4}'
}
Com_delete(){
     $Mysqladmin  extended-status 2> /dev/null  | grep  -w  "Com_delete" | awk  '{print $4}'
}
Com_rollback(){
     $Mysqladmin  extended-status 2> /dev/null  | grep  -w  "Com_rollback" | awk  '{print $4}'
}
Com_commit(){
     $Mysqladmin  extended-status 2> /dev/null  | grep  -w  "Com_commit" | awk  '{print $4}'
}
########################
 
db_size() {
 
       $Mysql -D information_schema -e  "select round(sum(data_length/1024/1024),2) as data from tables where table_schema='mysql'"  2> /dev/null  | awk  'NR==2'
}
 
table_size(){
     $Mysql -D information_schema -e  "select round(sum(data_length/1024/1024),2) as data from tables where table_schema='mysql' and table_name='user'"  2> /dev/null  | awk  'NR==2'
 
}
$1

 如果zabbix_get 取不到值,显示为NUll,原因是zabbix用户没有执行脚本的权限,需要修改以下两个地方

1
2
3
4
5
6
mysql执行命令写完整路径UserParameter=mysql.status[*], sudo  /etc/zabbix/scripts/mysql .sh $1
 
visudo 修改
## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL
zabbix          ALL=(ALL)       NOPASSWD: ALL

 报错信息如下

[root@localhost fonts]# zabbix_get -s 192.168.1.9 -k mysql_status[slave_status]
ERROR 1227 (42000) at line 1: Access denied; you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation
0
解决:在你的检测脚本上写上mysql的用户名和密码就可以了

在从库上做一个授权账号:check, 密码为:123456

1
2
3
4
5
6
#!/bin/bash
case $1 in
     slave_status)
     mysql -ucheck -p123456 -e "show slave status\G"|grep "Running"|grep "Yes"|awk -F ':' '{print $2}'|wc -l
     ;;
esac