Monitoring Website Traffic with a Simple Script

In the world of website management, keeping track of traffic is essential. Monitoring access logs can help you understand the volume of traffic and identify potential issues like DDoS attacks or server overloads. In this article, I will explain a simple yet effective script I wrote to monitor hourly traffic on multiple websites hosted on a server. This script can be particularly useful for anyone managing multiple sites and wanting a quick way to get an overview of traffic patterns.

Why I Wrote This Script

As a website administrator managing multiple domains, I needed a way to quickly assess the number of requests each site received in the last hour. This information is crucial for understanding traffic trends, identifying unusual spikes in traffic, and ensuring the server’s performance remains optimal. By running this script, I can get an immediate count of access requests, allowing me to take action if needed.

The Script Explained

Below is the script I use to monitor the traffic on my websites. For the sake of this article, I have replaced my actual domain paths with example domains to keep it generic and applicable to anyone managing multiple websites.

sudo grep "$(date +"%d/%b/%Y:%H" --date='1 hour ago')" /home/example1/logs/nginx/access.log | wc -l
sudo grep "$(date +"%d/%b/%Y:%H" --date='1 hour ago')" /home/example2/logs/nginx/access.log | wc -l
sudo grep "$(date +"%d/%b/%Y:%H" --date='1 hour ago')" /home/example3/logs/nginx/access.log | wc -l
sudo grep "$(date +"%d/%b/%Y:%H" --date='1 hour ago')" /home/example4/logs/nginx/access.log | wc -l

Breaking Down the Script

Let’s break down what each part of the script does:

  1. Date Command: $(date +"%d/%b/%Y:%H" --date='1 hour ago')
    • This command generates a timestamp for one hour ago, formatted as day/month/year:hour.
    • This format matches the timestamp in NGINX access logs.
  2. Grep Command: sudo grep "$(date +"%d/%b/%Y:%H" --date='1 hour ago')" /home/example1/logs/nginx/access.log
    • sudo grep searches the access log for entries that match the timestamp generated by the date command.
    • It reads the access log file for the specified domain (/home/example1/logs/nginx/access.log).
  3. Count Command: | wc -l
    • This part of the command pipes (|) the output of grep to wc -l.
    • wc -l counts the number of lines returned by grep, which corresponds to the number of access requests in the last hour.

Running the Script

To run this script, simply execute it in your terminal. You can also create a cron job to automate this process and get hourly reports without manual intervention.

Use Case and Benefits

This script is particularly useful for:

  • Website Administrators: Quickly assess the traffic to multiple websites.
  • Security Monitoring: Identify unusual spikes that might indicate a DDoS attack.
  • Performance Tuning: Understand traffic patterns and optimize server resources accordingly.

By monitoring your traffic hourly, you can proactively manage your server’s performance and security. This simple script provides immediate insights without the need for complex monitoring tools.


I hope this script helps you manage your websites more effectively. It has certainly made my job easier by providing quick, actionable insights into my server’s traffic. If you have any questions or suggestions, feel free to reach out. Does this script help you manage your website traffic? Let me know how it works for you!

Feel free to customize the script and article to better fit your specific needs and environments. Happy monitoring!

Leave a Reply

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