作業(yè):編寫腳本
持續(xù)循環(huán)監(jiān)控top命令
匯總系統(tǒng)CPU、內(nèi)存使用情況
匯總系統(tǒng)網(wǎng)絡(luò)、IO使用情況
先啟動nginx服務(wù),然后循環(huán)監(jiān)控它是否存在,如被kill,則再次啟動,重復(fù)監(jiān)控
持續(xù)循環(huán)監(jiān)控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:定時任務(wù)
思路2:寫個循環(huán),每隔多久執(zhí)行
- 匯總系統(tǒng)CPU、內(nèi)存使用情況
#!/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"
- 匯總系統(tǒng)網(wǎng)絡(luò)、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、內(nèi)存的使用 相同
awk 'BEGIN{total=0}{total+=$1}END{print total}'
根據(jù)某個網(wǎng)卡去查網(wǎng)絡(luò)使用情況
rxkB/s : 每秒接受的字節(jié)數(shù)
txkB/s : 每秒發(fā)送的字節(jié)數(shù)
#!/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}'
- 先啟動個nginx服務(wù),然后循環(huán)監(jiān)控它是否存在,如被kill,則再次啟動,重復(fù)監(jiān)控
#!/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后臺執(zhí)行顯示:top: failed tty get
Linux系統(tǒng)中top命令是交互式命令,故腳本中執(zhí)行時會卡住,不再執(zhí)行下一個命令。解決辦法是: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.
如要持續(xù)監(jiān)控top,沒有啟動時則立刻啟動
在shell腳本中可用 nohup top -b &