Monitor MySQL using Zabbix Agent and MySQL App template
With Zabbix (tested with Zabbix 2.4 and 3.0), we can monitor the MySQL server performances using Zabbix Agent and the Template App MySQL included in Zabbix Server.
First of all, we have to create a MySQL user account to be used to access mysql and get the necessary stats.
mysql -uroot -p -e"GRANT USAGE ON *.* TO 'zabbix'@'127.0.0.1' IDENTIFIED BY 'abc123'";
mysql -uroot -p -e"GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY 'abc123'";
mysql -uroot -p -e"flush privileges"
Note that the USAGE privilege specifier stands for “no privileges”, but change the 'abc123' password to something more secure: something like 'KJ37ae!$' is stronger (don't worry, we don't have to type it frequently)
Test the new zabbix mysql account to make sure it works with:
mysql -uzabbix -pabc123 -e"status"
If it works, the output will look something like:
mysql Ver 15.1 Distrib 5.5.47-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 412664
Current database:
Current user: zabbix@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 5.5.47-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 18 days 4 hours 27 min 56 sec
Now create a new .my.cnf file under the zabbix config folder /etc/zabbix. This will allow mysqladmin to connect to the mysql database.
vim /etc/zabbix/.my.cnf
and paste this content into the file:
[mysql]
user=zabbix
password=abc123
[mysqladmin]
user=zabbix
password=abc123
Edit the file /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf and replace HOME = /var/lib/zabbix with HOME = /etc/zabbix (should appear three times).
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/etc/zabbix mysql -N | awk '{print $$2}'
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=/etc/zabbix mysql -N
UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
Finally, restart Zabbix Agent with
service zabbix-agent restart
On the Zabbix Server you can now apply the Template App MySQL template to the host you want to monitor.
Comments