XCP/XenServer资源监控脚本分享[原创]


一直以来我都以每个月至少一篇原创技术文章来要求自己,不过有的时候真的太懒了,在这里鄙视一下自己。
今天已经是这个月的最后一天了,其实近期的工作,有很多东西都是可以总结成文章分享给大家的。

有一些用Python和Shell写的脚本,我都放到了GitHub上,其中一个就是XCP/XenServer资源监控的脚本。
XCP/XenServer的XenCenter只有Windows下的客户端,命令行要获取可用的CPU,内存以及磁盘,并没有那么方便,需要通过各个命令行去获取。有的命令还必须在host主机本身运行才可以,比如 xl,因此我在各个XCP/XenServer的宿主OS之间都配置了ssh-key的信任。

我们的环境是由10多台物理服务器装上XCP构成的一个Pool,Pool的Manager是第一个XCP,然后存储是用的每台服务器本身的硬盘。
脚本的输出结果如下:

[root@xen1 ~]# vm_monitor
-------------------------------------------------------
Host xen1: vm-server1 vm-server2
Available: Mem=13/95G  Disk=6/1108G  CPU=18/24Cores
-------------------------------------------------------
Host xen2: vm-server3 vm-server4
Available: Mem=13/95G  Disk=6/1108G  CPU=18/24Cores
-------------------------------------------------------
Host xen3: vm-server5 vm-server6 vm-server7 vm-server8 vm-server9 vm-server10
Available: Mem=13/63G  Disk=151/549G  CPU=8/24Cores
-------------------------------------------------------
Host xen4: vm-server11 vm-server12 vm-server13 vm-server14
Available: Mem=22/63G  Disk=11/549G  CPU=12/24Cores

具体的脚本内容如下:

#!/bin/bash

function get_info(){
  host_name=$1
  host_uuid=$(xe host-list |grep -w ${host_name} -B1 |grep -w uuid |awk '{print $NF}')
  guest_vm=$(xe vm-list resident-on=${host_uuid} is-control-domain=false |grep -w name-label |awk '{print $NF}' |sort -n |xargs)
   
  t_mem_b=$(xe host-param-list uuid=${host_uuid} |grep -w memory-total |awk '{print $NF}')
  f_mem_b=$(xe host-param-list uuid=${host_uuid} |grep -w memory-free-computed |awk '{print $NF}')
  t_mem_g=$(($t_mem_b/1024/1024/1024))
  f_mem_g=$(($f_mem_b/1024/1024/1024))

  xe sr-list |grep -A2 -B3 -w ${host_name} |grep -A1 -B4 -Ew 'lvm|ext' |grep -w name-label |awk -F ': ' '{print $2}' > /tmp/sr_items.tmp
  disk_info=""
  while read sr_name
  do
    disk_uuid=$(xe sr-list |grep -A 2 -B 1 "$sr_name" |grep -B 3 -w "$host" |grep uuid |awk -F ": " '{print $2}')
    t_disk_b=$(xe sr-param-list uuid=$disk_uuid |grep physical-size |cut -d : -f 2)
    u_disk_b=$(xe sr-param-list uuid=$disk_uuid |grep physical-utilisation |cut -d : -f 2)
    f_disk_b=$(($t_disk_b-$u_disk_b))
    t_disk_g=$(($t_disk_b/1024/1024/1024))
    f_disk_g=$(($f_disk_b/1024/1024/1024))
    disk_info="${f_disk_g}/${t_disk_g}G $disk_info"
  done < /tmp/sr_items.tmp

  t_cpu_num=$(xe host-param-list uuid=${host_uuid} |grep -w 'cpu_count' |awk '{print $4}' |cut -d";" -f1)
  v_cpu_sum=0
  for vm in $guest_vm
  do
    vm_uuid=$(xe vm-list |grep -B 1 -w $vm |head -n 1 |awk -F ": " '{print $2}')
    v_cpu_num=$(xe vm-list params=VCPUs-number uuid=${vm_uuid} |grep -w VCPUs |awk -F ": " '{print $2}')
    v_cpu_sum=$(($v_cpu_sum+$v_cpu_num))
  done
  f_cpu_num=$(($t_cpu_num-$v_cpu_sum))
   
  echo "Host $host_name: \"$guest_vm\""
  echo "Available: \"Mem=${f_mem_g}/${t_mem_g}G  Disk=${disk_info} CPU=${f_cpu_num}/${t_cpu_num}Cores\""
}

if [ $# == 0 ]; then
  host_list=$(xe host-list |grep name-label |awk '{print $4}' |cut -d. -f1 |sort -n)
  for host_name in $host_list
  do
    echo "-------------------------------------------------------"
    get_info ${host_name}
  done
else
  get_info $1
fi

,

  1. No comments yet.
(will not be published)
*