1 | #!/usr/bin/python |
---|
2 | |
---|
3 | """ |
---|
4 | OSReport.py - Handles the gathering and xml-formatting of operating |
---|
5 | system environment information. |
---|
6 | |
---|
7 | Copyright (C) International Business Machines Corp., 2005 |
---|
8 | Author: Dan Smith <danms@us.ibm.com> |
---|
9 | |
---|
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. |
---|
13 | |
---|
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. |
---|
18 | |
---|
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 |
---|
22 | |
---|
23 | """ |
---|
24 | |
---|
25 | import utils |
---|
26 | |
---|
27 | import posix |
---|
28 | import re |
---|
29 | import os |
---|
30 | import commands |
---|
31 | import sys |
---|
32 | import arch |
---|
33 | |
---|
34 | class Machine: |
---|
35 | |
---|
36 | def __parseInfoLine(self, line): |
---|
37 | if ":" in line: |
---|
38 | name, value = line.split(":", 1) |
---|
39 | name = name.strip() |
---|
40 | value = value.strip() |
---|
41 | |
---|
42 | name = re.sub(" ", "_", name) |
---|
43 | |
---|
44 | return name, value |
---|
45 | else: |
---|
46 | return None, None |
---|
47 | |
---|
48 | def __getCpuInfo(self, values): |
---|
49 | |
---|
50 | processors = 0 |
---|
51 | cpuinfo = file("/proc/cpuinfo") |
---|
52 | |
---|
53 | if not cpuinfo: |
---|
54 | return "Unable to read /proc/cpuinfo" |
---|
55 | |
---|
56 | lines = cpuinfo.readlines() |
---|
57 | |
---|
58 | for l in lines: |
---|
59 | name, value = self.__parseInfoLine(l) |
---|
60 | |
---|
61 | if name in values.keys(): |
---|
62 | values[name] = value |
---|
63 | |
---|
64 | if name == "processor": |
---|
65 | processors += 1 |
---|
66 | |
---|
67 | values["dom0procs"] = str(processors) |
---|
68 | |
---|
69 | return values |
---|
70 | |
---|
71 | def __getXenInfo(self, values): |
---|
72 | |
---|
73 | status, output = commands.getstatusoutput("xm info") |
---|
74 | if status != 0: |
---|
75 | self.errors += 1 |
---|
76 | return values |
---|
77 | |
---|
78 | lines = output.split("\n") |
---|
79 | |
---|
80 | for l in lines: |
---|
81 | name, value = self.__parseInfoLine(l) |
---|
82 | |
---|
83 | if name in values.keys(): |
---|
84 | values[name] = value |
---|
85 | |
---|
86 | return values |
---|
87 | |
---|
88 | def __init__(self): |
---|
89 | |
---|
90 | self.values = {} |
---|
91 | self.errors = 0 |
---|
92 | |
---|
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"} |
---|
100 | |
---|
101 | xen = self.__getXenInfo(xenValues) |
---|
102 | cpu = self.__getCpuInfo(arch.cpuValues) |
---|
103 | |
---|
104 | for k in xen.keys(): |
---|
105 | self.values[k] = xen[k] |
---|
106 | if xen[k] == "Unknown": |
---|
107 | self.errors += 1 |
---|
108 | |
---|
109 | for k in cpu.keys(): |
---|
110 | self.values[k] = cpu[k] |
---|
111 | if cpu[k] == "Unknown": |
---|
112 | self.errors += 1 |
---|
113 | |
---|
114 | |
---|
115 | def __str__(self): |
---|
116 | string = "<machine>\n" |
---|
117 | |
---|
118 | for k in self.values.keys(): |
---|
119 | string += " " + utils.tagify(k, self.values[k]) + "\n" |
---|
120 | |
---|
121 | string += "</machine>\n" |
---|
122 | |
---|
123 | return string |
---|
124 | |
---|
125 | class OperatingSystem: |
---|
126 | |
---|
127 | def __redhatStyleRelease(self): |
---|
128 | rFile = None |
---|
129 | |
---|
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") |
---|
138 | |
---|
139 | if not rFile: |
---|
140 | return None, None |
---|
141 | |
---|
142 | rLine = rFile.readline() |
---|
143 | rFile.close() |
---|
144 | |
---|
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 |
---|
150 | |
---|
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 |
---|
156 | |
---|
157 | if not rFile: |
---|
158 | return None, None |
---|
159 | |
---|
160 | line = rFile.readline() |
---|
161 | return "Debian", line.rstrip("\n"); |
---|
162 | |
---|
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 |
---|
168 | |
---|
169 | if not rFile: |
---|
170 | return None, None |
---|
171 | |
---|
172 | lines = rFile.readlines() |
---|
173 | |
---|
174 | vendor = None |
---|
175 | version = None |
---|
176 | |
---|
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) |
---|
184 | |
---|
185 | return vendor, version |
---|
186 | |
---|
187 | def __init__(self): |
---|
188 | |
---|
189 | self.values = {} |
---|
190 | self.errors = 0 |
---|
191 | |
---|
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 |
---|
200 | |
---|
201 | self.values["vendor"] = vendor or "Unknown vendor" |
---|
202 | self.values["version"] = version or "Unknown version" |
---|
203 | |
---|
204 | self.values["name"], nodename, release, version, self.values["arch"] = posix.uname() |
---|
205 | |
---|
206 | for k in self.values.keys(): |
---|
207 | if not self.values[k]: |
---|
208 | self.errors += 1 |
---|
209 | |
---|
210 | def __str__(self): |
---|
211 | string = "<os>\n" |
---|
212 | |
---|
213 | for k in self.values.keys(): |
---|
214 | string += " " + utils.tagify(k, self.values[k]) + "\n" |
---|
215 | |
---|
216 | string += "</os>\n" |
---|
217 | |
---|
218 | return string |
---|
219 | |
---|
220 | |
---|
221 | class OSReport: |
---|
222 | |
---|
223 | def __init__(self): |
---|
224 | |
---|
225 | self.reports = {} |
---|
226 | self.reports["os"] = OperatingSystem() |
---|
227 | self.reports["machine"] = Machine() |
---|
228 | self.errors = 0 |
---|
229 | |
---|
230 | for k in self.reports.keys(): |
---|
231 | self.errors += self.reports[k].errors |
---|
232 | |
---|
233 | def __str__(self): |
---|
234 | |
---|
235 | string = "" |
---|
236 | |
---|
237 | for k in self.reports.keys(): |
---|
238 | string += str(self.reports[k]) |
---|
239 | |
---|
240 | return string |
---|
241 | |
---|
242 | if __name__ == "__main__": |
---|
243 | r = OSReport() |
---|
244 | |
---|
245 | print str(r) |
---|
246 | |
---|
247 | sys.exit(r.errors) |
---|
248 | |
---|
249 | |
---|