CentOS 7.7 yum方式安装配置Zabbix 4.0 LTS详解(二)
一、添加Linux主机,自定义监控项、触发器和使用内置模板进行监控:
1、node-122节点部署Zabbix Agent:
(1)安装zabbix-agent软件包:# yum -y install zabbix-agent
(2)修改zabbix_agentd.conf配置文件:
# cd /etc/zabbix
# cp zabbix_agentd.conf{,.bak}
# vim zabbix_agentd.conf
修改前 | 修改后 |
PidFile=/var/run/zabbix/zabbix_agentd.pid | 保持默认 |
# LogType=file | LogType=file |
LogFile=/var/log/zabbix/zabbix_agentd.log | 保持默认 |
LogFileSize=0 | 保持默认 |
# EnableRemoteCommands=0 | EnableRemoteCommands=1 |
Server=127.0.0.1 | Server=192.168.0.120 |
# ListenPort=10050 | ListenPort=10050 |
# ListenIP=0.0.0.0 | ListenIP=192.168.0.122 |
ServerActive=127.0.0.1 | ServerActive=192.168.0.120 |
Hostname=Zabbix server | Hostname=node-122 |
# AllowRoot=0 | AllowRoot=0 |
# User=zabbix | User=zabbix |
Include=/etc/zabbix/zabbix_agentd.d/*.conf | 保持默认 |
# UnsafeUserParameters=0 | UnsafeUserParameters=1 |
(3)启动Zabbix Agent:
# systemctl start zabbix-agent
# systemctl status zabbix-agent
# ps aux | grep zabbix_agent
# ss -tunlp | grep -w 10050
# tail -100 /var/log/zabbix/zabbix_agentd.log
(4)配置开机自启:# systemctl enable zabbix-agent
2、配置Zabbix Web:
Configuration --> Hosts --> Create host --> Add
由于没有使用模板定义监控项,所以"ZBX"的图标不会变绿
3、node-122节点定义监控项:
(1)监控TCP 21号端口是否被监听(内置Key):
Create Item:
说明:Key的具体用法参见
https://www.zabbix.com/documentation/4.0/manual/config/items/itemtypes/zabbix_agent
(2)监控服务器根分区磁盘TPS(自定义无参数Key):
a、安装sysstat软件包:# yum -y install sysstat
b、使用awk命令获取服务器根分区磁盘TPS的数值:# iostat | awk '$1~/^dm-0$/{print $2}'
c、创建disk_tps.conf配置文件:
# vim /etc/zabbix/zabbix_agentd.d/disk_tps.conf
UserParameter=disk.tps,iostat | awk '$1~/^dm-0$/{print $2}'
# systemctl restart zabbix-agent
备注:Zabbix Agent查看所有Key的命令# zabbix_agentd -p | less
d、zabbix-server节点安装zabbix-get软件包,并尝试获取监控数据:
# yum -y install zabbix-get
# zabbix_get -s 192.168.0.122 -k disk.tps
e、创建监控项:
(3)监控服务器内存使用情况(自定义有参数Key):
a、使用awk命令获取服务器内存使用情况:
# free -h | awk '$1~/^Mem:$/{print $2}'
# free -h | awk '$1~/^Mem:$/{print $3}'
# free -h | awk '$1~/^Mem:$/{print $4}'
# free -h | awk '$1~/^Mem:$/{print $NF}'
b、将awk取值命令写成脚本:
# mkdir -pv /scripts
# vim /scripts/check_mem.sh
#!/bin/bash
case "$1" in
"total")
free -h | awk '$1~/^Mem:$/{print $2}'
;;
"used")
free -h | awk '$1~/^Mem:$/{print $3}'
;;
"free")
free -h | awk '$1~/^Mem:$/{print $4}'
;;
"available")
free -h | awk '$1~/^Mem:$/{print $NF}'
;;
*)
echo "Usage: { total | used | free | available }"
;;
esac
# chmod +x /scripts/check_mem.sh
c、创建check_mem.conf配置文件:
# vim /etc/zabbix/zabbix_agentd.d/check_mem.conf
UserParameter=mem.check[*],/scripts/check_mem.sh $1
# systemctl restart zabbix-agent
d、 zabbix-server节点尝试获取监控数据:
# zabbix_get -s 192.168.0.122 -k mem.check[total]
# zabbix_get -s 192.168.0.122 -k mem.check[used]
# zabbix_get -s 192.168.0.122 -k mem.check[free]
# zabbix_get -s 192.168.0.122 -k mem.check[available]
e、创建监控项:
备注:脚本取值得到的结果,如3.8G既不是整型,也不是浮点型,是字符串,所以Type of information选"Character",如果脚本中使用的是free -m命令,则创建Item时Type of information选"Numeric (unsigned)",Units填"MB"
同理克隆Item,Key分别为mem.check[used]、mem.check[free]、mem.check[available]
查看最新数据:Monitoring --> Latest data
4、node-122节点定义触发器:
(1)Check TCP 21:
Create trigger:
(2)根分区tps:最近3分钟的平均值大于1时触发告警
5、node-122节点使用内置模板进行监控:
按照上述方法自定义监控项和触发器,太过繁琐
(1)克隆"Template OS Linux"模板为"Template OS Linux Active",将监控项的默认类型修改为"Zabbix agent (active)",使用主动模式监控:
Configuration --> Templates --> Name中输入linux
(2)node-122节点链接"Template OS Linux Active"模板:
查看最新数据: