Zabbix低级自动发现及MySQL多实例监控

概述

Zabbix的网络发现是指zabbix server通过配置好的规则,自动添加host,group,template
Zabbix的主动注册刚好和网络发现是相反的,功能基本一致。zabbix agent主动联系zabbix server,server自动添加host,group,template
以上两种方式都是发现host,添加host,而low-level discovery(低级自动发现)更加底层,用于发现item,trigger,graph等等。

查看系统自带的网卡自动发现

查看网络自动发现规则

过滤规则

命令行过滤

1
2
[root@db01 ~]# zabbix_agentd -p |grep net.if.discovery
net.if.discovery [s|{"data":[{"{#IFNAME}":"eth0"},{"{#IFNAME}":"eth1"},{"{#IFNAME}":"lo"}]}]

查看自动发现监控项

查看key的值

1
2
3
4
5
6
7
8
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k net.if.in[eth0]
1552020663
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k net.if.in[eth1]
7983894
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k net.if.out[eth0]
269091531
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k net.if.out[eth1]
52448

监控mysql多实例

准备MySQL多实例

1
2
## 创建目录
mkdir -p /data/330{7,8}/{data,logs}
1
2
3
4
5
6
7
8
9
10
11
12
cat > /data/3307/my.cnf <<EOF
[mysqld]
basedir=/data/mysql/
datadir=/data/3307/data
user=mysql
secure-file-priv=/tmp
socket=/data/3307/mysql.sock
log_error=/data/3307/logs/mysql.log
port=3307
server_id=7
log_bin=/data/3307/logs/mysql-bin
EOF
1
2
3
4
5
6
7
8
9
10
11
12
cat > /data/3308/my.cnf <<EOF
[mysqld]
basedir=/data/mysql/
datadir=/data/3308/data
user=mysql
secure-file-priv=/tmp
socket=/data/3308/mysql.sock
log_error=/data/3308/logs/mysql.log
port=3308
server_id=8
log_bin=/data/3308/logs/mysql-bin
EOF

初始化

1
2
mysqld --initialize-insecure  --user=mysql --basedir=/data/mysql --datadir=/data/3307/data
mysqld --initialize-insecure --user=mysql --basedir=/data/mysql --datadir=/data/3308/data

启动MySQL多实例

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db01 ~]# cat  /etc/systemd/system/mysqld7.service
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/data/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db01 ~]# cat /etc/systemd/system/mysqld8.service 
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/data/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000

!– endtab –>

1
2
3
4
5
6
[root@db01 ~]# systemctl start mysqld7
[root@db01 ~]# systemctl start mysqld8
[root@db01 ~]# netstat -lntup|grep mysql
tcp6 0 0 :::3306 :::* LISTEN 44641/mysqld
tcp6 0 0 :::3307 :::* LISTEN 27551/mysqld
tcp6 0 0 :::3308 :::* LISTEN 27593/mysqld

创建自动发现多实例脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cat /server/scripts/mysql_discovery.sh                                                
#!/bin/bash
# from www.boysec.cn
#mysql low-level discovery
res=$(netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$4}')
port=($res)
printf '{'
printf '"data":['
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 ']'
printf '}\n'

测试自动发现脚本

1
2
[root@db01 scripts]# bash /server/scripts/mysql_discovery.sh            
{"data":[{"{#MYSQLPORT}":"3306"},{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"}]}

创建自动发现配置文件

1
2
[root@db01 ~]# cat /etc/zabbix/zabbix_agentd.d/mysql_discovery.conf
UserParameter=mysql.discovery,/bin/bash /server/scripts/mysql_discovery.sh

重启agent测试取值

1
2
3
4
5
6
7
8
## zabbix-agent段
[root@db01 scripts]# systemctl restart zabbix-agent.service

## zabbix-server
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k mysql.discovery
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
{"data":[]}

这时我们发现取不出来并提示了个错误
原因是zabbix用户不能使用netstat的-p参数
解决方法为给netstat命令添加s权限

1
[root@db01 scripts]# chmod u+s /usr/bin/netstat 

再一次取值

1
2
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k mysql.discovery
{"data":[{"{#MYSQLPORT}":"3306"},{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"}]}

web页面创建自动发现规则模版

监控MySQL是否存活,创建监控项原型

1
2
3
4
5
[root@db01 scripts]# mysqladmin ping -uroot  -h 127.0.0.1 -P3308 2>/dev/null|grep -c alive  
1
[root@db01 scripts]# mysqladmin ping -uroot -h 127.0.0.1 -P3306 2>/dev/null|grep -c alive
1
[root@db01 scripts]# mysqladmin ping -uroot -h 127.0.0.1 -P3307 2>/dev/null|grep -c alive

编写agent配置文件

1
2
3
4
[root@db01 scripts]# cat /etc/zabbix/zabbix_agentd.d/mysql_alive.conf 
UserParameter=mysql_alive[*],mysqladmin ping -uroot -h 127.0.0.1 -P$1 2>/dev/null|grep -c

systemctl restart zabbix-agent.service

server端验证

1
2
3
4
5
6
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k mysql_alive[3306]
1
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k mysql_alive[3307]
1
[root@zabbix ~]# zabbix_get -s 10.1.1.60 -k mysql_alive[3308]
1

web添加