Zabbix 采集mysql的方法有很多,常用的是通过python、shell执行show status 或者show global status 获取值
二者的区别:
Show status;是当前的会话,你查看到的很多参数都是为0
Show global status;是你全局的会话,你重启数据库或者关闭都会失效
所以,再采集数据的时候,以global status 为主
简单的脚本如下(只采集Com_select,delete,update,rollback为列)
#!/usr/bin/env python
import os
def mysql_info(Source,socket,Items):
User,Password,port = Source
mysqladmin=os.popen('which mysqladmin').read().strip()
comm='%s -u%s -p%s -P%s -S%s extended-status | grep -w "%s"|cut -d "|" -f 3' \
%(mysqladmin,User,Password,port,socket,Items)
Value=float(os.popen(comm).read().strip())
print Value
if __name__ == '__main__':
item = sys.argv[1]
Source=['root','ema',3306]
socket="/var/mysql/data/mysql.sock"
mysql_info(Source,socket,item)
这个只是一个简单的脚本,还有很多优化和调整的地方
执行结果:
[root@DB-Server ~]# python test.py Com_select
Warning: Using a password on the command line interface can be insecure.
3714689.0
Ok,把我们的脚本加入到zabbix的UserParameter.conf配置文件中
UserParameter=mysql_info