脚本如下:
#!/bin/sh
###################################################
# used to Collection CPU Top10 Informatiom
# script by shell
# writed by Deliver
# huchangxi 2022/10/18
###################################################
#获取所有进行并进行相同进行CPU使用率合并,再提取CPU使用率前10的进程。
toprocess=$(top -b -n 1 | tail -n +8 | awk '{a[$NF]+=$9}END{for(k in a) print a[k],k}' | sort -gr | head -10)
cpunum=`cat /proc/cpuinfo | grep "processor" | sort | uniq | awk -F":" '{print $2}' | wc -l`
count=$(echo "$toprocess" | wc -l)
index=0
echo "CpuUsedTop10"
echo "$toprocess" | while read line; do
proname=`echo $line | awk '{print $2}'`
provaluetemp=`echo $line | awk '{print $1}'`
provalue=`echo "scale=2;${provaluetemp}/${cpunum}"|bc`
echo -n ' "'$proname'":"'$provalue'" '
index=`expr $index + 1`
if [ $index -lt $count ]; then
echo ','
fi
done
输出:
CpuUsedTop10
"top":"1.67" ,
"zabbix_proxy":"0" ,
"zabbix_agentd":"0" ,
"xprtiod":"0" ,
"xfs-reclaim/vda":"0" ,
"xfs_mru_cache":"0" ,
"xfs-log/vda1":"0" ,
"xfs-eofblocks/v":"0" ,
"xfs-data/vda1":"0" ,
"xfs-conv/vda1":"0"