Posted on
Scripting for DevOps

Using Bash to Monitor and Scale Applications

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Monitoring and scaling applications using Bash can be a powerful way to automate management tasks for your applications and infrastructure. Here’s a guide on how to use Bash for these purposes:


1. Monitoring Applications

Monitoring involves tracking system performance, application logs, and metrics. Here's how you can use Bash scripts for monitoring:

System Metrics

You can monitor CPU, memory, disk usage, and network statistics using tools like top, htop, vmstat, iostat, and netstat.

Example Script: Monitor CPU and Memory Usage

#!/bin/bash

while true; do
  echo "CPU and Memory Usage at $(date):"
  top -b -n 1 | head -n 10
  sleep 60 # Check every minute
done

Application Logs

Monitor log files for errors or specific events using tail or grep.

Example Script: Check Logs for Errors

#!/bin/bash

LOGFILE="/var/log/app.log"
ERROR_KEYWORD="ERROR"

echo "Monitoring log file for errors: $LOGFILE"
tail -F "$LOGFILE" | grep --line-buffered "$ERROR_KEYWORD"

Health Checks

Ping application endpoints or services using curl or ping.

Example Script: Check Application Health

#!/bin/bash

APP_URL="http://localhost:8080/health"

while true; do
  STATUS=$(curl -s -o /dev/null -w '%{http_code}' "$APP_URL")
  if [ "$STATUS" -ne 200 ]; then
    echo "Application is down! HTTP status: $STATUS"
  else
    echo "Application is healthy. HTTP status: $STATUS"
  fi
  sleep 30
done

2. Scaling Applications

Scaling applications involves increasing or decreasing resources or instances based on load. Use Bash scripts to interact with scaling tools like Docker, Kubernetes, or cloud provider APIs.

Scaling with Docker

You can use docker-compose to scale services.

Example Script: Scale Docker Service

#!/bin/bash

SERVICE_NAME="web"
SCALE_COUNT=3

echo "Scaling service $SERVICE_NAME to $SCALE_COUNT instances..."
docker-compose up --scale "$SERVICE_NAME=$SCALE_COUNT" -d

Scaling with Kubernetes

You can use kubectl to scale deployments.

Example Script: Scale Kubernetes Deployment

#!/bin/bash

DEPLOYMENT_NAME="my-app"
NAMESPACE="default"
REPLICAS=5

echo "Scaling deployment $DEPLOYMENT_NAME to $REPLICAS replicas in namespace $NAMESPACE..."
kubectl scale deployment "$DEPLOYMENT_NAME" --replicas="$REPLICAS" -n "$NAMESPACE"

Cloud Provider Autoscaling

Use cloud provider CLI tools like aws, gcloud, or az to manage autoscaling groups.

Example Script: Adjust AWS Auto Scaling Group

#!/bin/bash

ASG_NAME="my-auto-scaling-group"
MIN_SIZE=2
MAX_SIZE=10

echo "Updating Auto Scaling Group: $ASG_NAME"
aws autoscaling update-auto-scaling-group --auto-scaling-group-name "$ASG_NAME" \
  --min-size "$MIN_SIZE" --max-size "$MAX_SIZE"

3. Automating Alerts

Integrate monitoring scripts with notification systems like email, Slack, or PagerDuty.

Example Script: Send Email Alert

#!/bin/bash

TO_EMAIL="admin@example.com"
SUBJECT="Application Down Alert"
BODY="The application is down as of $(date). Please check immediately."

echo "$BODY" | mail -s "$SUBJECT" "$TO_EMAIL"

Best Practices

  1. Use Cron Jobs: Schedule scripts to run at specific intervals using cron.
  2. Logging: Log script outputs for auditing and debugging.
  3. Error Handling: Add error checks and retries to scripts.
  4. Integration with Monitoring Tools: Use tools like Prometheus, Grafana, or Datadog for advanced monitoring and visualization.
  5. Secure Credentials: Store API keys or sensitive data securely using environment variables or secrets management tools.