Linux 第四课 监控脚本编写

作业:编写脚本

  1. 持续循环监控top命令

  2. 汇总系统CPU、内存使用情况

  3. 汇总系统网络、IO使用情况

  4. 先启动nginx服务,然后循环监控它是否存在,如被kill,则再次启动,重复监控

  5. 持续循环监控top命令

#!/bin/bash
result_file=top.log
baseline_cpu=100
filter_string="print \$9,\$1,\$12"
while true
do
top -b -n 1 > $result_file
free_cpu=`grep Cpu $result_file  | awk '{print $5}' | sed  "s|\%\([a-z]\)\([a-z]\)\,||g"`
used_cpu=`echo 100 - $free_cpu | bc`
echo "free cpu: $free_cpu ,  used_cpu: $used_cpu"
if [ $(echo "$free_cpu >  $baseline_cpu "|bc) -eq 1 ];then
  echo  "idle cpu is enough"
else
  echo "cpu is not enough , max used cpu pid info : "
  echo "cpu pid cmd"
  echo "`grep  %CPU  -A 100000 $result_file | grep -v  %CPU | awk '{print $9,$1,$12}'  | sort -nrk1 | head -n 1 ` "
fi
sleep 5  
echo 
done

思路1:定时任务
思路2:写个循环,每隔多久执行

  1. 汇总系统CPU、内存使用情况
#!/bin/bash
result_file=top.log
top -b -n 1 > $result_file
system_cpu=`grep Cpu $result_file | awk '{print $3}' | sed "s|\%\([a-z]\)\([a-z]\)\,||g"`
free_mem=`grep Mem $result_file | awk '{print $6}' | sed "s|\%\([a-z]\)\([a-z]\)\,||g"`
echo "sys cpu: $system_cpu ,free mem: $free_mem "
#!/bin/bash
result_file=top.log
result_cpu_log=cpu.log
result_mem_log=mem.log
cpu_total=0
mem_total=0
top -b -n 1 > $result_file
grep %CPU -A 100000 $result_file |grep -v %CPU | awk '{print $9}' | sort -nrk1 > $result_cpu_log
grep %MEM -A 100000 $result_file |grep -v %MEM | awk '{print $10}' | sort -nrk1 > $result_mem_log
echo "sum: %CPU %MEM "
for i in  `cat $result_cpu_log`
do
   cpu_total=`echo $cpu_total +  $i | bc`
done
for i in  `cat $result_mem_log`
 do
  mem_total=`echo $mem_total +  $i | bc` 
 done
echo "sum : $cpu_total, $mem_total"
  1. 汇总系统网络、IO使用情况
#!/bin/bash
result_file=iotop.log
result_io_log=io.log
io_total=0
iotop -b -n 1 > $result_file
grep IO -A 100000 $result_file |grep -v IO | awk '{print $10}' | sort -nrk1 > $result_io_log
echo "sum: IO "
for i in  `cat $result_io_log`
do
   io_total=`echo $io_total +  $i | bc`
done
echo "sum : $io_total"

脚本模式和获取cpu、内存的使用 相同
awk 'BEGIN{total=0}{total+=$1}END{print total}'

根据某个网卡去查网络使用情况
rxkB/s : 每秒接受的字节数
txkB/s : 每秒发送的字节数

#!/bin/bash
result_file=net.log
sar -n DEV 2 5 | grep em1 > $result_file
echo "rxkB/s,txkB/s"
awk 'NF{a=$0}END{print a}' $result_file | awk '{print $5,$6}'
  1. 先启动个nginx服务,然后循环监控它是否存在,如被kill,则再次启动,重复监控
#!/bin/sh
while true
do
ps -fe|grep nginx |grep -v grep
if [ $? -ne 0 ]
then
  echo "start process....."
  /usr/local/nginx/sbin/nginx
else
  echo "runing....."
fi
sleep 5
done

top后台执行显示:top: failed tty get
Linux系统中top命令是交互式命令,故脚本中执行时会卡住,不再执行下一个命令。解决办法是:top -b
-b : Batch mode operation
Starts top in Batch mode, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit youve set with the-ncommand-line option or until killed.
如要持续监控top,没有启动时则立刻启动
在shell脚本中可用 nohup top -b &

你可能感兴趣的:(Linux 第四课 监控脚本编写)