Petru Porumbescu

0 %
Petru Porumbescu
AWS Certified Developer & Solutions Architect | Python | Git & GitHub | Terraform | Docker
  • Residence:
    United Kingdom
  • Age:
    35
Licenses & Certifications:
  • AWS Certified Developer
  • AWS Certified Solutions Architect
  • HashiCorp Terraform Associate
  • Python for Beginners
  • Pythonic Programming
  • Developing on AWS
  • Python-based Microservices
English
Romanian
Skills:
  • Amazon Web Services (AWS)
  • Cloud Computing
  • Python Programming
  • Docker
  • Terraform
  • Git and Github
  • Linux, Windows, MacOS
  • WordPress
  • Microsoft Office Suite
  • Microsoft 365
  • Microsoft Teams
  • Slack
  • Leadership
  • Communication
  • Troubleshooting
  • Teamwork & Collaboration

HTTP Request Monitoring with Bash Script

April 30, 2024

Streamline Your HTTP Request Monitoring with This Simple Bash Script

In the world of web application management, staying informed about the traffic your applications handle is crucial. Whether you’re troubleshooting, performing routine checks, or simply curious about the activity your servers are handling, having a quick and efficient way to monitor HTTP requests can save you a significant amount of time. Today, I’m excited to share a Bash script that does exactly that.

Understanding the Script

This Bash script is designed to read through a list of applications from a file, count the number of HTTP `GET`, `POST`, and `DELETE` requests from their respective log files, and display the counts in a neatly formatted table. Here’s how it works:

1. Setup and Initial Display:
The script begins by setting up the environment and printing a header for the table, which includes columns for the application name and the counts of different types of HTTP requests.

2. Processing Each Application:
It reads each application name from a file named `apps.txt`, located in `/home/bob`. For each application, it counts how many `GET`, `POST`, and `DELETE` requests are logged in its corresponding log file in the `/var/log/apps` directory.

3. Output:
The results are displayed in a tabular format, making it easy to see the activity for each application at a glance.

The Script

#!/bin/bash

echo -e " Log name   \t      GET      \t      POST    \t   DELETE "
echo -e "------------------------------------------------------------"

# Loop through each application listed in apps.txt
for app in $(cat /home/bob/apps.txt)
do
  # Count the types of HTTP requests in each app's log file
  get_requests=$(grep "GET" /var/log/apps/${app}_app.log | wc -l)
  post_requests=$(grep "POST" /var/log/apps/${app}_app.log | wc -l)
  delete_requests=$(grep "DELETE" /var/log/apps/${app}_app.log | wc -l)

  # Output the counts in a formatted table
  echo -e " ${app}    \t ${get_requests}    \t    ${post_requests}   \t   ${delete_requests}"
done

Why This Script Matters

For system administrators and developers managing multiple web applications, this script is more than just a convenience—it’s a tool that can significantly streamline the monitoring process. By automating the counting of different types of HTTP requests, you can quickly identify trends, spot anomalies, and better understand the load on your servers.

Customizing the Script

While the script is ready to use as-is, you can easily customise it to fit your specific needs. Whether you need to monitor additional types of requests, handle different log file formats, or integrate with other monitoring tools, the flexibility of Bash scripting makes these adjustments straightforward.

Final Thoughts

This simple Bash script exemplifies how powerful shell scripting can be for routine system administration tasks. By automating the monitoring of HTTP requests, you can free up valuable time to focus on more complex problems. I hope this tool proves as useful in your toolkit as it has in mine.

Posted in BASH, Linux
Write a comment