135 lines
2.6 KiB
Markdown
135 lines
2.6 KiB
Markdown
# Shell Scripting and Automation
|
|
|
|
## Overview
|
|
This document tracks the implementation of shell scripts and automation tasks for our Ubuntu Server VM.
|
|
|
|
## Shell Scripting Basics
|
|
|
|
### 1. First Script: System Health Check ✅
|
|
```bash
|
|
#!/bin/bash
|
|
# system-health.sh - Basic system health check script
|
|
|
|
# Create script
|
|
sudo nano /usr/local/bin/system-health.sh
|
|
```
|
|
|
|
Script contents:
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
# System Health Check Script
|
|
echo "=== System Health Check ==="
|
|
echo "Date: $(date)"
|
|
echo
|
|
|
|
# System uptime
|
|
echo "=== System Uptime ==="
|
|
uptime
|
|
echo
|
|
|
|
# Memory usage
|
|
echo "=== Memory Usage ==="
|
|
free -h
|
|
echo
|
|
|
|
# Disk usage
|
|
echo "=== Disk Usage ==="
|
|
df -h /
|
|
echo
|
|
|
|
# CPU load
|
|
echo "=== CPU Load ==="
|
|
mpstat 1 1
|
|
echo
|
|
|
|
# Active system services
|
|
echo "=== System Services ==="
|
|
systemctl list-units --type=service --state=running
|
|
echo
|
|
|
|
# Recent system logs
|
|
echo "=== Recent System Logs ==="
|
|
tail -n 5 /var/log/syslog
|
|
```
|
|
|
|
### Script Setup ✅
|
|
```bash
|
|
# Install required package
|
|
sudo apt install sysstat
|
|
|
|
# Make script executable
|
|
sudo chmod +x /usr/local/bin/system-health.sh
|
|
|
|
# Test script
|
|
sudo /usr/local/bin/system-health.sh
|
|
```
|
|
|
|
### Test Results ✅
|
|
- Script executed successfully
|
|
- All system metrics collected:
|
|
- System uptime (4 users, load average visible)
|
|
- Memory usage (1.9GB total, 370MB used)
|
|
- Disk usage (75% used)
|
|
- CPU load (97.11% idle)
|
|
- Active services (15 services running)
|
|
- Recent system logs
|
|
|
|
### Next Improvements
|
|
1. Add Error Handling
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
# Add error handling
|
|
set -e # Exit on error
|
|
exec 2> >(tee -a /var/log/system-health.error.log) # Log errors
|
|
|
|
# Function for error handling
|
|
check_command() {
|
|
if ! command -v $1 &> /dev/null; then
|
|
echo "Error: $1 is not installed" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Check required commands
|
|
check_command free
|
|
check_command df
|
|
check_command mpstat
|
|
check_command systemctl
|
|
```
|
|
|
|
2. Add Logging
|
|
```bash
|
|
# Add timestamp and logging
|
|
LOG_FILE="/var/log/system-health.log"
|
|
exec 1> >(tee -a $LOG_FILE) # Log stdout
|
|
```
|
|
|
|
Would you like to:
|
|
1. Implement these improvements
|
|
2. Schedule regular execution
|
|
3. Add more system checks
|
|
|
|
### Automation Plan
|
|
1. System Health Monitoring
|
|
- [ ] Create basic health check script
|
|
- [ ] Add error checking
|
|
- [ ] Implement logging
|
|
- [ ] Schedule regular execution
|
|
|
|
2. Maintenance Tasks
|
|
- [ ] Create cleanup script
|
|
- [ ] Automate log analysis
|
|
- [ ] Monitor service status
|
|
|
|
3. Reporting
|
|
- [ ] Generate system reports
|
|
- [ ] Email notifications
|
|
- [ ] Performance tracking
|
|
|
|
## Implementation Status
|
|
- [x] Basic Script Creation
|
|
- [ ] Error Handling
|
|
- [ ] Logging Implementation
|
|
- [ ] Automated Scheduling |