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.
S G Sumanth 📧 sumanthusha01@gmail.com 🔗 LinkedIn
- Features
- Project Structure
- Prerequisites
- Installation
- Usage
- Configuration
- Examples
- Modules
- Logging
- Contributing
- License
✅ 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
✅ 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
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
- Linux/Unix operating system (Ubuntu, Debian, CentOS, RHEL, etc.)
- Bash 4.0 or higher
- Required commands:
tar- For backup and compressiongzip- For compressiondf- Disk usagedu- Directory usagesystemctl- Service managementbc- Mathematical calculations (optional)
Most of these tools are pre-installed on standard Linux distributions.
- Clone or download the repository:
cd ~/Documents
git clone <repository-url> shell-automation-toolkit
cd shell-automation-toolkit- Make scripts executable:
chmod +x automation-toolkit.sh health-monitor.sh
chmod +x modules/*.sh- Verify installation:
./automation-toolkit.sh --help
./health-monitor.sh --helpBasic 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
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
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=300Add services to monitor (one per line):
ssh
cron
nginx
mysql
docker
./automation-toolkit.sh --backup /home/user/documents# Remove files older than 30 days
./automation-toolkit.sh --cleanup /tmp --verbose./automation-toolkit.sh --compress /var/log/old_logs./automation-toolkit.sh --health./automation-toolkit.sh --all-health./automation-toolkit.sh --list-backups./automation-toolkit.sh --restore backups/documents_20240115_143022.tar.gz./automation-toolkit.sh --disk-usage /home./health-monitor.sh --check./health-monitor.sh --service nginx --service mysql --check./health-monitor.sh --monitor --auto-heal --verbose./health-monitor.sh --list-alerts./health-monitor.sh --list-recoveryProvides 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 messageslog_success()- Success messageslog_warning()- Warning messageslog_error()- Error messagescheck_exit_code()- Validate command exit codes
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 backuprestore_backup()- Restore from backupcleanup_old_files()- Remove old filescompress_directory()- Compress directorieslist_backups()- List available backupscheck_disk_usage()- Display disk usage
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 percentageget_memory_usage()- Get memory usage percentageget_disk_usage()- Get disk usage percentagegenerate_health_report()- Generate comprehensive reportcomprehensive_health_check()- Full system check with alerts
All operations are logged with timestamps for audit and troubleshooting:
-
logs/automation-toolkit.log- All automation toolkit operations
- Backup/restore activities
- File management operations
-
logs/health-monitor.log- Health check operations
- Service status checks
- Resource monitoring
-
logs/health_alerts.log- System alerts
- Threshold violations
- Service failures
-
logs/recovery.log- Auto-healing attempts
- Service restart operations
- Recovery success/failure
[2024-01-15 14:30:22] [INFO] Starting backup of /home/user/documents
[2024-01-15 14:30:25] [SUCCESS] Backup created successfully
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 --healthThe 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
Modify the send_alert() function in health-monitor.sh to add:
Email notifications:
echo "$message" | mail -s "System Alert" admin@example.comSlack notifications:
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$message\"}" \
YOUR_SLACK_WEBHOOK_URLIf you encounter permission errors:
chmod +x automation-toolkit.sh health-monitor.sh
chmod +x modules/*.shSome service checks require root privileges:
sudo ./health-monitor.sh --checkCheck for missing commands:
which tar gzip df du systemctl- Test backups regularly - Verify backups can be restored
- Monitor log files - Check logs for errors and warnings
- Set appropriate thresholds - Adjust alert thresholds based on your system
- Use verbose mode - Enable
-vfor detailed output during troubleshooting - Schedule regular cleanups - Prevent log and backup directories from growing too large
- Secure sensitive logs - Restrict access to log files containing system information
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
- 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
This project is open-source and available for educational and professional use.
S G Sumanth 📧 sumanthusha01@gmail.com 🔗 LinkedIn