1.低级自动发现介绍
自动发现:自动添加主机
低级自动发现:自动添加监控项,图形,触发器
自动发现(LLD):提供了一种在计算机上为不同实体自动创建监控项,触发器和图形的方法
2.添加自动发现监控项原型
2.1.编写配置文件
#必须是传参式的监控条件,$$2可避免视为第二个传参,还原本意,取第二列
[root@zabbix-server /etc/zabbix/zabbix_agentd.d]# cat mac.conf
UserParameter=net_mac[*],ifconfig $1|grep ether|awk '{print $$2}'
2.2.web界面添加
3.低级自动发现监控MySQL多实例
自动以自动发现规则:监控项,它的值必须是json格式
3.1.创建MySQL多实例
#准备配置文件
[root@zabbix-server ~]# cp /etc/my.cnf /etc/my3307.cnf
[root@zabbix-server ~]# vim /etc/my3307.cnf
[root@zabbix-server /etc]# vim my3307.cnf
[mysqld]
datadir=/data/3307/
socket=/data/3307/mysql.sock
port=3307
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/data/3307/mysqld.log
pid-file=/data/3307/mysqld.pid
[root@zabbix-server ~]# cp /etc/my3307.cnf /etc/my3308.cnf
[root@zabbix-server ~]# sed -i 's#3307#3308#g' /etc/my3308.cnf
#初始化数据库3307
[root@zabbix-server ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
[root@zabbix-server ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
#初始化数据库3308
[root@zabbix-server ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf
[root@zabbix-server ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &
#检查多实例是否正常
[root@zabbix-server ~]# mkdir /data/3306
[root@zabbix-server ~]# ln -s /var/lib/mysql/mysql.sock /data/3306/
#检查测试取值
[root@zabbix-server ~]# find /data -name "*.sock"
/data/3306/mysql.sock
/data/3308/mysql.sock
/data/3307/mysql.sock
[root@zabbix-server ~]# chmod 755 /data/*
[root@zabbix-server ~]# ll /data/* -d
drwxr-xr-x 2 root root 4096 Nov 23 01:45 /data/3306
drwxr-xr-x 4 mysql root 4096 Nov 23 01:39 /data/3307
drwxr-xr-x 4 mysql root 4096 Nov 23 01:40 /data/3308
[root@zabbix-server ~]# netstat -lntp|awk -F "[ :]+" '/mysqld/{print$5}'
3306
3307
3308
3.2.编辑脚本(取出json格式)
#编辑脚本
[root@zabbix-server /server/scripts]# cat discover_mysql.sh
#!/bin/bash
#mysql low-level discovery
res=`netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$5}'`
port=($res)
printf '['
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
printf '{'
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"},"
else [[ "${key}" -eq "((${#port[@]}-1))" ]]
printf '{'
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}"
fi
done
printf ']\n'
3.3.编写zabbix监控规则
[root@zabbix-server /etc/zabbix/zabbix_agentd.d]# cat mysql_status.conf
UserParameter=discovery.mysql,/bin/bash /server/scripts/discover_mysql.sh
Systemctl restart zabbix-agent.service
#添加权限
Chmod u+s /usr/bin/netstat
#测试取值
3.4.web界面添加自动发现规则
3.5.给自动发现规则添加监控项原型
#编写配置文件
root@zabbix-server /etc/zabbix/zabbix_agentd.d]# cat mysql_status.conf
UserParameter=discovery.mysql,/bin/bash /server/scripts/discover_mysql.sh
UserParameter=mysql_alive[*], /usr/bin/mysqladmin -h 127.0.0.1 -P $1 -uzabbix -p123456 ping |grep -c alive
4.自定义自动发现规则监控MySQL其他属性
4.1.查找监控MySQL模板文件
[root@zabbix-server ~/zabbix-5.0.6/conf/zabbix_agentd]# cat userparameter_mysql.conf
#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
#For OS Linux: You need create .my.cnf in zabbix-agent home directory (/var/lib/zabbix by default)
#For OS Windows: You need add PATH to mysql and mysqladmin and create my.cnf in %WINDIR%\my.cnf,C:\my.cnf,BASEDIR\my.cnf https://dev.mysql.com/doc/refman/5.7/en/option-files.html
#The file must have three strings:
#[client]
#user=zbx_monitor
#password=<password>
#
UserParameter=mysql.ping[*], mysqladmin -h"$1" -P"$2" ping
UserParameter=mysql.get_status_variables[*], mysql -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*], mysql -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*], mysql -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
4.2.编写自定义配置文件
[root@zabbix-server zabbix_agentd.d]# cat mysql.conf
UserParameter=discovery.mysql,/bin/bash /server/scripts/discovery_mysql.sh
UserParameter=mysql_alive[*],/usr/bin/mysqladmin -h 127.0.0.1 -P $1 ping 2>/dev/null |grep -c alive
UserParameter=mysql_status[*],echo "show global status where Variable_name='$2';"|/usr/bin/mysql -uroot -h 127.0.0.1 -P $1 -N|awk '{print $$2}'
#mysql -N 去掉表头