Project requirements:
Use shell to write a memory monitoring script, use more than 80% concurrent email alert
Step analysis:
free -m ##内存使用情况
show result:
/ | total | used | free | shared | buff / cache | available |
---|---|---|---|---|---|---|
Mem: | 3770 | 219 | 3170 | 3170 | 381 | 3325 |
Swap: | 2047 | 0 | 2047 |
Notes:
- Mem: An overview table of memory usage.
- total: the total physical memory unit of the machine: M
- used: Used memory.
- free: Free physical memory.
Calculate the percentage:
- Memory usage=100*used/tol
Make a judgment
- Determine whether the memory usage exceeds the warning value.
Script implementation:
#!/bin/bash
used=`free -m| grep "^Mem" | awk '{print $3}'` #筛选出used的值
tol=`free -m| grep "^Mem" | awk '{print $2}'` #筛选出total的值
let a=100*used/tol
echo $a
[ $a -ge 80 ] && /opt/sendEmail.sh [email protected] "内存警告" "内存使用率为$a%,请尽快处理"