性能检测脚本

#!/bin/bash

#获取需要监控的数据

function print_info_to_file(){

PID=$1

#当前时间

current_date=`date +'%H:%M:%S'`

#线程数

threads_count=`cat /proc/$PID/status | grep Threads | awk '{print $NF}'`

#打开的文件数

lsof_count=`lsof -p $PID |wc -l`

#cpu的使用率

cpu_usage=`top -n 1 -p $PID | tail -1 | awk '{print $(NF-3)}'`

#物理内存占用

tmp_mem=`cat /proc/$PID/statm | awk '{print $2}'`

let physic_mem=tmp_mem*4/1024

#虚拟内存占用

tmp_mem=`cat /proc/$PID/statm | awk '{print $1}'`

let virtual_mem=tmp_mem*4/1024

printf "%15s, %15s, %15s, %15s, %15s, %15s\n" $current_date $threads_count $lsof_count $cpu_usage $physic_mem $virtual_mem >> $2

}

#前置条件判断

if [[ "$1" = "--help" ]]; then

echo "usage:\"sh performanceMoniter.sh ${PID} [${duration} ${interval}]\""

exit 1

fi

if [[ ! "$1" ]]; then

echo "running error! please input PID(process ID)!"

exit 1

fi

if [[ ! "$2" ]]; then

echo "use default duration 1min due to no duration time is typed."

duration=1

else

duration=$2

fi

if [[ ! "$3" ]]; then

echo "use default interval 1s due to no duration time is typed."

interval=1

else

interval=$3

fi

#进程程基本信息

PID=$1

file_name="`pwd`/PerformanceMoniter_`date +'%Y_%m_%d_%H_%M_%S'`.csv"

process_name=`ps -p $PID | tail -1 | awk '{print $(NF)}'`

echo "PID : $PID"

echo "Process Name : ${process_name}"

echo "To moniter resource usage every ${interval} seconds, totally take ${duration} minutes."

#循环输出监控信息

let times=duration*60/interval

i=0

while (( i < times ))

do

print_info_to_file $PID $file_name

let i+=1

sleep $interval

done

#计算监控数据的最大值、最小值以及平均数

#计算的时候+0是为了去除数据里面的非数字字符,保证输出的max一定是数字

thread_max=`cat $file_name | awk 'BEGIN {max = 0} {if ($2+0>max+0) max=$2+0 fi} END{print max}'`

lsof_max=`cat $file_name | awk 'BEGIN {max = 0} {if ($3+0>max+0) max=$3+0 fi} END{print max}'`

cpu_max=`cat $file_name | awk 'BEGIN {max = 0} {if ($4+0>max+0) max=$4+0 fi} END{print max}'`

physic_mem_max=`cat $file_name | awk 'BEGIN {max = 0} {if ($5+0>max+0) max=$5+0 fi} END{print max}'`

virtual_mem_max=`cat $file_name | awk 'BEGIN {max = 0} {if ($6+0>max+0) max=$6+0 fi} END{print max}'`

thread_average=`cat $file_name | awk '{sum+=$2}END{print sum/NR}'`

lsof_average=`cat $file_name | awk '{sum+=$3}END{print sum/NR}'`

cpu_average=`cat $file_name | awk '{sum+=$4}END{print sum/NR}'`

physic_mem_average=`cat $file_name | awk '{sum+=$5}END{print sum/NR}'`

virtual_mem_average=`cat $file_name | awk '{sum+=$6}END{print sum/NR}'`

thread_min=`cat $file_name | awk 'BEGIN {min = 9999999} {if ($2+0$2+0 fi} END {print min}'`

lsof_min=`cat $file_name | awk 'BEGIN {min = 9999999} {if ($3+0$3+0 fi} END {print min}'`

cpu_min=`cat $file_name | awk 'BEGIN {min = 99999999} {if ($4+0$4+0 fi} END {print min}'`

physic_mem_min=`cat $file_name | awk 'BEGIN {min = 9999999} {if ($5+0$5+0 fi} END {print min}'`

virtual_mem_min=`cat $file_name | awk 'BEGIN {min = 9999999} {if ($6+0$6+0 fi} END {print min}'`

#监控信息标题

title=`printf "%15s, %15s, %15s, %15s, %15s, %15s\n" MONITER_TIME THREADS_COUNT LSOF_COUNT CPU_USAGE\(%\) PHYSIC_MEM\(MB\) VIRTUAL_MEM\(MB\)`

max=`printf "%15s, %15s, %15s, %15s, %15s, %15s\n" __MAX__ $thread_max $lsof_max $cpu_max $physic_mem_max $virtual_mem_max`

average=`printf "%15s, %15s, %15s, %15s, %15s, %15s\n" __AVERAGE__ $thread_average $lsof_average $cpu_average $physic_mem_average $virtual_mem_average`

min=`printf "%15s, %15s, %15s, %15s, %15s, %15s\n" __MIN__ $thread_min $lsof_min $cpu_min $physic_mem_min $virtual_mem_min`

#因为变量会被先解析,所以sed里面第一个字符串前面的空格会被自动略去,为此加了一个.

sed -i "1i PID : ${PID}" $file_name

sed -i "2i Process Name : ${process_name}" $file_name

sed -i "3i ----------" $file_name

sed -i "4i |${title}" $file_name 

sed -i "5i *${max}" $file_name

sed -i "6i *${average}" $file_name

sed -i "7i *${min}" $file_name

echo "\n${title}"

echo "${max}"

echo "${average}"

echo "${min}"

echo "\nplease find more details in: $file_name"

你可能感兴趣的:(性能检测脚本)