]> git.ipfire.org Git - oddments/fireinfo.git/blame - fireinfo/system.py
Cleanup unused code.
[oddments/fireinfo.git] / fireinfo / system.py
CommitLineData
b45f0e98
MT
1#!/usr/bin/python
2
31a30328 3import hashlib
b45f0e98
MT
4import json
5import os
6import string
7
8import cpu
9import device
715ba5ac 10import hypervisor
b45f0e98
MT
11
12class System(object):
13
14 def __init__(self):
15 # find all devices
16 self.devices = []
17 self.scan()
18 self.cpu = cpu.CPU()
715ba5ac
MT
19 self.hypervisor = hypervisor.Hypervisor()
20
b45f0e98
MT
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" : [],
d8614fc3
MT
33
34 "virtual" : self.virtual,
b45f0e98
MT
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,
b45f0e98
MT
52 "family" : self.cpu.family,
53 "count" : self.cpu.count
54 }
715ba5ac
MT
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
b45f0e98
MT
63 return json.dumps(p)
64
65
66 @property
67 def arch(self):
68 return os.uname()[4]
69
70 @property
71 def public_id(self):
31a30328
MT
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()
b45f0e98
MT
77
78 @property
79 def private_id(self):
31a30328
MT
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
b45f0e98
MT
98 @property
99 def language(self):
100 with open("/var/ipfire/main/settings", "r") as f:
101 for line in f.readlines():
102 key, val = line.split("=", 1)
103 if key=="LANGUAGE":
104 return val.strip()
105
106 @property
107 def release(self):
108 with open("/etc/system-release", "r") as f:
109 return f.read().strip()
110
111 @property
112 def memory(self):
113 with open("/proc/meminfo", "r") as f:
114 firstline = f.readline().strip()
115 return firstline.split()[1]
116
117 @property
118 def kernel(self):
119 return os.uname()[2]
120
121 @property
122 def root_disk(self):
123 with open("/etc/mtab", "r") as f:
124 dev, mountpoint, rest = f.readline().split(" ",2)
125 if mountpoint == "/":
126 dev = dev[5:]
127 # cut off all digits at end of string
128 while dev[-1] in string.digits:
129 dev = dev[:-1]
130 return dev
131
132 @property
133 def root_size(self):
134 path="/sys/block/%s/size" %self.root_disk
135 if not os.path.exists(path):
136 return
137 with open(path, "r") as f:
138 return int(f.readline())*512/1024
139
140 def scan(self):
141 toscan = (("/sys/bus/pci/devices", device.PCIDevice),
142 ("/sys/bus/usb/devices", device.USBDevice))
143 for path, cls in toscan:
144 dirlist = os.listdir(path)
145 for dir in dirlist:
146 self.devices.append(cls(os.path.join(path, dir)))
d8614fc3
MT
147
148 @property
149 def virtual(self):
150 """
151 Say if the host is running in a virtual environment.
152 """
715ba5ac 153 return self.hypervisor.virtual
b45f0e98
MT
154
155
156
157if __name__ == "__main__":
158 s=System()
159 print s.arch
160 print s.language
161 print s.release
162 print s.memory
163 print s.kernel
164 print s.root_disk
165 print s.root_size
166 print "------------\n", s.devices, "\n------------\n"
167 print s.profile()
168
169 import urllib2
170 import urllib
171 r = urllib2.Request("http://192.168.10.101:9001/send/%s" %s.public_id, data = urllib.urlencode({"profile" : s.profile()}))
5b1919ae 172 urllib2.urlopen(r)