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