xenserver 主机cpu一段时间内的使用情况

#!/bin/sh
#this script for get xenserver 6.5 7.0  cpu useage
#send xen-host-cpu.sh to xenserver use ftp or ssh first  ./xen-host-cpu.sh
#version: 0.03
#date:  2016-01-11 hayden
#yelang007sheng@163.com

log=`date "+%H_%M_%S"`

get_host_uuid(){

host_uuid=`xe host-list |grep uuid |awk -F\: '{print $2}' |sed 's/ //g'`
#host_uuid=`xe host-list --minimal`
sleep 1

}

get_host_cpu_avg(){

rrd2csv AVERAGE:host:"$host_uuid":cpu_avg >host_cpu_avg_"$log" & >/dev/null
rrd2csv_cpu_pid=$!

}

monitor_time(){
while :
do
    read -p "Please input a monitor time:[ exp> 60s or 5m or 1h or 1d ] " time
    # ???use && not executed 
    if [ `echo "${time:0:${#time}-1}" |grep -c '[^0-9]'` -ne 0 ] || [ `echo "${time:0-1}" |grep -c '[smhd]'` -ne 1 ]; then
        echo "Input monitor time error!!!"
        continue
    else
        if [ "${time:0:${#time}-1}" -gt 0 ]; then
            break
        else
            echo "Input monitor time error!!!"
            continue
        fi
    fi
done
}

#main

monitor_time
get_host_uuid
get_host_cpu_avg
echo "Monitor start ... Please wait $time "
sleep $time
sleep 1
kill -9 $rrd2csv_cpu_pid >/dev/null 2>&1
sleep 2
cp host_cpu_avg_"$log" cpu_temp_"$log"
sed -i '1d' cpu_temp_"$log"

cat cpu_temp_"$log" |awk -F\, '{print $2}' |sed 's/ //g' | sort -n >temp_"$log"
min_cpu_t=`head -n 1 temp_"$log"`
min_cpu=`echo "scale=2; $min_cpu_t * 100"|bc |awk '{printf "%.2f", $0}'`
max_cpu_t=`tail -n 1 temp_"$log"`
max_cpu=`echo "scale=2; $max_cpu_t * 100"|bc |awk '{printf "%.2f", $0}'`
count_temp=`cat temp_"$log" |wc -l`

sum=0
for i in `cat temp_"$log"`
do
    sum=`echo $sum + $i |bc`
done


avg_cpu_t=`echo "scale=4; $sum / $count_temp "|bc |awk '{printf "%.4f", $0}'`
avg_cpu=`echo "scale=2; $avg_cpu_t * 100"|bc |awk '{printf "%.2f", $0}'`

echo Host["$host_uuid"] min_cpu="$min_cpu""%" |tee host_cpu_"$log"
echo Host["$host_uuid"] max_cpu="$max_cpu""%" |tee -a host_cpu_"$log"
echo Host["$host_uuid"] avg_cpu="$avg_cpu""%" |tee -a host_cpu_"$log"

mkdir -p /tmp/"$log"
mv host_cpu_avg_"$log" host_cpu_"$log" /tmp/"$log" 
rm -rf  cpu_temp_"$log" temp_"$log"  

 

posted @ 2017-12-08 15:10  hayden__wang  阅读(525)  评论(0编辑  收藏  举报