3112213 发表于 2015-11-13 09:43:30

Zabbix 监控Mysql数据库及主从数据库

Zabbix监控mysql主从数据库在脚步出现用户名和密码是会出现如下报错“Warning: Using a password on the command line interface can be insecure”,报错原因是mysql 5.6版本增加了密码安全策略,之前版本可以使用的命令行里加上密码就会强制报错,所以使用zabbix监控mysql的时候,就会由于收到zabbix客户端日志报错信息。结合了网友的解决方案,现将整理出来供大家参考。




一,zabbix被监控端的设置:
1,首先配置mysql数据库,配置mysql的--login-pathde 安全登录:
设置--login-path:
# mysql_config_editor set --login-path=local --host=localhost --user=zabbix -p
Enter password:
# mysql_config_editor print --all

user = zabbix
password = *****
host = localhost
命令解释:--login-path是设置访问的名字,我设置的local;--host是指定允许访问的host地址,这个地址是你grant的时候配置的;--user是用户名,也是grant时候配置的;-p是指定密码,同样是grant配置2,进入mysql,修改zabbix账号的权限和密码:

mysql> create user 'zabbix' identified by'zabbix'
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------------+--------------+
| user       | host         |
+------------+--------------+
| databak    | %            |
| ppt      | %            |
| root       | %            |
| slave_user | %            |
| zabbix   | %            |
| slave_user | 192.168.1.49 |
+------------+--------------+
6 rows in set (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON `zabbix`.* TO 'zabbix'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> commit;

3,测试mysql login-path=local
# mysql --login-path=local
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection id is 542
Server version: 5.6.25-log Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit

以上就配置好了安全访问模式。

二,在客户端里,需要把监控的内容json化展示,然后服务端可以通过正则表达式过滤出结果,脚步的内容如下,然后放到/usr/local/zabbix/bin 文件夹里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
#Fucation:mysql low-level discovery
#Script_name mysql_low_discovery.sh
mysql() {
            port=($(sudo /bin/netstat -tpln | awk -F "[ :]+" '/ysql/ {print $4}'))
            printf '{\n'
            printf '\t"data":[\n'
               for key in ${!port[@]}
                   do
                     if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
            socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1`
                        printf '\t {\n'
                        printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n"
                     else [[ "${key}" -eq "((${#port[@]}-1))" ]]
            socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1`
                        printf '\t {\n'
                        printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n"
                     fi
               done
                        printf '\t ]\n'
                        printf '}\n'
}
$1






结果如下图:



2,给脚本low_level_discovery赋可执行权限
# chmod +x /usr/local/zabbix/bin/low_level_discovery
# ll /usr/local/zabbix/bin/low_level_discovery
-rwxr-xr-x 1 root root 1055 Nov 11 15:45 /usr/local/zabbix/bin/low_level_discovery

3,允许zabbix用户无密码运行mysql,netstat,/usr/local/mysql/bin是mysql程序地址,可根据情况自由修改。
# echo "zabbix ALL=(root) NOPASSWD:/usr/local/mysql/bin/mysql,/bin/netstat" >> /etc/sudoers
下图所示:



4,禁用 requiretty不关闭的话会无法获取数据,zabbix日志还会报错.
# sed -i 's/^Defaults.*.requiretty/#Defaults    requiretty/' /etc/sudoers



5,修改zabbix_agentd.conf文件的最后添加一下内容:
UserParameter=zabbix_low_discovery[*],/bin/bash /usr/local/zabbix/bin/low_level_discovery$1
UserParameter=mysql_stats_5.6[*],sudo /usr/local/mysql/bin/mysql --login-path=local -P $1-e "show global status"|grep "\<$2\>"|cut-f2
UserParameter=mysql_stats_slave_5.6[*],sudo /usr/local/mysql/bin/mysql --login-path=local -P $1-e "show slave status\G"|grep "\<$2\>"|awk '{if($NF=="Yes") {print 1} else {print 0}}'

6,在服务端进行测试。



三,zabbix服务端web设置:
1,模板导入
把template mysql auto discovery导入到zabbix里,具体:配置-----模板------载入-------选择文件。
2,设置正则表达式:管理-----一般------正则表达式--------新的正则表达式-------输入名称和测试字符串--------测试--------保存。



3,设置模板的更新间隔并把主机关联到模板(如果设置太小的话,一是服务器压力大,另一个就是你检测的端口突然宕掉了,还没有来的急报警,主机通过jsonlai 来获取不到这个信息,就会认为没有这个端口,模板里就会自动关闭这个监控项的内容)


4,最后监控的内容如下:





页: [1]
查看完整版本: Zabbix 监控Mysql数据库及主从数据库