zabbix自动化监控

zabbix自动化监控

第1章 自动发现和自动注册

0.快速添加客户端

# 1.安装zabbix-agent客户端
[root@git_web02 ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.11-1.el7.x86_64.rpm

# 2.配置客户端配置文件
[root@git_web02 ~]# cat >/etc/zabbix/zabbix_agentd.conf<<EOF 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.71
Include=/etc/zabbix/zabbix_agentd.d/*.conf
EOF

# 3.启动客户端
[root@git_web02 ~]# systemctl start zabbix-agent
[root@git_web02 ~]# systemctl enable zabbix-agent.service
[root@git_web02 ~]# netstat -lntup|grep 10050

1.自动发现

web页面操作

img

img

img

2.自动注册

要用自动注册就先把自动发现关掉

修改zabbix-agent配置文件

[root@git_web02 ~]# cat /etc/zabbix/zabbix_agentd.conf        
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.71
ServerActive=10.0.0.71
HostMetadata=git_web02
Include=/etc/zabbix/zabbix_agentd.d/*.conf

web页面操作

img

img

把页面上前面添加的web02删掉,重启一下

刷新查看 瞬间添加上了

第2章 主动模式和被动模式

默认为被动模式:100个监控项要100个来回,要的时候才返回
主动模式:100个监控项1个回合,将所需要的100个打包,然后一次发过去,发过去之后,客户端全部执行完再一次返回给服务端。

1.克隆模版

2.修改克隆后的模版为主动模式

监控项全选 批量更新

[root@zabbix ~]# systemctl restart zabbix-server.service 
[root@zabbix ~]# systemctl restart zabbix-agent.service 

3.修改监控主机关联的模版为主动模式

img

4.修改客户端配置文件并重启

[root@git_web01 ~]# cat /etc/zabbix/zabbix_agentd.conf        
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.71
ServerActive=10.0.0.71
Hostname=web01
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@git_web01 ~]# systemctl restart zabbix-agent.service 

5.查看最新数据

发现获取数据的时间是一样的

img

第3章 低级自动发现

自动发现 :自动添加主机

低级自动发现:自动添加监控项

0.为什么需要这个东西?

1.场景一:

有2台mysql数据库
每一台都是多实例
每个mysql实例都要监控指标uptime
db01: 3306 3307 3308
db02: 3309 3310 3311

# shell命令如何实现:
mysql -uroot -p123456 -P 3307 -e "show status  where Variable_name='uptime';"
mysql -uroot -p123456 -P 3308 -e "show status  where Variable_name='uptime';"
mysql -uroot -p123456 -P 3309 -e "show status  where Variable_name='uptime';"

# zabbix使用变量实现替换:
UserParameter=mysql.status[*],mysql -uroot -p123456 -P $1 -e "show status  where Variable_name='uptime';"

# 低级自动发现解释:
先通过某种方法把每台主机的Mysql端口都收集起来
db01 
mysqlport=3307,3308,3309
db02 
mysqlport=3310,3311

for port in ${mysqlport}
do
UserParameter=mysql.status[${port}],mysql -uroot -p123 -P $1 -e "show status  where Variable_name='uptime';"
done 

1.查看系统自带分区自动发现

系统自带的自动发现会显示红字,比如自带的磁盘分区发现规则

img

1.查看zabbbix所有的key过滤后展示

img

2.解析成json后的格式

百度在线json转换

img

3.过滤规则

实质上是从mount命令获取的分区名和类型

img

我们zabbix显示的并没有这么多是因为做了正则表达式过滤

正则表达式是在管理里面配置的

把不想要的过滤掉

img

4.使用zabbix_get获取key因为根据过滤规则,只发现了一个xfs的key,使用zabbix_get可以查看到这个key

img


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

1.查看网络自动发现规则

img

2.过滤规则

img

img

3.命令行过滤

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

4.查看自动添加的监控项
我们会发现添加了四个监控项
2个eth0
2个eth1

img

img

5.查看key的值

[root@zabbix ~]# zabbix_get -s 10.0.0.7 -k net.if.in[eth0]
2191453
[root@zabbix ~]# zabbix_get -s 10.0.0.7 -k net.if.in[eth1]
7152

3.监控mac地址

1.提取mac地址 编辑配置文件

[root@git_web01 ~]# ifconfig eth0 |grep -oP '(?<=ether )\S+'
00:0c:29:c0:c8:a9
[零宽断言]

[root@git_web01 ~]# vim /etc/zabbix/zabbix_agentd.d/mac.conf
UserParameter=net_mac[*],ifconfig $1 |grep -oP '(?<=ether )\S+'

[root@git_web01 ~]# systemctl restart zabbix-agent.service

2.页面操作 创建监控项原型

4.监控mysql多实例

参考强哥博客

[https://www.qstack.com.cn/archives/108.html](https://www.qstack.com.cn/archives/108.html)

1.复制并修改数据库配置文件

[root@gitweb01 ~]#cat >/etc/my3307.cnf<<EOF    
[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
EOF
[root@git_web01 ~]# cp /etc/my3307.cnf /etc/my3308.cnf
[root@git_web01 ~]# sed -i 's#3307#3308#g' /etc/my3308.cnf

2.创建数据目录并初始化

[root@git_web01 ~]# mkdir /data/{3307,3308} -p
[root@git_web01~]# chown -R mysql:mysql /data/330*

[root@git_web01 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
[root@git_web01 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf

3.启动多实例

[root@git_web01 ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
[root@git_web01 ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &

4.检查端口

[root@git_web01 ~]# netstat -lntup|grep mysql
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2042/mysqld         
tcp        0      0 0.0.0.0:3307            0.0.0.0:*               LISTEN      84790/mysqld        
tcp        0      0 0.0.0.0:3308            0.0.0.0:*               LISTEN      85439/mysqld 

[root@git_web01 ~]# mysql -h127.0.0.1 -P3307
[root@git_web01 ~]# mysql -h127.0.0.1 -P3308

5.创建自动发现配置文件

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

6.创建自动发现多实例脚本

[root@git_web01 ~]# cat /server/scripts/mysql_discovery.sh                                                
#!/bin/bash 
#mysql low-level discovery 
res=$(netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$5}')
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'

7.测试自动发现脚本

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

8.重启zabbix-agent

[root@zabbix ~]# systemctl restart zabbix-agent.service 

9.zabbix_get测试取key

[root@zabbix ~]# zabbix_get -s 10.0.0.7 -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权限

[root@zabbix ~]# which netstat 
/usr/bin/netstat
[root@zabbix ~]# chmod u+s /usr/bin/netstat 

然后再次测试就发现可以取到值了

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

10.编写mysql多实例监控脚本

[root@git_web01 ~]# mv /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf /tmp
[root@git_web01 ~]# vim /etc/zabbix/zabbix_agentd.d/mysql_uptime.conf
UserParameter=mysql.status[*],mysql -h127.0.0.1 -P$2 -N -e "show global status where Variable_name='$1';" | awk '{print $$2}'

# 脚本解释
1:-N 只显示你要找的那一行
[root@git_web01 ~]# mysql -e "show global status where Variable_name='uptime'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 15296 |
+---------------+-------+
[root@git_web01 ~]# mysql -N  -e "show global status where Variable_name='uptime'"
+--------+-------+
| Uptime | 15356 |
+--------+-------+

2:-h 端口错误拒绝登录
[root@git_web01 ~]# mysql -N -P35656 -e "show global status where Variable_name='uptime'"|awk '{print $2}'
15597
[root@git_web01 /etc/zabbix/zabbix_agentd.d]# mysql -N -h 127.0.0.1 -P35656 -e "show global status where Variable_name='uptime'"|awk '{print $2}'
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

# 重启zabbix客户端
[root@git_web01 ~]# systemctl restart zabbix-agent

# zabbix服务端测试能否取到数据
[root@zabbix ~]# zabbix_get -s 10.0.0.7 -k mysql.status[Uptime,3307]
[root@zabbix ~]# zabbix_get -s 10.0.0.7 -k mysql.status[Uptime,3308]

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

创建模版

img

创建自动发现规则

img

设置过滤规则

img

创建过滤 规则

img

12.web页面添加监控项原型

img

13.web页面设置主机关联模版

img

14.查看是否已经自动添加成功

img

15.git_web02也做一次

# 1.git_web02安装多实例
[root@git_web02 ~]# yum install mariadb-server -y
[root@git_web02 ~]# systemctl start mariadb 
[root@git_web02 ~]# cat >/etc/my3310.cnf<<EOF    
[mysqld]
datadir=/data/3310/
socket=/data/3310/mysql.sock
port=3310
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/data/3310/mysqld.log
pid-file=/data/3310/mysqld.pid
EOF
[root@git_web02 ~]# cp /etc/my3310.cnf /etc/my3311.cnf
[root@git_web02 ~]# sed -i 's#3310#3311#g' /etc/my3311.cnf
[root@git_web02 ~]# mkdir /data/{3310,3311} -p
[root@git_web02 ~]# chown -R mysql:mysql /data/331*

[root@git_web02 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3310.cnf --force
[root@git_web02 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3311.cnf --force
[root@git_web02 ~]# mysqld_safe --defaults-file=/etc/my3310.cnf &
[root@git_web02 ~]# mysqld_safe --defaults-file=/etc/my3311.cnf &

[root@git_web02 ~]# netstat -lntup|grep mysql
tcp        0      0 0.0.0.0:3306
tcp        0      0 0.0.0.0:3310
tcp        0      0 0.0.0.0:3311


# 2.git_web02复制git_web01的数据
[root@git_web02 ~]# chmod u+s /usr/bin/netstat
[root@git_web02 ~]# rm -rf /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
[root@git_web02 ~]# scp 10.0.0.7:/etc/zabbix/zabbix_agentd.d/* /etc/zabbix/zabbix_agentd.d/
[root@git_web02 ~]# systemctl restart zabbix-agent.service

16.git_web02关联模板

因为我直接链接模板出不来 所以创建了监控项原型 就好了

posted @ 2019-12-24 13:36  干瘪的柠檬  阅读(281)  评论(0)    收藏  举报