获取cpu与内存的巅峰值与低谷值

大耗子 2020年09月17日 4次浏览

获取cpu与内存的巅峰值与低谷值

#!/bin/bash
# 根据实际脚本名字执行
# 使用方法 默认60秒: ./powerinfo 
# 指定时间100秒 ./powerinfo 100
# cpu信息在cpu.log
# 内存信息在mem.log
# 脚本在运行前会先删除log文件,如需保存,记得改log文件名,避免被删除
max_mem=0;
min_mem=100;
max_cpu=0;
min_cpu=100;



#readtime代表秒数,实际时间比该数值大
readtime=60;
if [ $1 ];then
	readtime=$1;
fi

rm cpu.log;
rm mem.log;
get_mem_info(){
	node=0;
	max=0;
	min=100;

	node=`free | awk -F: '/Mem/{print $2}' | awk -F" " '{print 1-$2/$1}'` ;
	echo "内存占用率: "$node%;
	echo "内存占用率: "$node >> mem.log;
	max_mem=`echo $max_mem $node | awk '{ if($1 < $2){print $2} else {print $1}}'`;
	min_mem=`echo $min_mem $node | awk '{ if($1 > $2){print $2} else {print $1}}'`;
}


get_cpu_info(){
	node=0;
	
	# 根据实际情况,有可能变成以下几种情况,根据电脑情况来
	# mpstat -P ALL 1 1 | awk -F: '/Average:     all/{print $2}' | awk -F" " '{print $11}'
	# mpstat -P ALL 1 1 | awk -F: '/平均/{print $2}' | awk -F" " '{print $11}'
	# mpstat -P ALL 1 1 | awk -F: '/平均/{print $2}' | awk -F" " '{print $10}'
	node=`mpstat -P ALL 1 1 | awk -F: '/Average:     all/{print $2}' | awk -F" " '{print $10}'` ;

	node=`echo 100 $node | awk '{print $1-$2}'`;
	echo "cpu占用率: "$node%;
	echo "cpu占用率: "$node >> cpu.log;
	max_cpu=`echo $max_cpu $node | awk '{ if($1 < $2){print $2} else {print $1}}'`;
	min_cpu=`echo $min_cpu $node | awk '{ if($1 > $2){print $2} else {print $1}}'`;

}


while [ $readtime -ne 0  ]
do
	get_dish_info;
	get_mem_info ;
	get_cpu_info ;
	readtime=$[$readtime-1]
done

echo =====================================;
echo cpu峰值为:  $max ;
echo cpu最低值为:$min ;
echo 内存占用率峰值为:  $max_mem%;
echo 内存占用率最低值为:$min_mem%;
echo =====================================;

echo cpu峰值为:  $max >> cpu.log ;
echo cpu最低值为:$min >> cpu.log ;
echo 内存占用率峰值为:  $max_mem%>> mem.log ;
echo 内存占用率最低值为:$min_mem%>> mem.log ;



获取cpu与内存的巅峰值与低谷值(指定获取0和3号cpu)

#!/bin/bash
# 根据实际脚本名字执行
# 使用方法 默认60秒: ./powerinfo 
# 指定时间100秒 ./powerinfo 100
# cpu信息在cpu.log
# 内存信息在mem.log
# 脚本在运行前会先删除log文件,如需保存,记得改log文件名,避免被删除

max_mem=0;
min_mem=100;
max_cpu0=0;
min_cpu0=100;
max_cpu3=0;
min_cpu3=100;


#readtime代表秒数,实际时间比该数值大
readtime=60;
if [ $1 ];then
	readtime=$1;
fi

rm cpu.log;
rm mem.log;

get_mem_info(){
	node=0;
	max=0;
	min=100;

	node=`free | awk -F: '/Mem/{print $2}' | awk -F" " '{print 1-$2/$1}'` ;
	echo "内存占用率: "$node%;
	echo "内存占用率: "$node >> mem.log;
	max_mem=`echo $max_mem $node | awk '{ if($1 < $2){print $2} else {print $1}}'`;
	min_mem=`echo $min_mem $node | awk '{ if($1 > $2){print $2} else {print $1}}'`;



}


get_cpu_info(){
	max0=0;
	min0=100;
	max3=0;
	min3=100;
	node=0;
	node0=0;
	node3=0;


	# 根据实际情况,有可能变成以下几种情况,根据电脑情况来
	# mpstat -P ALL 1 1 | awk -F: '/Average/{print $2}' | awk -F" " '{print $11}'
	# mpstat -P ALL 1 1 | awk -F: '/平均/{print $2}' | awk -F" " '{print $11}'
	# mpstat -P ALL 1 1 | awk -F: '/Average/{print $2}' | awk -F" " '{print $10}'
	# mpstat -P ALL 1 1 | awk -F: '/平均/{print $2}' | awk -F" " '{print $10}'
	node=`mpstat -P ALL 1 1 | awk -F: '/Average/{print $2}' | awk -F" " '{print $10}'` ;

	node0=`echo 4 $node | awk '{print $$1}'`;
	node0=`echo 100 $node0 | awk '{print $1-$2}'`;
	echo "cpu0占用率: "$node0%;
	echo "cpu0占用率: "$node0 >> cpu.log;
	max_cpu0=`echo $max_cpu0 $node0 | awk '{ if($1 < $2){print $2} else {print $1}}'`;
	min_cpu0=`echo $min_cpu0 $node0 | awk '{ if($1 > $2){print $2} else {print $1}}'`;
	
	
	node3=`echo 7 $node | awk '{print $$1}'`;
	node3=`echo 100 $node3 | awk '{print $1-$2}'`;
	echo "cpu3占用率: "$node3%;
	echo "cpu3占用率: "$node3 >> cpu.log;
	max_cpu3=`echo $max_cpu3 $node3 | awk '{ if($1 < $2){print $2} else {print $1}}'`;
	min_cpu3=`echo $min_cpu3 $node3 | awk '{ if($1 > $2){print $2} else {print $1}}'`;
	




}



while [ $readtime -ne 0  ]
do
	get_dish_info;
	get_mem_info ;
	get_cpu_info ;
	readtime=$[$readtime-1]
done

echo =====================================;
echo cpu0峰值为:  $max_cpu0 ;
echo cpu0最低值为:$min_cpu0 ;
echo cpu3峰值为:  $max_cpu3 ;
echo cpu3最低值为:$min_cpu3 ;
echo 内存占用率峰值为:  $max_mem%;
echo 内存占用率最低值为:$min_mem%;
echo =====================================;

echo cpu0峰值为:  $max_cpu0 >> cpu.log;
echo cpu0最低值为:$min_cpu0 >> cpu.log;
echo cpu3峰值为:  $max_cpu3 >> cpu.log;
echo cpu3最低值为:$min_cpu3 >> cpu.log;
echo 内存占用率峰值为:  $max_mem%>> mem.log;
echo 内存占用率最低值为:$min_mem%>> mem.log;