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

本文介绍了一个用于XenServer 6.5/7.0的Shell脚本,该脚本可以获取并记录主机的CPU使用率。通过设置监控时间,用户可以获得最小、最大及平均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"  

 

转载于:https://www.cnblogs.com/hayden1106/p/8005469.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值