46b182132b1d8fb4ef0b39c1557478316063eb97
[certmaster.git] / certmaster / minion / modules / hardware.py
1 ##
2 ## Hardware profiler plugin
3 ## requires the "smolt" client package be installed
4 ## but also relies on lspci for some things
5 ##
6 ## Copyright 2007, Red Hat, Inc
7 ## Michael DeHaan <mdehaan@redhat.com>
8 ##
9 ## This software may be freely redistributed under the terms of the GNU
10 ## general public license.
11 ##
12 ## You should have received a copy of the GNU General Public License
13 ## along with this program; if not, write to the Free Software
14 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 ##
16
17
18 # other modules
19 import sys
20
21 # our modules
22 import sub_process
23 import func_module
24
25 # =================================
26
27 class HardwareModule(func_module.FuncModule):
28
29 version = "0.0.1"
30 api_version = "0.0.1"
31 description = "Hardware profiler."
32
33 def hal_info(self):
34 """
35 Returns the output of lshal, but split up into seperate devices
36 for easier parsing. Each device is a entry in the return hash.
37 """
38
39 cmd = sub_process.Popen(["/usr/bin/lshal"],shell=False,stdout=sub_process.PIPE)
40 data = cmd.communicate()[0]
41
42 data = data.split("\n")
43
44 results = {}
45 current = ""
46 label = data[0]
47 for d in data:
48 if d == '':
49 results[label] = current
50 current = ""
51 label = ""
52 else:
53 if label == "":
54 label = d
55 current = current + d
56
57 return results
58
59 def inventory(self):
60 data = hw_info(with_devices=True)
61 # remove bogomips because it keeps changing for laptops
62 # and makes inventory tracking noisy
63 if data.has_key("bogomips"):
64 del data["bogomips"]
65 return data
66
67 def info(self,with_devices=True):
68 """
69 Returns a struct of hardware information. By default, this pulls down
70 all of the devices. If you don't care about them, set with_devices to
71 False.
72 """
73 return hw_info(with_devices)
74
75 # =================================
76
77 def hw_info(with_devices=True):
78
79 # this may fail if smolt is not installed. That's ok. hal_info will
80 # still work.
81
82 # hack: smolt is not installed in site-packages
83 sys.path.append("/usr/share/smolt/client")
84 import smolt
85
86 hardware = smolt.Hardware()
87 host = hardware.host
88
89 # NOTE: casting is needed because these are DBusStrings, not real strings
90 data = {
91 'os' : str(host.os),
92 'defaultRunlevel' : str(host.defaultRunlevel),
93 'bogomips' : str(host.bogomips),
94 'cpuVendor' : str(host.cpuVendor),
95 'cpuModel' : str(host.cpuModel),
96 'numCpus' : str(host.numCpus),
97 'cpuSpeed' : str(host.cpuSpeed),
98 'systemMemory' : str(host.systemMemory),
99 'systemSwap' : str(host.systemSwap),
100 'kernelVersion' : str(host.kernelVersion),
101 'language' : str(host.language),
102 'platform' : str(host.platform),
103 'systemVendor' : str(host.systemVendor),
104 'systemModel' : str(host.systemModel),
105 'formfactor' : str(host.formfactor),
106 'selinux_enabled' : str(host.selinux_enabled),
107 'selinux_enforce' : str(host.selinux_enforce)
108 }
109
110 # if no hardware info requested, just return the above bits
111 if not with_devices:
112 return data
113
114 collection = data["devices"] = []
115
116 for item in hardware.deviceIter():
117
118 (VendorID,DeviceID,SubsysVendorID,SubsysDeviceID,Bus,Driver,Type,Description) = item
119
120 collection.append({
121 "VendorID" : str(VendorID),
122 "DeviceID" : str(DeviceID),
123 "SubsysVendorID" : str(SubsysVendorID),
124 "Bus" : str(Bus),
125 "Driver" : str(Driver),
126 "Type" : str(Type),
127 "Description" : str(Description)
128 })
129
130 return data