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