
在
批量扫描添加主机




自定义模板
监控内存使用情况
#监控内存使用情况
[root@jpress2 ~]#vim /etc/zabbix/zabbix_agent2.d/mem.conf
UserParameter=mem_use_percent,free | awk 'NR==2{print $3/$2*100}'
UserParameter=mem_unuse_percent,free | awk 'NR==2{print 100-($3/$2*100)}'
#本机测试
[root@jpress2 ~]#zabbix_agent2 -t mem_use_percent
mem_use_percent [s|45.4037]
[root@jpress2 ~]#zabbix_agent2 -t mem_unuse_percent
mem_unuse_percent [s|54.5945]
[root@jpress2 ~]#systemctl restart zabbix-agent2.service
#服务端测试
[root@ntpzabbix ~]#zabbix_get -s 10.0.0.207 -k "mem_use_percent"
45.137
[root@ntpzabbix ~]#zabbix_get -s 10.0.0.207 -k "mem_unuse_percent"
54.8438







TCP十一种状态监控
ESTABLISHED
SYN_SENT
SYN_RECV
FIN_WAIT1
FIN_WAIT2
TIME_WAIT
CLOSE
CLOSE_WAIT
LAST_ACK
LISTEN
CLOSING
[root@jpress2 ~]#vim /etc/zabbix/zabbix_agent2.d/mem.conf
UserParameter=tcp_state_[*],netstat -ant|grep -c $1
[root@jpress2 ~]#zabbix_agent2 -t tcp_state_[ESTABLISHED]
tcp_state_[ESTABLISHED] [s|1]
[root@jpress2 ~]#zabbix_agent2 -t tcp_state_[CLOSE_WAIT]
tcp_state_[CLOSE_WAIT] [s|0]
[root@jpress2 ~]#systemctl restart zabbix-agent2.service
#服务端测试
[root@ntpzabbix ~]#zabbix_get -s 10.0.0.207 -k "tcp_state_[CLOSE_WAIT]"
0
[root@ntpzabbix ~]#zabbix_get -s 10.0.0.207 -k "tcp_state_[ESTABLISHED]"
2




监控mysql数据库
mysql> create user zabbix@'10.0.0.%' identified by '123456';
[root@wpmsyqlslave zabbix_agent2.d]#cat mysql.conf
UserParameter=mysql.slave[*],/etc/zabbix/zabbix_agent2.d/mysql_slave_state.sh "$1"
UserParameter=mysql_version,/usr/local/mysql/bin/mysql -V
UserParameter=mysql_ping,/usr/local/mysql/bin/mysqladmin -uzabbix -p123456 -P3306 -h10.0.0.155 ping 2>/dev/null | grep -c alive
[root@wpmsyqlslave zabbix_agent2.d]#cat mysql_slave_state.sh
#!/bin/bash
#
#********************************************************************
#Author: shuhong
#QQ: 985347841
#Date: 2022-10-08
#FileName: mysql_slave_state.sh
#URL: hhhhh
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
KEY=$1
PASS='123456'
/usr/local/mysql/bin/mysql -uzabbix -p$PASS -h 10.0.0.155 -e "show slave status\G" 2> /dev/null |grep "${KEY}:"|awk '{print $NF}'

