System Monitoring Scripts
Monitoring system resources and performance metrics is crucial for maintaining the health of your server or workstation. By utilizing shell scripting, you can automate the process of gathering essential performance data and set alerts to notify you of any potential issues. In this article, we'll walk through a series of scripts designed to help you monitor CPU, memory, disk usage, and network performance effectively.
Basics of System Monitoring
Before diving into the scripts themselves, it’s essential to understand the key system metrics you should monitor:
- CPU Usage: High CPU usage can indicate a runaway process or insufficient resources.
- Memory Usage: Monitoring memory helps prevent swapping, which can lead to performance degradation.
- Disk Usage: Running out of disk space can result in application crashes or data loss.
- Network Performance: Slow network speeds can severely affect application performance and user experience.
By automating the monitoring process, system administrators can proactively address issues and maintain optimum performance.
1. CPU Usage Monitoring Script
Using a simple shell script, you can regularly check the CPU usage and log the data for analysis. Here’s a small script that uses the top command to retrieve CPU information and log it.
#!/bin/bash
# CPU Monitoring Script
CPU_LOG="/var/log/cpu_usage.log"
# Function to log CPU usage
log_cpu_usage() {
echo "CPU Usage at $(date):" >> $CPU_LOG
top -b -n1 | grep "Cpu(s)" >> $CPU_LOG
echo "--------------------------------------------------" >> $CPU_LOG
}
# Schedule the script to run every 5 minutes
while true; do
log_cpu_usage
sleep 300
done
How the Script Works:
- Log File Setup: The script defines a log file at
/var/log/cpu_usage.log. - Logging Function: It creates a function that appends the current CPU usage and timestamp to the log file.
- Looping: The script runs in an infinite loop, calling the logging function every 300 seconds (5 minutes).
2. Memory Usage Monitoring Script
Memory monitoring ensures that your applications have enough RAM to function efficiently. Below is a memory-monitoring script that logs memory usage statistics.
#!/bin/bash
# Memory Monitoring Script
MEMORY_LOG="/var/log/memory_usage.log"
# Function to log memory usage
log_memory_usage() {
echo "Memory Usage at $(date):" >> $MEMORY_LOG
free -h >> $MEMORY_LOG
echo "--------------------------------------------------" >> $MEMORY_LOG
}
# Schedule the script to run every 5 minutes
while true; do
log_memory_usage
sleep 300
done
Explanation of the Script:
- Log File: It sets up a log file at
/var/log/memory_usage.log. - Logging Function: This function uses the
free -hcommand to log human-readable memory usage. - Looping: Similar to the CPU script, this one runs every 5 minutes.
3. Disk Usage Monitoring Script
Keeping track of disk usage is vital to prevent applications from encountering space-related errors. Here’s a disk usage script to help you monitor available space.
#!/bin/bash
# Disk Usage Monitoring Script
DISK_LOG="/var/log/disk_usage.log"
# Function to log disk usage
log_disk_usage() {
echo "Disk Usage at $(date):" >> $DISK_LOG
df -h >> $DISK_LOG
echo "--------------------------------------------------" >> $DISK_LOG
}
# Schedule the script to run every 5 minutes
while true; do
log_disk_usage
sleep 300
done
Script Insights:
- Log File: This script will write logs to
/var/log/disk_usage.log. - Disk Logging: It uses the
df -hcommand to show the disk usage of mounted filesystems in a human-readable format. - Execution Frequency: Like the previous scripts, it runs every 5 minutes.
4. Network Performance Monitoring Script
Network performance can often be the bottleneck in system performance. This script monitors your network bandwidth utilization.
#!/bin/bash
# Network Monitoring Script
NETWORK_LOG="/var/log/network_usage.log"
# Function to log network usage
log_network_usage() {
echo "Network Usage at $(date):" >> $NETWORK_LOG
ifstat -S -n 1 1 >> $NETWORK_LOG
echo "--------------------------------------------------" >> $NETWORK_LOG
}
# Schedule the script to run every 5 minutes
while true; do
log_network_usage
sleep 300
done
Understanding the Network Script:
- Log File: The log entries are stored in
/var/log/network_usage.log. - Network Monitoring: Utilizes
ifstatto monitor the network interface(s) statistics. - Loop and Interval: Logs network usage every 5 minutes.
5. Automation and Alerts
While logging the data is crucial, being notified of potential problems is just as important. To enhance the above scripts, consider integrating email alerts. Here’s how to add a basic alert if CPU usage exceeds a threshold.
#!/bin/bash
# CPU Monitoring Script with Alerts
THRESHOLD=90
CPU_LOG="/var/log/cpu_usage.log"
EMAIL="your-email@example.com"
log_cpu_usage() {
echo "CPU Usage at $(date):" >> $CPU_LOG
Usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\\([0-9.]*\\)%* id.*/\1/" | awk '{print 100 - $1}')
echo "Current CPU Usage: $Usage%" >> $CPU_LOG
alert_if_high_cpu $Usage
echo "--------------------------------------------------" >> $CPU_LOG
}
alert_if_high_cpu() {
if (( $(echo "$1 > $THRESHOLD" | bc -l) )); then
echo "ALERT: CPU usage exceeded the threshold at $1%" | mail -s "CPU Alert" $EMAIL
fi
}
# Schedule the script to run every 5 minutes
while true; do
log_cpu_usage
sleep 300
done
Key Additions in This Script:
- Threshold Variable: Defining a CPU usage threshold (90% in this case).
- Alert Function: If the CPU usage exceeds the specified threshold, the script sends an alert email.
- Email Notification: Make sure
mailcommand is configured on your system to send emails.
Conclusion
With these simple yet effective shell scripts, you can monitor essential system resources, ensuring that your server or workstation runs smoothly. By logging the information and implementing alert systems, you can proactively manage your system's performance to avoid critical failures.
Remember, while automation is fantastic, it’s also important to regularly check the logs and fine-tune your monitoring scripts based on your specific environment's needs. Happy scripting!