Get the App
SLTechnology News&Howtos  ›  Servers  › 

Zabbix automatically discovers host, port, and custom script key values

Shulou Source: shulou.com Published: 2022-06-02 08:34:41 09月17日 Update

Autodiscover host rules

Start by creating an AutoDiscovery host:

Configure AutoDiscovery Rules:

After completion of the automatic discovery in the action to create a discovery action:

custom scripts

zabbix custom monitoring key value automatic discovery automatic execution of remote scripts

For example:

Monitoring whether a process exists:

mkdir /etc/zabbix/alertscripts/

vim check_badvp.sh

#!/ bin/bash

result=`ps -ef | grep badvp-udpgw | grep -v grep`

if [ -n "$result" ]

then

echo '1'

else

echo '0'

fi

Through the script to determine whether this process exists returns 1 does not exist returns 0, we customize this script to the above directory

Configure the script path and key value in zabbix_agent.conf

UnsafeUserParameters=1

UserParameter=check_badvp,/bin/bash /etc/zabbix/alertscripts/check_badvp.sh

Modify timeout

Timeout = 8

Set timeout to avoid script execution failure

Set the script to start the service/opt/autorestart.sh

Finally, modify the sudoers file and add zabiix user's startup permissions visudo in the following line

root ALL=(ALL) ALL

zabbix ALL=(root) NOPASSWD:/opt/autorestart.sh

Finally restart the agent.

interface settings

Create a monitoring item:

Create Trigger:

Last conditional action notification:

Finally realize alarm:

zabbix port auto-discovery

After many experiments, due to the large number and irregular business ports on the company server, a large number of useless ports will be found by using the automatic port discovery rule (netstat) method on the Internet, and it is not friendly to only display ports when alarming. Finally, a solution has been found.

Python scripts and pyadmin scripts

Use zabbix custom key to call the company server existing process management tool pyadmin

Python script:

#!/ usr/bin/python

import os,json

#command = "sudo /usr/skymobi/admin/PYsanpadmin |awk -F'_' {'print $2'} | awk '{print $1}' | sort|uniq"

command = "sudo /usr/skymobi/admin/PYsanpadmin"

lines = os.popen(command).readlines()

resultLi = []

for line in lines:

lineLi = line.split()

newLineLi = lineLi[0].split("_")

resDic = {"{#SERVICES}":newLineLi[0],"{#TCP_PORT}":newLineLi[1]}

resultLi.append(resDic)

data = {"data":resultLi}

jsonStr = json.dumps(data, sort_keys=True, indent=4)

jsonStr=jsonStr.replace(r"\u001b[1;32;40m","").replace(r"\u001b[1;31;40m","").replace(r"\u001b[1;33;40m","")

print jsonStr

Replace in the script is to filter me PY output color green red No in the print json format will have color code

PYadmin Output Results

The principle is to use python script to separate the output of pyadmin management tool, filter out the process name and port, and pass it to zabbix automatic discovery rules.

vim zabbix-agentd.conf

UserParameter=nettcpservice,/usr/bin/python /etc/zabbix/alertscripts/discover_tcpport.py "$1" "$2"

Restart agent.

Configure AutoDiscovery Rules

Key value corresponds to key value in our configuration file, here type is active, in order to reduce the number of client server connections caused by server detection.

Finally bind to our server ~

zabbix agent Value should be a JSON object Troubleshooting

Consider script execution permissions first

chmod 755 discovery script

Second visudo, last line added

zabbix ALL=(ALL) NOPASSWD: ALL

Tags: Scripts ports services rules servers processes configurations actions results monitoring output host line company tools files time permissions administrative tools colors Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei MySQL Docker Redmi