监控内存使用率,若超过70%,则记录占内存最高的前10个进程,并统计本机流量(连接数),假设本机是web服务器
代码如下:
#!/bin/bash
while true
do
echo "-------------------" >> /mnt/log/mem.log
date +"20%y-%m-%d %H:%M:%S" >> /mnt/log/mem.log
total=`free | grep -i "mem" | awk '{print $2}'`
used=`free | grep -i "mem" | awk '{print $2}'`
let lv=$used*100/$total
if [ $lv -ge 70 ]; then
echo "memory used percent highly,used percent is : $lv" >> /mnt/log/mem.log
ps -aux | sort -rn -k 4 | head -n 10 >> /mnt/log/mem.log
count =`netstat -antl | grep -iv "listen" | grep "80" | wc -l `
echo "now client count is : $count" >> /mnt/log/mem.log
fi
sleep 30
done
解题方法很多,这里只提供一种思路。