首页 > 技术文章 > xenserver 主机网卡使用情况

hayden1106 2017-12-08 15:17 原文

#! /bin/sh
#this script for get xenserver 6.5 7.0  network useage
#xen-host-network.sh v0.02
#send xen-host-network.sh to xenserver use ftp or ssh first  ./xen-host-network.sh
#date:  2016-08-12  hayden
#yelang007sheng@163.com

log=`date "+%H_%M_%S"`
mkdir -p /tmp/"$log"

get_iface(){
  sar -n DEV 1 1 |grep -i Average |awk '{print $2}' >iface.tmp
  sed -i '1d' iface.tmp
  iface=`cat iface.tmp`
  echo $iface |tr '\n' ' ' >iface.tmp
  iface=`cat iface.tmp`
}

net_unit(){
  if [ `sar -n DEV 1 1 |grep -i Average |head -n 1 | grep -c "kB/s"`  -eq 1 ]; then
    net_unit="kB/s"
  elif [ `sar -n DEV 1 1 |grep -i Average |head -n 1 |grep -c "byt/s"` -eq 1 ]; then
    net_unit="byt/s"
  else
    net_unit=unknow
    echo "Please check sar command network unit for rx and tx "
    exit 1
  fi
}


monitor_time(){
#for((;;))
while :
do
  get_iface
  read -p "Please input monitor network intface id: [ $iface ]: " eth
  if [ `cat iface.tmp |grep -c "$eth\>"` -ne 1 ];then
    echo "IFACE input error!!!"
    continue
  fi
  read -p "Please input monitor each time:[ second exp: 1 2 5 .... ] " time
  read -p "Please input monitor count: [ count exp: 10 100 1000 .. ] " count
  if [ `echo "$time" |grep -c '[^0-9]'` -ne 0 ] || [ `echo "$count" |grep -c '[^0-9]'` -ne 0 ]; then
    echo "Input each time or count error!!!"
    continue
  else
     if [ "$time" -gt 0 ] && [ "$count" -gt 0 ]; then
        break
     else
        echo "Input each time or count error!!!"
        continue
     fi
  fi
done
wait_t=`echo "$time * $count" |bc`
echo "Monitor start... Please wait $wait_t second!!!"
}

get_net_data(){
  time_t=$1
  count_t=$2
  sar -n DEV 1 1 |grep -i Average |head -n 1 |sed s'/Average:/Items    /' > "$eth"_"$log"
  sar -n DEV $time_t $count_t |grep "$eth" |sed 's/ AM/_AM/g' |sed 's/ PM/_PM/g' >> "$eth"_"$log" 
  cp "$eth"_"$log" /tmp/"$log"
}

get_net_value(){
  sed -i '1d' "$eth"_"$log"
  cat "$eth"_"$log" |awk '{print $5+$6}'|awk '{printf "%.2f\n",$0}'>"$eth"_temp
  avg_value=`cat "$eth"_temp |tail -n 1`
  min_value=`cat "$eth"_temp |sort -n |head -n 1`
  max_value=`cat "$eth"_temp |sort -n |tail -n 1`
  
}

KBs_to_Mbs(){
  value=$1
  value=`echo "scale=4; $value * 8 / 1024"|bc |awk '{printf"%.4f", $0}'`
}

byts_to_Mbs(){
  value=$1
  value=`echo "scale=4; $value *8 / 1024 / 1024"|bc |awk '{printf"%.4f", $0}'`
}

#main
net_unit
monitor_time
get_net_data "$time" "$count"
get_net_value
if [ "$net_unit" == "kB/s" ]; then
  KBs_to_Mbs "$avg_value"
  avg_value_Mbs="$value"
  
  KBs_to_Mbs "$min_value"
  min_value_Mbs="$value"
  
  KBs_to_Mbs "$max_value"
  max_value_Mbs="$value"
  
elif [ "$net_unit" == "byt/s" ]; then
  byts_to_Mbs "$avg_value"
  avg_value_Mbs="$value"
  
  byts_to_Mbs "$min_value"
  min_value_Mbs="$value"
  
  byts_to_Mbs "$max_value"
  max_value_Mbs="$value"

else
 echo "error!!!"
 exit 1 
fi

echo "$eth"_avg = "$avg_value_Mbs"Mb/s |tee -a result_"$eth"_"$log" 
echo "$eth"_min = "$min_value_Mbs"Mb/s |tee -a result_"$eth"_"$log" 
echo "$eth"_max = "$max_value_Mbs"Mb/s |tee -a result_"$eth"_"$log" 
mv result_"$eth"_"$log" /tmp/"$log"
rm -rf 

#rm -rf "$eth"_"$log" "$eth"_temp1 "$eth"_temp2 iface.tmp
rm -rf "$eth"_"$log" "$eth"_temp iface.tmp 

 

推荐阅读