]> git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/system.py
Add sendprofile script (version 0.2).
[oddments/fireinfo.git] / fireinfo / system.py
1 #!/usr/bin/python
2
3 import hashlib
4 import json
5 import os
6 import string
7
8 import cpu
9 import device
10 import hypervisor
11
12 class System(object):
13
14 def __init__(self):
15 # find all devices
16 self.devices = []
17 self.scan()
18 self.cpu = cpu.CPU()
19 self.hypervisor = hypervisor.Hypervisor()
20
21 def profile(self):
22 p = {
23 "public_id" : self.public_id,
24 "private_id" : self.private_id,
25
26 "arch" : self.arch,
27 "language" : self.language,
28 "release" : self.release,
29 "memory" : self.memory,
30 "kernel" : self.kernel,
31 "root_size" : self.root_size,
32 "devices" : [],
33
34 "virtual" : self.virtual,
35 }
36
37 for device in self.devices:
38 p["devices"].append({
39 "subsystem" : device.subsystem.lower(),
40 "vendor" : device.vendor.lower(),
41 "model" : device.model.lower(),
42 "deviceclass" : device.deviceclass
43 })
44
45 p["cpu"] = {
46 "vendor" : self.cpu.vendor,
47 "model" : self.cpu.model,
48 "stepping" : self.cpu.stepping,
49 "flags" : self.cpu.flags,
50 "bogomips" : self.cpu.bogomips,
51 "speed" : self.cpu.speed,
52 "family" : self.cpu.family,
53 "count" : self.cpu.count
54 }
55
56 # Only append hypervisor information if we are virtualized.
57 if self.virtual:
58 p["hypervisor"] = {
59 "type" : self.hypervisor.type,
60 "vendor" : self.hypervisor.vendor,
61 }
62
63 return p
64
65
66 @property
67 def arch(self):
68 return os.uname()[4]
69
70 @property
71 def public_id(self):
72 """
73 This returns a globally (hopefully) ID to identify the host
74 later (by request) in the database.
75 """
76 return hashlib.sha1(self._unique_id).hexdigest()
77
78 @property
79 def private_id(self):
80 """
81 The private ID is built out of the _unique_id and used to
82 permit a host to do changes on the database.
83
84 No one could ever guess this without access to the host.
85 """
86 return hashlib.sha1("%s" % reversed(self._unique_id)).hexdigest()
87
88 @property
89 def _unique_id(self):
90 """
91 This is a helper ID which is generated out of some hardware information
92 that is considered to be constant over a PC's lifetime.
93
94 None of the data here is ever sent to the server.
95 """
96 return "123456789" # XXX just a dummy
97
98 @property
99 def language(self):
100 # Return "unknown" if settings file does not exist.
101 filename = "/var/ipfire/main/settings"
102 if not os.path.exists(filename):
103 return "unknown"
104
105 with open(filename, "r") as f:
106 for line in f.readlines():
107 key, val = line.split("=", 1)
108 if key=="LANGUAGE":
109 return val.strip()
110
111 @property
112 def release(self):
113 with open("/etc/system-release", "r") as f:
114 return f.read().strip()
115
116 @property
117 def memory(self):
118 with open("/proc/meminfo", "r") as f:
119 firstline = f.readline().strip()
120 return firstline.split()[1]
121
122 @property
123 def kernel(self):
124 return os.uname()[2]
125
126 @property
127 def root_disk(self):
128 with open("/etc/mtab", "r") as f:
129 dev, mountpoint, rest = f.readline().split(" ",2)
130 if mountpoint == "/":
131 dev = dev[5:]
132 # cut off all digits at end of string
133 while dev[-1] in string.digits:
134 dev = dev[:-1]
135 return dev
136
137 @property
138 def root_size(self):
139 path="/sys/block/%s/size" %self.root_disk
140 if not os.path.exists(path):
141 return
142 with open(path, "r") as f:
143 return int(f.readline())*512/1024
144
145 def scan(self):
146 toscan = (("/sys/bus/pci/devices", device.PCIDevice),
147 ("/sys/bus/usb/devices", device.USBDevice))
148 for path, cls in toscan:
149 dirlist = os.listdir(path)
150 for dir in dirlist:
151 self.devices.append(cls(os.path.join(path, dir)))
152
153 @property
154 def virtual(self):
155 """
156 Say if the host is running in a virtual environment.
157 """
158 return self.hypervisor.virtual
159
160
161
162 if __name__ == "__main__":
163 s=System()
164 print s.arch
165 print s.language
166 print s.release
167 print s.memory
168 print s.kernel
169 print s.root_disk
170 print s.root_size
171 print "------------\n", s.devices, "\n------------\n"
172 print json.dumps(s.profile(), sort_keys=True, indent=4)