forked from samli008/kvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vminfo
208 lines (187 loc) · 6.2 KB
/
vminfo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
#Time:2015-3-20
#Author:www.isjian.com
#Version:2.1
#Update:2016-03-15
###说明:
#1.在centos6.x平台上测试通过.
#2.列出当前宿主机上所有运行中的虚拟机(KVM)详细信息.
###更新日志
## 2016-01-25
#1.更新版本为2.1
#2.增加"-s"参数,可以计算出所有虚机占用的总虚拟cpu,虚拟内存,虚机磁盘,以及当前所使用的物理磁盘空间
## 2016-01-21
#1.更新版本为2.0
#2.使用"-i"参数可获取虚拟机ip地址,此命令运行后会提示用户是否进行arp广播,若选择不进行arp广播,可能无法获取某些虚机ip地址
#注意:此方法首先利用arp广播更新系统arp表,再由虚机mac地址匹配出其ip地址,若不希望进行arp广播,可在随后提示中使用"n"
#3.无法获取ip地址的虚拟机会显示"noip".
#4.使用"-d"参数可显示虚机所有虚拟磁盘文件及其大小,大小格式为"[a][b]",a值表示此虚拟磁盘文件所占物理硬盘大小,b值表示虚拟磁盘虚拟大小
#5.使用"-h"可显示帮助菜单
## 2015-11-24
#1.更新版本为1.6.
#2.使用"-i"参数可显示虚拟机ip地址.
#3.无法获取ip地址的虚拟机会用"-"代替.
##2015-9-15
#1.更新版本为1.5
#2.可显示虚机每块磁盘大小.
#3.默认只列出虚机的根磁盘,加上"-d"参数可列出所有磁盘.
#get vms name and print to /tmp/vmhost.txt
#"break" can shorten the awk running time,even though very short
function Get_Vmnames() {
ps aux | grep "qemu-kvm " | grep -v 'grep' | \
awk '{
for(i=1;i<=NF;i++) {
if($i == "-name"){ print $(i+1);break }
}
}' >> /tmp/vmhost.txt
}
#Use of a domain name as the parameter and output it's %cpu,%mem,port on one line
function Get_Consoleinfo () {
ps aux | grep "qemu-kvm" | grep -v grep | grep " \-name $1 " | egrep "( \-spice port| \-vnc )" | \
awk '{
for(i=1;i<=NF;i++){
if($i == "-spice") {
Port=$(i+1)
print $2,$3,$4,"spice:"substr(Port,1,match(Port,/,addr.*/)-1) }
else if($i == "-vnc") {
Port=$(i+1);
print $2,$3,$4,"vnc:"substr(Port,match(Port,/:.*/)+1)+5900 }
}
}' | sed -r "s/(port=|tls-port=)//g"
}
#get all ip address from host and execute arp broadcast
function Get_Arptable() {
read -p "Whether to use arp broadcast(y|n):" whether
if [ "${whether}" == "y" ];then
IpLists=`ifconfig | awk '/inet addr/ && !/127.0.0.1/ {print substr($2,6)}' | cut -d. -f 1-3`
for iip in $IpLists
do
for ij in `seq 1 254`;do
( ping -W 1 -c 1 ${iip}.${ij} &>/dev/null) &
done
wait
done
fi
}
#get vhost cpu,memory
function Get_Vcpuvmems() {
virsh dominfo "$1" | \
awk '
/^CPU\(s\)/ {print $2}
/^Used memory/ {print $3/1024/1024"G"}
' | xargs
}
#get vhost block
#awk: change the unit "M" to "G"
function Get_Vmblk() {
blklist=$(virsh domblklist "$1" | awk 'NR>=3{if($2 != "-" && NF>=1) print $1":"$2}' | xargs)
declare -i num && num=1
for ii in ${blklist}
do
blkbasename=`basename ${ii}`
qemu-img info $(echo $ii | awk -F '[:]' '{print $2}') | \
awk -v a="$ii" -v b="${blkbasename}" -v c="${num}" -v d="f${alter}" '
/^virtual size:/ {Vsize=$3}
/^disk size:/ {if($3 ~ /M/ && d == "f-s") Dsize=int(substr($3,1,match($3,/M/)-1)/1024*100)/100"G";else Dsize=$3}
END{
if(d == "f-s") print "d:"Dsize":v:"Vsize
else if(d == "f-d") print a"["Dsize"]""["Vsize"]"
else if(c >= 2) print "["Dsize"]""["Vsize"]"
else print a"["Dsize"]""["Vsize"]"
}'
num+=1
done | xargs | sed "s/ /,/g"
}
#get vmip
#can have multiple ip
function Get_Vmip() {
vm_mac="`virsh domiflist "$1" | awk 'NR>2{if($0 != "") print $5}' | xargs`"
vm_mac=${vm_mac:-no_mac}
iplists=`
for ik in ${vm_mac}
do
arp -n | grep -i "${ik}" | awk '{print $1}'
done | xargs | sed "s/ /,/g"
`
echo ${iplists:-noip}
}
function usage() {
echo "option args: [-d | -i | -s ]"
echo "usage:"
echo "-d display all the disks and it's size"
echo "-i display the vm ip"
echo "-s display the total resources"
echo "-h display this help"
}
function format_line() {
Get_Vmnames
[ "${alter}" == "-i" ] && Get_Arptable
for i in `cat /tmp/vmhost.txt | awk '{print $1}'`;do
if [ "${alter}" == "-d" ];then
blkinfo_temp="`Get_Vmblk ${i}`"
blkinfo=$(echo ${blkinfo_temp} | sed -r 's/\//\\\//g')
sed -i -r "/^${i}/s/.*/& ${blkinfo}/g" /tmp/vmhost.txt
elif [ "${alter}" == "-i" ];then
vmipinfo="`Get_Vmip ${i}`"
sed -i -r "/^${i}/s/.*/& ${vmipinfo}/g" /tmp/vmhost.txt
elif [ "${alter}" == "-s" ];then
vcpumem="`Get_Vcpuvmems ${i}`"
blkinfo_temp="`Get_Vmblk ${i}`"
blkinfo=$(echo ${blkinfo_temp} | sed -r 's/\//\\\//g')
sed -i -r "/^${i}/s/.*/& ${vcpumem} ${blkinfo}/g" /tmp/vmhost.txt
else
consoleinfo="`Get_Consoleinfo ${i}`"
vcpumem="`Get_Vcpuvmems ${i}`"
blkinfo_temp="`Get_Vmblk ${i}`"
blkinfo=$(echo ${blkinfo_temp} | sed -r 's/\//\\\//g')
sed -i -r "/^${i}/s/.*/& ${consoleinfo} ${vcpumem} ${blkinfo}/g" /tmp/vmhost.txt
fi
done
}
function format_printf() {
/bin/rm -f /tmp/vmhost.txt
format_line
#Get the first output field width according to the size of the domain name
Length_tmp=$(cat /tmp/vmhost.txt | awk 'BEGIN{A=0} {if(length($1)>=A) A=length($1)} END{print A}')
Length=$((Length_tmp + 5))
case ${alter} in
-d)
cat /tmp/vmhost.txt | \
awk -v L=${Length} '
BEGIN{printf "%-*s %-15s\n",L,"VHOSTS","Vdisks";for(i=1;i<=46;i++)printf "---";print ""}
{printf "%-*s %-15s\n",L,$1,$2}'
;;
-i)
cat /tmp/vmhost.txt | \
awk -v L=${Length} '
BEGIN{printf "%-*s %-15s\n",L,"VHOSTS","Vip";for(i=1;i<=14;i++)printf "---";print ""}
{printf "%-*s %-15s\n",L,$1,$2}'
;;
-s)
cat /tmp/vmhost.txt | sed "s/G//g" | \
awk -F'[ :,]' '
BEGIN{ a=0;b=0 } {
for(i=1;i<=NF;i++){
if($i == "d") a+=$(i+1);
else if($i == "v") b+=$(i+1)
};
c+=$2;
m+=$3 }
END { printf "%-20s %-20s %-20s %-20s\n","Vcpus","Vmems","DiskUsage","VdiskUsage";for(i=1;i<=34;i++)printf "---";print ""}
END { printf "%-20s %-20s %-20s %-20s\n",c,m,b"G",a"G" }
'
;;
-h)
usage
;;
*)
cat /tmp/vmhost.txt | \
awk -v L=${Length} '
BEGIN{printf "%-*s %-7s %-6s %-6s %-12s %-6s %-6s %-20s\n",L,"VHOSTS","PID","%CPU","%MEM","PORT","Vcpus","Vmems","Vdisks";for(i=1;i<=46;i++)printf "---";print ""}
{printf "%-*s %-7s %-6s %-6s %-12s %-6s %-6s %-20s\n",L,$1,$2,$3,$4,$5,$6,$7,$8}'
;;
esac
/bin/rm -f /tmp/vmhost.txt
}
alter=$1
format_printf