#!/bin/bash
date=`date -d today +%Y%m%d`
#判断是否存在目录
if [ ! -d "$HOME/sunshine" ]
then
mkdir "$HOME/sunshine"
fi
#判断是否存在文件
if [ ! -f "$HOME/sunshine/$date.log" ]
then
touch "$HOME/sunshine/$date.log"
fi
# function handle
function monitor()
{
# 获取函数参数
iowait_val=$1
#CPU: ps aux | grep -v PID | sort -nr -k +3 | head
#MEM: ps aux | grep -v PID | sort -nr -k +4 | head
time_now=`date -d today +%H:%M:%S`
echo -e "==========$time_now=======IO:$iowait_val======
" >> $HOME/sunshine/$date.log
echo -e "---------CPU----------
" >> $HOME/sunshine/$date.log
$(ps aux | grep -v PID | sort -nr -k +3 | head >> $HOME/sunshine/$date.log)
echo -e "
---------MEM----------
" >> $HOME/sunshine/$date.log
$(ps aux | grep -v PID | sort -nr -k +4 | head >> $HOME/sunshine/$date.log)
# 不采用这种方式,即便上加上了-e也会导致数据格式混乱
#echo $content > $HOME/sunshine/$date.log
}
#提取iowait值
iowait_val=$(iostat -c | grep -v avg | grep '[^/s]' | grep -v Li | awk '{print $4}')
#将浮点数处理成整数
val=$(echo "scale=0; $iowait_val/1"|bc)
#比较大小
if [ $val -ge 15 ]
then
echo "iowait is"$iowait_val
monitor $iowait_val
fi