Skip to content

ThunderShadows/Shell-Scripting-Automation-and-Health-Monitoring

Repository files navigation

Shell Scripting Automation & Health Monitoring Toolkit

A modular Bash Script toolkit designed to automate core tasks in Linux/Unix environments, streamline DevOps workflows, and enhance system efficiency through comprehensive health monitoring and automated management.

Author

S G Sumanth 📧 sumanthusha01@gmail.com 🔗 LinkedIn


Table of Contents


Features

Automation Toolkit

File Management

  • Automated backup with compression
  • Cleanup old files based on age
  • Directory/file compression
  • Backup restoration
  • Disk usage analysis

System Health Reporting

  • Real-time CPU, memory, and disk metrics
  • System uptime and load average
  • Top resource-consuming processes
  • Network interface status
  • Comprehensive health reports with timestamps

Interactive CLI

  • Command-line flags for all operations
  • Built-in help menu
  • Verbose output mode
  • User-friendly interface

Robust Error Handling

  • Exit codes for all operations
  • Dependency checking
  • User-friendly logging with timestamps
  • Color-coded console output

Health Check & Alert System

Service Monitoring

  • Monitor critical services (ssh, cron, nginx, mysql, etc.)
  • Systemctl and process status checks
  • Configurable service lists

Alert System

  • Real-time anomaly detection
  • Resource usage alerts (CPU, memory, disk)
  • Alert logging with timestamps
  • Extensible for email/SMS/Slack notifications

Self-Healing Mechanism

  • Optional auto-restart of failed services
  • Recovery attempt logging
  • Verification of successful restarts
  • Manual intervention alerts when auto-healing fails

Continuous Monitoring

  • Configurable monitoring intervals
  • Persistent monitoring mode
  • Comprehensive status reports

Project Structure

shell-automation-toolkit/
├── automation-toolkit.sh          # Main automation toolkit script
├── health-monitor.sh              # Health check and alert system
├── modules/
│   ├── logger.sh                  # Logging utilities
│   ├── file_manager.sh            # File management functions
│   └── system_health.sh           # System health monitoring
├── config/
│   ├── config.conf                # Main configuration file
│   └── services.conf              # Services to monitor
├── logs/                          # Log files directory
│   ├── automation-toolkit.log
│   ├── health-monitor.log
│   ├── health_alerts.log
│   └── recovery.log
├── backups/                       # Backup storage directory
└── README.md                      # This file

Prerequisites

  • Linux/Unix operating system (Ubuntu, Debian, CentOS, RHEL, etc.)
  • Bash 4.0 or higher
  • Required commands:
    • tar - For backup and compression
    • gzip - For compression
    • df - Disk usage
    • du - Directory usage
    • systemctl - Service management
    • bc - Mathematical calculations (optional)

Most of these tools are pre-installed on standard Linux distributions.


Installation

  1. Clone or download the repository:
cd ~/Documents
git clone <repository-url> shell-automation-toolkit
cd shell-automation-toolkit
  1. Make scripts executable:
