Log the Total Number of Connections to a Port From an IP 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


netstat -ntlp|grep :80|wc -l

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.


echo "Time"
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.

Fri Jul 18 01:11:02 MSD 2014
Total no: of connection in port 80

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


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

Sample output

Originally posted 2016-02-17 22:39:39.


Please enter your comment!
Please enter your name here