监控java程序
[root@jpress2 zabbix_agent2.d]#vim /usr/local/tomcat/bin/catalina.sh
CATALINA_OPTS="$CATALINA__OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.manageme
nt.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.207"
[root@jpress2 zabbix_agent2.d]#systemctl restart tomcat.service
#查看12345端口
[root@jpress2 zabbix_agent2.d]#ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=739,fd=13))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=772,fd=3))
LISTEN 0 50 *:33003 *:* users:(("java",pid=461515,fd=20))
LISTEN 0 100 *:8080 *:* users:(("java",pid=461515,fd=74))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=772,fd=4))
LISTEN 0 50 *:12345 *:* users:(("java",pid=461515,fd=21))
LISTEN 0 4096 *:10050 *:* users:(("zabbix_agent2",pid=156484,fd=8))
LISTEN 0 50 *:43589 *:* users:(("java",pid=461515,fd=24))
#测试通过java工具 jconsole 进行 JMX 监控
[root@ntpzabbix ~]#rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/8/x86_64/zabbix-release-5.0-1.el8.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/5.0/rhel/8/x86_64/zabbix-release-5.0-1.el8.noarch.rpm
Verifying... ################################# [100%]
Preparing... ################################# [100%]
package zabbix-release-5.0-1.el8.noarch is already installed
[root@ntpzabbix ~]#sed -i.bak 's/repo.zabbix.com/mirrors.aliyun.com\/zabbix/' /etc/yum.repos.d/zabbix.repo
[root@ntpzabbix ~]#vim /etc/zabbix/zabbix_server.conf
JavaGateway=10.0.0.162
JavaGatewayPort=10052
StartJavaPollers=10
[root@ntpzabbix ~]#systemctl restart zabbix-java-gateway.service
[root@ntpzabbix ~]#systemctl restart zabbix-server.service
#查看10052端口开启
[root@ntpzabbix ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10051 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 [::]:10051 [::]:*
LISTEN 0 50 *:10052 *:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
java -jar cmdline-jmxclient-0.10.3.jar - 10.0.0.207:12345 'Catalina:name="http-nio-8080",type=ThreadPool' maxThreads


创建自定义触发器







创建图形




聚合图形




创建用户和组


告警通知
邮件告警





#可以修改两个消息模板
# 问题
主题: 告警: {EVENT.NAME}
告警主机:{HOST.NAME1}
告警服务: {ITEM.NAME1}
告警Key1: {ITEM.KEY1}:{ITEM.VALUE1}
告警Key2: {ITEM.KEY2}:{ITEM.VALUE2}
严重级别: {TRIGGER.SEVERITY}
# 恢复 Problem Recovery
主题: 恢复: {EVENT.DURATION}: {EVENT.NAME}
恢复主机:{HOST.NAME1}
恢复服务: {ITEM.NAME1}
恢复Key1:{ITEM.KEY1}:{ITEM.VALUE1}
恢复Key2: {ITEM.KEY2}:{ITEM.VALUE2}


添加动作



微信通知
[root@ntpzabbix ~]#vim /lib/zabbix/alertscripts/wechat.sh
#!/bin/bash
#
#********************************************************************
#Author: shuhong
#QQ: 985347841
#Date: 2022-10-09
#FileName: /lib/zabbix/alertscripts/wechat.sh
#URL: hhhhh
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
CorpID="ww644a0d95807e476b" #我的企业下面的CorpID
Secret="cGp4gHUpHLL1KQZY5abM3panGp-uCrEZyrgDvOT0OZA" #创建的应用那里的Secret
agentid=1000003 #改为 AgentId 在创建的应用可以查看
#PartyID=1 #通讯录中的部门ID,可选项
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CorpID&corpsecret=$Secret"
Token=$(/usr/bin/curl -s -G $GURL |awk -F\": '{print $4}'|awk -F\" '{print $2}')
#echo $Token
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Token"
function body(){
local userid=$1 #脚本的第1个参数为接收微信的企业用户帐号,在通讯录中可以查看
#local partyid=$PartyID
local subject=$2 #脚本的第2个参数,表示消息主题
local msg=$(echo "$@" | cut -d" " -f3-) #脚本的第3个参数,表示消息正文
printf '{\n'
printf '\t"touser": "'"$userid"\"",\n"
#printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"msgtype": "text",\n'
printf '\t"agentid": "'"$agentid"\"",\n"
printf '\t"text": {\n'
printf '\t\t"content": "'"$subject\n\n$msg"\""\n"
printf '\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'
}
[root@ntpzabbix ~]#chmod +x /lib/zabbix/alertscripts/wechat.sh
#测试
[root@ntpzabbix ~]#/lib/zabbix/alertscripts/wechat.sh 微信号 主题 内容
{"errcode":0,"errmsg":"ok","msgid":"WpLDpQFMGSE843kRbNhgXa8rMsdgvvCQpJzHtUnVG6YuCaVccGcuaEX--ysOAvVub0va_kJW3ZuIW7oD5U1tyg"}



被动模式切换主动模式
Zabbix Agent 默认是被动模式,需要修改配置才能支持主动模式
Server=<Zabbix Server> #被动模式指向 Zabbix Server的IP或FQDN,默认模式
ServerActive=<Zabbix Server> #主动模式指向 Zabbix Server的IP或FQDN
Hostname=<当前主机IP> #当前主机的IP,此项必须和Zabbix Server中设置的主机名称相同
[root@LVS zabbix]#vim zabbix_agent2.conf
Server=10.0.0.162
ServerActive=10.0.0.162
Hostname=10.0.0.152
[root@LVS zabbix]#systemctl restart zabbix-agent2.service

更换模板
利用 Zabbix 监控 LInux 主机(SNMP)
[root@wordpress1 ~]#apt install snmpd
[root@wordpress1 ~]#snmpd -v
NET-SNMP version: 5.8
Web: http://www.net-snmp.org/
Email: net-snmp-coders@lists.sourceforge.net
[root@wordpress1 ~]#vi /etc/snmp/snmpd.conf
agentaddress 0.0.0.0:161
view systemonly included .1
#服务端安装测试工具
[root@ntpzabbix ~]#yum -y install net-snmp-utils
[root@ntpzabbix ~]#snmpwalk -v 2c -cpublic 10.0.0.202 .1.3.6.1.2.1.1.5.0
SNMPv2-MIB::sysName.0 = STRING: wordpress1.sh

