]>
git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/system.py
3a62d05528da741d54bf32c9c3c7c0a142be0c80
16 SYS_CLASS_DMI
= "/sys/class/dmi/id"
17 SECRET_ID_FILE
= "/etc/fireinfo-id"
19 INVALID_ID_STRINGS
= (
20 "OEM", "O.E.M.", "o.e.m.",
22 "12345", "54321", "202020",
26 class Singleton(type):
27 def __init__(cls
, name
, bases
, dict):
28 super(Singleton
, cls
).__init
__(name
, bases
, dict)
31 def __call__(cls
, *args
, **kw
):
32 if cls
.instance
is None:
33 cls
.instance
= super(Singleton
, cls
).__call
__(*args
, **kw
)
38 def read_from_file(filename
):
40 Read all data from filename.
42 if not os
.path
.exists(filename
):
46 with
open(filename
) as f
:
47 return f
.read().strip()
52 __metaclass__
= Singleton
59 self
.hypervisor
= hypervisor
.Hypervisor()
66 "vendor" : self
.vendor
,
68 # Indicator if the system is running in a
69 # virtual environment.
70 "virtual" : self
.virtual
,
73 "language" : self
.language
,
76 "release" : self
.release
,
77 "kernel_release" : self
.kernel_release
,
79 "memory" : self
.memory
,
80 "root_size" : self
.root_size
,
84 for device
in self
.devices
:
86 "subsystem" : device
.subsystem
.lower(),
87 "vendor" : device
.vendor
.lower(),
88 "model" : device
.model
.lower(),
89 "deviceclass" : device
.deviceclass
,
90 "driver" : device
.driver
,
93 # PCI devices provide subsystem information, USB don't.
94 if d
["subsystem"] == "pci":
95 d
["sub_model"] = device
.sub_model
96 d
["sub_vendor"] = device
.sub_vendor
98 p
["devices"].append(d
)
102 "vendor" : self
.cpu
.vendor
,
103 "model" : self
.cpu
.model
,
104 "model_string" : self
.cpu
.model_string
,
105 "stepping" : self
.cpu
.stepping
,
106 "flags" : self
.cpu
.flags
,
107 "bogomips" : self
.cpu
.bogomips
,
108 "speed" : self
.cpu
.speed
,
109 "family" : self
.cpu
.family
,
110 "count" : self
.cpu
.count
113 # Only append hypervisor information if we are virtualized.
116 "type" : self
.hypervisor
.type,
117 "vendor" : self
.hypervisor
.vendor
,
122 "profile_version" : PROFILE_VERSION
,
124 # Identification and authorization codes
125 "public_id" : self
.public_id
,
126 "private_id" : self
.private_id
,
129 "secret_id" : self
._unique
_id
,
131 # Actual profile data
143 This returns a globally (hopefully) ID to identify the host
144 later (by request) in the database.
146 public_id
= self
.secret_id
150 return hashlib
.sha1(public_id
).hexdigest()
153 def private_id(self
):
155 The private ID is built out of the _unique_id and used to
156 permit a host to do changes on the database.
158 No one could ever guess this without access to the host.
161 for i
in reversed(self
.secret_id
):
167 return hashlib
.sha1(private_id
).hexdigest()
172 Read a "secret" ID from a file if available
173 or calculate it from the hardware.
175 if os
.path
.exists(SECRET_ID_FILE
):
176 return read_from_file(SECRET_ID_FILE
)
178 return hashlib
.sha1(self
._unique
_id
).hexdigest()
181 def _unique_id(self
):
183 This is a helper ID which is generated out of some hardware information
184 that is considered to be constant over a PC's lifetime.
186 None of the data here is ever sent to the server.
190 # Virtual machines (for example) and some boards have a UUID
191 # which is globally unique.
192 for file in ("product_uuid", "product_serial", "chassis_serial"):
193 id = read_from_file(os
.path
.join(SYS_CLASS_DMI
, file))
196 # Sort out all bogous or invalid strings from the list.
202 for i
in INVALID_ID_STRINGS
:
212 # Use serial number from root disk (if available)
213 root_disk_serial
= self
.root_disk_serial
215 ids
.append(root_disk_serial
)
217 # As last resort, we use the UUID from pakfire.
219 id = read_from_file("/opt/pakfire/db/uuid")
226 # Return "unknown" if settings file does not exist.
227 filename
= "/var/ipfire/main/settings"
228 if not os
.path
.exists(filename
):
231 with
open(filename
, "r") as f
:
232 for line
in f
.readlines():
233 key
, val
= line
.split("=", 1)
239 return read_from_file("/etc/system-release")
242 def bios_vendor(self
):
243 return read_from_file("/sys/class/dmi/id/bios_vendor")
248 for file in ("sys_vendor", "board_vendor", "chassis_vendor",):
249 ret
= read_from_file(os
.path
.join(SYS_CLASS_DMI
, file))
258 for file in ("chassis_model", "board_model", "product_name",):
259 ret
= read_from_file(os
.path
.join(SYS_CLASS_DMI
, file))
267 with
open("/proc/meminfo", "r") as f
:
268 firstline
= f
.readline().strip()
269 return int(firstline
.split()[1])
272 def kernel_release(self
):
277 with
open("/etc/mtab", "r") as f
:
278 dev
, mountpoint
, rest
= f
.readline().split(" ",2)
279 if mountpoint
== "/":
281 # cut off all digits at end of string
282 while dev
[-1] in string
.digits
:
288 path
="/sys/block/%s/size" %self
.root_disk
289 if not os
.path
.exists(path
):
291 with
open(path
, "r") as f
:
292 return int(f
.readline())*512/1024
295 def root_disk_serial(self
):
296 serial
= _fireinfo
.get_harddisk_serial("/dev/%s" % self
.root_disk
)
300 return serial
.strip()
303 toscan
= (("/sys/bus/pci/devices", device
.PCIDevice
),
304 ("/sys/bus/usb/devices", device
.USBDevice
))
305 for path
, cls
in toscan
:
306 dirlist
= os
.listdir(path
)
308 self
.devices
.append(cls(os
.path
.join(path
, dir)))
313 Say if the host is running in a virtual environment.
315 return self
.hypervisor
.virtual
319 if __name__
== "__main__":
329 print "------------\n", s
.devices
, "\n------------\n"
330 print json
.dumps(s
.profile(), sort_keys
=True, indent
=4)