monitoring

All posts tagged monitoring by Linux Bash
  • Posted on

    Monitoring disk usage is essential for maintaining system health and ensuring adequate storage space. Here’s how you can monitor disk usage using various Bash commands:


    1. Check Disk Space Usage

    • Command: df
    • Usage:

      • View disk usage for all mounted filesystems:
      df -h
      
      • -h: Displays output in human-readable format (e.g., GB, MB).

      • Filter for a specific filesystem or directory:

      df -h /path/to/directory
      

    2. Analyze Directory Sizes

    • Command: du
    • Usage:
      • Display the size of a directory and its subdirectories: bash du -h /path/to/directory
      • Show only the total size of a directory: bash du -sh /path/to/directory
      • -s: Summarize the total size.
      • -h: Human-readable format.

    3. Monitor Disk Space in Real-Time

    • Command: watch
    • Usage:
      • Use watch to run df repeatedly at intervals: bash watch -n 5 df -h
      • -n 5: Refresh every 5 seconds.

    4. Find Large Files

    • Command: find
    • Usage:
      • Search for files larger than 1 GB: bash find /path/to/search -type f -size +1G
      • List large files in human-readable format: bash find /path/to/search -type f -size +1G -exec ls -lh {} \;

    5. Monitor Inode Usage

    • Command: df (with the -i option)
    • Usage:
      • Check inode usage to ensure you’re not running out: bash df -i

    6. Automate Monitoring with a Script

    • Example Bash Script:

      #!/bin/bash
      
      # Define thresholds
      THRESHOLD=80
      ALERT_EMAIL="admin@example.com"
      
      # Monitor disk space
      df -h | awk 'NR>1 {if ($5+0 > '"$THRESHOLD"') print $0}' > /tmp/disk_alert.txt
      
      # Send email alert if thresholds are exceeded
      if [ -s /tmp/disk_alert.txt ]; then
      mail -s "Disk Space Alert" "$ALERT_EMAIL" < /tmp/disk_alert.txt
      fi
      

    7. System-Wide Disk Monitoring Tools

    Consider using tools like ncdu or iotop for more advanced monitoring: - ncdu: A fast, interactive disk usage analyzer. bash sudo apt install ncdu # For Debian-based systems ncdu /path/to/analyze - iotop: Monitor real-time disk I/O usage.


    These commands and tools provide both basic and advanced methods for monitoring disk usage, helping you maintain system performance and prevent storage issues.

  • Posted on

    Using Bash for Linux system monitoring and automation allows administrators to efficiently manage systems, optimize resources, and automate repetitive tasks. Here's an overview of how Bash can be leveraged for these purposes:


    System Monitoring with Bash

    Bash scripts can gather and display system performance data in real-time or at scheduled intervals.

    1. Monitor CPU and Memory Usage

    Use commands like top, htop, or free within Bash scripts to capture resource utilization.

    Example Script:

    #!/bin/bash
    
    echo "CPU and Memory Usage:"
    echo "----------------------"
    top -b -n1 | head -n 10
    free -h
    

    2. Disk Usage Monitoring

    The df and du commands can be used to check disk space and usage.

    Example Script:

    #!/bin/bash
    
    echo "Disk Space Usage:"
    echo "-----------------"
    df -h
    
    echo "Largest Files/Directories:"
    echo "--------------------------"
    du -ah / | sort -rh | head -n 10
    

    3. Network Monitoring

    Monitor active connections and network usage using tools like netstat, ss, or ping.

    Example Script:

    #!/bin/bash
    
    echo "Active Network Connections:"
    echo "---------------------------"
    netstat -tuln
    
    echo "Network Latency to Google:"
    echo "--------------------------"
    ping -c 4 google.com
    

    4. Log Monitoring

    Use tail, grep, or awk to analyze log files.

    Example Script:

    #!/bin/bash
    
    log_file="/var/log/syslog"
    echo "Last 10 Log Entries:"
    echo "---------------------"
    tail -n 10 $log_file
    
    echo "Error Logs:"
    echo "-----------"
    grep "error" $log_file | tail -n 10
    

    Automation with Bash

    Bash scripts are ideal for automating repetitive tasks, improving efficiency, and reducing manual errors.

    1. Automated Backups

    Use rsync or tar to automate file backups.

    Example Script:

    #!/bin/bash
    
    src_dir="/home/user/documents"
    backup_dir="/backup/documents"
    timestamp=$(date +%F_%T)
    
    mkdir -p $backup_dir
    tar -czf "$backup_dir/backup_$timestamp.tar.gz" $src_dir
    
    echo "Backup completed: $backup_dir/backup_$timestamp.tar.gz"
    

    2. Task Scheduling

    Combine Bash scripts with cron to execute tasks at specified intervals.

    Schedule a Script: 1. Edit the crontab file: crontab -e 2. Add a line to schedule the script: bash 0 2 * * * /path/to/backup_script.sh

    3. Software Updates

    Automate system updates using package managers.

    Example Script:

    #!/bin/bash
    
    echo "Updating system packages..."
    sudo apt-get update -y && sudo apt-get upgrade -y
    echo "System update completed."
    

    4. Service Monitoring and Restart

    Check and restart services automatically if they fail.

    Example Script:

    #!/bin/bash
    
    service_name="apache2"
    
    if ! systemctl is-active --quiet $service_name; then
        echo "$service_name is not running. Restarting..."
        systemctl restart $service_name
        echo "$service_name restarted."
    else
        echo "$service_name is running."
    fi
    

    5. Automated Alerts

    Send email notifications or logs when specific events occur.

    Example Script:

    #!/bin/bash
    
    log_file="/var/log/syslog"
    alert_email="admin@example.com"
    
    if grep -q "error" $log_file; then
        echo "Errors detected in $log_file" | mail -s "System Alert" $alert_email
    fi
    

    Combining Monitoring and Automation

    Advanced scripts can combine monitoring and automation, such as detecting high CPU usage and killing processes.

    Example Script:

    #!/bin/bash
    
    threshold=80
    process=$(ps aux --sort=-%cpu | awk 'NR==2 {print $2, $3}')
    
    cpu_usage=$(echo $process | awk '{print $2}')
    pid=$(echo $process | awk '{print $1}')
    
    if (( $(echo "$cpu_usage > $threshold" | bc -l) )); then
        echo "High CPU usage detected: $cpu_usage%"
        echo "Terminating process with PID $pid..."
        kill -9 $pid
        echo "Process terminated."
    fi
    

    Best Practices for Bash Monitoring and Automation

    1. Modular Scripts: Break tasks into reusable functions.
    2. Error Handling: Use checks (if conditions) to handle errors gracefully.
    3. Logging: Record script outputs and errors to log files for auditing.
    4. Testing: Test scripts in a safe environment before deploying.
    5. Permissions: Ensure scripts have appropriate permissions (chmod) and use sudo responsibly.

    By combining these techniques, administrators can use Bash to monitor Linux systems proactively, automate essential tasks, and maintain operational efficiency.