]>
Commit | Line | Data |
---|---|---|
66862195 MT |
1 | #!/usr/bin/python |
2 | ||
3 | from __future__ import division | |
4 | ||
5 | import datetime | |
6 | import hwdata | |
7 | import logging | |
8 | import re | |
9 | ||
10 | import util | |
11 | from misc import Object | |
12 | ||
13 | N_ = lambda x: x | |
14 | ||
15 | CPU_VENDORS = { | |
16 | "AMDisbetter!" : "AMD", | |
17 | "AuthenticAMD" : "AMD", | |
18 | "CentaurHauls" : "VIA", | |
19 | "CyrixInstead" : "Cyrix", | |
20 | "GenuineIntel" : "Intel", | |
21 | "TransmetaCPU" : "Transmeta", | |
22 | "GenuineTMx86" : "Transmeta", | |
23 | "Geode by NSC" : "NSC", | |
24 | "NexGenDriven" : "NexGen", | |
25 | "RiseRiseRise" : "Rise", | |
26 | "SiS SiS SiS" : "SiS", | |
27 | "SiS SiS SiS " : "SiS", | |
28 | "UMC UMC UMC " : "UMC", | |
29 | "VIA VIA VIA " : "VIA", | |
30 | "Vortex86 SoC" : "Vortex86", | |
31 | } | |
32 | ||
33 | CPU_STRINGS = ( | |
34 | # AMD | |
35 | (r"(AMD Athlon).*(XP).*", r"\1 \2"), | |
36 | (r"(AMD Phenom).* ([0-9]+) .*", r"\1 \2"), | |
37 | (r"(AMD Phenom).*", r"\1"), | |
38 | (r"(AMD Sempron).*", r"\1"), | |
39 | (r"AMD Athlon.* II X2 ([a-z0-9]+).*", r"AMD Athlon X2 \1"), | |
40 | (r"(Geode).*", r"\1"), | |
41 | ||
42 | # Intel | |
43 | (r"Intel\(R\) (Atom|Celeron).*CPU\s*([A-Z0-9]+) .*", r"Intel \1 \2"), | |
44 | (r"(Intel).*(Celeron).*", r"\1 \2"), | |
45 | (r"Intel.* Core.*2 Duo *CPU .* ([A-Z0-9]+) .*", r"Intel C2D \1"), | |
46 | (r"Intel.* Core.*2 CPU .* ([A-Z0-9]+) .*", r"Intel C2 \1"), | |
47 | (r"Intel.* Core.*2 Quad *CPU .* ([A-Z0-9]+) .*", r"Intel C2Q \1"), | |
48 | (r"Intel.* Xeon.* CPU .* ([A-Z0-9]+) .*", r"Intel Xeon \1"), | |
49 | (r"(Intel).*(Xeon).*", r"\1 \2"), | |
50 | (r"Intel.* Pentium.* (D|4) .*", r"Intel Pentium \1"), | |
51 | (r"Intel.* Pentium.* Dual .* ([A-Z0-9]+) .*", r"Intel Pentium Dual \1"), | |
52 | (r"Pentium.* Dual-Core .* ([A-Z0-9]+) .*", r"Intel Pentium Dual \1"), | |
53 | (r"(Pentium I{2,3}).*", r"Intel \1"), | |
54 | (r"(Celeron \(Coppermine\))", r"Intel Celeron"), | |
55 | ||
56 | # VIA | |
57 | (r"(VIA \w*).*", r"\1"), | |
58 | ||
59 | # Qemu | |
60 | (r"QEMU Virtual CPU version .*", r"QEMU CPU"), | |
61 | ||
62 | # ARM | |
63 | (r"Feroceon .*", r"ARM Feroceon"), | |
64 | ) | |
65 | ||
66 | IGNORED_DEVICES = ["usb",] | |
67 | ||
68 | class ProfileDict(object): | |
69 | def __init__(self, data): | |
70 | self._data = data | |
71 | ||
72 | ||
73 | class ProfileNetwork(ProfileDict): | |
74 | def __eq__(self, other): | |
75 | if not self.has_red == other.has_red: | |
76 | return False | |
77 | ||
78 | if not self.has_green == other.has_green: | |
79 | return False | |
80 | ||
81 | if not self.has_orange == other.has_orange: | |
82 | return False | |
83 | ||
84 | if not self.has_blue == other.has_blue: | |
85 | return False | |
86 | ||
87 | return True | |
88 | ||
89 | def __iter__(self): | |
90 | ret = [] | |
91 | ||
92 | for zone in ("red", "green", "orange", "blue"): | |
93 | if self.has_zone(zone): | |
94 | ret.append(zone) | |
95 | ||
96 | return iter(ret) | |
97 | ||
98 | def has_zone(self, name): | |
99 | return self._data.get("has_%s" % name) | |
100 | ||
101 | @property | |
102 | def has_red(self): | |
103 | return self._data.get("has_red", False) | |
104 | ||
105 | @property | |
106 | def has_green(self): | |
107 | return self._data.get("has_green", False) | |
108 | ||
109 | @property | |
110 | def has_orange(self): | |
111 | return self._data.get("has_orange", False) | |
112 | ||
113 | @property | |
114 | def has_blue(self): | |
115 | return self._data.get("has_blue", False) | |
116 | ||
117 | ||
118 | class Processor(Object): | |
119 | def __init__(self, backend, id, data=None, clock_speed=None, bogomips=None): | |
120 | Object.__init__(self, backend) | |
121 | ||
122 | self.id = id | |
123 | self.__data = data | |
124 | self.__clock_speed = clock_speed | |
125 | self.__bogomips = bogomips | |
126 | ||
127 | def __str__(self): | |
128 | s = [] | |
129 | ||
130 | if not self.model_string.startswith(self.vendor): | |
131 | s.append(self.vendor) | |
132 | s.append("-") | |
133 | ||
134 | s.append(self.model_string) | |
135 | ||
136 | if self.core_count > 1: | |
137 | s.append("x%s" % self.core_count) | |
138 | ||
139 | return " ".join(s) | |
140 | ||
141 | @property | |
142 | def data(self): | |
143 | if self.__data is None: | |
144 | self.__data = self.db.get("SELECT * FROM fireinfo_processors \ | |
145 | WHERE id = %s", self.id) | |
146 | ||
147 | return self.__data | |
148 | ||
149 | @property | |
150 | def vendor(self): | |
151 | try: | |
152 | return CPU_VENDORS[self.data.vendor] | |
153 | except KeyError: | |
154 | return self.data.vendor | |
155 | ||
156 | @property | |
157 | def model(self): | |
158 | return self.data.model | |
159 | ||
160 | @property | |
161 | def model_string(self): | |
162 | s = self.data.model_string.split() | |
163 | ||
164 | return " ".join((e for e in s if e)) | |
165 | ||
166 | @property | |
167 | def flags(self): | |
168 | return self.data.flags | |
169 | ||
170 | def has_flag(self, flag): | |
171 | return flag in self.flags | |
172 | ||
173 | def uses_ht(self): | |
174 | return self.has_flag("ht") | |
175 | ||
176 | @property | |
177 | def core_count(self): | |
178 | return self.data.core_count | |
179 | ||
180 | @property | |
181 | def count(self): | |
182 | if self.uses_ht(): | |
183 | return self.core_count // 2 | |
184 | ||
185 | return self.core_count | |
186 | ||
187 | @property | |
188 | def clock_speed(self): | |
189 | return self.__clock_speed | |
190 | ||
191 | def format_clock_speed(self): | |
192 | if not self.clock_speed: | |
193 | return | |
194 | ||
195 | if self.clock_speed < 1000: | |
196 | return "%dMHz" % self.clock_speed | |
197 | ||
198 | return "%.2fGHz" % round(self.clock_speed / 1000, 2) | |
199 | ||
200 | @property | |
201 | def bogomips(self): | |
202 | return self.__bogomips | |
203 | ||
204 | @property | |
205 | def capabilities(self): | |
206 | caps = [ | |
207 | ("64bit", self.has_flag("lm")), | |
208 | ("aes", self.has_flag("aes")), | |
209 | ("nx", self.has_flag("nx")), | |
210 | ("pae", self.has_flag("pae") or self.has_flag("lpae")), | |
211 | ("rdrand", self.has_flag("rdrand")), | |
212 | ] | |
213 | ||
214 | # If the system is already running in a virtual environment, | |
215 | # we cannot correctly detect if the CPU supports svm or vmx | |
216 | if self.has_flag("hypervisor"): | |
217 | caps.append(("virt", None)) | |
218 | else: | |
219 | caps.append(("virt", self.has_flag("vmx") or self.has_flag("svm"))) | |
220 | ||
221 | return caps | |
222 | ||
223 | def format_model(self): | |
224 | s = self.model_string | |
225 | for pattern, repl in CPU_STRINGS: | |
226 | if re.match(pattern, s) is None: | |
227 | continue | |
228 | return re.sub(pattern, repl, s) | |
229 | ||
230 | # Otherwise remove the symbols | |
231 | for i in ("C", "R", "TM"): | |
232 | s = s.replace("(%s)" % i, "") | |
233 | ||
234 | return s | |
235 | ||
236 | @property | |
237 | def friendly_string(self): | |
238 | s = [] | |
239 | ||
240 | model = self.format_model() | |
241 | s.append(model) | |
242 | ||
243 | clock_speed = self.format_clock_speed() | |
244 | if clock_speed: | |
245 | s.append("@ %s" % clock_speed) | |
246 | ||
247 | if self.count > 1: | |
248 | s.append("x%s" % self.count) | |
249 | ||
250 | return " ".join(s) | |
251 | ||
252 | ||
253 | class Device(Object): | |
254 | classid2name = { | |
255 | "pci" : { | |
256 | "00" : N_("Unclassified"), | |
257 | "01" : N_("Mass storage"), | |
258 | "02" : N_("Network"), | |
259 | "03" : N_("Display"), | |
260 | "04" : N_("Multimedia"), | |
261 | "05" : N_("Memory controller"), | |
262 | "06" : N_("Bridge"), | |
263 | "07" : N_("Communication"), | |
264 | "08" : N_("Generic system peripheral"), | |
265 | "09" : N_("Input device"), | |
266 | "0a" : N_("Docking station"), | |
267 | "0b" : N_("Processor"), | |
268 | "0c" : N_("Serial bus"), | |
269 | "0d" : N_("Wireless"), | |
270 | "0e" : N_("Intelligent controller"), | |
271 | "0f" : N_("Satellite communications controller"), | |
272 | "10" : N_("Encryption"), | |
273 | "11" : N_("Signal processing controller"), | |
274 | "ff" : N_("Unassigned class"), | |
275 | }, | |
276 | ||
277 | "usb" : { | |
278 | "00" : N_("Unclassified"), | |
279 | "01" : N_("Multimedia"), | |
280 | "02" : N_("Communication"), | |
281 | "03" : N_("Input device"), | |
282 | "05" : N_("Generic system peripheral"), | |
283 | "06" : N_("Image"), | |
284 | "07" : N_("Printer"), | |
285 | "08" : N_("Mass storage"), | |
286 | "09" : N_("Hub"), | |
287 | "0a" : N_("Communication"), | |
288 | "0b" : N_("Smart card"), | |
289 | "0d" : N_("Encryption"), | |
290 | "0e" : N_("Display"), | |
291 | "0f" : N_("Personal Healthcare"), | |
292 | "dc" : N_("Diagnostic Device"), | |
293 | "e0" : N_("Wireless"), | |
294 | "ef" : N_("Unclassified"), | |
295 | "fe" : N_("Unclassified"), | |
296 | "ff" : N_("Unclassified"), | |
297 | } | |
298 | } | |
299 | ||
300 | def __init__(self, backend, id, data=None): | |
301 | Object.__init__(self, backend) | |
302 | ||
303 | self.id = id | |
304 | self.__data = data | |
305 | ||
306 | def __repr__(self): | |
307 | return "<%s vendor=%s model=%s>" % (self.__class__.__name__, | |
308 | self.vendor_string, self.model_string) | |
309 | ||
310 | def __cmp__(self, other): | |
311 | if self.id and self.id == other.id: | |
312 | return 0 | |
313 | ||
314 | return \ | |
315 | cmp(self.subsystem, other.subsystem) or \ | |
316 | cmp(self.vendor_string, other.vendor_string) or \ | |
317 | cmp(self.vendor, other.vendor) or \ | |
318 | cmp(self.model_string, other.model_string) or \ | |
319 | cmp(self.model, other.model) or \ | |
320 | cmp(self.driver, other.driver) | |
321 | ||
322 | @property | |
323 | def data(self): | |
324 | if self.__data is None: | |
325 | assert self.id | |
326 | ||
327 | self.__data = self.db.get("SELECT * FROM fireinfo_devices \ | |
328 | WHERE id = %s", self.id) | |
329 | ||
330 | return self.__data | |
331 | ||
332 | def is_showable(self): | |
333 | if self.driver in IGNORED_DEVICES: | |
334 | return False | |
335 | ||
336 | if self.driver in ("pcieport", "hub"): | |
337 | return False | |
338 | ||
339 | return True | |
340 | ||
341 | @property | |
342 | def subsystem(self): | |
343 | return self.data.subsystem | |
344 | ||
345 | @property | |
346 | def model(self): | |
347 | return self.data.model | |
348 | ||
349 | @property | |
350 | def model_string(self): | |
351 | return self.fireinfo.get_model_string(self.subsystem, | |
352 | self.vendor, self.model) | |
353 | ||
354 | @property | |
355 | def vendor(self): | |
356 | return self.data.vendor | |
357 | ||
358 | @property | |
359 | def vendor_string(self): | |
360 | return self.fireinfo.get_vendor_string(self.subsystem, self.vendor) | |
361 | ||
362 | @property | |
363 | def driver(self): | |
364 | return self.data.driver | |
365 | ||
366 | @property | |
367 | def cls(self): | |
368 | classid = self.data.deviceclass | |
369 | ||
370 | if self.subsystem == "pci": | |
371 | classid = classid[:-4] | |
372 | if len(classid) == 1: | |
373 | classid = "0%s" % classid | |
374 | ||
375 | elif self.subsystem == "usb" and classid: | |
376 | classid = classid.split("/")[0] | |
377 | classid = "%02x" % int(classid) | |
378 | ||
379 | try: | |
380 | return self.classid2name[self.subsystem][classid] | |
381 | except KeyError: | |
382 | return "N/A" | |
383 | ||
384 | @property | |
385 | def percentage(self): | |
386 | return self.data.get("percentage", None) | |
387 | ||
388 | ||
389 | class Profile(Object): | |
390 | def __init__(self, backend, id, data=None): | |
391 | Object.__init__(self, backend) | |
392 | ||
393 | self.id = id | |
394 | self.__data = data | |
395 | ||
396 | def __repr__(self): | |
397 | return "<%s %s>" % (self.__class__.__name__, self.public_id) | |
398 | ||
399 | def __cmp__(self, other): | |
400 | return cmp(self.id, other.id) | |
401 | ||
402 | def is_showable(self): | |
403 | if self.arch_id: | |
404 | return True | |
405 | ||
406 | return False | |
407 | ||
408 | @property | |
409 | def data(self): | |
410 | if self.__data is None: | |
411 | self.__data = self.db.get("SELECT * FROM fireinfo_profiles \ | |
412 | WHERE id = %s", self.id) | |
413 | ||
414 | return self.__data | |
415 | ||
416 | @property | |
417 | def public_id(self): | |
418 | return self.data.public_id | |
419 | ||
420 | @property | |
421 | def private_id(self): | |
422 | raise NotImplementedError | |
423 | ||
424 | @property | |
425 | def time_created(self): | |
426 | return self.data.time_created | |
427 | ||
428 | @property | |
429 | def time_updated(self): | |
430 | return self.data.time_updated | |
431 | ||
432 | def updated(self, profile_parser=None, location=None, when=None): | |
433 | valid = self.settings.get_int("fireinfo_profile_days_valid", 14) | |
434 | ||
435 | self.db.execute("UPDATE fireinfo_profiles \ | |
436 | SET \ | |
437 | time_updated = then_or_now(%s), \ | |
438 | time_valid = then_or_now(%s) + INTERVAL '%s days', \ | |
439 | updates = updates + 1 \ | |
440 | WHERE id = %s", when, when, valid, self.id) | |
441 | ||
442 | if profile_parser: | |
443 | self.set_processor_speeds( | |
444 | profile_parser.processor_clock_speed, | |
445 | profile_parser.processor_bogomips, | |
446 | ) | |
447 | ||
448 | if location: | |
449 | self.set_location(location) | |
450 | ||
451 | def expired(self, when=None): | |
452 | self.db.execute("UPDATE fireinfo_profiles \ | |
453 | SET time_valid = then_or_now(%s) WHERE id = %s", when, self.id) | |
454 | ||
455 | def parse(self, parser): | |
456 | # Processor | |
457 | self.processor = parser.processor | |
458 | self.set_processor_speeds(parser.processor_clock_speed, parser.processor_bogomips) | |
459 | ||
460 | # All devices | |
461 | self.devices = parser.devices | |
462 | ||
463 | # System | |
464 | self.system_id = parser.system_id | |
465 | ||
466 | # Memory | |
467 | self.memory = parser.memory | |
468 | ||
469 | # Storage | |
470 | self.storage = parser.storage | |
471 | ||
472 | # Kernel | |
473 | self.kernel_id = parser.kernel_id | |
474 | ||
475 | # Arch | |
476 | self.arch_id = parser.arch_id | |
477 | ||
478 | # Release | |
479 | self.release_id = parser.release_id | |
480 | ||
481 | # Language | |
482 | self.language = parser.language | |
483 | ||
484 | # Virtual | |
485 | if parser.virtual: | |
486 | self.hypervisor_id = parser.hypervisor_id | |
487 | ||
488 | # Network | |
489 | self.network = parser.network | |
490 | ||
491 | # Location | |
492 | ||
493 | def get_location(self): | |
494 | if not hasattr(self, "_location"): | |
495 | res = self.db.get("SELECT location FROM fireinfo_profiles_locations \ | |
496 | WHERE profile_id = %s", self.id) | |
497 | ||
498 | if res: | |
499 | self._location = res.location | |
500 | else: | |
501 | self._location = None | |
502 | ||
503 | return self._location | |
504 | ||
505 | def set_location(self, location): | |
506 | if self.location == location: | |
507 | return | |
508 | ||
509 | self.db.execute("DELETE FROM fireinfo_profiles_locations \ | |
510 | WHERE profile_id = %s", self.id) | |
511 | self.db.execute("INSERT INTO fireinfo_profiles_locations(profile_id, location) \ | |
512 | VALUES(%s, %s)", self.id, location) | |
513 | ||
514 | self._location = location | |
515 | ||
516 | location = property(get_location, set_location) | |
517 | ||
518 | @property | |
519 | def location_string(self): | |
520 | return self.geoip.get_country_name(self.location) | |
521 | ||
522 | # Devices | |
523 | ||
524 | @property | |
525 | def device_ids(self): | |
526 | if not hasattr(self, "_device_ids"): | |
527 | res = self.db.query("SELECT device_id FROM fireinfo_profiles_devices \ | |
528 | WHERE profile_id = %s", self.id) | |
529 | ||
530 | self._device_ids = sorted([r.device_id for r in res]) | |
531 | ||
532 | return self._device_ids | |
533 | ||
534 | def get_devices(self): | |
535 | if not hasattr(self, "_devices"): | |
536 | res = self.db.query("SELECT * FROM fireinfo_devices \ | |
537 | LEFT JOIN fireinfo_profiles_devices ON \ | |
538 | fireinfo_devices.id = fireinfo_profiles_devices.device_id \ | |
539 | WHERE fireinfo_profiles_devices.profile_id = %s", self.id) | |
540 | ||
541 | self._devices = [] | |
542 | for row in res: | |
543 | device = Device(self.backend, row.id, row) | |
544 | self._devices.append(device) | |
545 | ||
546 | return self._devices | |
547 | ||
548 | def set_devices(self, devices): | |
549 | device_ids = [d.id for d in devices] | |
550 | ||
551 | self.db.execute("DELETE FROM fireinfo_profiles_devices WHERE profile_id = %s", self.id) | |
552 | self.db.executemany("INSERT INTO fireinfo_profiles_devices(profile_id, device_id) \ | |
553 | VALUES(%s, %s)", ((self.id, d) for d in device_ids)) | |
554 | ||
555 | self._devices = devices | |
556 | self._device_ids = device_ids | |
557 | ||
558 | devices = property(get_devices, set_devices) | |
559 | ||
560 | def count_device(self, vendor, model): | |
561 | counter = 0 | |
562 | ||
563 | for dev in self.devices: | |
564 | if dev.vendor == vendor and dev.model == model: | |
565 | counter += 1 | |
566 | ||
567 | return counter | |
568 | ||
569 | # System | |
570 | ||
571 | def get_system_id(self): | |
572 | if not hasattr(self, "_system_id"): | |
573 | res = self.db.get("SELECT system_id AS id FROM fireinfo_profiles_systems \ | |
574 | WHERE profile_id = %s", self.id) | |
575 | ||
576 | if res: | |
577 | self._system_id = res.id | |
578 | else: | |
579 | self._system_id = None | |
580 | ||
581 | return self._system_id | |
582 | ||
583 | def set_system_id(self, system_id): | |
584 | self.db.execute("DELETE FROM fireinfo_profiles_systems WHERE profile_id = %s", self.id) | |
585 | ||
586 | if system_id: | |
587 | self.db.execute("INSERT INTO fireinfo_profiles_systems(profile_id, system_id) \ | |
588 | VALUES(%s, %s)", self.id, system_id) | |
589 | ||
590 | self._system_id = None | |
591 | if hasattr(self, "_system"): | |
592 | del self._system | |
593 | ||
594 | system_id = property(get_system_id, set_system_id) | |
595 | ||
596 | @property | |
597 | def system(self): | |
598 | if not hasattr(self, "_system"): | |
599 | res = self.db.get("SELECT fireinfo_systems.vendor AS vendor, fireinfo_systems.model AS model \ | |
600 | FROM fireinfo_profiles_systems \ | |
601 | LEFT JOIN fireinfo_systems ON fireinfo_profiles_systems.system_id = fireinfo_systems.id \ | |
602 | WHERE fireinfo_profiles_systems.profile_id = %s", self.id) | |
603 | ||
604 | if res: | |
605 | self._system = (res.vendor, res.model) | |
606 | else: | |
607 | self._system = (None, None) | |
608 | ||
609 | return self._system | |
610 | ||
611 | @property | |
612 | def system_vendor(self): | |
613 | try: | |
614 | v, m = self.system | |
615 | return v | |
616 | except TypeError: | |
617 | pass | |
618 | ||
619 | @property | |
620 | def system_model(self): | |
621 | try: | |
622 | v, m = self.system | |
623 | return m | |
624 | except TypeError: | |
625 | pass | |
626 | ||
627 | @property | |
628 | def appliance_id(self): | |
629 | if not hasattr(self, "_appliance_id"): | |
630 | appliances = ( | |
631 | ("lightningwirelabs-eco", self._appliance_test_lightningwirelabs_eco), | |
632 | ) | |
633 | ||
634 | self._appliance_id = None | |
635 | for name, test_func in appliances: | |
636 | if not test_func(): | |
637 | continue | |
638 | ||
639 | self._appliance_id = name | |
640 | break | |
641 | ||
642 | return self._appliance_id | |
643 | ||
644 | @property | |
645 | def appliance(self): | |
646 | if self.appliance_id == "lightningwirelabs-eco": | |
647 | return "Lightning Wire Labs IPFire Eco Appliance" | |
648 | ||
649 | def _appliance_test_lightningwirelabs_eco(self): | |
650 | if not self.system == ("MSI", "MS-9877"): | |
651 | return False | |
652 | ||
653 | # Must have four Intel network adapters | |
654 | network_adapters_count = self.count_device("8086", "10d3") | |
655 | if not network_adapters_count == 4: | |
656 | return False | |
657 | ||
658 | # 4GB of memory | |
659 | if not self.memory >= 4230823936 * 0.95: | |
660 | return False | |
661 | ||
662 | return True | |
663 | ||
664 | # Processors | |
665 | ||
666 | @property | |
667 | def processor_id(self): | |
668 | if hasattr(self, "_processor"): | |
669 | return self._processor.id | |
670 | ||
671 | if not hasattr(self, "_processor_id"): | |
672 | res = self.db.get("SELECT processor_id FROM fireinfo_profiles_processors \ | |
673 | WHERE profile_id = %s", self.id) | |
674 | ||
675 | if res: | |
676 | self._processor_id = res.processor_id | |
677 | else: | |
678 | self._processor_id = None | |
679 | ||
680 | return self._processor_id | |
681 | ||
682 | def get_processor(self): | |
683 | if not self.processor_id: | |
684 | return | |
685 | ||
686 | if not hasattr(self, "_processor"): | |
687 | res = self.db.get("SELECT * FROM fireinfo_profiles_processors \ | |
688 | WHERE profile_id = %s", self.id) | |
689 | ||
690 | if res: | |
691 | self._processor = self.fireinfo.get_processor_by_id(res.processor_id, | |
692 | clock_speed=res.clock_speed, bogomips=res.bogomips) | |
693 | else: | |
694 | self._processor = None | |
695 | ||
696 | return self._processor | |
697 | ||
698 | def set_processor(self, processor): | |
699 | self.db.execute("DELETE FROM fireinfo_profiles_processors \ | |
700 | WHERE profile_id = %s", self.id) | |
701 | ||
702 | if processor: | |
703 | self.db.execute("INSERT INTO fireinfo_profiles_processors(profile_id, processor_id) \ | |
704 | VALUES(%s, %s)", self.id, processor.id) | |
705 | ||
706 | self._processor = processor | |
707 | ||
708 | processor = property(get_processor, set_processor) | |
709 | ||
710 | def set_processor_speeds(self, clock_speed, bogomips): | |
711 | self.db.execute("UPDATE fireinfo_profiles_processors \ | |
712 | SET clock_speed = %s, bogomips = %s WHERE profile_id = %s", | |
713 | clock_speed, bogomips, self.id) | |
714 | ||
715 | # Compat | |
716 | @property | |
717 | def cpu(self): | |
718 | return self.processor | |
719 | ||
720 | # Memory | |
721 | ||
722 | def get_memory(self): | |
723 | if not hasattr(self, "_memory"): | |
724 | res = self.db.get("SELECT amount FROM fireinfo_profiles_memory \ | |
725 | WHERE profile_id = %s", self.id) | |
726 | ||
727 | if res: | |
728 | self._memory = res.amount * 1024 | |
729 | else: | |
730 | self._memory = None | |
731 | ||
732 | return self._memory | |
733 | ||
734 | def set_memory(self, amount): | |
735 | if self.memory == amount: | |
736 | return | |
737 | ||
738 | amount /= 1024 | |
739 | ||
740 | self.db.execute("DELETE FROM fireinfo_profiles_memory WHERE profile_id = %s", self.id) | |
741 | if amount: | |
742 | self.db.execute("INSERT INTO fireinfo_profiles_memory(profile_id, amount) \ | |
743 | VALUES(%s, %s)", self.id, amount) | |
744 | ||
745 | self._memory = amount * 1024 | |
746 | ||
747 | memory = property(get_memory, set_memory) | |
748 | ||
749 | @property | |
750 | def friendly_memory(self): | |
751 | return util.format_size(self.memory) | |
752 | ||
753 | # Storage | |
754 | ||
755 | def get_storage(self): | |
756 | if not hasattr(self, "_storage"): | |
757 | res = self.db.get("SELECT amount FROM fireinfo_profiles_storage \ | |
758 | WHERE profile_id = %s", self.id) | |
759 | ||
760 | if res: | |
761 | self._storage = res.amount * 1024 | |
762 | else: | |
763 | self._storage = None | |
764 | ||
765 | return self._storage | |
766 | ||
767 | def set_storage(self, amount): | |
768 | if self.storage == amount: | |
769 | return | |
770 | ||
771 | amount /= 1024 | |
772 | ||
773 | self.db.execute("DELETE FROM fireinfo_profiles_storage WHERE profile_id = %s", self.id) | |
774 | if amount: | |
775 | self.db.execute("INSERT INTO fireinfo_profiles_storage(profile_id, amount) \ | |
776 | VALUES(%s, %s)", self.id, amount) | |
777 | ||
778 | self._storage = amount * 1024 | |
779 | ||
780 | storage = property(get_storage, set_storage) | |
781 | ||
782 | @property | |
783 | def friendly_storage(self): | |
784 | return util.format_size(self.storage) | |
785 | ||
786 | # Kernel | |
787 | ||
788 | def get_kernel_id(self): | |
789 | if not hasattr(self, "_kernel_id"): | |
790 | res = self.db.get("SELECT fireinfo_profiles_kernels.kernel_id AS id FROM fireinfo_profiles \ | |
791 | LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \ | |
792 | WHERE fireinfo_profiles.id = %s", self.id) | |
793 | ||
794 | if res: | |
795 | self._kernel_id = res.id | |
796 | else: | |
797 | self._kernel_id = None | |
798 | ||
799 | return self._kernel_id | |
800 | ||
801 | def set_kernel_id(self, kernel_id): | |
802 | if self.kernel_id == kernel_id: | |
803 | return | |
804 | ||
805 | self.db.execute("DELETE FROM fireinfo_profiles_kernels WHERE profile_id = %s", self.id) | |
806 | if kernel_id: | |
807 | self.db.execute("INSERT INTO fireinfo_profiles_kernels(profile_id, kernel_id) \ | |
808 | VALUES(%s, %s)", self.id, kernel_id) | |
809 | ||
810 | self._kernel_id = kernel_id | |
811 | if hasattr(self, "_kernel"): | |
812 | del self._kernel | |
813 | ||
814 | kernel_id = property(get_kernel_id, set_kernel_id) | |
815 | ||
816 | @property | |
817 | def kernel(self): | |
818 | if not hasattr(self, "_kernel"): | |
819 | res = self.db.get("SELECT fireinfo_kernels.name AS name FROM fireinfo_profiles \ | |
820 | LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \ | |
821 | LEFT JOIN fireinfo_kernels ON fireinfo_kernels.id = fireinfo_profiles_kernels.kernel_id \ | |
822 | WHERE fireinfo_profiles.id = %s", self.id) | |
823 | ||
824 | if res: | |
825 | self._kernel = res.name | |
826 | else: | |
827 | self._kernel = None | |
828 | ||
829 | return self._kernel | |
830 | ||
831 | # Arch | |
832 | ||
833 | def get_arch_id(self): | |
834 | if not hasattr(self, "_arch_id"): | |
835 | res = self.db.get("SELECT fireinfo_profiles_arches.arch_id AS id FROM fireinfo_profiles \ | |
836 | LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \ | |
837 | WHERE fireinfo_profiles.id = %s", self.id) | |
838 | ||
839 | if res: | |
840 | self._arch_id = res.id | |
841 | else: | |
842 | self._arch_id = None | |
843 | ||
844 | return self._arch_id | |
845 | ||
846 | def set_arch_id(self, arch_id): | |
847 | if self.arch_id == arch_id: | |
848 | return | |
849 | ||
850 | self.db.execute("DELETE FROM fireinfo_profiles_arches WHERE profile_id = %s", self.id) | |
851 | if arch_id: | |
852 | self.db.execute("INSERT INTO fireinfo_profiles_arches(profile_id, arch_id) \ | |
853 | VALUES(%s, %s)", self.id, arch_id) | |
854 | ||
855 | self._arch_id = None | |
856 | if hasattr(self, "_arch"): | |
857 | del self._arch | |
858 | ||
859 | arch_id = property(get_arch_id, set_arch_id) | |
860 | ||
861 | @property | |
862 | def arch(self): | |
863 | if not hasattr(self, "_arch"): | |
864 | res = self.db.get("SELECT fireinfo_arches.name AS name FROM fireinfo_profiles \ | |
865 | LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \ | |
866 | LEFT JOIN fireinfo_arches ON fireinfo_arches.id = fireinfo_profiles_arches.arch_id \ | |
867 | WHERE fireinfo_profiles.id = %s", self.id) | |
868 | ||
869 | if res: | |
870 | self._arch = res.name | |
871 | else: | |
872 | self._arch = None | |
873 | ||
874 | return self._arch | |
875 | ||
876 | # Release | |
877 | ||
878 | def get_release_id(self): | |
879 | if not hasattr(self, "_release_id"): | |
880 | res = self.db.get("SELECT fireinfo_profiles_releases.release_id AS id FROM fireinfo_profiles \ | |
881 | LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \ | |
882 | WHERE fireinfo_profiles.id = %s", self.id) | |
883 | ||
884 | if res: | |
885 | self._release_id = res.id | |
886 | else: | |
887 | self._release_id = None | |
888 | ||
889 | return self._release_id | |
890 | ||
891 | def set_release_id(self, release_id): | |
892 | if self.release_id == release_id: | |
893 | return | |
894 | ||
895 | self.db.execute("DELETE FROM fireinfo_profiles_releases WHERE profile_id = %s", self.id) | |
896 | if release_id: | |
897 | self.db.execute("INSERT INTO fireinfo_profiles_releases(profile_id, release_id) \ | |
898 | VALUES(%s, %s)", self.id, release_id) | |
899 | ||
900 | self._release_id = release_id | |
901 | if hasattr(self, "_release"): | |
902 | del self._release | |
903 | ||
904 | release_id = property(get_release_id, set_release_id) | |
905 | ||
906 | @property | |
907 | def release(self): | |
908 | if not hasattr(self, "_release"): | |
909 | res = self.db.get("SELECT fireinfo_releases.name AS name FROM fireinfo_profiles \ | |
910 | LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \ | |
911 | LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \ | |
912 | WHERE fireinfo_profiles.id = %s", self.id) | |
913 | ||
914 | if res: | |
915 | self._release = self._format_release(res.name) | |
916 | else: | |
917 | self._release = None | |
918 | ||
919 | return self._release | |
920 | ||
921 | @staticmethod | |
922 | def _format_release(r): | |
923 | if not r: | |
924 | return r | |
925 | ||
926 | # Remove the development header | |
927 | r = r.replace("Development Build: ", "") | |
928 | ||
929 | pairs = ( | |
930 | ("-beta", " - Beta "), | |
931 | ("-rc", " - Release Candidate "), | |
932 | ("rc", "Release Candidate "), | |
933 | ("core", "Core Update "), | |
934 | ("beta", "Beta "), | |
935 | ) | |
936 | ||
937 | for k, v in pairs: | |
938 | r = r.replace(k, v) | |
939 | ||
940 | return r | |
941 | ||
942 | # Virtual | |
943 | ||
944 | @property | |
945 | def virtual(self): | |
946 | if not hasattr(self, "_virtual"): | |
947 | res = self.db.get("SELECT 1 FROM fireinfo_profiles_virtual \ | |
948 | WHERE profile_id = %s", self.id) | |
949 | ||
950 | if res: | |
951 | self._virtual = True | |
952 | else: | |
953 | self._virtual = False | |
954 | ||
955 | return self._virtual | |
956 | ||
957 | def get_hypervisor_id(self): | |
958 | if not hasattr(self, "_hypervisor_id"): | |
959 | res = self.db.get("SELECT fireinfo_profiles_virtual.hypervisor_id AS id FROM fireinfo_profiles \ | |
960 | LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \ | |
961 | WHERE fireinfo_profiles.id = %s", self.id) | |
962 | ||
963 | if res: | |
964 | self._hypervisor_id = res.id | |
965 | else: | |
966 | self._hypervisor_id = None | |
967 | ||
968 | return self._hypervisor_id | |
969 | ||
970 | def set_hypervisor_id(self, hypervisor_id): | |
971 | self.db.execute("DELETE FROM fireinfo_profiles_virtual WHERE profile_id = %s", self.id) | |
972 | self.db.execute("INSERT INTO fireinfo_profiles_virtual(profile_id, hypervisor_id) \ | |
973 | VALUES(%s, %s)", self.id, hypervisor_id) | |
974 | ||
975 | self._hypervisor_id = hypervisor_id | |
976 | ||
977 | hypervisor_id = property(get_hypervisor_id, set_hypervisor_id) | |
978 | ||
979 | @property | |
980 | def hypervisor(self): | |
981 | if not hasattr(self, "_hypervisor"): | |
982 | res = self.db.get("SELECT fireinfo_hypervisors.name AS hypervisor FROM fireinfo_profiles \ | |
983 | LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \ | |
984 | LEFT JOIN fireinfo_hypervisors ON fireinfo_profiles_virtual.hypervisor_id = fireinfo_hypervisors.id \ | |
985 | WHERE fireinfo_profiles.id = %s", self.id) | |
986 | ||
987 | if res: | |
988 | self._hypervisor = res.hypervisor | |
989 | else: | |
990 | self._hypervisor = None | |
991 | ||
992 | return self._hypervisor | |
993 | ||
994 | # Language | |
995 | ||
996 | def get_language(self): | |
997 | if not hasattr(self, "_language"): | |
998 | res = self.db.get("SELECT language FROM fireinfo_profiles_languages \ | |
999 | WHERE profile_id = %s", self.id) | |
1000 | ||
1001 | if res: | |
1002 | self._language = res.language | |
1003 | else: | |
1004 | self._language = None | |
1005 | ||
1006 | return self._language | |
1007 | ||
1008 | def set_language(self, language): | |
1009 | self.db.execute("DELETE FROM fireinfo_profiles_languages WHERE profile_id = %s", self.id) | |
1010 | ||
1011 | if language: | |
1012 | self.db.execute("INSERT INTO fireinfo_profiles_languages(profile_id, language) \ | |
1013 | VALUES(%s, %s)", self.id, language) | |
1014 | ||
1015 | self._language = language | |
1016 | ||
1017 | language = property(get_language, set_language) | |
1018 | ||
1019 | # Network | |
1020 | ||
1021 | def get_network(self): | |
1022 | if not hasattr(self, "_network"): | |
1023 | res = self.db.get("SELECT * FROM fireinfo_profiles_networks \ | |
1024 | WHERE profile_id = %s", self.id) | |
1025 | ||
1026 | if not res: | |
1027 | res = {} | |
1028 | ||
1029 | self._network = ProfileNetwork(res) | |
1030 | ||
1031 | return self._network | |
1032 | ||
1033 | def set_network(self, network): | |
1034 | self.db.execute("DELETE FROM fireinfo_profiles_networks WHERE profile_id = %s", self.id) | |
1035 | ||
1036 | if network: | |
1037 | self.db.execute("INSERT INTO fireinfo_profiles_networks(profile_id, \ | |
1038 | has_red, has_green, has_orange, has_blue) VALUES(%s, %s, %s, %s, %s)", | |
1039 | self.id, network.has_red, network.has_green, network.has_orange, network.has_blue) | |
1040 | ||
1041 | self._network = network | |
1042 | ||
1043 | network = property(get_network, set_network) | |
1044 | ||
1045 | ||
1046 | class ProfileData(Object): | |
1047 | def __init__(self, backend, id, data=None, profile=None): | |
1048 | Object.__init__(self, backend) | |
1049 | ||
1050 | self.id = id | |
1051 | self._data = data | |
1052 | self._profile = profile | |
1053 | ||
1054 | @property | |
1055 | def data(self): | |
1056 | if self._data is None: | |
1057 | self._data = self.db.get("SELECT * FROM fireinfo_profile_data \ | |
1058 | WHERE id = %s", self.id) | |
1059 | ||
1060 | return self._data | |
1061 | ||
1062 | @property | |
1063 | def profile(self): | |
1064 | if not self._profile: | |
1065 | self._profile = self.fireinfo.get_profile_by_id(self.profile_id) | |
1066 | ||
1067 | return self._profile | |
1068 | ||
1069 | @property | |
1070 | def profile_id(self): | |
1071 | return self.data.profile_id | |
1072 | ||
1073 | ||
1074 | class ProfileParserError(Exception): | |
1075 | pass | |
1076 | ||
1077 | ||
1078 | class ProfileParser(Object): | |
1079 | __device_args = ( | |
1080 | "subsystem", | |
1081 | "vendor", | |
1082 | "model", | |
1083 | "sub_vendor", | |
1084 | "sub_model", | |
1085 | "driver", | |
1086 | "deviceclass", | |
1087 | ) | |
1088 | ||
1089 | __processor_args = ( | |
1090 | "vendor", | |
1091 | "model_string", | |
1092 | "family", | |
1093 | "model", | |
1094 | "stepping", | |
1095 | "core_count", | |
1096 | "flags", | |
1097 | ) | |
1098 | ||
1099 | __processor_args_mandatory = ( | |
1100 | "vendor", | |
1101 | "model_string", | |
1102 | ) | |
1103 | ||
1104 | def __init__(self, backend, public_id, blob=None): | |
1105 | Object.__init__(self, backend) | |
1106 | ||
1107 | self.public_id = public_id | |
1108 | self.private_id = None | |
1109 | self.devices = [] | |
1110 | self.processor = None | |
1111 | self.processor_clock_speed = None | |
1112 | self.processor_bogomips = None | |
1113 | self.system_id = None | |
1114 | self.memory = None | |
1115 | self.storage = None | |
1116 | self.kernel = None | |
1117 | self.kernel_id = None | |
1118 | self.arch = None | |
1119 | self.arch_id = None | |
1120 | self.release = None | |
1121 | self.release_id = None | |
1122 | self.language = None | |
1123 | self.virtual = None | |
1124 | self.hypervisor_id = None | |
1125 | self.network = None | |
1126 | ||
1127 | self.__parse_blob(blob) | |
1128 | ||
1129 | def equals(self, other): | |
1130 | if not self.processor_id == other.processor_id: | |
1131 | return False | |
1132 | ||
1133 | if not self.device_ids == other.device_ids: | |
1134 | return False | |
1135 | ||
1136 | if not self.system_id == other.system_id: | |
1137 | return False | |
1138 | ||
1139 | if not self.memory == other.memory: | |
1140 | return False | |
1141 | ||
1142 | if not self.storage == other.storage: | |
1143 | return False | |
1144 | ||
1145 | if not self.kernel_id == other.kernel_id: | |
1146 | return False | |
1147 | ||
1148 | if not self.arch_id == other.arch_id: | |
1149 | return False | |
1150 | ||
1151 | if not self.release_id == other.release_id: | |
1152 | return False | |
1153 | ||
1154 | if not self.language == other.language: | |
1155 | return False | |
1156 | ||
1157 | if not self.virtual == other.virtual: | |
1158 | return False | |
1159 | ||
1160 | if other.virtual: | |
1161 | if not self.hypervisor_id == other.hypervisor_id: | |
1162 | return False | |
1163 | ||
1164 | if not self.network == other.network: | |
1165 | return False | |
1166 | ||
1167 | return True | |
1168 | ||
1169 | def __parse_blob(self, blob): | |
1170 | _profile = blob.get("profile", {}) | |
1171 | self.private_id = blob.get("private_id") | |
1172 | ||
1173 | # Do not try to parse an empty profile | |
1174 | if not _profile: | |
1175 | return | |
1176 | ||
1177 | # Processor | |
1178 | _processor = _profile.get("cpu", {}) | |
1179 | self.__parse_processor(_processor) | |
1180 | ||
1181 | # Find devices | |
1182 | _devices = _profile.get("devices", []) | |
1183 | self.__parse_devices(_devices) | |
1184 | ||
1185 | # System | |
1186 | _system = _profile.get("system") | |
1187 | if _system: | |
1188 | self.__parse_system(_system) | |
1189 | ||
1190 | # Memory (convert to bytes) | |
1191 | memory = _system.get("memory", None) | |
1192 | if memory: | |
1193 | self.memory = memory * 1024 | |
1194 | ||
1195 | # Storage size (convert to bytes) | |
1196 | storage = _system.get("root_size", None) | |
1197 | if storage: | |
1198 | self.storage = storage * 1024 | |
1199 | ||
1200 | # Kernel | |
1201 | kernel = _system.get("kernel_release", None) | |
1202 | if kernel: | |
1203 | self.__parse_kernel(kernel) | |
1204 | ||
1205 | # Release | |
1206 | release = _system.get("release", None) | |
1207 | if release: | |
1208 | self.__parse_release(release) | |
1209 | ||
1210 | # Language | |
1211 | language = _system.get("language", None) | |
1212 | if language: | |
1213 | self.__parse_language(language) | |
1214 | ||
1215 | # Virtual | |
1216 | self.virtual = _system.get("virtual", False) | |
1217 | if self.virtual: | |
1218 | hypervisor = _profile.get("hypervisor") | |
1219 | self.__parse_hypervisor(hypervisor) | |
1220 | ||
1221 | # Network | |
1222 | _network = _profile.get("network") | |
1223 | if _network: | |
1224 | self.__parse_network(_network) | |
1225 | ||
1226 | @property | |
1227 | def device_ids(self): | |
1228 | return sorted([d.id for d in self.devices]) | |
1229 | ||
1230 | def __parse_devices(self, _devices): | |
1231 | self.devices = [] | |
1232 | ||
1233 | for _device in _devices: | |
1234 | args = {} | |
1235 | for arg in self.__device_args: | |
1236 | args[arg] = _device.get(arg, None) | |
1237 | ||
1238 | # Find the device or create a new one. | |
1239 | device = self.fireinfo.get_device(**args) | |
1240 | if not device: | |
1241 | device = self.fireinfo.create_device(**args) | |
1242 | ||
1243 | self.devices.append(device) | |
1244 | ||
1245 | def __parse_system(self, system): | |
1246 | vendor = system.get("vendor", None) | |
1247 | if not vendor: | |
1248 | vendor = None | |
1249 | ||
1250 | model = system.get("model", None) | |
1251 | if not model: | |
1252 | model = None | |
1253 | ||
1254 | self.system_id = self.fireinfo.get_system(vendor, model) | |
1255 | if not self.system_id: | |
1256 | self.system_id = self.fireinfo.create_system(vendor, model) | |
1257 | ||
1258 | @property | |
1259 | def processor_id(self): | |
1260 | if not self.processor: | |
1261 | return | |
1262 | ||
1263 | return self.processor.id | |
1264 | ||
1265 | def __parse_processor(self, _processor): | |
1266 | args = {} | |
1267 | for arg in self.__processor_args: | |
1268 | if arg == "core_count": | |
1269 | _arg = "count" | |
1270 | else: | |
1271 | _arg = arg | |
1272 | ||
1273 | args[arg] = _processor.get(_arg, None) | |
1274 | ||
1275 | for arg in self.__processor_args_mandatory: | |
1276 | if not args.get(arg, None): | |
1277 | raise ProfileParserError, "Mandatory argument missing: %s" % arg | |
1278 | ||
1279 | self.processor = self.fireinfo.get_processor(**args) | |
1280 | if not self.processor: | |
1281 | self.processor = self.fireinfo.create_processor(**args) | |
1282 | ||
1283 | self.processor_clock_speed = _processor.get("speed", None) | |
1284 | self.processor_bogomips = _processor.get("bogomips", None) | |
1285 | ||
1286 | arch = _processor.get("arch", None) | |
1287 | if arch: | |
1288 | self.__parse_arch(arch) | |
1289 | ||
1290 | def __parse_kernel(self, kernel): | |
1291 | self.kernel_id = self.fireinfo.get_kernel(kernel) | |
1292 | if not self.kernel_id: | |
1293 | self.kernel_id = self.fireinfo.create_kernel(kernel) | |
1294 | assert self.kernel_id | |
1295 | ||
1296 | self.kernel = kernel | |
1297 | ||
1298 | def __parse_arch(self, arch): | |
1299 | self.arch_id = self.fireinfo.get_arch(arch) | |
1300 | if not self.arch_id: | |
1301 | self.arch_id = self.fireinfo.create_arch(arch) | |
1302 | ||
1303 | self.arch = arch | |
1304 | ||
1305 | def __parse_release(self, release): | |
1306 | # Remove the arch bit | |
1307 | if release: | |
1308 | r = [e for e in release.split() if e] | |
1309 | for s in ("(i586)", "(armv5tel)"): | |
1310 | try: | |
1311 | r.remove(s) | |
1312 | break | |
1313 | except ValueError: | |
1314 | pass | |
1315 | ||
1316 | release = " ".join(r) | |
1317 | ||
1318 | self.release_id = self.fireinfo.get_release(release) | |
1319 | if not self.release_id: | |
1320 | self.release_id = self.fireinfo.create_release(release) | |
1321 | assert self.release_id | |
1322 | ||
1323 | self.release = release | |
1324 | ||
1325 | def __parse_language(self, language): | |
1326 | self.language, delim, rest = language.partition(".") | |
1327 | self.language, delim, rest = language.partition("_") | |
1328 | ||
1329 | def __parse_hypervisor(self, hypervisor): | |
1330 | vendor = hypervisor.get("vendor", "other") | |
1331 | ||
1332 | if vendor in ("other", "unknown"): | |
1333 | self.hypervisor_id = None | |
1334 | return | |
1335 | ||
1336 | self.hypervisor_id = self.fireinfo.get_hypervisor(vendor) | |
1337 | if not self.hypervisor_id: | |
1338 | self.hypervisor_id = self.fireinfo.create_hypervisor(vendor) | |
1339 | ||
1340 | def __parse_network(self, network): | |
1341 | self.network = ProfileNetwork({ | |
1342 | "has_red" : network.get("red", False), | |
1343 | "has_green" : network.get("green", False), | |
1344 | "has_orange" : network.get("orange", False), | |
1345 | "has_blue" : network.get("blue", False), | |
1346 | }) | |
1347 | ||
1348 | ||
1349 | class Fireinfo(Object): | |
1350 | def get_profile_count(self, when=None): | |
1351 | res = self.db.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \ | |
1352 | WHERE then_or_now(%s) BETWEEN time_created AND time_valid", when) | |
1353 | ||
1354 | if res: | |
1355 | return res.count | |
1356 | ||
1357 | def get_archives_count(self): | |
1358 | res = self.db.get("SELECT COUNT(*) AS count FROM fireinfo_profiles") | |
1359 | ||
1360 | if res: | |
1361 | return res.count | |
1362 | ||
1363 | # Parser | |
1364 | ||
1365 | def parse_profile(self, public_id, blob): | |
1366 | return ProfileParser(self.backend, public_id, blob) | |
1367 | ||
1368 | # Profiles | |
1369 | ||
1370 | def profile_exists(self, public_id): | |
1371 | res = self.db.get("SELECT id FROM fireinfo_profiles \ | |
1372 | WHERE public_id = %s LIMIT 1", public_id) | |
1373 | ||
1374 | if res: | |
1375 | return True | |
1376 | ||
1377 | return False | |
1378 | ||
1379 | def can_update_profile(self, public_id, private_id, when=None): | |
1380 | res = self.db.get("SELECT 1 FROM fireinfo_profiles \ | |
1381 | WHERE public_id = %s AND private_id = %s \ | |
1382 | AND time_updated + INTERVAL '60 minutes' <= then_or_now(%s) \ | |
1383 | AND time_valid >= then_or_now(%s) ORDER BY time_updated DESC LIMIT 1", | |
1384 | public_id, private_id, when, when) | |
1385 | ||
1386 | if res: | |
1387 | return True | |
1388 | ||
1389 | return False | |
1390 | ||
1391 | def get_profile(self, public_id, private_id=None, when=None): | |
1392 | res = self.db.get("SELECT * FROM fireinfo_profiles \ | |
1393 | WHERE public_id = %s AND \ | |
1394 | (CASE WHEN %s IS NULL THEN TRUE ELSE private_id = %s END) AND \ | |
1395 | (CASE WHEN %s IS NULL THEN TRUE ELSE \ | |
1396 | then_or_now(%s) BETWEEN time_created AND time_valid END) \ | |
1397 | ORDER BY time_updated DESC LIMIT 1", public_id, | |
1398 | private_id, private_id, when, when) | |
1399 | ||
1400 | if res: | |
1401 | return Profile(self.backend, res.id, res) | |
1402 | ||
1403 | def get_profiles(self, public_id): | |
1404 | res = self.db.query("SELECT * FROM fireinfo_profiles \ | |
1405 | WHERE public_id = %s ORDER BY time_created DESC", public_id) | |
1406 | ||
1407 | profiles = [] | |
1408 | for row in res: | |
1409 | profile = Profile(self.backend, row.id, row) | |
1410 | profiles.append(profile) | |
1411 | ||
1412 | return profiles | |
1413 | ||
1414 | def create_profile(self, public_id, private_id, when=None): | |
1415 | valid = self.settings.get_int("fireinfo_profile_days_valid", 14) | |
1416 | ||
1417 | res = self.db.get("INSERT INTO fireinfo_profiles(public_id, private_id, \ | |
1418 | time_created, time_updated, time_valid) VALUES(%s, %s, then_or_now(%s), \ | |
1419 | then_or_now(%s), then_or_now(%s) + INTERVAL '%s days') RETURNING id", | |
1420 | public_id, private_id, when, when, when, valid) | |
1421 | ||
1422 | if res: | |
1423 | return Profile(self.backend, res.id) | |
1424 | ||
1425 | # Devices | |
1426 | ||
1427 | def create_device(self, subsystem, vendor, model, sub_vendor=None, sub_model=None, | |
1428 | driver=None, deviceclass=None): | |
1429 | res = self.db.get("INSERT INTO fireinfo_devices(subsystem, vendor, model, \ | |
1430 | sub_vendor, sub_model, driver, deviceclass) VALUES(%s, %s, %s, %s, %s, %s, %s) \ | |
1431 | RETURNING id", subsystem, vendor, model, sub_vendor, sub_model, driver, deviceclass) | |
1432 | ||
1433 | if res: | |
1434 | return Device(self.backend, res.id) | |
1435 | ||
1436 | def get_device(self, subsystem, vendor, model, sub_vendor=None, sub_model=None, | |
1437 | driver=None, deviceclass=None): | |
1438 | res = self.db.get("SELECT * FROM fireinfo_devices \ | |
1439 | WHERE subsystem = %s AND vendor = %s AND model = %s \ | |
1440 | AND sub_vendor IS NOT DISTINCT FROM %s \ | |
1441 | AND sub_model IS NOT DISTINCT FROM %s \ | |
1442 | AND driver IS NOT DISTINCT FROM %s \ | |
1443 | AND deviceclass IS NOT DISTINCT FROM %s \ | |
1444 | LIMIT 1", subsystem, vendor, model, sub_vendor, | |
1445 | sub_model, driver, deviceclass) | |
1446 | ||
1447 | if res: | |
1448 | return Device(self.backend, res.id, res) | |
1449 | ||
1450 | # System | |
1451 | ||
1452 | def create_system(self, vendor, model): | |
1453 | res = self.db.get("INSERT INTO fireinfo_systems(vendor, model) \ | |
1454 | VALUES(%s, %s) RETURNING id", vendor, model) | |
1455 | ||
1456 | if res: | |
1457 | return res.id | |
1458 | ||
1459 | def get_system(self, vendor, model): | |
1460 | res = self.db.get("SELECT id FROM fireinfo_systems WHERE vendor IS NOT DISTINCT FROM %s \ | |
1461 | AND model IS NOT DISTINCT FROM %s LIMIT 1", vendor, model) | |
1462 | ||
1463 | if res: | |
1464 | return res.id | |
1465 | ||
1466 | # Processors | |
1467 | ||
1468 | def create_processor(self, vendor, model_string, family, model, stepping, core_count, flags=None): | |
1469 | res = self.db.get("INSERT INTO fireinfo_processors(vendor, model_string, \ | |
1470 | family, model, stepping, core_count, flags) VALUES(%s, %s, %s, %s, %s, %s, %s) \ | |
1471 | RETURNING id", vendor, model_string, family, model, stepping, core_count, flags) | |
1472 | ||
1473 | if res: | |
1474 | return Processor(self.backend, res.id) | |
1475 | ||
1476 | def get_processor_by_id(self, processor_id, **kwargs): | |
1477 | res = self.db.get("SELECT * FROM fireinfo_processors \ | |
1478 | WHERE id = %s", processor_id) | |
1479 | ||
1480 | if res: | |
1481 | return Processor(self.backend, res.id, data=res, **kwargs) | |
1482 | ||
1483 | def get_processor(self, vendor, model_string, family, model, stepping, core_count, flags=None): | |
1484 | if flags is None: | |
1485 | flags = [] | |
1486 | ||
1487 | res = self.db.get("SELECT * FROM fireinfo_processors \ | |
1488 | WHERE vendor = %s AND model_string = %s \ | |
1489 | AND family IS NOT DISTINCT FROM %s AND model IS NOT DISTINCT FROM %s \ | |
1490 | AND stepping IS NOT DISTINCT FROM %s AND core_count = %s \ | |
1491 | AND flags <@ %s AND flags @> %s", vendor, model_string, family, model, | |
1492 | stepping, core_count, flags, flags) | |
1493 | ||
1494 | if res: | |
1495 | return Processor(self.backend, res.id, res) | |
1496 | ||
1497 | # Kernel | |
1498 | ||
1499 | def create_kernel(self, kernel): | |
1500 | res = self.db.get("INSERT INTO fireinfo_kernels(name) VALUES(%s) \ | |
1501 | RETURNING id", kernel) | |
1502 | ||
1503 | if res: | |
1504 | return res.id | |
1505 | ||
1506 | def get_kernel(self, kernel): | |
1507 | res = self.db.get("SELECT id FROM fireinfo_kernels WHERE name = %s", kernel) | |
1508 | ||
1509 | if res: | |
1510 | return res.id | |
1511 | ||
1512 | # Arch | |
1513 | ||
1514 | def create_arch(self, arch): | |
1515 | res = self.db.get("INSERT INTO fireinfo_arches(name) VALUES(%s) \ | |
1516 | RETURNING id", arch) | |
1517 | ||
1518 | if res: | |
1519 | return res.id | |
1520 | ||
1521 | def get_arch(self, arch): | |
1522 | res = self.db.get("SELECT id FROM fireinfo_arches WHERE name = %s", arch) | |
1523 | ||
1524 | if res: | |
1525 | return res.id | |
1526 | ||
1527 | # Release | |
1528 | ||
1529 | def create_release(self, release): | |
1530 | res = self.db.get("INSERT INTO fireinfo_releases(name) VALUES(%s) \ | |
1531 | RETURNING id", release) | |
1532 | ||
1533 | if res: | |
1534 | return res.id | |
1535 | ||
1536 | def get_release(self, release): | |
1537 | res = self.db.get("SELECT id FROM fireinfo_releases WHERE name = %s", release) | |
1538 | ||
1539 | if res: | |
1540 | return res.id | |
1541 | ||
1542 | # Hypervisor | |
1543 | ||
1544 | def create_hypervisor(self, hypervisor): | |
1545 | res = self.db.get("INSERT INTO fireinfo_hypervisors(name) VALUES(%s) \ | |
1546 | RETURNING id", hypervisor) | |
1547 | ||
1548 | if res: | |
1549 | return res.id | |
1550 | ||
1551 | def get_hypervisor(self, hypervisor): | |
1552 | res = self.db.get("SELECT id FROM fireinfo_hypervisors WHERE name = %s", | |
1553 | hypervisor) | |
1554 | ||
1555 | if res: | |
1556 | return res.id | |
1557 | ||
1558 | # Handle profile | |
1559 | ||
1560 | def handle_profile(self, *args, **kwargs): | |
1561 | self.db.execute("START TRANSACTION") | |
1562 | ||
1563 | # Wrap all the handling of the profile in a huge transaction. | |
1564 | try: | |
1565 | self._handle_profile(*args, **kwargs) | |
1566 | ||
1567 | except: | |
1568 | self.db.execute("ROLLBACK") | |
1569 | raise | |
1570 | ||
1571 | else: | |
1572 | self.db.execute("COMMIT") | |
1573 | ||
1574 | def _handle_profile(self, public_id, profile_blob, location=None, when=None): | |
1575 | private_id = profile_blob.get("private_id", None) | |
1576 | assert private_id | |
1577 | ||
1578 | # Check if the profile already exists in the database. | |
1579 | profile = self.fireinfo.get_profile(public_id, private_id=private_id, when=when) | |
1580 | ||
1581 | # Check if the update can actually be updated | |
1582 | if profile and not self.fireinfo.can_update_profile(public_id, private_id, when=when): | |
1583 | logging.warning("Profile not updated because private ID does not" | |
1584 | " match or last update is too recent: %s" % public_id) | |
1585 | return | |
1586 | ||
1587 | # Parse the profile | |
1588 | profile_parser = self.parse_profile(public_id, profile_blob) | |
1589 | ||
1590 | # If a profile exists, check if it matches and if so, just update the | |
1591 | # timestamp. | |
1592 | if profile: | |
1593 | # Check if the profile has changed. If so, update the data. | |
1594 | if profile_parser.equals(profile): | |
1595 | profile.updated(profile_parser, location=location, when=when) | |
1596 | return | |
1597 | ||
1598 | # If it does not match, we assume that it is expired and | |
1599 | # create a new profile. | |
1600 | profile.expired(when=when) | |
1601 | ||
1602 | # Replace the old profile with a new one | |
1603 | profile = self.fireinfo.create_profile(public_id, private_id, when=when) | |
1604 | profile.parse(profile_parser) | |
1605 | ||
1606 | if location: | |
1607 | profile.set_location(location) | |
1608 | ||
1609 | return profile | |
1610 | ||
1611 | # Data outputs | |
1612 | ||
1613 | def get_random_profile(self, when=None): | |
1614 | # Check if the architecture exists so that we pick a profile with some data | |
1615 | res = self.db.get("SELECT public_id FROM fireinfo_profiles \ | |
1616 | LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \ | |
1617 | WHERE fireinfo_profiles_arches.profile_id IS NOT NULL \ | |
1618 | AND then_or_now(%s) BETWEEN time_created AND time_valid ORDER BY RANDOM() LIMIT 1", when) | |
1619 | ||
1620 | if res: | |
1621 | return res.public_id | |
1622 | ||
1623 | def get_active_profiles(self, when=None): | |
1624 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \ | |
1625 | SELECT COUNT(*) AS with_data, (SELECT COUNT(*) FROM profiles) AS count FROM profiles \ | |
1626 | LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \ | |
1627 | WHERE fireinfo_profiles_releases.profile_id IS NOT NULL", when) | |
1628 | ||
1629 | if res: | |
1630 | return res.with_data, res.count | |
1631 | ||
1632 | def get_archive_size(self, when=None): | |
1633 | res = self.db.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \ | |
1634 | WHERE time_created <= then_or_now(%s)", when) | |
1635 | ||
1636 | if res: | |
1637 | return res.count | |
1638 | ||
1639 | def get_geo_location_map(self, when=None): | |
1640 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \ | |
1641 | SELECT location, COUNT(location)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \ | |
1642 | LEFT JOIN fireinfo_profiles_locations ON profiles.id = fireinfo_profiles_locations.profile_id \ | |
1643 | WHERE fireinfo_profiles_locations.location IS NOT NULL GROUP BY location ORDER BY count DESC", when) | |
1644 | ||
1645 | return ((r.location, r.count) for r in res) | |
1646 | ||
1647 | def get_language_map(self, when=None): | |
1648 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \ | |
1649 | SELECT language, COUNT(language)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \ | |
1650 | LEFT JOIN fireinfo_profiles_languages ON profiles.id = fireinfo_profiles_languages.profile_id \ | |
1651 | WHERE fireinfo_profiles_languages.language IS NOT NULL GROUP BY language ORDER BY count DESC", when) | |
1652 | ||
1653 | return ((r.language, r.count) for r in res) | |
1654 | ||
1655 | @property | |
1656 | def cpu_vendors(self): | |
1657 | res = self.db.query("SELECT DISTINCT vendor FROM fireinfo_processors ORDER BY vendor") | |
1658 | ||
1659 | return (CPU_VENDORS.get(r.vendor, r.vendor) for r in res) | |
1660 | ||
1661 | def get_cpu_vendors_map(self, when=None): | |
1662 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1663 | SELECT vendor, COUNT(vendor)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \ | |
1664 | LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \ | |
1665 | LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \ | |
1666 | WHERE NOT fireinfo_profiles_processors.processor_id IS NULL GROUP BY vendor ORDER BY count DESC", when) | |
1667 | ||
1668 | return ((CPU_VENDORS.get(r.vendor, r.vendor), r.count) for r in res) | |
1669 | ||
1670 | def get_cpu_clock_speeds(self, when=None): | |
1671 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1672 | SELECT AVG(fireinfo_profiles_processors.clock_speed) AS avg, \ | |
1673 | STDDEV(fireinfo_profiles_processors.clock_speed) AS stddev, \ | |
1674 | MIN(fireinfo_profiles_processors.clock_speed) AS min, \ | |
1675 | MAX(fireinfo_profiles_processors.clock_speed) AS max FROM profiles \ | |
1676 | LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \ | |
1677 | WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \ | |
1678 | AND fireinfo_profiles_processors.clock_speed > 0", when) | |
1679 | ||
1680 | if res: | |
1681 | return (res.avg or 0, res.stddev or 0, res.min or 0, res.max or 0) | |
1682 | ||
1683 | def get_cpus_with_platform_and_flag(self, platform, flag, when=None): | |
1684 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \ | |
1685 | processors AS (SELECT fireinfo_processors.id AS id, fireinfo_processors.flags AS flags FROM profiles \ | |
1686 | LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \ | |
1687 | LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \ | |
1688 | LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \ | |
1689 | LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \ | |
1690 | WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \ | |
1691 | AND fireinfo_arches.platform = %s AND NOT 'hypervisor' = ANY(fireinfo_processors.flags)) \ | |
1692 | SELECT (COUNT(*)::float / (SELECT NULLIF(COUNT(*), 0) FROM processors)) AS count FROM processors \ | |
1693 | WHERE %s = ANY(processors.flags)", when, platform, flag) | |
1694 | ||
1695 | return res.count or 0 | |
1696 | ||
1697 | def get_common_cpu_flags_by_platform(self, platform, when=None): | |
1698 | if platform == "arm": | |
1699 | flags = ( | |
1700 | "lpae", "neon", "thumb", "thumb2", "thumbee", "vfpv3", "vfpv4", | |
1701 | ) | |
1702 | elif platform == "x86": | |
1703 | flags = ( | |
1704 | "aes", "avx", "avx2", "lm", "mmx", "mmxext", "nx", "pae", | |
1705 | "pni", "popcnt", "sse", "sse2", "rdrand", "ssse3", "sse4a", | |
1706 | "sse4_1", "sse4_2" | |
1707 | ) | |
1708 | else: | |
1709 | return | |
1710 | ||
1711 | ret = [] | |
1712 | for flag in flags: | |
1713 | ret.append((flag, self.get_cpus_with_platform_and_flag(platform, flag, when=when))) | |
1714 | ||
1715 | # Add virtual CPU flag "virt" for virtualization support | |
1716 | if platform == "x86": | |
1717 | ret.append(("virt", | |
1718 | self.get_cpus_with_platform_and_flag(platform, "vmx", when=when) + \ | |
1719 | self.get_cpus_with_platform_and_flag(platform, "svm", when=when))) | |
1720 | ||
1721 | return sorted(ret, key=lambda x: x[1], reverse=True) | |
1722 | ||
1723 | def get_common_memory_amounts(self, when=None): | |
1724 | amounts = (128, 256, 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024) | |
1725 | ||
1726 | ret = [] | |
1727 | for amount in amounts: | |
1728 | ret.append((amount, self.get_memory_amount(amount * 1024 * 0.95, when=when))) | |
1729 | ||
1730 | return ret | |
1731 | ||
1732 | def get_memory_amount(self, greather_than, when=None): | |
1733 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1734 | SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles \ | |
1735 | LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \ | |
1736 | WHERE NOT fireinfo_profiles_memory.amount IS NULL) AS percentage FROM profiles \ | |
1737 | LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \ | |
1738 | WHERE fireinfo_profiles_memory.amount >= %s", when, greather_than) | |
1739 | ||
1740 | if res: | |
1741 | return res.percentage | |
1742 | ||
1743 | def get_memory_amounts(self, when=None): | |
1744 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1745 | SELECT AVG(fireinfo_profiles_memory.amount) AS avg, \ | |
1746 | STDDEV(fireinfo_profiles_memory.amount) AS stddev, \ | |
1747 | MIN(fireinfo_profiles_memory.amount) AS min, \ | |
1748 | MAX(fireinfo_profiles_memory.amount) AS max FROM profiles \ | |
1749 | LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id", when) | |
1750 | ||
1751 | if res: | |
1752 | return (res.avg or 0, res.stddev or 0, res.min or 0, res.max or 0) | |
1753 | ||
1754 | def get_arch_map(self, when=None): | |
1755 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1756 | SELECT fireinfo_arches.name AS arch, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count \ | |
1757 | FROM profiles \ | |
1758 | LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \ | |
1759 | LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \ | |
1760 | WHERE NOT fireinfo_profiles_arches.profile_id IS NULL \ | |
1761 | GROUP BY fireinfo_arches.id ORDER BY count DESC", when) | |
1762 | ||
1763 | return ((r.arch, r.count) for r in res) | |
1764 | ||
1765 | # Virtual | |
1766 | ||
1767 | def get_hypervisor_map(self, when=None): | |
1768 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \ | |
1769 | virtual_profiles AS (SELECT profiles.id AS profile_id, fireinfo_profiles_virtual.hypervisor_id FROM profiles \ | |
1770 | LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \ | |
1771 | WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL) \ | |
1772 | SELECT COALESCE(fireinfo_hypervisors.name, %s) AS name, \ | |
1773 | COUNT(*)::float / (SELECT COUNT(*) FROM virtual_profiles) AS count FROM virtual_profiles \ | |
1774 | LEFT JOIN fireinfo_hypervisors ON virtual_profiles.hypervisor_id = fireinfo_hypervisors.id \ | |
1775 | GROUP BY fireinfo_hypervisors.name ORDER BY count DESC", when, "unknown") | |
1776 | ||
1777 | return ((r.name, r.count) for r in res) | |
1778 | ||
1779 | def get_virtual_ratio(self, when=None): | |
1780 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1781 | SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \ | |
1782 | LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \ | |
1783 | WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL", when) | |
1784 | ||
1785 | if res: | |
1786 | return res.count | |
1787 | ||
1788 | # Releases | |
1789 | ||
1790 | def get_releases_map(self, when=None): | |
1791 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1792 | SELECT fireinfo_releases.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \ | |
1793 | LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \ | |
1794 | LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \ | |
1795 | GROUP BY fireinfo_releases.name ORDER BY count DESC", when) | |
1796 | ||
1797 | return ((r.name, r.count) for r in res) | |
1798 | ||
1799 | def get_kernels_map(self, when=None): | |
1800 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1801 | SELECT fireinfo_kernels.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \ | |
1802 | LEFT JOIN fireinfo_profiles_kernels ON profiles.id = fireinfo_profiles_kernels.profile_id \ | |
1803 | LEFT JOIN fireinfo_kernels ON fireinfo_profiles_kernels.kernel_id = fireinfo_kernels.id \ | |
1804 | GROUP BY fireinfo_kernels.name ORDER BY count DESC", when) | |
1805 | ||
1806 | return ((r.name, r.count) for r in res) | |
1807 | ||
1808 | def _process_devices(self, devices): | |
1809 | result = [] | |
1810 | ||
1811 | for dev in devices: | |
1812 | dev = Device(self.backend, dev.get("id", None), dev) | |
1813 | result.append(dev) | |
1814 | ||
1815 | return result | |
1816 | ||
1817 | def get_driver_map(self, driver, when=None): | |
1818 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \ | |
1819 | devices AS (SELECT * FROM profiles \ | |
1820 | LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \ | |
1821 | LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \ | |
1822 | WHERE driver = %s) \ | |
1823 | SELECT subsystem, model, vendor, driver, deviceclass, \ | |
1824 | COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \ | |
1825 | GROUP BY subsystem, model, vendor, driver, deviceclass \ | |
1826 | ORDER BY percentage DESC", when, driver) | |
1827 | ||
1828 | return self._process_devices(res) | |
1829 | ||
1830 | subsystem2class = { | |
1831 | "pci" : hwdata.PCI(), | |
1832 | "usb" : hwdata.USB(), | |
1833 | } | |
1834 | ||
1835 | def get_vendor_string(self, subsystem, vendor_id): | |
1836 | try: | |
1837 | cls = self.subsystem2class[subsystem] | |
1838 | except KeyError: | |
1839 | return | |
1840 | ||
1841 | return cls.get_vendor(vendor_id) | |
1842 | ||
1843 | def get_model_string(self, subsystem, vendor_id, model_id): | |
1844 | try: | |
1845 | cls = self.subsystem2class[subsystem] | |
1846 | except KeyError: | |
1847 | return | |
1848 | ||
1849 | return cls.get_device(vendor_id, model_id) | |
1850 | ||
1851 | def get_vendor_list(self, when=None): | |
1852 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1853 | SELECT DISTINCT fireinfo_devices.subsystem AS subsystem, fireinfo_devices.vendor AS vendor FROM profiles \ | |
1854 | LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \ | |
1855 | LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \ | |
1856 | WHERE NOT fireinfo_devices.driver = ANY(%s)", when, IGNORED_DEVICES) | |
1857 | ||
1858 | vendors = {} | |
1859 | for row in res: | |
1860 | vendor = self.get_vendor_string(row.subsystem, row.vendor) | |
1861 | ||
1862 | # Drop if vendor could not be determined | |
1863 | if vendor is None: | |
1864 | continue | |
1865 | ||
1866 | try: | |
1867 | vendors[vendor].append((row.subsystem, row.vendor)) | |
1868 | except KeyError: | |
1869 | vendors[vendor] = [(row.subsystem, row.vendor)] | |
1870 | ||
1871 | vendors = vendors.items() | |
1872 | return sorted(vendors) | |
1873 | ||
1874 | def get_devices_by_vendor(self, subsystem, vendor, when=None): | |
1875 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \ | |
1876 | devices AS (SELECT * FROM profiles \ | |
1877 | LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \ | |
1878 | LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \ | |
1879 | WHERE NOT fireinfo_devices.driver = ANY(%s)), \ | |
1880 | vendor_devices AS (SELECT * FROM devices WHERE devices.subsystem = %s AND devices.vendor = %s) \ | |
1881 | SELECT subsystem, model, vendor, driver, deviceclass FROM vendor_devices \ | |
1882 | GROUP BY subsystem, model, vendor, driver, deviceclass", when, IGNORED_DEVICES, subsystem, vendor) | |
1883 | ||
1884 | return self._process_devices(res) | |
1885 | ||
1886 | def get_device_percentage(self, subsystem, vendor, model, when=None): | |
1887 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \ | |
1888 | devices AS (SELECT * FROM profiles \ | |
1889 | LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \ | |
1890 | LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id) \ | |
1891 | SELECT COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \ | |
1892 | WHERE devices.subsystem = %s AND devices.vendor = %s AND devices.model = %s", | |
1893 | when, subsystem, vendor, model) | |
1894 | ||
1895 | if res: | |
1896 | return res.percentage | |
1897 | ||
1898 | def get_device_in_profile(self, subsystem, vendor, model, limit=10, when=None): | |
1899 | res = self.db.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \ | |
1900 | profiles_with_device AS (SELECT DISTINCT fireinfo_profiles.public_id FROM profiles \ | |
1901 | LEFT JOIN fireinfo_profiles ON profiles.id = fireinfo_profiles.id \ | |
1902 | LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \ | |
1903 | LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \ | |
1904 | WHERE fireinfo_devices.subsystem = %s AND fireinfo_devices.vendor = %s \ | |
1905 | AND fireinfo_devices.model = %s) \ | |
1906 | SELECT * FROM profiles_with_device ORDER BY RANDOM() LIMIT %s", | |
1907 | when, subsystem, vendor, model, limit) | |
1908 | ||
1909 | return (r.public_id for r in res) | |
1910 | ||
1911 | def get_network_zones_map(self, when=None): | |
1912 | res = self.db.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \ | |
1913 | SELECT COUNT(NULLIF(has_red, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_red, \ | |
1914 | COUNT(NULLIF(has_green, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_green, \ | |
1915 | COUNT(NULLIF(has_orange, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_orange, \ | |
1916 | COUNT(NULLIF(has_blue, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_blue FROM profiles \ | |
1917 | LEFT JOIN fireinfo_profiles_networks ON profiles.id = fireinfo_profiles_networks.profile_id \ | |
1918 | WHERE fireinfo_profiles_networks.profile_id IS NOT NULL", when) | |
1919 | ||
1920 | return res |