Keep a log for total number of connections to a particular port from Foreign Address

Is there any log entries to find-out directly the total number of connections in server?

In some high connection high load servers, this log would be helpful to monitor and tune the server with number of connections on it. We can simply sort out the total number of connections in a port by using the command netstat. There isn’t any log entries with total number of connections. But, we will get the history of resource usage information by installing sar (Systat) on the server. Then, we can create a cronjob to monitor the server connections. In this post I am explaining the method to create a log for total number of connection to server. Before creating a script and setting cron, you must have the idea to use the command “netstat” to list total number of connections in server.

By considering the service Apache, we can sort it by using the port 80.

netstat -ntlp|grep :80|wc -l

Example:

netstat -ntlp|grep :80|wc -l
3385

Refer this for more about the command “netstat”, >> netstat command’s practical usages with example <<

If you want to monitor the total connection to your Apache service at times, create a cronjob to save this to a file as a log. Here I am using the command “date” to get the time details when the “netstat” taking the connection log. Please do follow these steps to create a log with connection details.

Step 1 : Create a file to get the log.

touch connection.txt

Step 2 : Create a script for the same.

2.1 –> Use the command ‘date’ for time details.
2.2 –> Use ‘echo’ to print your instructions.
2.3 –> Use ‘netstat’ for connection details.

Simply;

echo "Time"
date
echo "Total no: of connection in port 80"
netstat -ntlp|grep :80|wc -l
echo ""

Step 3 : Change the file permission as executable.

chmod 755 connections.log

Step 4 : Test the script from the location.

./connection.txt
Time
Fri Jul 18 01:11:02 MSD 2014
Total no: of connection in port 80
1

Step 5 : Create a file to log the connection information.

touch connections.log

Step 6 : Create a cronjob to execute this periodically.

crontab -e

*/30 * * * * /root/connection.txt >> connections.log

DONE!!

This will save the total number of connections to the file connections.log.

Sample output
log

🙂 🙂

Related posts:
How to find the total number of connections for a cPanel account from the log files.

Load & Memory monitoring using SAR

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

One thought on “Keep a log for total number of connections to a particular port from Foreign Address

  1. You can also log the number of connections from a specific IP address, which could be a very useful information.

    Command:

    netstat -plane | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n

    Example:

    1 1.2.3.3
    12 111.222.333.444
    26 222.333.444.555

    The first row represents the number of connections and the second one is the IP address.

Leave a Reply

Your email address will not be published. Required fields are marked *