How to monitor port status in batches in Zabbix
Introduction
When monitoring services in the production environment, we usually need to monitor multiple ports. If we manually add them one by one, it will be very troublesome, to reduce this situation, we use the method of adding monitoring ports in batches. This is a very common requirement. Zabbix also supports this method and needs to use the zabbix's Discovery function, the following small series will share with you how to add counterpart occupation in batches.
The general process of using Zabbix to monitor the server port status is as follows: The zabbix monitoring service has its own port monitoring metrics, so we need to manually define the monitored item, the port list obtained by the client is sent to the server through the agent. You only need to configure the port monitoring template on the server, then customize the monitoring image, and add monitoring items!
Next, let's share with you how to add ports in batches!
1. automatically scan ports and monitor alarms 1. write scripts to scan ports
Vim check_port.py
#!/usr/bin/env python
import os
import json
portlist = []
new_port_list = []
port_dict = {"data":None}
cmd = '''netstat -tnlp|egrep -i "$1"|awk {'print $4'}|'''
cmd += '''awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort -n| uniq 2>/dev/null'''
auto_localport = os.popen(cmd).readlines()
for ports in auto_localport:
new_port = ports.strip()
portlist.append(new_port)
for port in portlist:
pdict = {}
pdict["{#TCP_PORT}"] = port
new_port_list.append(pdict)
port_dict["data"] = new_port_list
jsonStr = json.dumps(port_dict,sort_keys=True,indent=4)
#python3
#print(jsonStr)
#python2
print jsonStr
The role of a script is to collect data from the monitored server and convert it to a specific dictionary format. This format is a data dictionary format that zabbix can recognize, it is easier to compile shell with python.
Place the script in the/usr/lib/zabbix/alertscripts path of the client.
2. Modify the zabbix_agent.conf configuration file of the monitored end.
UnsafeUserParameters = 1
UserParameter = tcpportlisten,/usr/lib/zabbix/alertscripts/check_port.sh
Restart the Client Service
3. server test
Run zabbix_get-s 192.168.92.9-p 10050-k tcpportlisten.
If the data in the preceding format is displayed, the script is successfully written !!!
Second: page configuration 1. Add a template
2. Add an automatic discovery rule
In the newly created template, click --> automatic discovery rule --> and select create discovery rule.
Note: The above key must be consistent with the key in the configuration file. The parameter key added in the second line of the two lines of code in the zabbix_agent.conf File
3. Create a metric prototype
# TCP_PORT} in the key value above is consistent with the parameter in our script check_port.sh
4. Create a trigger type
Add button to go to --> select prototype
Note that count (#3, 0, eq)> 1 indicates that the return value for the last three times is 0. An alarm is triggered when this condition is triggered.
After the configuration is completed, zabbix automatically scans and monitors the system.
Third: Batch add specified ports
Sometimes we do not need to monitor all the ports automatically scanned. We need to specify these ports. This requirement is also quite common. With the above foundation, it is quite simple to implement this, in fact, a closer look at the script can be achieved.
To implement this function, we only need to replace the script with the following content:
#!/usr/bin/env python
import json
port_list = ["80",
"10050",
"3306",
"22",
"8080"]
new_port = []
port_dict = {"data":None}
for port in port_list:
pdict = {}
pdict["{#TCP_PORT}"] = port
new_port.append(pdict)
port_dict["data"] = new_port
jsonStr = json.dumps(port_dict,sort_keys=True,indent=4)
#python3
#print(jsonStr)
#python2
print jsonStr
We only need to add the port number to be monitored in port_list! Note the format, separated by commas!
More Zabbix Tutorials:
Install Zabbix 16.04 on Ubuntu 3.2 Server
Set up Zabbix3.0 in CentOS 7 LNMP Environment
Install and deploy the monitoring system Zabbix2.4 on Ubuntu 16.04
Zabbix monitoring installation and deployment and alarm Configuration
Detailed description of the Zabbix trigger expression
Install and deploy Zabbix3.0 in Ubuntu 16.04
Under CentOS 6.3, Zabbix monitors apache server-status
How to install Zabbix 3.0 in CentOS 7
Install Zabbix 2.0.6 in 64-bit CentOS 6.2
Zabbix 3.2.6 monitor Oracle database through Orabbix
ZABBIX details: click here
ZABBIX: click here