


Monitoring ports in shell programming practice (port 80, port 443, etc.)
一般企业用的服务器上面都会跑各种服务,比如nginx、php、mysql、redis、MongoDB等等。一般系统的运行可能会需要多个服务的配合,比如我司的系统需要php、mysql、redis、apache、MongoDB服务。这些服务缺一不可。
所以我们要实时监控这些服务,如果发现有服务出现异常,需要立即告警。这里我们不打算通过进程名来判断服务的状态。我们打算通过端口的监听来判断服务的运行状态。
linux服务器上有一个命令可以用来查看端口状态:netstat。但是在centOS7上,需要先安装net-tools工具,才有这个命令。已经安装这个工具包后,我们来使用netstat命令,看看它会显示哪些信息
# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 11213/redis-server tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1556/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 984/sshd tcp 0 0 127.0.0.1:88 0.0.0.0:* LISTEN 17446/httpd tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1556/nginx: master tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 25859/mongod tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 7138/mysqld
上面显示的信息,不管是端口6379的redis,还是3306的mysql等等,都是运行我司系统必须的服务。然后,我们通过grep命令,过滤掉第一行以及第二行
# netstat -tlnp | grep tcp tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 11213/redis-server tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1556/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 984/sshd tcp 0 0 127.0.0.1:88 0.0.0.0:* LISTEN 17446/httpd tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1556/nginx: master tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 25859/mongod tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 7138/mysqld
然后再通过awk命令获取第四列的信息。
# netstat -tlnp | grep tcp | awk '{print $4}' 127.0.0.1:6379 0.0.0.0:80 0.0.0.0:22 127.0.0.1:88 0.0.0.0:443 127.0.0.1:27017 0.0.0.0:3306
最后再通过cut命令获取到端口号。
# netstat -tlnp | grep tcp | awk '{print $4}' | cut -d: -f2 6379 80 22 88 443 27017 3306
通过上面的操作,我们能获取目前服务器端口的情况,然后我们将需要运行服务的端口存放在数组中,然后遍历该数组和拿到的这些信息做对比。
ports="80 88 443 3306 6379 27017" for port in $ports do echo $port done
遍历这些需要监听的端口后,我们来将端口与netstat获取到的信息作对比。我们这里用grep命令来做判断。下面我们给出完整的代码:
#!/bin/bash # 监控服务器端口情况 export LANG=en ports="80 88 443 3306 6379 27017" netstat_info=$(netstat -tlnp | grep tcp | awk '{print $4}' | cut -d: -f2) for port in $ports do flag=$(echo $netstat_info | grep $port) if [ -z "$flag" ];then echo "$port is dead" fi done
对于端口监控的脚本编写还是很容易的。下面我们来测试下该脚本是否能正常监控端口。所有服务都正常的情况下,执行该脚本不会输出任何信息。如果关闭了nginx服务,则应该出现信息。
首先,所有服务都正常的情况下,执行脚本
# ./port.sh # #不会有任何信息出现
现在我们来主动关闭nginx服务,然后再来运行该脚本,查看会出现什么信息
# /etc/init.d/nginx stop Stoping nginx... done # ./port.sh 80 is dead 443 is dead
由此得知,该脚本能正常监控服务器端口情况。日常工作中,经常将上述脚本和定时任务以及告警程序一起使用。将此脚本发到定人任务去,没分钟执行一次,当发现指定端口没有被监听,则触发告警程序。
The above is the detailed content of Monitoring ports in shell programming practice (port 80, port 443, etc.). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Solution: 1. Check the eMule settings to make sure you have entered the correct server address and port number; 2. Check the network connection, make sure the computer is connected to the Internet, and reset the router; 3. Check whether the server is online. If your settings are If there is no problem with the network connection, you need to check whether the server is online; 4. Update the eMule version, visit the eMule official website, and download the latest version of the eMule software; 5. Seek help.

What should I do if the RPC server is unavailable and cannot be accessed on the desktop? In recent years, computers and the Internet have penetrated into every corner of our lives. As a technology for centralized computing and resource sharing, Remote Procedure Call (RPC) plays a vital role in network communication. However, sometimes we may encounter a situation where the RPC server is unavailable, resulting in the inability to enter the desktop. This article will describe some of the possible causes of this problem and provide solutions. First, we need to understand why the RPC server is unavailable. RPC server is a

As a LINUX user, we often need to install various software and servers on CentOS. This article will introduce in detail how to install fuse and set up a server on CentOS to help you complete the related operations smoothly. CentOS installation fuseFuse is a user space file system framework that allows unprivileged users to access and operate the file system through a customized file system. Installing fuse on CentOS is very simple, just follow the following steps: 1. Open the terminal and Log in as root user. 2. Use the following command to install the fuse package: ```yuminstallfuse3. Confirm the prompts during the installation process and enter `y` to continue. 4. Installation completed

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

Methods to view server status include command line tools, graphical interface tools, monitoring tools, log files, and remote management tools. Detailed introduction: 1. Use command line tools. On Linux or Unix servers, you can use command line tools to view the status of the server; 2. Use graphical interface tools. For server operating systems with graphical interfaces, you can use the graphics provided by the system. Use interface tools to view server status; 3. Use monitoring tools. You can use special monitoring tools to monitor server status in real time, etc.

The steps to start the TFTP server include selecting the TFTP server software, downloading and installing the software, configuring the TFTP server, and starting and testing the server. Detailed introduction: 1. When choosing TFTP server software, you first need to choose the TFTP server software that suits your needs. Currently, there are many TFTP server software to choose from, such as Tftpd32, PumpKIN, tftp-hpa, etc., which all provide simple and easy-to-use functions. interface and configuration options; 2. Download and install TFTP server software, etc.

What should I do if I can’t enter the game when the epic server is offline? This problem must have been encountered by many friends. When this prompt appears, the genuine game cannot be started. This problem is usually caused by interference from the network and security software. So how should it be solved? The editor of this issue will explain I would like to share the solution with you, I hope today’s software tutorial can help you solve the problem. What to do if the epic server cannot enter the game when it is offline: 1. It may be interfered by security software. Close the game platform and security software and then restart. 2. The second is that the network fluctuates too much. Try restarting the router to see if it works. If the conditions are OK, you can try to use the 5g mobile network to operate. 3. Then there may be more
