Zabbix configures DingTalk alarms, fault self-healing, and monitoring Java

1. Configure DingTalk alarms

server configuration

DingTalk alert python script

Screenplay 1

cd  /lib/zabbix/alertscripts/
 cat  python20 
#!/usr/bin/python3
#coding:utf-8
import requests,json,sys,os,datetime
# 机器人的Webhook地址
webhook="钉钉"
user=sys.argv[1]
text=sys.argv[3]
data={
    
    
    "msgtype": "text",
    "text": {
    
    
        "content": text
    },
    "at": {
    
    
        "atMobiles": [
            # 在web端用户绑定媒介中通过指定用户注册是的手机号,可以通知指定人员,否则只发消息,不艾特具体人
           user
        ],
        "isAtAll": False
        # 设置为1,则@所有人
        # "isAtAll": 1
    }
}
headers = {
    
    'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/var/log/zabbix/dingding.log"):
    f=open("/var/log/zabbix/dingding.log","a+")
else:
    f=open("/var/log/zabbix/dingding.log","w+")
f.write("\n"+"--"*30)
if x.json()["errcode"] == 0:
    f.write("\n"+str(datetime.datetime.now())+"    "+str(user)+"    "+"发送成功"+"\n"+str(text))
    f.close()
else:
    f.write("\n"+str(datetime.datetime.now()) + "    " + str(user) + "    " + "发送失败" + "\n" + str(text))

Screenplay 2

cat ding
#!/usr/bin/env python3
#zabbix钉钉报警
import requests,json,sys,os,datetime
##刚刚保存的Webhook
webhook="钉钉Webhook"
user=sys.argv[1]  ##执行脚本时传递的第一个参数,发送用户
text=sys.argv[3]  ##执行脚本时传递的第三个参数,发送内容
data={
    
    
  "msgtype": "text",
  "text": {
    
    
    "content": text
  },
  "at": {
    
    
    "atMobiles": [
      user
    ],
    "isAtAll": False
  }
}
headers = {
    
    'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/usr/lib/zabbix/logs/dingding.log"):
  f=open("/usr/lib/zabbix/logs/dingding.log","a+")
else:
  f=open("/usr/lib/zabbix/logs/dingding.log","w+")
f.write("\n"+"--"*30)
if x.json()["errcode"] == 0:
  f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text))
  f.close()

web interface creation medium

Error 1:
insert image description here


sh: 1: /usr/lib/zabbix/alertscripts/dingding.py: not found
单python3 文件名字 测试正常,还以为ubuntu不支持,结果多次测试是因为是在window复制创建的文件,使用./文件名发现有其他字符^M,^M字符表示回车符,它可能是由于使用Windows风格的换行符引起的。在Linux系统上,需要使用Unix风格的换行符。
你可以尝试使用dos2unix命令来转换文件的换行符格式。打开终端,并运行以下命令:dos2unix python20

root@ubuntu20:/usr/lib/zabbix/alertscripts# ./dingding3.py 
-bash: ./dingding3.py: /usr/bin/python3^M: bad interpreter: No such file or directory




Add media to users

insert image description here

Test alarm

Implement fault self-healing function

When Zabbix monitors an abnormality in a specified monitoring item, it can automatically recover from the fault through specified operations. Taking the nginx service as an example, when the nginx service stops, it automatically restarts the service to realize the fault self-healing function.

agent add configuration

vim /etc/zabbix/zabbix_agentd.conf 
AllowKey=system.run[*] #允许所有远程命令,zabbix5.0版本以上,代替EnableRemoteCommands,agent2默认没有此命令手工加入
#EnableRemoteCommands=1 #开启远程执行命令,此指令在zabbix5.0版本以上淘汰
AllowRoot=1  #开启此项,直接使用root进行远程管理,而无须对zabbix用户授权sudo权限,agent2不支持,使用下面的对zabbix授权
UnsafeUserParameters=1  #允许远程执行命令的时候使用不安全的参数(特殊字符串,如: \ ' *

