跳转至

01 各个物理机CPU MEM 统计

脚本说明

  • vm_running_info.sh :统计物理机上 虚拟机的 内存CPU 设置信息;
  • virsh_running_count.sh:遍历 物理机列表【wlj_list】执行vm_running_info.sh 脚本,统计所有物理机上虚拟机信息;
  • wlj_list:物理机列表;

脚本详见

点击查看: vm_running_info.sh
#!/bin/bash

# Max memory:
#for i in `virsh list --all | grep running | awk '{print $2}'`; do echo -n "$i: "; virsh dominfo $i | awk 'NR==8' | awk '{print $3/1024/1024" G"}'; done

# Used memory:

#for i in `virsh list --all | grep running | awk '{print $2}'`; do echo -n "$i: "; virsh dominfo $i | awk 'NR==9' | awk '{print $3/1024/1024" G"}'; done
#for i in `virsh list --all | grep running | awk '{print $2}'`; do echo -n "$i: "; virsh dominfo $i | awk 'NR==9' | awk '{print $2/1024/1024" G"}'; done

count_mem(){
total=0

for i in $(virsh list --all | grep running | awk '{print $2}'); do
    mem=$(virsh dominfo $i | awk 'NR==9' | awk '{print $2}')
    mem_g=$(echo "scale=3; $mem / 1024 / 1024" | bc)
    echo -n "$i: "
    echo "$mem_g G"
    total=$(echo "scale=3; $total + $mem_g" | bc)
done
echo "------------------------"
echo "Total MEM used: $total G"
}

# CPU(s):

#for i in `virsh list --all | grep running | awk '{print $2}'`; do echo -n "$i: "; virsh dominfo $i | awk 'NR==6' | awk '{print $2" C"}'; done
count_cpu(){
total=0
for i in $(virsh list --all | grep running | awk '{print $2}'); do
    cpu=$(virsh dominfo $i | awk 'NR==6 {print $2}')
    echo "$i: $cpu C"
    total=$((total + cpu))
done
echo "------------------------"
echo "Total CPU used: $total C"
}


#count_cpu
#count_mem


if [ $# -ne 1 ]; then
    echo "用法:$0 [-mem|-cpu]"
    echo "  -mem: 统计内存使用情况"
    echo "  -cpu: 统计CPU使用情况"
    exit 1
fi

case "$1" in
    -mem)
        #echo "count_mem"
        count_mem
        ;;
    -cpu)
        #echo "count_cpu"
        count_cpu
        ;;
    *)
        echo "错误:无效选项 '$1'"
        echo "用法:$0 [-mem|-cpu]"
        exit 1
        ;;
esac
点击查看: virsh_running_count.sh
#!/bin/bash

#ansible wlj_list -m script -a "/opt/virsh_running_use_mem.sh" -i wlj_list -o | awk -v RS='\n' '
#    / => / {
#        host = $1;
#        gsub(/\[|\]/, "", host);
#        result = $0;
#        sub(/^[^=>]+=> /, "");
#    }
#    /"stdout_lines": \[/ {
#        match(result, /"stdout_lines": \[([^]]+)\]/, arr);
#        split(arr[1], lines, /", "/);
#        for (i in lines) {
#            gsub(/"|,/, "", lines[i]);
#            print host " => " lines[i];
#        }
#    }
#'


ansible wlj_list -m script -a "/opt/vm_running_info.sh $1" -i wlj_list -o | awk -v IP="10.0.1.20*" '$0 ~ IP {
    # 提取IP地址
    ip = substr($0, 1, index($0, " | ") - 1)

    # 提取stdout_lines部分
    start = index($0, "\"stdout_lines\": [")
    end = index(substr($0, start), "]}") + start - 1
    stdout = substr($0, start, end - start + 1)

    # 输出格式化结果
    print ip " :\n" stdout
    print "======================="
}' | sed -e 's/"stdout_lines": \[//' -e 's/\]//' -e 's/"//g' -e 's/, /\n/g'
点击查看: wlj_list
1
2
3
4
5
6
7
[wlj_list]
10.0.1.201
10.0.1.202
10.0.1.203
10.0.1.204
10.0.1.205
10.0.1.206

脚本使用说明

统计所有物理机上各虚拟机 MEM设置情况:

virsh_running_count.sh -mem

统计所有物理机上各虚拟机 CPU 设置情况:

virsh_running_count.sh -cpu


返回:常用脚本