ia64/xen-unstable

view tools/xm-test/lib/XmTestReport/OSReport.py @ 12765:43671a60b0f1

Merge
author Alastair Tse <atse@xensource.com>
date Fri Dec 01 18:33:11 2006 +0000 (2006-12-01)
parents 4d5e9f9caed2
children
line source
1 #!/usr/bin/python
3 """
4 OSReport.py - Handles the gathering and xml-formatting of operating
5 system environment information.
7 Copyright (C) International Business Machines Corp., 2005
8 Author: Dan Smith <danms@us.ibm.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; under version 2 of the License.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 """
25 import utils
27 import posix
28 import re
29 import os
30 import commands
31 import sys
32 import arch
34 class Machine:
36 def __parseInfoLine(self, line):
37 if ":" in line:
38 name, value = line.split(":", 1)
39 name = name.strip()
40 value = value.strip()
42 name = re.sub(" ", "_", name)
44 return name, value
45 else:
46 return None, None
48 def __getCpuInfo(self, values):
50 processors = 0
51 cpuinfo = file("/proc/cpuinfo")
53 if not cpuinfo:
54 return "Unable to read /proc/cpuinfo"
56 lines = cpuinfo.readlines()
58 for l in lines:
59 name, value = self.__parseInfoLine(l)
61 if name in values.keys():
62 values[name] = value
64 if name == "processor":
65 processors += 1
67 values["dom0procs"] = str(processors)
69 return values
71 def __getXenInfo(self, values):
73 status, output = commands.getstatusoutput("xm info")
74 if status != 0:
75 self.errors += 1
76 return values
78 lines = output.split("\n")
80 for l in lines:
81 name, value = self.__parseInfoLine(l)
83 if name in values.keys():
84 values[name] = value
86 return values
88 def __init__(self):
90 self.values = {}
91 self.errors = 0
93 xenValues = {"nr_cpus" : "Unknown",
94 "nr_nodes" : "Unknown",
95 "sockets_per_node" : "Unknown",
96 "cores_per_socket" : "Unknown",
97 "threads_per_core" : "Unknown",
98 "cpu_mhz" : "Unknown",
99 "total_memory" : "Unknown"}
101 xen = self.__getXenInfo(xenValues)
102 cpu = self.__getCpuInfo(arch.cpuValues)
104 for k in xen.keys():
105 self.values[k] = xen[k]
106 if xen[k] == "Unknown":
107 self.errors += 1
109 for k in cpu.keys():
110 self.values[k] = cpu[k]
111 if cpu[k] == "Unknown":
112 self.errors += 1
115 def __str__(self):
116 string = "<machine>\n"
118 for k in self.values.keys():
119 string += " " + utils.tagify(k, self.values[k]) + "\n"
121 string += "</machine>\n"
123 return string
125 class OperatingSystem:
127 def __redhatStyleRelease(self):
128 rFile = None
130 if os.access("/etc/redhat-release", os.R_OK):
131 rFile = file("/etc/redhat-release")
132 if os.access("/etc/SuSe-release", os.R_OK):
133 rFile = file("/etc/SuSe-release")
134 if os.access("/etc/SuSE-release", os.R_OK):
135 rFile = file("/etc/SuSE-release")
136 if os.access("/etc/mandrake-release", os.R_OK):
137 rFile = file("/etc/mandrake-release")
139 if not rFile:
140 return None, None
142 rLine = rFile.readline()
143 rFile.close()
145 match = re.match("^([^0-9]+)([0-9\.]+).*$", rLine)
146 if match:
147 return match.group(1), match.group(2)
148 else:
149 return None, None
151 def __debianStyleRelease(self):
152 if os.access("/etc/debian_version", os.R_OK):
153 rFile = file("/etc/debian_version")
154 else:
155 rFile = None
157 if not rFile:
158 return None, None
160 line = rFile.readline()
161 return "Debian", line.rstrip("\n");
163 def __lsbStyleRelease(self):
164 if os.access("/etc/lsb-release", os.R_OK):
165 rFile = file("/etc/lsb-release")
166 else:
167 rFile = None
169 if not rFile:
170 return None, None
172 lines = rFile.readlines()
174 vendor = None
175 version = None
177 for l in lines:
178 match = re.match("^DISTRIB_ID=(.*)$", l)
179 if match:
180 vendor = match.group(1)
181 match = re.match("^DISTRIB_RELEASE=(.*)$", l)
182 if match:
183 version = match.group(1)
185 return vendor, version
187 def __init__(self):
189 self.values = {}
190 self.errors = 0
192 # Try to resolve the vendor and version information
193 # for the distro we're running on
194 vendor = None
195 version = None
196 for r in [self.__redhatStyleRelease, self.__debianStyleRelease, self.__lsbStyleRelease]:
197 vendor, version = r()
198 if vendor and version:
199 break
201 self.values["vendor"] = vendor or "Unknown vendor"
202 self.values["version"] = version or "Unknown version"
204 self.values["name"], nodename, release, version, self.values["arch"] = posix.uname()
206 for k in self.values.keys():
207 if not self.values[k]:
208 self.errors += 1
210 def __str__(self):
211 string = "<os>\n"
213 for k in self.values.keys():
214 string += " " + utils.tagify(k, self.values[k]) + "\n"
216 string += "</os>\n"
218 return string
221 class OSReport:
223 def __init__(self):
225 self.reports = {}
226 self.reports["os"] = OperatingSystem()
227 self.reports["machine"] = Machine()
228 self.errors = 0
230 for k in self.reports.keys():
231 self.errors += self.reports[k].errors
233 def __str__(self):
235 string = ""
237 for k in self.reports.keys():
238 string += str(self.reports[k])
240 return string
242 if __name__ == "__main__":
243 r = OSReport()
245 print str(r)
247 sys.exit(r.errors)