Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of batch Monitoring Port status by Zabbix

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Introduction

When monitoring services in the production environment, we usually need to monitor multiple ports. If we add one by one manually, it will be very troublesome. In order to reduce this situation, we use the method of adding monitoring ports in batches, which is a very common requirement. Zabbix also supports this method, which needs to be implemented using the Discovery function of zabbix. The following editor will share with you about the occupation of bulk adding ports.

Use Zabbix to monitor how to monitor the port status of the server. The general process: the zabbix monitoring service comes with the monitoring items monitored by the port, so we need to manually define the monitored item. The list of ports obtained by the client is transmitted to the server through agent.

Just configure the port monitoring template on the server, then customize the monitoring graph and add monitoring items!

Let's share with you how to add ports in batch!

First: automatically scan the port and monitor the alarm

1. Script scan port

Vim checkcheckcheck port.pyknosis pedigree cmd binenv pythonimport osimport jsonportlist = [] new_port_list = [] port_dict = {"data": None} cmd = 'netstat-tnlp | egrep-I "$1" | awk {' print $4'} |''cmd + =' 'awk-NF''{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}"] = portnew_port_list.append (pdict) port_dict ["data"] = new_port_listjsonStr = json.dumps (port_dict Sort_keys=True,indent=4) # python3#print (jsonStr) # python2print jsonStr

The function of the script is to collect data from the monitored server and convert it to a specific dictionary format, which is a data dictionary format recognized by zabbix. It is more convenient to use python to write than shell.

Put the script under the client / usr/lib/zabbix/alertscripts path

two。 Modify the zabbix_agent.conf configuration file of the monitored side

UnsafeUserParameters=1UserParameter=tcpportlisten,/usr/lib/zabbix/alertscripts/check_port.sh

Restart client service

3. Server-side testing

Execute the command: zabbix_get-s 192.168.92.9-p 10050-k tcpportlisten

The data in the above format appears, which means that the script is written successfully!

Second: page configuration

1. Add templat

two。 Add an autodiscovery rule

Click-- > Auto-Discovery Rule-- > in the newly created template above, and then select create Discovery Rule.

Note: the above key must be consistent with the key in the configuration file. This is the parameter key that we added in the second line of the two lines of code we added in the zabbix_agent.conf file.

3. Create a monitoring item prototype

The # TCP_PORT} in the key values above is the same as the parameters in our script check_port.sh

4. Create trigger type

Add button to enter-- > Select prototype

Note that the statement here shows that count (# 3 ~ 0 ~ eq) > 1 means that the return value of the last 3 times is 0. If this condition is triggered once, the alarm will be given.

With this configuration, we have completed the function of automatic scanning and monitoring of zabbix.

Third: add designated ports in batch

Sometimes we do not need to monitor all the ports that are scanned automatically, these ports need to be specified, and this requirement is quite common. With the above foundation, it is actually relatively simple to implement this thing. In fact, it can be achieved by taking a closer look at the script.

To do this, we just need to replace the script with the following:

#! / usr/bin/env pythonimport jsonport_list = ["80", "10050", "3306", "22", "8080"] new_port = [] port_dict = {"data": None} for port in port_list:pdict = {} pdict [{# TCP_PORT} "] = portnew_port.append (pdict) port_dict [" data "] = new_portjsonStr = json.dumps (port_dict,sort_keys=True Indent=4) # python3#print (jsonStr) # python2print jsonStr

We just need to add the port number that we want to monitor in the port_list! Pay attention to the format, separated by commas!

Summary

The above is the method of Zabbix to achieve batch monitoring port status, which is introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report