-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory and CPU Check.sh
27 lines (23 loc) · 1.1 KB
/
Memory and CPU Check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
# Set the thresholds for CPU and memory usage (in percentage)
CPU_THRESHOLD=80 # Declaring variables and Assigning values
MEMORY_THRESHOLD=80 # Declaring variables and Assigning values
# Function to check CPU usage
check_cpu_usage() {
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d. -f1) #converting o/p in batch mode to stop cont flow of info.
if [[ "$CPU_USAGE" -ge "$CPU_THRESHOLD" ]]; then
echo "High CPU usage detected: $CPU_USAGE" | mail -s "Alert: CPU" [email protected] # usage is greater than 80%
fi
}
# Function to check memory usage
check_memory_usage() {
MEMORY_USAGE=$(free | grep "Mem" | awk '{print $3/$2 * 100.0}' | tr ',' '.') #o/p is showing in commas so traslating it to make decimals
if [[ "$MEMORY_USAGE" -ge "$MEMORY_THRESHOLD" ]]; then
fi
}
while true; do
check_cpu_usage
check_memory_usage
sleep 30 # It will sleep for 60 seconds once checked and will check again after 60 seconds.
done