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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
#!/usr/bin/python
"""
Copyright (C) International Business Machines Corp., 2005
Author: Dan Smith <danms@us.ibm.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; under version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
##
## These are miscellaneous utility functions that query xm
##
import commands
import re
import os
import time
from Test import *;
class XmError(Exception):
def __init__(self, msg, trace="", status=0):
self.msg = msg
self.trace = trace
try:
self.status = int(status)
except Exception, e:
self.status = -1
def __str__(self):
return trace
def domid(name):
status, output = traceCommand("xm domid " + name);
if status != 0 or "Traceback" in output:
return -1
if output == "None":
return -1
try:
return int(output)
except:
raise XmError("xm domid failed", trace=output, status=status)
def domname(id):
status, output = traceCommand("xm domname " + str(id));
return output;
def isDomainRunning(domain):
id = domid(domain);
if id == -1:
return False;
else:
return True;
def getRunningDomains():
status, output = traceCommand("xm list");
if status != 0 or "Traceback" in output:
raise XmError("xm failed", trace=output, status=status)
lines = output.splitlines();
domains = [];
for l in lines[1:]:
elms = l.split(" ", 1);
domains.append(elms[0]);
return domains;
def destroyDomU(name):
status, output = traceCommand("xm destroy " + name, logOutput=False);
return status;
def destroyAllDomUs():
if verbose:
print "*** Cleaning all running domU's"
attempt = 0
trying = True
while trying:
try:
attempt += 1
domainList = getRunningDomains()
trying = False
except XmError, e:
if attempt >= 10:
FAIL("XM-TEST: xm list not responding")
time.sleep(1)
print e.trace
print "!!! Trying again to get a clean domain list..."
for d in domainList:
if not d == "Domain-0":
destroyDomU(d);
if verbose:
print "*** Finished cleaning domUs"
def getDomMem(domain):
status, output = traceCommand("xm list")
if status != 0:
if verbose:
print "xm list failed with %i" % status
return None
lines = re.split("\n", output)
for line in lines:
fields = re.sub(" +", " ", line).split()
if domain.isdigit():
if fields[1] == domain:
return int(fields[2])
else:
if fields[0] == domain:
return int(fields[2])
if verbose:
print "Did not find domain " + str(domain)
return None
def getDomInfo(domain, key, opts=None):
if opts:
cmd = "xm list %s" % opts
else:
cmd = "xm list"
status, output = traceCommand(cmd)
if status != 0:
if verbose:
print "xm list failed with %i" % status
return None
lines = output.split("\n")
# Get the key values from the first line headers
cleanHeader = re.sub("\([^\)]+\)", "", lines[0])
colHeaders = re.split(" +", cleanHeader)
doms = {}
for line in lines[1:]:
domValues = {}
values = re.split(" +", line)
i = 1
for value in values[1:]:
domValues[colHeaders[i]] = value
i += 1
doms[values[0]] = domValues
if doms.has_key(domain) and doms[domain].has_key(key):
return doms[domain].get(key)
return ""
def getVcpuInfo(domain):
status, output = traceCommand("xm vcpu-list %s" % domain)
lines = output.split("\n")
vcpus = {}
for line in lines[1:]:
cols = re.split(" +", line)
if cols[3] == '-':
vcpus[int(cols[2])] = None
else:
vcpus[int(cols[2])] = int(cols[3])
return vcpus
def getInfo(key):
info = {}
status, output = traceCommand("xm info")
lines = output.split("\n")
for line in lines:
match = re.match("^([A-z_]+)[^:]*: (.*)$", line)
if match:
info[match.group(1)] = match.group(2)
if info.has_key(key):
return info[key]
else:
return ""
def restartXend():
if verbose:
print "*** Restarting xend ..."
if os.access("/etc/init.d/xend", os.X_OK):
status, output = traceCommand("/etc/init.d/xend stop")
time.sleep(1)
status, output = traceCommand("/etc/init.d/xend start")
return status
else:
status, output = traceCommand("xend stop")
time.sleep(1)
status, output = traceCommand("xend start")
return status
def smpConcurrencyLevel():
nr_cpus = int(getInfo("nr_cpus"))
return nr_cpus
if __name__ == "__main__":
if isDomainRunning("0"):
print "Domain-0 is running; I must be working!"
else:
print "Domain-0 is not running; I may be broken!"
mem = getDomMem("Domain-0")
if not mem:
print "Failed to get memory for Domain-0!"
else:
print "Domain-0 mem: %i" % mem
cpu = getDomInfo("Domain-0", "CPU")
state = getDomInfo("Domain-0", "State")
print "Domain-0 CPU: " + cpu
print "Domain-0 state: " + state
v = getVcpuInfo("Domain-0")
for key in v.keys():
print "VCPU%i is on CPU %i" % (key, v[key])
|