標籤:監控 shell 進程 load cpu
引用原文shell指令碼:
#/bin/bash
LOAD=$(awk ‘{print $1}‘ /proc/loadavg)
CPUNUM=$(grep -c processor /proc/cpuinfo)
if [ $(echo "$LOAD > $CPUNUM" | bc) = 1 ]; then
RESULT=$(ps -eo pcpu,pmem,user,args | awk ‘$1 > 0‘ | sort -nr)
if [ -n "$RESULT" ]; then
echo "$RESULT" > /var/log/ps.$(date +"%Y%m%d%H%M")
fi
fi
shell指令碼抓取問題進程
晚上某個時段收到某台伺服器load很高,但再登入伺服器查看時候load可能已經下降了,
所以使用shell指令碼來處理解決問題 監控頻率30秒1次 將系統負載超過一定數值就記錄到文本裡,
我這邊修改了原文shell指令碼
cat load.sh #!/bin/bash#--------------------------------------------------#Author:jimmygong#Email:[email protected]#FileName:load.sh#Function: #Version:1.0 #Created:2015-06-02#--------------------------------------------------sleeptime=30logpath="/opt/scripts"[[ -d $logpath ]] || mkdir $logpathechosucc () { succstatus="[ Ok ]" printf "\033[32m $succstatus $* \033[0m\n"}function usage (){ echo "Usage: $0 {start|stop}"}if [[ $# -ne 1 ]]then usage exit 1fifunction start () { echosucc "Starting load monitor" while true do load=$(cat /proc/loadavg|awk ‘{print $1}‘) cpunum=$(cat /proc/cpuinfo|grep -c processor) if [[ $(echo "$load > $cpunum" | bc) = 1 ]] then result=$(ps -eo pcpu,pmem,user,args|awk ‘$1 > 0‘ |sort -nr) if [[ -n "$result" ]] then echo "$result" > $logpath/load.$(date +"%Y%m%d%H%M") fi fi sleep $sleeptime done}function stop () { echo $$ > "$logpath/pidfile" for PID in $(ps -eo pid,command|grep "$0"|grep -v grep|awk ‘{print $1}‘); do [[ $PID != $(cat $logpath/pidfile) ]] && kill -s TERM $PID >& /dev/null done echosucc ‘Stopping load monitor‘}case $1 in start) start & ;; stop) stop ;; *) usage ;;esac
==========================參數說明==============================
man ps 可以看到下面說明
CODE NORMAL HEADER
%C pcpu %CPU
%G group GROUP
%P ppid PPID
%U user USER
%a args COMMAND
%c comm COMMAND
%g rgroup RGROUP
%n nice NI
%p pid PID
%r pgid PGID
%t etime ELAPSED
%u ruser RUSER
%x time TIME
%y tty TTY
%z vsz VSZ
本文出自 “7928217” 部落格,請務必保留此出處http://7938217.blog.51cto.com/7928217/1657964
shell指令碼抓取問題進程