如何找到N个给定主机中可用内存大且可用内存最少的主机?

时间:2017-09-07 10:06:55

标签: bash

我想找到哪个主机具有最大可用内存和N个阵列主机可用的最少可用内存,并将VM从最小可用内存主机迁移到最大内存主机。目前,我的脚本仅适用于三台主机,但我需要动态数量的主机脚本。你能帮我解决一下脚本逻辑吗?提前谢谢。

这是我的代码:

#!/usr/bin/bash
array_host=($(xe host-list params=uuid | cut -c 17-))
#Loop throguh each xen server node

for i in ${array_host[@]};

do
   host_name+=($(xe host-list params=name-label uuid=$i| cut -c 23-))

   free_mem1=$(xe host-data-source-query data-source=memory_free_kib uuid=$i)

   mem_int=( ${free_mem1/\.*} )

   mem_mb=$(($mem_int/1024))

   free_mem+=( $mem_mb )

   total_mem+=($(xe host-data-source-query data-source=memory_total_kib uuid=$i))

   load_avg+=($(xe host-data-source-query data-source=loadavg uuid=$i))

   cpu_avg+=($(xe host-data-source-query data-source=cpu_avg uuid=$i))

   no_vms+=$(xe vm-list resident-on=$i | grep -c running)

done


if (( ${free_mem[2]} < ${free_mem[1]} && ${free_mem[2]} < ${free_mem[0]} )); 
then

vm_list=($(xe vm-list params=uuid is-control-domain=false resident-on=${array_host[2]} | cut -c 17-))

for vm in ${vm_list[@]};

do
   if (( ${free_mem[1]} <  ${free_mem[0]} )); then

      xe vm-migrate uuid=${vm_list[0]} host-uuid=${array_host[0]} --live

      echo ${vm_list[0]} has been migrated from xen-node2 to xen-node3
   else

      xe vm-migrate uuid=${vm_list[0]} host-uuid=${array_host[1]} --live

      echo ${vm_list[0]} has been migrated from xen-node2 to xen-node1
fi

done

fi

if (( ${free_mem[1]} < ${free_mem[2]} && ${free_mem[1]} < ${free_mem[0]} )); 
then


vm_list=($(xe vm-list params=uuid is-control-domain=false resident-on=${array_host[1]} | cut -c 17-))

for vm in ${vm_list[@]};

do

   if (( ${free_mem[2]} <  ${free_mem[0]} )); then

      xe vm-migrate uuid=${vm_list[0]} host-uuid=${array_host[0]} --live

      echo ${vm_list[0]} has been migrated from xen-node1 to xen-node3

   else

      vm_list=($(xe vm-list params=uuid is-control-domain=false resident-on=${array_host[1]} | cut -c 17-))

      xe vm-migrate uuid=${vm_list[0]} host-uuid=${array_host[2]} --live

      echo ${vm_list[0]} has been migrated from xen-node1 to xen-node2
 fi

done

fi



if (( ${free_mem[0]} < ${free_mem[1]} && ${free_mem[0]} < ${free_mem[2]} )); 
then

vm_list=($(xe vm-list params=uuid is-control-domain=false resident-on=${array_host[0]} | cut -c 17-))

for vm in {vm_list[@]};

do
   if (( ${free_mem[1]} <  ${free_mem[2]} )); then

      xe vm-migrate uuid=${vm_list[0]} host-uuid=${array_host[2]} --live

      echo ${vm_list[0]} has been migrated from xen-node3 to xen-node2
   else

      xe vm-migrate uuid=${vm_list[0]} host-uuid=${array_host[1]} --live

      echo ${vm_list[0]} has been migrated from xen-node3 to xen-node1
fi

done

fi

2 个答案:

答案 0 :(得分:0)

使用tmp_min_memorytmp_min_hosttmp_max_memorytmp_max_host个临时变量。将有关主机的数据放入数组中,将第一个元素数据放在tmp_min_memorytmp_min_hosttmp_max_memorytmp_max_host中。循环数组。当您找到可用内存较少的主机tmp_min_memory时,请更新tmp_min_memorytmp_min_host。同样,每次遇到内存大于tmp_max_memory的主机时,请更新tmp_max_memorytmp_max_host。最后执行与您相同的操作。

答案 1 :(得分:0)

似乎有6个数组,各自的顺序为host_name free_mem total_mem load_avg cpu_avg no_vms

运行数组索引,这是一个拥有最小和最大可用内存的主机的示例

tmp_min_free_mem=9999999999999
tmp_max_free_mem=0

for ((i=0;i<${#host_name[@]};i+=1)); do

    if ((free_mem[i]<tmp_min_free_mem)); then

        tmp_min_free_mem=${free_mem[i]}
        tmp_min_free_mem_host=${host_name[i]}

    fi


    if ((free_mem[i]>tmp_max_free_mem)); then

        tmp_max_free_mem=${free_mem[i]}
        tmp_max_free_mem_host=${host_name[i]}

    fi


done

echo "$tmp_min_free_mem_host $tmp_min_free_mem"
echo "$tmp_max_free_mem_host $tmp_max_free_mem"

编辑:评论后第一个答案显示了如何保持最小值和最大值,以整理整个表格

# sort to a file
for ((i=0;i<${#host_name[@]};i+=1)); do
    echo "$i,${host_name[i]},${free_mem[i]}"
done | sort -t, -nk3 > sorted.txt

# loop read the file to feed new arrays
new_hosts=()
new_freemem=()
while IFS=, read index host freemem; do
    new_hosts+=("$host")
    new_freemem+=("$freemem")        
done < sorted.txt