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

Arunlal Ashok

Linux Server Administrator. I'm dealing with Linux servers since 2012. I started this blog to share and discuss my ideas with the world. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

1 Response

  1. RoseHosting says:

    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 *