1. shell脚本
将一下脚本,保存为shell脚本(collectInfo.sh)
#!/bin/bash
if echo $1 | grep -q '^[0-9]'
then
pid=`pidof %1`
else
pid=$1 #获取进程pid
fi
echo $pid
interval=5 #设置采集间隔
while true
do
echo $(date +"%y-%m-%d %H:%M:%S") >> proc_ingfo.log
cat /proc/$pid/status|grep -e VmRSS >> proc_ingfo.log #获取内存占用
#cpu=`top -n 1 -p $pid|tail -3|head -1|awk '{ssd=NF-4} {print $ssd}'` #获取cpu占用此方法无法在无终端中获取到数据,换用如下方法
#echo "Cpu: " $cpu >> proc_ingfo.log
cpu_core=$(grep -c processor /proc/cpuinfo)
total_time1=$(awk '{if ($1 == "cpu") {sum = $2 + $3 + $4 + $5 + $6 + $7 + $8 + $9 + $10 + $11;print sum}}' /proc/stat)
cpu_time1=$(awk '{sum=$14 + $15;print sum}' /proc/$pid/stat)
sleep 1
total_time2=$(awk '{if ($1 == "cpu") {sum = $2 + $3 + $4 + $5 + $6 + $7 + $8 + $9 + $10 + $11;print sum}}' /proc/stat)
cpu_time2=$(awk '{sum=$14 + $15;print sum}' /proc/$pid/stat)
#cpu_core=$((($cpu_time2 - $cpu_time1) / ($total_time2 - $total_time1)))
curr=$(($cpu_time2 - $cpu_time1))
total=$(($total_time2 - $total_time1))
cpu_core=$(( 100*$curr/$total))
echo "Cpu: " $cpu_core " %" >> proc_memlog.txt
diskRB=`awk '/pgpgin/{print $2}' /proc/vmstat`
sleep 1
diskRE=`awk '/pgpgin/{print $2}' /proc/vmstat`
diskR=$(($diskRE-$diskRB))
echo "disk in: " $diskR >> proc_ingfo.log
diskRBs=`awk '/pgpgout/{print $2}' /proc/vmstat`
sleep 1
diskREs=`awk '/pgpgout/{print $2}' /proc/vmstat`
diskRs=$(($diskREs-$diskRBs))
echo "disk out: " $diskRs >> proc_ingfo.log
traffic_be=(`awk 'BEGIN{ORS=" "}/enp136/{print $3,$11}' /proc/net/dev`)
sleep 1
traffic_af=(`awk 'BEGIN{ORS=" "}/enp136/{print $3,$11}' /proc/net/dev`)
enp136_in=$(((${traffic_af[0]}-${traffic_be[0]})))
enp136_out=$(((${traffic_af[1]}-${traffic_be[1]})))
echo "enp136_in: " $enp136_in " KB">> proc_ingfo.log
echo "enp136_out: " $enp136_out " KB" >> proc_ingfo.log
echo $blank >> proc_ingfo.log
sleep $interval
done
2. 调用方式
xxxx 可以是进程id,也可是程序名称
sh collectInfo.sh xxxx