By default, the zabbix agent is started using the zabbix user. Some privileged commands are not executed by the zabbix user, which will cause the defined
self-healing strategy to fail due to permission denial, so the zabbix user needs to be authorized in advance.

[root@centos8 ~]#vim /etc/sudoers
zabbix ALL=(ALL) NOPASSWD: ALL
#Check syntax
[root@centos8 ~]#visudo -c
restart
systemctl restart zabbix-agent2.service
server configuration

Create action
Create action, trigger
insert image description here

add in action
insert image description here

Stop nginx and the systemctl start nginx command will be automatically executed to recover from the fault.

insert image description here

Active mode
Active mode and passive mode relative to agent

ServerActive=192.168.1.150 #To whom to report, turn on the active mode
Hostname=192.168.1.220 #The Hostname in the agent must be the same as the host name set in Zabbix Server, otherwise the data cannot be collected, and an error will be reported that it cannot be found.

Monitoring items are changed to proactive
insert image description here

Active mode monitoring items
insert image description here

Copy the template and update monitoring items in batches to be proactive
insert image description here

Host parallel connection with new template
insert image description here

Question 1

2023/09/04 19:34:48.385426 [101] no active checks on server [192.168.1.150:10051]: host [zhujiming-192.168.1.220] not found
2023/09/04 19:36:49.387602 [101] no active checks on server [192.168.1.150:10051]: host [zhujiming-192.168.1.220] not found
2023/09/04 19:38:50.397644 [101] no active checks on server [192.168.1.150:10051]: host [zhujiming-192.168.1.220] not found
2023/09/04 19:40:51.382330 [101] no active checks on server [192.168.1.150:10051]: host [zhujiming-192.168.1.220] not found
界面查看数据没有更新,查看日志,agent配置文件中hostname和服务端页面的主机名称要一样

Question 2: zbx is gray, but the data is normal, because the templates of this host are all in active mode. The server will not send it to the agent. Just add other passive mode templates.
insert image description here

Monitor Java

Zabbix does not support direct monitoring of JAVA applications.
If you want to monitor JAVA programs such as Tomcat, you need to use Java gateway as a proxy to obtain data from JAVA applications.

Zabbix monitors the JVM process.
zabbix-server notifies zabbix-Java-Gateway which monitoring items need to be obtained from the monitoring host.
Zabbix-Java-Gateway requests the collection of Java process data through the JMX protocol.
The Java program returns the data to zabbix-Java-Gateway through the JMX protocol.
zabbix-Java -Gateway finally returns the data to zabbix-server.
zabbix-server stores the collected JAVA data and then displays it on the Web.

192.168.1.250 Install the java+tomcat environment
Tomcat 开启 JMX 功能
192.168.1.250填本机ip
#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.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.1.250”

systemctl restart tomcat

[root@rocky8 ~]# ss -tnl|grep 12345
LISTEN 0 50 *:12345 :

zabbix服务端测试
root@ubuntu20:~# java -jar cmdline-jmxclient-0.10.3.jar - 192.168.1.250:12345  'Catalina:name="http-nio-8080",type=ThreadPool' currentThreadCount

09/04/2023 23:03:05 +0800 org.archive.jmx.Client currentThreadCount: 10


java线程数
java -jar cmdline-jmxclient-0.10.3.jar - 192.168.1.250:12345  'Catalina:name="http-nio-8080",type=ThreadPool' maxThreads
09/04/2023 23:07:41 +0800 org.archive.jmx.Client maxThreads: 200

zabbix server installation java gateway

Java gateway is a component independent of zabbix server and zabbix agent. It uses port 10052/tcp by default, so java
gateway can be a separate server, but it can also share a server with zabbix server or zabbix agent.

apt -y install zabbix-java-gateway

Configure Zabbix Server to support Java gateway

zabbix modifies and adds configuration
root@ubuntu20:~# vi /etc/zabbix/zabbix_server.conf
JavaGateway=192.168.1.150 #Point to the JAVA gateway host
StartJavaPollers=10 #Specify the number of processes to open

Use the system's built-in template to monitor the tomcat host

Add hostj

Add java template to view data
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/m0_37749659/article/details/132658639