监控CPU使用率,当使用率达到70%时,查看user% 和 iowait%,若user%高到50%,则统计服务进程数(假设时MySQL),若iowait%高到30%,则记录磁盘读写速度
这里我们用 sar 命令查看,(用的虚拟机,比较空)
代码如下:
#!/bin/bash
while true
do
echo "-------------------" >> /mnt/log/cpu.log
date +"20%y-%m-%d %H:%M:%S" >> /mnt/log/cpu.log
cpuinfo=`sar 1 1 | tail -n 1`
idle=`echo $cpuinfo | awk '{print $8}' | awk -F "." '{print $1}'`
if [ $idle -le 30 ]; then
echo "cpu used highly. the idle is :$idle" >> /mnt/log/cpu.log
user=`echo $cpuinfo | awk '{print $3}' | awk -F "." '{print $1}'`
if [ $user -ge 50 ]; then
count=`ps -aux | grep "mysql" |