ia64/xen-unstable

view tools/xm-test/lib/XmTestReport/OSReport.py @ 7757:139aabe357a4

Various chmod a+x.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Nov 10 12:00:09 2005 +0100 (2005-11-10)
parents ecb5997f7b25
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
33 class Machine:
35 def __parseInfoLine(self, line):
36 if ":" in line:
37 name, value = line.split(":", 1)
38 name = name.strip()
39 value = value.strip()
41 name = re.sub(" ", "_", name)
43 return name, value
44 else:
45 return None, None
47 def __getCpuInfo(self, values):
49 processors = 0
50 cpuinfo = file("/proc/cpuinfo")
52 if not cpuinfo:
53 return "Unable to read /proc/cpuinfo"
55 lines = cpuinfo.readlines()
57 for l in lines:
58 name, value = self.__parseInfoLine(l)
60 if name in values.keys():
61 values[name] = value
63 if name == "processor":
64 processors += 1
66 values["dom0procs"] = str(processors)
68 return values
70 def __getXenInfo(self, values):
72 status, output = commands.getstatusoutput("xm info")
73 if status != 0:
74 self.errors += 1
75 return values
77 lines = output.split("\n")
79 for l in lines:
80 name, value = self.__parseInfoLine(l)
82 if name in values.keys():
83 values[name] = value
85 return values
87 def __init__(self):
89 self.values = {}
90 self.errors = 0
92 cpuValues = {"model_name" : "Unknown",
93 "flags" : "Unknown"}
94 xenValues = {"nr_cpus" : "Unknown",
95 "nr_nodes" : "Unknown",
96 "sockets_per_node" : "Unknown",
97 "cores_per_socket" : "Unknown",
98 "threads_per_core" : "Unknown",
99 "cpu_mhz" : "Unknown",
100 "memory" : "Unknown"}
102 xen = self.__getXenInfo(xenValues)
103 cpu = self.__getCpuInfo(cpuValues)
105 for k in xen.keys():
106 self.values[k] = xen[k]
107 if xen[k] == "Unknown":
108 self.errors += 1
110 for k in cpu.keys():
111 self.values[k] = cpu[k]
112 if cpu[k] == "Unknown":
113 self.errors += 1
116 def __str__(self):
117 string = "<machine>\n"
119 for k in self.values.keys():
120 string += " " + utils.tagify(k, self.values[k]) + "\n"
122 string += "</machine>\n"
124 return string
126 class OperatingSystem:
128 def __redhatStyleRelease(self):
129 rFile = None
131 if os.access("/etc/redhat-release", os.R_OK):
132 rFile = file("/etc/redhat-release")
133 if os.access("/etc/SuSe-release", os.R_OK):
134 rFile = file("/etc/SuSe-release")
135 if os.access("/etc/SuSE-release", os.R_OK):
136 rFile = file("/etc/SuSE-release")
137 if os.access("/etc/mandrake-release", os.R_OK):
138 rFile = file("/etc/mandrake-release")
140 if not rFile:
141 return None, None
143 rLine = rFile.readline()
144 rFile.close()
146 match = re.match("^([^0-9]+)([0-9\.]+).*$", rLine)
147 if match:
148 return match.group(1), match.group(2)
149 else:
150 return None, None
152 def __debianStyleRelease(self):
153 if os.access("/etc/debian_version", os.R_OK):
154 rFile = file("/etc/debian_version")
155 else:
156 rFile = None
158 if not rFile:
159 return None, None
161 line = rFile.readline()
162 return "Debian", line.rstrip("\n");
164 def __lsbStyleRelease(self):
165 if os.access("/etc/lsb-release", os.R_OK):
166 rFile = file("/etc/lsb-release")
167 else:
168 rFile = None
170 if not rFile:
171 return None, None
173 lines = rFile.readlines()
175 vendor = None
176 version = None
178 for l in lines:
179 match = re.match("^DISTRIB_ID=(.*)$", l)
180 if match:
181 vendor = match.group(1)
182 match = re.match("^DISTRIB_RELEASE=(.*)$", l)
183 if match:
184 version = match.group(1)
186 return vendor, version
188 def __init__(self):
190 self.values = {}
191 self.errors = 0
193 # Try to resolve the vendor and version information
194 # for the distro we're running on
195 vendor = None
196 version = None
197 for r in [self.__redhatStyleRelease, self.__debianStyleRelease, self.__lsbStyleRelease]:
198 vendor, version = r()
199 if vendor and version:
200 break
202 self.values["vendor"] = vendor or "Unknown vendor"
203 self.values["version"] = version or "Unknown version"
205 self.values["name"], nodename, release, version, self.values["arch"] = posix.uname()
207 for k in self.values.keys():
208 if not self.values[k]:
209 self.errors += 1
211 def __str__(self):
212 string = "<os>\n"
214 for k in self.values.keys():
215 string += " " + utils.tagify(k, self.values[k]) + "\n"
217 string += "</os>\n"
219 return string
222 class OSReport:
224 def __init__(self):
226 self.reports = {}
227 self.reports["os"] = OperatingSystem()
228 self.reports["machine"] = Machine()
229 self.errors = 0
231 for k in self.reports.keys():
232 self.errors += self.reports[k].errors
234 def __str__(self):
236 string = ""
238 for k in self.reports.keys():
239 string += str(self.reports[k])
241 return string
243 if __name__ == "__main__":
244 r = OSReport()
246 print str(r)
248 sys.exit(r.errors)