pidstat命令监控一分钟所有的状态,排查问题足够用.
使用ps命令监控当时的状态.
脚本如下
#!/bin/bash
export MONTH=$(date +%m)
export DAY=$(date +%d)
export HOUR=$(date +%H)
export MIN=$(date +%M)
export SECOND=$(date +%S)
# pidstat monitor
mkdir -p /opt/scripts/ps_threads.log/pidstatlog/${MONTH}/${DAY}/${HOUR}
pidstat -u -l 5 12 &> /opt/scripts/ps_threads.log/pidstatlog/${MONTH}/${DAY}/${HOUR}/cpu${MIN}_${SECOND}.log &
# pid io
pidstat -d -l 5 12 &> /opt/scripts/ps_threads.log/pidstatlog/${MONTH}/${DAY}/${HOUR}/io${MIN}_${SECOND}.log &
# mpstat monitor
mkdir -p /opt/scripts/ps_threads.log/mpstatlog/${MONTH}/${DAY}/${HOUR}
mpstat -P ALL 5 12 &> /opt/scripts/ps_threads.log/mpstatlog/${MONTH}/${DAY}/${HOUR}/${MIN}_${SECOND}.log &
for i in $(seq 2);do
export MIN=$(date +%M)
export SECOND=$(date +%S)
mkdir -p /opt/scripts/ps_threads.log/pslog/${MONTH}/${DAY}/${HOUR}
echo ${MONTH}-${DAY}-${HOUR}-${MIN}-${SECOND} > /opt/scripts/ps_threads.log/pslog/${MONTH}/${DAY}/${HOUR}/${MIN}_${SECOND}.log
ps -efL >> /opt/scripts/ps_threads.log/pslog/${MONTH}/${DAY}/${HOUR}/${MIN}_${SECOND}.log
# memory top 10
echo "memory top 10" >> /opt/scripts/ps_threads.log/pslog/${MONTH}/${DAY}/${HOUR}/${MIN}_${SECOND}.log
ps -A --sort -rss -o comm,pmem,pcpu |uniq -c |head -10 >> /opt/scripts/ps_threads.log/pslog/${MONTH}/${DAY}/${HOUR}/${MIN}_${SECOND}.log
# cpu top 10
echo "cpu top 10" >> /opt/scripts/ps_threads.log/pslog/${MONTH}/${DAY}/${HOUR}/${MIN}_${SECOND}.log
ps -A --sort -%cpu -o comm,pmem,pcpu |uniq -c |head -10 >> /opt/scripts/ps_threads.log/pslog/${MONTH}/${DAY}/${HOUR}/${MIN}_${SECOND}.log
sleep 20
done
find /opt/scripts/ps_threads.log -name "*.log" -mtime +7 -exec rm -f {} \;