In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to monitor the port of shell programming practice, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
General enterprise servers will run a variety of services, such as nginx, php, mysql, redis, MongoDB and so on. In general, the operation of the system may require the cooperation of multiple services, for example, our system needs php, mysql, redis, apache, MongoDB services. These services are indispensable.
Therefore, we need to monitor these services in real time, and if we find any service anomalies, we need to warn them immediately. Here we are not going to judge the status of the service by the process name. We intend to determine the running status of the service by listening on the port.
There is one command on the linux server that can be used to view the port status: netstat. However, on centOS7, you need to install the net-tools tool before you have this command. Now that the toolkit is installed, let's use the netstat command to see what information it displays
# netstat-tlnpActive Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0127.0.0.1 only servers 6379 0.0.0.0 LISTEN 11213/redis-server tcp 0 0 0.0.0.015 80 0.0.0.0 * LISTEN 1556/nginx: master tcp 0 0 0 LISTEN 984/sshd tcp 22 0 0 0 * LISTEN 984/sshd tcp 0 0 127 0 0 0 1 V 88 0 0 0 * LISTEN 17446/httpd tcp 0 0 0 .0.0LISTEN 25859/mongod tcp 443 0.0.0.0LISTEN 25859/mongod tcp * LISTEN 1556/nginx: LISTEN 25859/mongod tcp 0 0127.0.0.1 LISTEN 25859/mongod tcp 27017 0.0.0.0 LISTEN 25859/mongod tcp 3306 0.0.0.0 LISTEN 7138/mysqld
The information shown above, whether it is redis on port 6379 or mysql on 3306, are all necessary services for running our system. Then we filter out the first and second lines through the grep command
# netstat-tlnp | grep tcptcp 0 0127.0.0.1 grep tcptcp 6379 0.0.0.0 LISTEN 11213/redis-server tcp 0 0 0.0.0 0 grep tcptcp 80 0.0.0 0 LISTEN 1556/nginx: master tcp 0 0 0.0 0 22 0.0.0.0LISTEN * LISTEN 984/sshd tcp 0 0127.0.0.1 LISTEN 88 0.0.0.0 LISTEN 1556 / Nginx: master tcp 0 0127.0.0.1 LISTEN 25859/mongod tcp 27017 0.0.0.0 LISTEN 25859/mongod tcp 0 00.0.0.0 LISTEN 25859/mongod tcp 3306 0.0.0.0
Then get the information about the fourth column through the awk command.
# netstat-tlnp | grep tcp | awk'{print $4} '127.0.0.1VOV 63790.0.0.0VOV 800.0.0.0For 22127.0.0.1UR 880.0.0.0For 443127.0.0.1For 270170.0.0.03306
Finally, the port number is obtained through the cut command.
# netstat-tlnp | grep tcp | awk'{print $4}'| cut-d:-f26379802288443270173306
Through the above operation, we can get the current situation of the server port, then we store the ports that need to run the service in an array, and then traverse the array and compare the information we get.
Ports= "80 88 443 3306 6379 27017" for port in $portsdo echo $portdone
After traversing these ports that need to be listened on, let's compare the port with the information obtained by netstat. We use grep commands to make judgments here. Here's the complete code:
#! / bin/bash# Monitoring Server Port condition 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 $portsdo flag=$ (echo $netstat_info | grep $port) if [- z "$flag"]; then echo "$port is dead" fidone
It is easy to script port monitoring. Let's test whether the script can properly monitor the port. When all services are normal, executing the script does not output any information. If the nginx service is turned off, a message should appear.
First, execute the script when all services are normal
#. / port.sh # # No information will appear
Now let's take the initiative to shut down the nginx service, and then run the script to see what happens.
# / etc/init.d/nginx stopStoping nginx... Done#. / port.sh 80 is dead443 is dead
From this, it is known that the script can properly monitor the server port. In daily work, the above scripts are often used together with scheduled tasks and alarm programs. Send this script to the appointment task and execute it every minute. When it is found that the specified port is not being monitored, the alarm program will be triggered.
The above is all the contents of how to monitor the port in shell programming practice. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.