chmod +x automation-toolkit.sh health-monitor.sh
chmod +x modules/*.sh
  1. Verify installation:
./automation-toolkit.sh --help
./health-monitor.sh --help

Usage

Automation Toolkit

Basic Usage:

./automation-toolkit.sh [OPTIONS]

Available Options:

-h, --help              Display help menu
-b, --backup <path>     Backup specified directory or file
-c, --cleanup <path>    Clean up old files in specified directory
-z, --compress <path>   Compress specified directory or file
-s, --health            Generate system health report
-a, --all-health        Comprehensive system health check with alerts
-r, --restore <backup>  Restore from backup file
-l, --list-backups      List all available backups
-d, --disk-usage <path> Show disk usage for specified path
-v, --verbose           Enable verbose output

Health Monitor

Basic Usage:

./health-monitor.sh [OPTIONS]

Available Options:

-h, --help              Display help menu
-s, --service <name>    Add a service to monitor
-c, --check             Run health check once
-m, --monitor           Run continuous monitoring (every 5 minutes)
-a, --auto-heal         Enable self-healing (auto-restart failed services)
-l, --list-alerts       Show recent alerts
-r, --list-recovery     Show recovery log
-v, --verbose           Enable verbose output

Configuration

Main Configuration (config/config.conf)

Edit the configuration file to customize settings:

# System health thresholds
CPU_THRESHOLD=80
MEMORY_THRESHOLD=85
DISK_THRESHOLD=90

# Self-healing
SELF_HEALING=false

# Monitoring interval (seconds)
MONITORING_INTERVAL=300

Services Configuration (config/services.conf)

Add services to monitor (one per line):

ssh
cron
nginx
mysql
docker

Examples

1. Backup a Directory

./automation-toolkit.sh --backup /home/user/documents

2. Clean Up Old Files

# Remove files older than 30 days
./automation-toolkit.sh --cleanup /tmp --verbose

3. Compress a Directory

./automation-toolkit.sh --compress /var/log/old_logs

4. Generate System Health Report

./automation-toolkit.sh --health

5. Comprehensive Health Check with Alerts

./automation-toolkit.sh --all-health

6. List Available Backups

./automation-toolkit.sh --list-backups

7. Restore from Backup

./automation-toolkit.sh --restore backups/documents_20240115_143022.tar.gz

8. Check Disk Usage

./automation-toolkit.sh --disk-usage /home

9. Run Single Health Check

./health-monitor.sh --check

10. Monitor Specific Services

./health-monitor.sh --service nginx --service mysql --check

11. Continuous Monitoring with Auto-Healing

./health-monitor.sh --monitor --auto-heal --verbose

12. View Recent Alerts

./health-monitor.sh --list-alerts

13. View Recovery Log

./health-monitor.sh --list-recovery

Modules

1. Logger Module (modules/logger.sh)

Provides comprehensive logging functionality:

  • Color-coded output for different log levels
  • Timestamp logging for all operations
  • Multiple log levels: INFO, SUCCESS, WARNING, ERROR
  • File and console logging
  • Exit code checking and validation

Functions:

  • log_info() - Informational messages
  • log_success() - Success messages
  • log_warning() - Warning messages
  • log_error() - Error messages
  • check_exit_code() - Validate command exit codes

2. File Manager Module (modules/file_manager.sh)

Handles all file operations:

  • Automated backups with compression
  • File cleanup based on age
  • Directory compression
  • Backup restoration
  • Disk usage analysis
  • Backup listing

Functions:

  • create_backup() - Create compressed backup
  • restore_backup() - Restore from backup
  • cleanup_old_files() - Remove old files
  • compress_directory() - Compress directories
  • list_backups() - List available backups
  • check_disk_usage() - Display disk usage

3. System Health Module (modules/system_health.sh)

Monitors system resources and generates reports:

  • CPU usage monitoring
  • Memory usage tracking
  • Disk space monitoring
  • System uptime and load
  • Process analysis
  • Service status checking

Functions:

  • get_cpu_usage() - Get CPU usage percentage
  • get_memory_usage() - Get memory usage percentage
  • get_disk_usage() - Get disk usage percentage
  • generate_health_report() - Generate comprehensive report
  • comprehensive_health_check() - Full system check with alerts

Logging

All operations are logged with timestamps for audit and troubleshooting:

Log Files

  1. logs/automation-toolkit.log

    • All automation toolkit operations
    • Backup/restore activities
    • File management operations
  2. logs/health-monitor.log

    • Health check operations
    • Service status checks
    • Resource monitoring
  3. logs/health_alerts.log

    • System alerts
    • Threshold violations
    • Service failures
  4. logs/recovery.log

    • Auto-healing attempts
    • Service restart operations
    • Recovery success/failure

Log Format

[2024-01-15 14:30:22] [INFO] Starting backup of /home/user/documents
[2024-01-15 14:30:25] [SUCCESS] Backup created successfully

Advanced Usage

Scheduled Automation with Cron

Add to crontab for automated execution:

# Daily backup at 2 AM
0 2 * * * /path/to/automation-toolkit.sh --backup /home/user/data

# Health check every 5 minutes
*/5 * * * * /path/to/health-monitor.sh --check --auto-heal

# Weekly cleanup of temp files
0 3 * * 0 /path/to/automation-toolkit.sh --cleanup /tmp

# Daily health report
0 8 * * * /path/to/automation-toolkit.sh --health

Integration with Monitoring Systems

The toolkit can be integrated with monitoring systems like:

  • Nagios/Icinga - Use exit codes for status checks
  • Prometheus - Export metrics from health reports
  • Grafana - Visualize log data
  • Email alerts - Extend alert functions to send emails

Custom Alert Notifications

Modify the send_alert() function in health-monitor.sh to add:

Email notifications:

echo "$message" | mail -s "System Alert" admin@example.com

Slack notifications:

curl -X POST -H 'Content-type: application/json' \
  --data "{\"text\":\"$message\"}" \
  YOUR_SLACK_WEBHOOK_URL

Troubleshooting

Permission Issues

If you encounter permission errors:

chmod +x automation-toolkit.sh health-monitor.sh
chmod +x modules/*.sh

Service Monitoring Requires Root

Some service checks require root privileges:

sudo ./health-monitor.sh --check

Missing Dependencies

Check for missing commands:

which tar gzip df du systemctl

Best Practices

  1. Test backups regularly - Verify backups can be restored
  2. Monitor log files - Check logs for errors and warnings
  3. Set appropriate thresholds - Adjust alert thresholds based on your system
  4. Use verbose mode - Enable -v for detailed output during troubleshooting
  5. Schedule regular cleanups - Prevent log and backup directories from growing too large
  6. Secure sensitive logs - Restrict access to log files containing system information

Technical Highlights

This project demonstrates:

Modular Architecture - Reusable, maintainable code structure ✅ Error Handling - Comprehensive error checking and logging ✅ Best Practices - Following shell scripting standards ✅ DevOps Integration - Ready for CI/CD pipelines ✅ Production Ready - Tested and robust implementations ✅ Documentation - Clear, comprehensive documentation ✅ User Experience - Intuitive CLI with helpful messages


Future Enhancements

  • Email/SMS/Slack alert integration
  • Web dashboard for monitoring
  • Database backup support (MySQL, PostgreSQL)
  • Docker container health monitoring
  • Kubernetes cluster monitoring
  • Performance metrics collection
  • Integration with cloud storage (AWS S3, Google Cloud)
  • Automated backup rotation policies

License

This project is open-source and available for educational and professional use.


Contact

S G Sumanth 📧 sumanthusha01@gmail.com 🔗 LinkedIn

About

Modular Bash toolkit automating DevOps tasks (backup, cleanup, compression) with cron job integration, reducing manual operational overhead significantly

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages