从Zabbix 2.2开始,Zabbix官方已经支持了MySQL监控,但是MySQL监控默认是不可用的,需要经过额外的设置才可以使用。Kaijia将Zabbix换到了新的服务器时候性能绰绰有余,于是决定充分发挥剩余的内存和SSD性能,把MySQL、Apache、PHP-FPM等的监控也开起来
首先要确定Zabbix服务有MySQL的监控模板。MySQL模板默认包含在了Zabbix 2.2中,但如果是从较旧版本的Zabbix升级而来的话可能这个模板并没有导入。这种情况的话需要访问Zabbix官方的Wiki页面下载此模板Template_App_MySQL-2.2.0.xml然后在Zabbix服务器的前端Web界面的组态
接下来就是被监控端(需要安装Zabbix Agent)的设置了
怎么安装Zabbix Agent我就简单说明下好了将zabbix源码文件下载到/tmp/install目录下
./configure --enable-agent
make install
vi /usr/local/etc/zabbix_agentd.conf文件
Server=192.168.153.181#Zabbix Server轮询Agent ListenPort=10050 ListenIP=0.0.0.0 ServerActive=192.168.153.181#Agent主动请求Zabbix Server
首先要建立一个MySQL帐户用于Zabbix Agent登录获取MySQL状态,这个帐户不需要任何权限当然可以登录被监控端的MySQL新建一个帐户。
GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY '密码'; FLUSH PRIVILEGES;
修改mysql配置文件 vim /etc/my.cnf (yum默认安装的mysql,配置文件就在/etc/my.cnf)
[client] port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set=utf8 [mysqld] port = 3306 socket = /var/lib/mysql/mysql.sock skip-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M thread_concurrency = 8 default-character-set=utf8 log-bin=mysql-bin binlog_format=mixed server-id = 1 [mysqldump] quick max_allowed_packet = 16M #将一下配置内容放到/var/lib/zabbix更好一些 [mysql] host=localhost user=zabbix password=zabbix socket=/var/lib/mysql/mysql.sock no-auto-rehash [myisamchk] key_buffer_size = 128M sort_buffer_size = 128M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout #将一下配置内容放到/var/lib/zabbix更好一些 [mysqladmin] host=localhost user=zabbix password=zabbix socket=/var/lib/mysql/mysql.sock
这里推荐将以下配置,创建一个文件.my.cnf放到/var/lib/zabbix目录下【推荐这种方法】
因为在/usr/local/etc/zabbix_agentd.conf.d/userparameter_mysql.conf文件中指定了目录
UserParameter=mysql.size[*],echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema='$1'")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name='$2'");" | HOME=/var/lib/zabbix mysql -N UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive UserParameter=mysql.version,mysql -V
[mysql] host=localhost user=zabbix password=zabbix socket=/var/lib/mysql/mysql.sock [mysqladmin] host=localhost user=zabbix password=zabbix socket=/var/lib/mysql/mysql.sock
修改zabbix agent配置
vim /usr/local/etc/zabbix_agentd.conf (源码安装的,没有设置--prefix,配置文件目录就在这个)
拷贝userparameter_mysql.conf 文件到/usr/local/etc/zabbix_agentd.conf.d/目录下(具体安装路径请根据实际情况修改)
# cp /tmp/install/zabbix-2.2.9/conf/zabbix_agentd/userparameter_mysql.conf /usr/local/etc/zabbix_agentd.conf.d/userparameter_mysql.conf
拷贝服务脚本,放到/etc/rc.d/init.d 使得可以让chkconfig管理服务
cp /tmp/install/zabbix-2.2.9/misc/init.d/fedora/core/zabbix_agentd /etc/rc.d/init.d/
启动zabbix_agent服务(要先创建zabbix用户 Linux用户)
如果已经开启过了,就重启服务
service zabbix_agentd restart
最后别忘了开启防火墙10050端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10050 -j ACCEPT
上图
参考文章
http://www.kaijia.me/2014/01/use-zabbix-monitor-mysql-server/
http://waringid.blog.51cto.com/65148/1156013/