目录
web界面设置
server.zabbix.com 服务器操作
编辑 chk_mysql.sh脚本
查看web效果
web界面设置
1.
2.
3.
4.
5.
6.
7.
8.
server.zabbix.com 服务器操作
[root@server ~]# cd /usr/local/zabbix/etc/
[root@server etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh $1
UserParameter=mysql.ping,mysqladmin -uroot -p123123 -P3306 -h192.168.50.51 ping | grep -c alive
解释
UnsafeUserParameters=1 //允许所有字符的参数传递给用户定义的参数。
UserParameter=mysql.version,mysql -V //定义键值mysql.version,以及键值的值mysql -V
UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh $1 //定义键值mysql.status[*]
UserParameter=mysql.ping,mysqladmin -uroot -p123456 -P3306 -h192.168.200.111 ping | grep -c alive ///定义键值mysql.ping,指定chk_mysql.sh脚本,使用此脚本检查mysql的运行状态,使用mysqladmin命令指定agent端的数据库连接用户密码ip地址,注意保证mysqladmin命令的链接;
编辑 chk_mysql.sh脚本
[root@server etc]# pwd
/usr/local/zabbix/etc
[root@server etc]# vim chk_mysql.sh
脚本
1 #!/bin/bash
2 #FileName: check_mysql.sh
3 # Revision: 1.0
4 # Date: 2015/06/09
5 # Author: DengYun
6 # Email: dengyun@ttlsa.com
7 # Website: www.ttlsa.com
8 # Description:
9 # Notes: ~
10 # -------------------------------------------------------------------------------
11 # Copyright: 2015 (c) DengYun
12 # License: GPL
13
14 # 用户名
15 MYSQL_USER='root'
16
17 # 密码
18 MYSQL_PWD='123123'
19
20 # 主机地址/IP
21 MYSQL_HOST='192.168.50.51'
22
23 # 端口
24 MYSQL_PORT='3306'
25
26 # 数据连接
27 MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
28
29 # 参数是否正确
30 if [ $# -ne "1" ];then
31 echo "arg error!"
32 fi
33
34 # 获取数据
35 case $1 in
36 Uptime)
37 result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
38 echo $result
39 ;;
40 Com_update)
41 result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
42 echo $result
43 ;;
44 Slow_queries)
45 result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
46 echo $result
47 ;;
48 Com_select)
49 result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
50 echo $result
51 ;;
52 Com_rollback)
53 result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
54 echo $result
55 ;;
56 Questions)
57 result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
58 echo $result
59 ;;
60 Com_insert)
61 result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
62 echo $result
63 ;;
64 Com_delete)
65 result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
66 echo $result
67 ;;
68 Com_commit)
69 result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
70 echo $result
71 ;;
72 Bytes_sent)
73 result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
74 echo $result
75 ;;
76 Bytes_received)
77 result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
78 echo $result
79 ;;
80 Com_begin)
81 result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
82 echo $result
83 ;;
84
85 *)
86 echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert| Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
87 ;;
88 esac
给脚本添加权限
[root@server etc]# chmod 777 chk_mysql.sh
给MySQL授权
[root@server etc]# mysql -uroot -p123123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 290
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to 'root'@'server.zabbix.com' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
重启
[root@server ~]# killall -9 zabbix_server
[root@server ~]# killall -9 zabbix_agentd
[root@server ~]# /usr/local/zabbix/sbin/zabbix_agentd
[root@server ~]# /usr/local/zabbix/sbin/zabbix_server
查看端口
[root@server ~]# netstat -anpt | egrep ':10050|10051'
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 34683/zabbix_agentd
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 34691/zabbix_server
tcp6 0 0 :::10050 :::* LISTEN 34683/zabbix_agentd
tcp6 0 0 :::10051 :::* LISTEN
测试
root@server etc]# zabbix_get -s 192.168.233.10 -k mysql.ping
1
[root@server etc]# zabbix_get -s 192.168.233.10 -k mysql.status[Com_update]
3164
查看web效果
1.
2.