3 from __future__
import division
11 from misc
import Object
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",
36 (r
"AMD (Sempron)\(tm\) (\d+) APU with Radeon\(tm\) R\d+", r
"AMD \1 \2 APU"),
37 (r
"AMD ([\w\-]+) APU with Radeon\(tm\) HD Graphics", r
"AMD \1 APU"),
38 (r
"AMD ([\w\-]+) Radeon R\d+, \d+ Compute Cores \d+C\+\d+G", r
"AMD \1 APU"),
40 (r
"AMD Athlon.* II X2 ([a-z0-9]+).*", r
"AMD Athlon X2 \1"),
41 (r
"AMD Athlon\(tm\) 64 Processor (\w+)", r
"AMD Athlon64 \1"),
42 (r
"AMD Athlon\(tm\) 64 X2 Dual Core Processor (\w+)", r
"AMD Athlon64 X2 \1"),
43 (r
"(AMD Athlon).*(XP).*", r
"\1 \2"),
44 (r
"(AMD Phenom).* ([0-9]+) .*", r
"\1 \2"),
45 (r
"(AMD Phenom).*", r
"\1"),
46 (r
"(AMD Sempron).*", r
"\1"),
48 (r
"Geode\(TM\) Integrated Processor by AMD PCS", r
"AMD Geode"),
49 (r
"(Geode).*", r
"\1"),
51 (r
"Mobile AMD (Athlon|Sempron)\(tm\) Processor (\d+\+?)", r
"AMD \1-M \2"),
54 (r
"Intel\(R\) (Atom|Celeron).*CPU\s*([A-Z0-9]+) .*", r
"Intel \1 \2"),
55 (r
"(Intel).*(Celeron).*", r
"\1 \2"),
56 (r
"Intel\(R\)? Core\(TM\)?2 Duo *CPU .* ([A-Z0-9]+) .*", r
"Intel C2D \1"),
57 (r
"Intel\(R\)? Core\(TM\)?2 Duo CPU (\w+)", r
"Intel C2D \1"),
58 (r
"Intel\(R\)? Core\(TM\)?2 CPU .* ([A-Z0-9]+) .*", r
"Intel C2 \1"),
59 (r
"Intel\(R\)? Core\(TM\)?2 Quad *CPU .* ([A-Z0-9]+) .*", r
"Intel C2Q \1"),
60 (r
"Intel\(R\)? Core\(TM\)? (i[753]\-\w+) CPU", r
"Intel Core \1"),
61 (r
"Intel\(R\)? Xeon\(R\)? CPU (\w+) (0|v\d+)", r
"Intel Xeon \1 \2"),
62 (r
"Intel\(R\)? Xeon\(R\)? CPU\s+(\w+)", r
"Intel Xeon \1"),
63 (r
"(Intel).*(Xeon).*", r
"\1 \2"),
64 (r
"Intel.* Pentium.* (D|4) .*", r
"Intel Pentium \1"),
65 (r
"Intel.* Pentium.* Dual .* ([A-Z0-9]+) .*", r
"Intel Pentium Dual \1"),
66 (r
"Pentium.* Dual-Core .* ([A-Z0-9]+) .*", r
"Intel Pentium Dual \1"),
67 (r
"(Pentium I{2,3}).*", r
"Intel \1"),
68 (r
"(Celeron \(Coppermine\))", r
"Intel Celeron"),
71 (r
"Geode\(TM\) Integrated Processor by National Semi", r
"NSC Geode"),
74 (r
"(VIA \w*).*", r
"\1"),
77 (r
"QEMU Virtual CPU version .*", r
"QEMU CPU"),
80 (r
"Feroceon .*", r
"ARM Feroceon"),
83 IGNORED_DEVICES
= ["usb",]
85 class ProfileDict(object):
86 def __init__(self
, data
):
90 class ProfileNetwork(ProfileDict
):
91 def __eq__(self
, other
):
95 if not self
.has_red
== other
.has_red
:
98 if not self
.has_green
== other
.has_green
:
101 if not self
.has_orange
== other
.has_orange
:
104 if not self
.has_blue
== other
.has_blue
:
112 for zone
in ("red", "green", "orange", "blue"):
113 if self
.has_zone(zone
):
118 def has_zone(self
, name
):
119 return self
._data
.get("has_%s" % name
)
123 return self
._data
.get("has_red", False)
127 return self
._data
.get("has_green", False)
130 def has_orange(self
):
131 return self
._data
.get("has_orange", False)
135 return self
._data
.get("has_blue", False)
138 class Processor(Object
):
139 def __init__(self
, backend
, id, data
=None, clock_speed
=None, bogomips
=None):
140 Object
.__init
__(self
, backend
)
144 self
.__clock
_speed
= clock_speed
145 self
.__bogomips
= bogomips
150 if not self
.model_string
.startswith(self
.vendor
):
151 s
.append(self
.vendor
)
154 s
.append(self
.model_string
)
156 if self
.core_count
> 1:
157 s
.append("x%s" % self
.core_count
)
163 if self
.__data
is None:
164 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_processors \
165 WHERE id = %s", self
.id)
172 return CPU_VENDORS
[self
.data
.vendor
]
174 return self
.data
.vendor
178 return self
.data
.family
182 return self
.data
.model
186 return self
.data
.stepping
189 def model_string(self
):
190 s
= self
.data
.model_string
.split()
192 return " ".join((e
for e
in s
if e
))
196 return self
.data
.flags
198 def has_flag(self
, flag
):
199 return flag
in self
.flags
202 if self
.vendor
== "Intel" and self
.family
== 6 and self
.model
in (15, 55, 76, 77):
205 return self
.has_flag("ht")
208 def core_count(self
):
209 return self
.data
.core_count
214 return self
.core_count
// 2
216 return self
.core_count
219 def clock_speed(self
):
220 return self
.__clock
_speed
222 def format_clock_speed(self
):
223 if not self
.clock_speed
:
226 if self
.clock_speed
< 1000:
227 return "%dMHz" % self
.clock_speed
229 return "%.2fGHz" % round(self
.clock_speed
/ 1000, 2)
233 return self
.__bogomips
236 def capabilities(self
):
238 ("64bit", self
.has_flag("lm")),
239 ("aes", self
.has_flag("aes")),
240 ("nx", self
.has_flag("nx")),
241 ("pae", self
.has_flag("pae") or self
.has_flag("lpae")),
242 ("rdrand", self
.has_flag("rdrand")),
245 # If the system is already running in a virtual environment,
246 # we cannot correctly detect if the CPU supports svm or vmx
247 if self
.has_flag("hypervisor"):
248 caps
.append(("virt", None))
250 caps
.append(("virt", self
.has_flag("vmx") or self
.has_flag("svm")))
254 def format_model(self
):
255 s
= self
.model_string
257 # Remove everything after the @: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
258 s
, sep
, rest
= s
.partition("@")
260 for pattern
, repl
in CPU_STRINGS
:
261 if re
.match(pattern
, s
) is None:
264 s
= re
.sub(pattern
, repl
, s
)
267 # Otherwise remove the symbols
268 for i
in ("C", "R", "TM", "tm"):
269 s
= s
.replace("(%s)" % i
, "")
271 # Replace too long strings with shorter ones
273 ("Quad-Core Processor", ""),
274 ("Dual-Core Processor", ""),
275 ("Processor", "CPU"),
276 ("processor", "CPU"),
281 # Remove too many spaces
282 s
= " ".join((e
for e
in s
.split() if e
))
287 def friendly_string(self
):
290 model
= self
.format_model()
293 clock_speed
= self
.format_clock_speed()
295 s
.append("@ %s" % clock_speed
)
298 s
.append("x%s" % self
.count
)
303 class Device(Object
):
306 "00" : N_("Unclassified"),
307 "01" : N_("Mass storage"),
308 "02" : N_("Network"),
309 "03" : N_("Display"),
310 "04" : N_("Multimedia"),
311 "05" : N_("Memory controller"),
313 "07" : N_("Communication"),
314 "08" : N_("Generic system peripheral"),
315 "09" : N_("Input device"),
316 "0a" : N_("Docking station"),
317 "0b" : N_("Processor"),
318 "0c" : N_("Serial bus"),
319 "0d" : N_("Wireless"),
320 "0e" : N_("Intelligent controller"),
321 "0f" : N_("Satellite communications controller"),
322 "10" : N_("Encryption"),
323 "11" : N_("Signal processing controller"),
324 "ff" : N_("Unassigned class"),
328 "00" : N_("Unclassified"),
329 "01" : N_("Multimedia"),
330 "02" : N_("Communication"),
331 "03" : N_("Input device"),
332 "05" : N_("Generic system peripheral"),
334 "07" : N_("Printer"),
335 "08" : N_("Mass storage"),
337 "0a" : N_("Communication"),
338 "0b" : N_("Smart card"),
339 "0d" : N_("Encryption"),
340 "0e" : N_("Display"),
341 "0f" : N_("Personal Healthcare"),
342 "dc" : N_("Diagnostic Device"),
343 "e0" : N_("Wireless"),
344 "ef" : N_("Unclassified"),
345 "fe" : N_("Unclassified"),
346 "ff" : N_("Unclassified"),
350 def __init__(self
, backend
, id, data
=None):
351 Object
.__init
__(self
, backend
)
357 return "<%s vendor=%s model=%s>" % (self
.__class
__.__name
__,
358 self
.vendor_string
, self
.model_string
)
360 def __cmp__(self
, other
):
361 if self
.id and self
.id == other
.id:
365 cmp(self
.subsystem
, other
.subsystem
) or \
366 cmp(self
.vendor_string
, other
.vendor_string
) or \
367 cmp(self
.vendor
, other
.vendor
) or \
368 cmp(self
.model_string
, other
.model_string
) or \
369 cmp(self
.model
, other
.model
) or \
370 cmp(self
.driver
, other
.driver
)
374 if self
.__data
is None:
377 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_devices \
378 WHERE id = %s", self
.id)
382 def is_showable(self
):
383 if self
.driver
in IGNORED_DEVICES
:
386 if self
.driver
in ("pcieport", "hub"):
393 return self
.data
.subsystem
397 return self
.data
.model
400 def model_string(self
):
401 return self
.fireinfo
.get_model_string(self
.subsystem
,
402 self
.vendor
, self
.model
)
406 return self
.data
.vendor
409 def vendor_string(self
):
410 return self
.fireinfo
.get_vendor_string(self
.subsystem
, self
.vendor
)
414 return self
.data
.driver
418 classid
= self
.data
.deviceclass
420 if self
.subsystem
== "pci":
421 classid
= classid
[:-4]
422 if len(classid
) == 1:
423 classid
= "0%s" % classid
425 elif self
.subsystem
== "usb" and classid
:
426 classid
= classid
.split("/")[0]
427 classid
= "%02x" % int(classid
)
430 return self
.classid2name
[self
.subsystem
][classid
]
435 def percentage(self
):
436 return self
.data
.get("percentage", None)
439 class Profile(Object
):
440 def __init__(self
, backend
, id, data
=None):
441 Object
.__init
__(self
, backend
)
447 return "<%s %s>" % (self
.__class
__.__name
__, self
.public_id
)
449 def __cmp__(self
, other
):
450 return cmp(self
.id, other
.id)
452 def is_showable(self
):
460 if self
.__data
is None:
461 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_profiles \
462 WHERE id = %s", self
.id)
468 return self
.data
.public_id
471 def private_id(self
):
472 raise NotImplementedError
475 def time_created(self
):
476 return self
.data
.time_created
479 def time_updated(self
):
480 return self
.data
.time_updated
482 def updated(self
, profile_parser
=None, location
=None, when
=None):
483 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
485 self
.db
.execute("UPDATE fireinfo_profiles \
487 time_updated = then_or_now(%s), \
488 time_valid = then_or_now(%s) + INTERVAL '%s days', \
489 updates = updates + 1 \
490 WHERE id = %s", when
, when
, valid
, self
.id)
493 self
.set_processor_speeds(
494 profile_parser
.processor_clock_speed
,
495 profile_parser
.processor_bogomips
,
499 self
.set_location(location
)
501 self
.log_profile_update()
503 def log_profile_update(self
):
504 # Log that an update was performed for this profile id
505 self
.db
.execute("INSERT INTO fireinfo_profiles_log(public_id) \
506 VALUES(%s)", self
.public_id
)
508 def expired(self
, when
=None):
509 self
.db
.execute("UPDATE fireinfo_profiles \
510 SET time_valid = then_or_now(%s) WHERE id = %s", when
, self
.id)
512 def parse(self
, parser
):
514 self
.processor
= parser
.processor
515 self
.set_processor_speeds(parser
.processor_clock_speed
, parser
.processor_bogomips
)
518 self
.devices
= parser
.devices
521 self
.system_id
= parser
.system_id
524 self
.memory
= parser
.memory
527 self
.storage
= parser
.storage
530 self
.kernel_id
= parser
.kernel_id
533 self
.arch_id
= parser
.arch_id
536 self
.release_id
= parser
.release_id
539 self
.language
= parser
.language
543 self
.hypervisor_id
= parser
.hypervisor_id
546 self
.network
= parser
.network
550 def get_location(self
):
551 if not hasattr(self
, "_location"):
552 res
= self
.db
.get("SELECT location FROM fireinfo_profiles_locations \
553 WHERE profile_id = %s", self
.id)
556 self
._location
= res
.location
558 self
._location
= None
560 return self
._location
562 def set_location(self
, location
):
563 if self
.location
== location
:
566 self
.db
.execute("DELETE FROM fireinfo_profiles_locations \
567 WHERE profile_id = %s", self
.id)
568 self
.db
.execute("INSERT INTO fireinfo_profiles_locations(profile_id, location) \
569 VALUES(%s, %s)", self
.id, location
)
571 self
._location
= location
573 location
= property(get_location
, set_location
)
576 def location_string(self
):
577 return self
.geoip
.get_country_name(self
.location
)
582 def device_ids(self
):
583 if not hasattr(self
, "_device_ids"):
584 res
= self
.db
.query("SELECT device_id FROM fireinfo_profiles_devices \
585 WHERE profile_id = %s", self
.id)
587 self
._device
_ids
= sorted([r
.device_id
for r
in res
])
589 return self
._device
_ids
591 def get_devices(self
):
592 if not hasattr(self
, "_devices"):
593 res
= self
.db
.query("SELECT * FROM fireinfo_devices \
594 LEFT JOIN fireinfo_profiles_devices ON \
595 fireinfo_devices.id = fireinfo_profiles_devices.device_id \
596 WHERE fireinfo_profiles_devices.profile_id = %s", self
.id)
600 device
= Device(self
.backend
, row
.id, row
)
601 self
._devices
.append(device
)
605 def set_devices(self
, devices
):
606 device_ids
= [d
.id for d
in devices
]
608 self
.db
.execute("DELETE FROM fireinfo_profiles_devices WHERE profile_id = %s", self
.id)
609 self
.db
.executemany("INSERT INTO fireinfo_profiles_devices(profile_id, device_id) \
610 VALUES(%s, %s)", ((self
.id, d
) for d
in device_ids
))
612 self
._devices
= devices
613 self
._device
_ids
= device_ids
615 devices
= property(get_devices
, set_devices
)
617 def count_device(self
, subsystem
, vendor
, model
):
620 for dev
in self
.devices
:
621 if dev
.subsystem
== subsystem
and dev
.vendor
== vendor
and dev
.model
== model
:
628 def get_system_id(self
):
629 if not hasattr(self
, "_system_id"):
630 res
= self
.db
.get("SELECT system_id AS id FROM fireinfo_profiles_systems \
631 WHERE profile_id = %s", self
.id)
634 self
._system
_id
= res
.id
636 self
._system
_id
= None
638 return self
._system
_id
640 def set_system_id(self
, system_id
):
641 self
.db
.execute("DELETE FROM fireinfo_profiles_systems WHERE profile_id = %s", self
.id)
644 self
.db
.execute("INSERT INTO fireinfo_profiles_systems(profile_id, system_id) \
645 VALUES(%s, %s)", self
.id, system_id
)
647 self
._system
_id
= None
648 if hasattr(self
, "_system"):
651 system_id
= property(get_system_id
, set_system_id
)
655 if not hasattr(self
, "_system"):
656 res
= self
.db
.get("SELECT fireinfo_systems.vendor AS vendor, fireinfo_systems.model AS model \
657 FROM fireinfo_profiles_systems \
658 LEFT JOIN fireinfo_systems ON fireinfo_profiles_systems.system_id = fireinfo_systems.id \
659 WHERE fireinfo_profiles_systems.profile_id = %s", self
.id)
662 self
._system
= (res
.vendor
, res
.model
)
664 self
._system
= (None, None)
669 def system_vendor(self
):
677 def system_model(self
):
685 def appliance_id(self
):
686 if not hasattr(self
, "_appliance_id"):
688 ("fountainnetworks-prime", self
._appliance
_test
_fountainnetworks
_prime
),
689 ("lightningwirelabs-eco-plus", self
._appliance
_test
_lightningwirelabs
_eco
_plus
),
690 ("lightningwirelabs-eco", self
._appliance
_test
_lightningwirelabs
_eco
),
693 self
._appliance
_id
= None
694 for name
, test_func
in appliances
:
698 self
._appliance
_id
= name
701 return self
._appliance
_id
705 if self
.appliance_id
== "fountainnetworks-prime":
706 return "Fountain Networks - IPFire Prime Box"
708 elif self
.appliance_id
== "lightningwirelabs-eco-plus":
709 return "Lightning Wire Labs - IPFire Eco Plus Appliance"
711 elif self
.appliance_id
== "lightningwirelabs-eco":
712 return "Lightning Wire Labs - IPFire Eco Appliance"
714 def _appliance_test_fountainnetworks_prime(self
):
715 if not self
.system
in (("SECO", None), ("SECO", "0949")):
718 # Must have a wireless device
719 if self
.count_device("usb", "148f", "5572") < 1:
724 def _appliance_test_lightningwirelabs_eco(self
):
725 if not self
.system
== ("MSI", "MS-9877"):
728 # Must have four Intel network adapters
729 network_adapters_count
= self
.count_device("pci", "8086", "10d3")
730 if not network_adapters_count
== 4:
735 def _appliance_test_lightningwirelabs_eco_plus(self
):
736 if not self
.system_vendor
== "ASUS":
739 if not self
.system_model
.startswith("P9A-I/2550"):
742 # Must have four Intel network adapters
743 network_adapters_count
= self
.count_device("pci", "8086", "1f41")
744 if not network_adapters_count
== 4:
752 def processor_id(self
):
753 if hasattr(self
, "_processor"):
754 return self
._processor
.id
756 if not hasattr(self
, "_processor_id"):
757 res
= self
.db
.get("SELECT processor_id FROM fireinfo_profiles_processors \
758 WHERE profile_id = %s", self
.id)
761 self
._processor
_id
= res
.processor_id
763 self
._processor
_id
= None
765 return self
._processor
_id
767 def get_processor(self
):
768 if not self
.processor_id
:
771 if not hasattr(self
, "_processor"):
772 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_processors \
773 WHERE profile_id = %s", self
.id)
776 self
._processor
= self
.fireinfo
.get_processor_by_id(res
.processor_id
,
777 clock_speed
=res
.clock_speed
, bogomips
=res
.bogomips
)
779 self
._processor
= None
781 return self
._processor
783 def set_processor(self
, processor
):
784 self
.db
.execute("DELETE FROM fireinfo_profiles_processors \
785 WHERE profile_id = %s", self
.id)
788 self
.db
.execute("INSERT INTO fireinfo_profiles_processors(profile_id, processor_id) \
789 VALUES(%s, %s)", self
.id, processor
.id)
791 self
._processor
= processor
793 processor
= property(get_processor
, set_processor
)
795 def set_processor_speeds(self
, clock_speed
, bogomips
):
796 self
.db
.execute("UPDATE fireinfo_profiles_processors \
797 SET clock_speed = %s, bogomips = %s WHERE profile_id = %s",
798 clock_speed
, bogomips
, self
.id)
803 return self
.processor
807 def get_memory(self
):
808 if not hasattr(self
, "_memory"):
809 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_memory \
810 WHERE profile_id = %s", self
.id)
813 self
._memory
= res
.amount
* 1024
819 def set_memory(self
, amount
):
820 if self
.memory
== amount
:
825 self
.db
.execute("DELETE FROM fireinfo_profiles_memory WHERE profile_id = %s", self
.id)
827 self
.db
.execute("INSERT INTO fireinfo_profiles_memory(profile_id, amount) \
828 VALUES(%s, %s)", self
.id, amount
)
830 self
._memory
= amount
* 1024
832 memory
= property(get_memory
, set_memory
)
835 def friendly_memory(self
):
836 return util
.format_size(self
.memory
or 0)
840 def get_storage(self
):
841 if not hasattr(self
, "_storage"):
842 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_storage \
843 WHERE profile_id = %s", self
.id)
846 self
._storage
= res
.amount
* 1024
852 def set_storage(self
, amount
):
853 if self
.storage
== amount
:
858 self
.db
.execute("DELETE FROM fireinfo_profiles_storage WHERE profile_id = %s", self
.id)
860 self
.db
.execute("INSERT INTO fireinfo_profiles_storage(profile_id, amount) \
861 VALUES(%s, %s)", self
.id, amount
)
863 self
._storage
= amount
* 1024
865 storage
= property(get_storage
, set_storage
)
868 def friendly_storage(self
):
869 return util
.format_size(self
.storage
)
873 def get_kernel_id(self
):
874 if not hasattr(self
, "_kernel_id"):
875 res
= self
.db
.get("SELECT fireinfo_profiles_kernels.kernel_id AS id FROM fireinfo_profiles \
876 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
877 WHERE fireinfo_profiles.id = %s", self
.id)
880 self
._kernel
_id
= res
.id
882 self
._kernel
_id
= None
884 return self
._kernel
_id
886 def set_kernel_id(self
, kernel_id
):
887 if self
.kernel_id
== kernel_id
:
890 self
.db
.execute("DELETE FROM fireinfo_profiles_kernels WHERE profile_id = %s", self
.id)
892 self
.db
.execute("INSERT INTO fireinfo_profiles_kernels(profile_id, kernel_id) \
893 VALUES(%s, %s)", self
.id, kernel_id
)
895 self
._kernel
_id
= kernel_id
896 if hasattr(self
, "_kernel"):
899 kernel_id
= property(get_kernel_id
, set_kernel_id
)
903 if not hasattr(self
, "_kernel"):
904 res
= self
.db
.get("SELECT fireinfo_kernels.name AS name FROM fireinfo_profiles \
905 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
906 LEFT JOIN fireinfo_kernels ON fireinfo_kernels.id = fireinfo_profiles_kernels.kernel_id \
907 WHERE fireinfo_profiles.id = %s", self
.id)
910 self
._kernel
= res
.name
918 def get_arch_id(self
):
919 if not hasattr(self
, "_arch_id"):
920 res
= self
.db
.get("SELECT fireinfo_profiles_arches.arch_id AS id FROM fireinfo_profiles \
921 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
922 WHERE fireinfo_profiles.id = %s", self
.id)
925 self
._arch
_id
= res
.id
931 def set_arch_id(self
, arch_id
):
932 if self
.arch_id
== arch_id
:
935 self
.db
.execute("DELETE FROM fireinfo_profiles_arches WHERE profile_id = %s", self
.id)
937 self
.db
.execute("INSERT INTO fireinfo_profiles_arches(profile_id, arch_id) \
938 VALUES(%s, %s)", self
.id, arch_id
)
941 if hasattr(self
, "_arch"):
944 arch_id
= property(get_arch_id
, set_arch_id
)
948 if not hasattr(self
, "_arch"):
949 res
= self
.db
.get("SELECT fireinfo_arches.name AS name FROM fireinfo_profiles \
950 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
951 LEFT JOIN fireinfo_arches ON fireinfo_arches.id = fireinfo_profiles_arches.arch_id \
952 WHERE fireinfo_profiles.id = %s", self
.id)
955 self
._arch
= res
.name
963 def get_release_id(self
):
964 if not hasattr(self
, "_release_id"):
965 res
= self
.db
.get("SELECT fireinfo_profiles_releases.release_id AS id FROM fireinfo_profiles \
966 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
967 WHERE fireinfo_profiles.id = %s", self
.id)
970 self
._release
_id
= res
.id
972 self
._release
_id
= None
974 return self
._release
_id
976 def set_release_id(self
, release_id
):
977 if self
.release_id
== release_id
:
980 self
.db
.execute("DELETE FROM fireinfo_profiles_releases WHERE profile_id = %s", self
.id)
982 self
.db
.execute("INSERT INTO fireinfo_profiles_releases(profile_id, release_id) \
983 VALUES(%s, %s)", self
.id, release_id
)
985 self
._release
_id
= release_id
986 if hasattr(self
, "_release"):
989 release_id
= property(get_release_id
, set_release_id
)
993 if not hasattr(self
, "_release"):
994 res
= self
.db
.get("SELECT fireinfo_releases.name AS name FROM fireinfo_profiles \
995 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
996 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
997 WHERE fireinfo_profiles.id = %s", self
.id)
1000 self
._release
= self
._format
_release
(res
.name
)
1002 self
._release
= None
1004 return self
._release
1007 def _format_release(r
):
1011 # Remove the development header
1012 r
= r
.replace("Development Build: ", "")
1015 ("-beta", " - Beta "),
1016 ("-rc", " - Release Candidate "),
1017 ("rc", "Release Candidate "),
1018 ("core", "Core Update "),
1028 def release_short(self
):
1030 (r
"Release Candidate (\d+)", r
"RC\1"),
1034 for pattern
, repl
in pairs
:
1035 if re
.search(pattern
, s
) is None:
1038 s
= re
.sub(pattern
, repl
, s
)
1046 if not hasattr(self
, "_virtual"):
1047 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles_virtual \
1048 WHERE profile_id = %s", self
.id)
1051 self
._virtual
= True
1053 self
._virtual
= False
1055 return self
._virtual
1057 def get_hypervisor_id(self
):
1058 if not hasattr(self
, "_hypervisor_id"):
1059 res
= self
.db
.get("SELECT fireinfo_profiles_virtual.hypervisor_id AS id FROM fireinfo_profiles \
1060 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
1061 WHERE fireinfo_profiles.id = %s", self
.id)
1064 self
._hypervisor
_id
= res
.id
1066 self
._hypervisor
_id
= None
1068 return self
._hypervisor
_id
1070 def set_hypervisor_id(self
, hypervisor_id
):
1071 self
.db
.execute("DELETE FROM fireinfo_profiles_virtual WHERE profile_id = %s", self
.id)
1072 self
.db
.execute("INSERT INTO fireinfo_profiles_virtual(profile_id, hypervisor_id) \
1073 VALUES(%s, %s)", self
.id, hypervisor_id
)
1075 self
._hypervisor
_id
= hypervisor_id
1077 hypervisor_id
= property(get_hypervisor_id
, set_hypervisor_id
)
1080 def hypervisor(self
):
1081 if not hasattr(self
, "_hypervisor"):
1082 res
= self
.db
.get("SELECT fireinfo_hypervisors.name AS hypervisor FROM fireinfo_profiles \
1083 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
1084 LEFT JOIN fireinfo_hypervisors ON fireinfo_profiles_virtual.hypervisor_id = fireinfo_hypervisors.id \
1085 WHERE fireinfo_profiles.id = %s", self
.id)
1088 self
._hypervisor
= res
.hypervisor
1090 self
._hypervisor
= None
1092 return self
._hypervisor
1096 def get_language(self
):
1097 if not hasattr(self
, "_language"):
1098 res
= self
.db
.get("SELECT language FROM fireinfo_profiles_languages \
1099 WHERE profile_id = %s", self
.id)
1102 self
._language
= res
.language
1104 self
._language
= None
1106 return self
._language
1108 def set_language(self
, language
):
1109 self
.db
.execute("DELETE FROM fireinfo_profiles_languages WHERE profile_id = %s", self
.id)
1112 self
.db
.execute("INSERT INTO fireinfo_profiles_languages(profile_id, language) \
1113 VALUES(%s, %s)", self
.id, language
)
1115 self
._language
= language
1117 language
= property(get_language
, set_language
)
1121 def get_network(self
):
1122 if not hasattr(self
, "_network"):
1123 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_networks \
1124 WHERE profile_id = %s", self
.id)
1129 self
._network
= ProfileNetwork(res
)
1131 return self
._network
1133 def set_network(self
, network
):
1134 self
.db
.execute("DELETE FROM fireinfo_profiles_networks WHERE profile_id = %s", self
.id)
1137 self
.db
.execute("INSERT INTO fireinfo_profiles_networks(profile_id, \
1138 has_red, has_green, has_orange, has_blue) VALUES(%s, %s, %s, %s, %s)",
1139 self
.id, network
.has_red
, network
.has_green
, network
.has_orange
, network
.has_blue
)
1141 self
._network
= network
1143 network
= property(get_network
, set_network
)
1146 class ProfileData(Object
):
1147 def __init__(self
, backend
, id, data
=None, profile
=None):
1148 Object
.__init
__(self
, backend
)
1152 self
._profile
= profile
1156 if self
._data
is None:
1157 self
._data
= self
.db
.get("SELECT * FROM fireinfo_profile_data \
1158 WHERE id = %s", self
.id)
1164 if not self
._profile
:
1165 self
._profile
= self
.fireinfo
.get_profile_by_id(self
.profile_id
)
1167 return self
._profile
1170 def profile_id(self
):
1171 return self
.data
.profile_id
1174 class ProfileParserError(Exception):
1178 class ProfileParser(Object
):
1189 __processor_args
= (
1199 __processor_args_mandatory
= (
1204 def __init__(self
, backend
, public_id
, blob
=None):
1205 Object
.__init
__(self
, backend
)
1207 self
.public_id
= public_id
1208 self
.private_id
= None
1210 self
.processor
= None
1211 self
.processor_clock_speed
= None
1212 self
.processor_bogomips
= None
1213 self
.system_id
= None
1217 self
.kernel_id
= None
1221 self
.release_id
= None
1222 self
.language
= None
1224 self
.hypervisor_id
= None
1227 self
.__parse
_blob
(blob
)
1229 def equals(self
, other
):
1230 if not self
.processor_id
== other
.processor_id
:
1233 if not self
.device_ids
== other
.device_ids
:
1236 if not self
.system_id
== other
.system_id
:
1239 if not self
.memory
== other
.memory
:
1242 if not self
.storage
== other
.storage
:
1245 if not self
.kernel_id
== other
.kernel_id
:
1248 if not self
.arch_id
== other
.arch_id
:
1251 if not self
.release_id
== other
.release_id
:
1254 if not self
.language
== other
.language
:
1257 if not self
.virtual
== other
.virtual
:
1261 if not self
.hypervisor_id
== other
.hypervisor_id
:
1264 if not self
.network
== other
.network
:
1269 def __parse_blob(self
, blob
):
1270 _profile
= blob
.get("profile", {})
1271 self
.private_id
= blob
.get("private_id")
1273 # Do not try to parse an empty profile
1278 _processor
= _profile
.get("cpu", {})
1279 self
.__parse
_processor
(_processor
)
1282 _devices
= _profile
.get("devices", [])
1283 self
.__parse
_devices
(_devices
)
1286 _system
= _profile
.get("system")
1288 self
.__parse
_system
(_system
)
1290 # Memory (convert to bytes)
1291 memory
= _system
.get("memory", None)
1293 self
.memory
= memory
* 1024
1295 # Storage size (convert to bytes)
1296 storage
= _system
.get("root_size", None)
1298 self
.storage
= storage
* 1024
1301 kernel
= _system
.get("kernel_release", None)
1303 self
.__parse
_kernel
(kernel
)
1306 release
= _system
.get("release", None)
1308 self
.__parse
_release
(release
)
1311 language
= _system
.get("language", None)
1313 self
.__parse
_language
(language
)
1316 self
.virtual
= _system
.get("virtual", False)
1318 hypervisor
= _profile
.get("hypervisor")
1319 self
.__parse
_hypervisor
(hypervisor
)
1322 _network
= _profile
.get("network")
1324 self
.__parse
_network
(_network
)
1327 def device_ids(self
):
1328 return sorted([d
.id for d
in self
.devices
])
1330 def __parse_devices(self
, _devices
):
1333 for _device
in _devices
:
1335 for arg
in self
.__device
_args
:
1336 args
[arg
] = _device
.get(arg
, None)
1338 # Skip if the subsystem is not set
1339 if not args
.get("subsystem", None):
1342 # Find the device or create a new one.
1343 device
= self
.fireinfo
.get_device(**args
)
1345 device
= self
.fireinfo
.create_device(**args
)
1347 self
.devices
.append(device
)
1349 def __parse_system(self
, system
):
1350 vendor
= system
.get("vendor", None)
1354 model
= system
.get("model", None)
1358 self
.system_id
= self
.fireinfo
.get_system(vendor
, model
)
1359 if not self
.system_id
:
1360 self
.system_id
= self
.fireinfo
.create_system(vendor
, model
)
1363 def processor_id(self
):
1364 if not self
.processor
:
1367 return self
.processor
.id
1369 def __parse_processor(self
, _processor
):
1371 for arg
in self
.__processor
_args
:
1372 if arg
== "core_count":
1377 args
[arg
] = _processor
.get(_arg
, None)
1379 for arg
in self
.__processor
_args
_mandatory
:
1380 if not args
.get(arg
, None):
1381 raise ProfileParserError
, "Mandatory argument missing: %s" % arg
1383 self
.processor
= self
.fireinfo
.get_processor(**args
)
1384 if not self
.processor
:
1385 self
.processor
= self
.fireinfo
.create_processor(**args
)
1387 self
.processor_clock_speed
= _processor
.get("speed", None)
1388 self
.processor_bogomips
= _processor
.get("bogomips", None)
1390 arch
= _processor
.get("arch", None)
1392 self
.__parse
_arch
(arch
)
1394 def __parse_kernel(self
, kernel
):
1395 self
.kernel_id
= self
.fireinfo
.get_kernel(kernel
)
1396 if not self
.kernel_id
:
1397 self
.kernel_id
= self
.fireinfo
.create_kernel(kernel
)
1398 assert self
.kernel_id
1400 self
.kernel
= kernel
1402 def __parse_arch(self
, arch
):
1403 self
.arch_id
= self
.fireinfo
.get_arch(arch
)
1404 if not self
.arch_id
:
1405 self
.arch_id
= self
.fireinfo
.create_arch(arch
)
1409 def __parse_release(self
, release
):
1410 # Remove the arch bit
1412 r
= [e
for e
in release
.split() if e
]
1413 for s
in ("(x86_64)", "(i586)", "(armv5tel)"):
1420 release
= " ".join(r
)
1422 self
.release_id
= self
.fireinfo
.get_release(release
)
1423 if not self
.release_id
:
1424 self
.release_id
= self
.fireinfo
.create_release(release
)
1425 assert self
.release_id
1427 self
.release
= release
1429 def __parse_language(self
, language
):
1430 self
.language
= language
1431 self
.language
, delim
, rest
= self
.language
.partition(".")
1432 self
.language
, delim
, rest
= self
.language
.partition("_")
1434 def __parse_hypervisor(self
, hypervisor
):
1435 vendor
= hypervisor
.get("vendor", "other")
1437 if vendor
in ("other", "unknown"):
1438 self
.hypervisor_id
= None
1441 self
.hypervisor_id
= self
.fireinfo
.get_hypervisor(vendor
)
1442 if not self
.hypervisor_id
:
1443 self
.hypervisor_id
= self
.fireinfo
.create_hypervisor(vendor
)
1445 def __parse_network(self
, network
):
1446 self
.network
= ProfileNetwork({
1447 "has_red" : network
.get("red", False),
1448 "has_green" : network
.get("green", False),
1449 "has_orange" : network
.get("orange", False),
1450 "has_blue" : network
.get("blue", False),
1454 class Fireinfo(Object
):
1455 def get_profile_count(self
, when
=None):
1456 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1457 WHERE then_or_now(%s) BETWEEN time_created AND time_valid", when
)
1462 def get_total_updates_count(self
, when
=None):
1463 res
= self
.db
.get("SELECT COUNT(*) + SUM(updates) AS count \
1464 FROM fireinfo_profiles WHERE time_created <= then_or_now(%s)", when
)
1471 def parse_profile(self
, public_id
, blob
):
1472 return ProfileParser(self
.backend
, public_id
, blob
)
1476 def profile_exists(self
, public_id
):
1477 res
= self
.db
.get("SELECT id FROM fireinfo_profiles \
1478 WHERE public_id = %s LIMIT 1", public_id
)
1485 def profile_rate_limit_active(self
, public_id
, when
=None):
1486 # Remove all outdated entries
1487 self
.db
.execute("DELETE FROM fireinfo_profiles_log \
1488 WHERE ts <= then_or_now(%s) - INTERVAL '60 minutes'", when
)
1490 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles_log \
1491 WHERE public_id = %s", public_id
)
1493 if res
and res
.count
>= 10:
1498 def is_private_id_change_permitted(self
, public_id
, private_id
, when
=None):
1499 # Check if a profile exists with a different private id that is still valid
1500 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles \
1501 WHERE public_id = %s AND NOT private_id = %s \
1502 AND time_valid >= then_or_now(%s) LIMIT 1", public_id
, private_id
, when
)
1509 def get_profile(self
, public_id
, private_id
=None, when
=None):
1510 res
= self
.db
.get("SELECT * FROM fireinfo_profiles \
1511 WHERE public_id = %s AND \
1512 (CASE WHEN %s IS NULL THEN TRUE ELSE private_id = %s END) AND \
1513 then_or_now(%s) BETWEEN time_created AND time_valid \
1514 ORDER BY time_updated DESC LIMIT 1",
1515 public_id
, private_id
, private_id
, when
)
1518 return Profile(self
.backend
, res
.id, res
)
1520 def get_profile_with_data(self
, public_id
, when
=None):
1521 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1522 SELECT * FROM profiles JOIN fireinfo_profiles ON profiles.id = fireinfo_profiles.id \
1523 WHERE public_id = %s ORDER BY time_updated DESC LIMIT 1", when
, public_id
)
1526 return Profile(self
.backend
, res
.id, res
)
1528 def get_profiles(self
, public_id
):
1529 res
= self
.db
.query("SELECT * FROM fireinfo_profiles \
1530 WHERE public_id = %s ORDER BY time_created DESC", public_id
)
1534 profile
= Profile(self
.backend
, row
.id, row
)
1535 profiles
.append(profile
)
1539 def create_profile(self
, public_id
, private_id
, when
=None):
1540 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
1542 res
= self
.db
.get("INSERT INTO fireinfo_profiles(public_id, private_id, \
1543 time_created, time_updated, time_valid) VALUES(%s, %s, then_or_now(%s), \
1544 then_or_now(%s), then_or_now(%s) + INTERVAL '%s days') RETURNING id",
1545 public_id
, private_id
, when
, when
, when
, valid
)
1548 p
= Profile(self
.backend
, res
.id)
1549 p
.log_profile_update()
1555 def create_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1556 driver
=None, deviceclass
=None):
1557 res
= self
.db
.get("INSERT INTO fireinfo_devices(subsystem, vendor, model, \
1558 sub_vendor, sub_model, driver, deviceclass) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1559 RETURNING id", subsystem
, vendor
, model
, sub_vendor
, sub_model
, driver
, deviceclass
)
1562 return Device(self
.backend
, res
.id)
1564 def get_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1565 driver
=None, deviceclass
=None):
1566 res
= self
.db
.get("SELECT * FROM fireinfo_devices \
1567 WHERE subsystem = %s AND vendor = %s AND model = %s \
1568 AND sub_vendor IS NOT DISTINCT FROM %s \
1569 AND sub_model IS NOT DISTINCT FROM %s \
1570 AND driver IS NOT DISTINCT FROM %s \
1571 AND deviceclass IS NOT DISTINCT FROM %s \
1572 LIMIT 1", subsystem
, vendor
, model
, sub_vendor
,
1573 sub_model
, driver
, deviceclass
)
1576 return Device(self
.backend
, res
.id, res
)
1580 def create_system(self
, vendor
, model
):
1581 res
= self
.db
.get("INSERT INTO fireinfo_systems(vendor, model) \
1582 VALUES(%s, %s) RETURNING id", vendor
, model
)
1587 def get_system(self
, vendor
, model
):
1588 res
= self
.db
.get("SELECT id FROM fireinfo_systems WHERE vendor IS NOT DISTINCT FROM %s \
1589 AND model IS NOT DISTINCT FROM %s LIMIT 1", vendor
, model
)
1596 def create_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1597 res
= self
.db
.get("INSERT INTO fireinfo_processors(vendor, model_string, \
1598 family, model, stepping, core_count, flags) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1599 RETURNING id", vendor
, model_string
, family
, model
, stepping
, core_count
, flags
)
1602 return Processor(self
.backend
, res
.id)
1604 def get_processor_by_id(self
, processor_id
, **kwargs
):
1605 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1606 WHERE id = %s", processor_id
)
1609 return Processor(self
.backend
, res
.id, data
=res
, **kwargs
)
1611 def get_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1615 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1616 WHERE vendor = %s AND model_string = %s \
1617 AND family IS NOT DISTINCT FROM %s AND model IS NOT DISTINCT FROM %s \
1618 AND stepping IS NOT DISTINCT FROM %s AND core_count = %s \
1619 AND flags <@ %s AND flags @> %s", vendor
, model_string
, family
, model
,
1620 stepping
, core_count
, flags
, flags
)
1623 return Processor(self
.backend
, res
.id, res
)
1627 def create_kernel(self
, kernel
):
1628 res
= self
.db
.get("INSERT INTO fireinfo_kernels(name) VALUES(%s) \
1629 RETURNING id", kernel
)
1634 def get_kernel(self
, kernel
):
1635 res
= self
.db
.get("SELECT id FROM fireinfo_kernels WHERE name = %s", kernel
)
1642 def create_arch(self
, arch
):
1643 res
= self
.db
.get("INSERT INTO fireinfo_arches(name) VALUES(%s) \
1644 RETURNING id", arch
)
1649 def get_arch(self
, arch
):
1650 res
= self
.db
.get("SELECT id FROM fireinfo_arches WHERE name = %s", arch
)
1657 def create_release(self
, release
):
1658 res
= self
.db
.get("INSERT INTO fireinfo_releases(name) VALUES(%s) \
1659 RETURNING id", release
)
1664 def get_release(self
, release
):
1665 res
= self
.db
.get("SELECT id FROM fireinfo_releases WHERE name = %s", release
)
1672 def create_hypervisor(self
, hypervisor
):
1673 res
= self
.db
.get("INSERT INTO fireinfo_hypervisors(name) VALUES(%s) \
1674 RETURNING id", hypervisor
)
1679 def get_hypervisor(self
, hypervisor
):
1680 res
= self
.db
.get("SELECT id FROM fireinfo_hypervisors WHERE name = %s",
1688 def handle_profile(self
, *args
, **kwargs
):
1689 self
.db
.execute("START TRANSACTION")
1691 # Wrap all the handling of the profile in a huge transaction.
1693 self
._handle
_profile
(*args
, **kwargs
)
1696 self
.db
.execute("ROLLBACK")
1700 self
.db
.execute("COMMIT")
1702 def _handle_profile(self
, public_id
, profile_blob
, location
=None, when
=None):
1703 private_id
= profile_blob
.get("private_id", None)
1706 # Check if the profile already exists in the database.
1707 profile
= self
.fireinfo
.get_profile(public_id
, private_id
=private_id
, when
=when
)
1709 # Check if the update can actually be updated
1710 if profile
and self
.fireinfo
.profile_rate_limit_active(public_id
, when
=when
):
1711 logging
.warning("There were too many updates for this profile in the last hour: %s" % public_id
)
1714 elif not self
.is_private_id_change_permitted(public_id
, private_id
, when
=when
):
1715 logging
.warning("Changing private id is not permitted for profile: %s" % public_id
)
1719 profile_parser
= self
.parse_profile(public_id
, profile_blob
)
1721 # If a profile exists, check if it matches and if so, just update the
1724 # Check if the profile has changed. If so, update the data.
1725 if profile_parser
.equals(profile
):
1726 profile
.updated(profile_parser
, location
=location
, when
=when
)
1729 # If it does not match, we assume that it is expired and
1730 # create a new profile.
1731 profile
.expired(when
=when
)
1733 # Replace the old profile with a new one
1734 profile
= self
.fireinfo
.create_profile(public_id
, private_id
, when
=when
)
1735 profile
.parse(profile_parser
)
1738 profile
.set_location(location
)
1744 def get_random_profile(self
, when
=None):
1745 # Check if the architecture exists so that we pick a profile with some data
1746 res
= self
.db
.get("SELECT public_id FROM fireinfo_profiles \
1747 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
1748 WHERE fireinfo_profiles_arches.profile_id IS NOT NULL \
1749 AND then_or_now(%s) BETWEEN time_created AND time_valid ORDER BY RANDOM() LIMIT 1", when
)
1752 return res
.public_id
1754 def get_active_profiles(self
, when
=None):
1755 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1756 SELECT COUNT(*) AS with_data, (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1757 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1758 WHERE fireinfo_profiles_releases.profile_id IS NOT NULL", when
)
1761 return res
.with_data
, res
.count
1763 def get_archive_size(self
, when
=None):
1764 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1765 WHERE time_created <= then_or_now(%s)", when
)
1770 def get_geo_location_map(self
, when
=None):
1771 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1772 SELECT location, COUNT(location)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1773 LEFT JOIN fireinfo_profiles_locations ON profiles.id = fireinfo_profiles_locations.profile_id \
1774 WHERE fireinfo_profiles_locations.location IS NOT NULL GROUP BY location ORDER BY count DESC", when
)
1776 return ((r
.location
, r
.count
) for r
in res
)
1778 def get_language_map(self
, when
=None):
1779 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1780 SELECT language, COUNT(language)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1781 LEFT JOIN fireinfo_profiles_languages ON profiles.id = fireinfo_profiles_languages.profile_id \
1782 WHERE fireinfo_profiles_languages.language IS NOT NULL GROUP BY language ORDER BY count DESC", when
)
1784 return ((r
.language
, r
.count
) for r
in res
)
1787 def cpu_vendors(self
):
1788 res
= self
.db
.query("SELECT DISTINCT vendor FROM fireinfo_processors ORDER BY vendor")
1790 return (CPU_VENDORS
.get(r
.vendor
, r
.vendor
) for r
in res
)
1792 def get_cpu_vendors_map(self
, when
=None):
1793 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1794 SELECT vendor, COUNT(vendor)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1795 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1796 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1797 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL GROUP BY vendor ORDER BY count DESC", when
)
1799 return ((CPU_VENDORS
.get(r
.vendor
, r
.vendor
), r
.count
) for r
in res
)
1801 def get_cpu_clock_speeds(self
, when
=None):
1802 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1803 SELECT AVG(fireinfo_profiles_processors.clock_speed) AS avg, \
1804 STDDEV(fireinfo_profiles_processors.clock_speed) AS stddev, \
1805 MIN(fireinfo_profiles_processors.clock_speed) AS min, \
1806 MAX(fireinfo_profiles_processors.clock_speed) AS max FROM profiles \
1807 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1808 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1809 AND fireinfo_profiles_processors.clock_speed > 0 \
1810 AND fireinfo_profiles_processors.clock_speed < fireinfo_profiles_processors.bogomips \
1811 AND fireinfo_profiles_processors.bogomips <= %s", when
, 10000)
1814 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1816 def get_cpus_with_platform_and_flag(self
, platform
, flag
, when
=None):
1817 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1818 processors AS (SELECT fireinfo_processors.id AS id, fireinfo_processors.flags AS flags FROM profiles \
1819 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1820 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1821 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1822 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1823 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1824 AND fireinfo_arches.platform = %s AND NOT 'hypervisor' = ANY(fireinfo_processors.flags)) \
1825 SELECT (COUNT(*)::float / (SELECT NULLIF(COUNT(*), 0) FROM processors)) AS count FROM processors \
1826 WHERE %s = ANY(processors.flags)", when
, platform
, flag
)
1828 return res
.count
or 0
1830 def get_common_cpu_flags_by_platform(self
, platform
, when
=None):
1831 if platform
== "arm":
1833 "lpae", "neon", "thumb", "thumb2", "thumbee", "vfpv3", "vfpv4",
1835 elif platform
== "x86":
1837 "aes", "avx", "avx2", "lm", "mmx", "mmxext", "nx", "pae",
1838 "pni", "popcnt", "sse", "sse2", "rdrand", "ssse3", "sse4a",
1846 ret
.append((flag
, self
.get_cpus_with_platform_and_flag(platform
, flag
, when
=when
)))
1848 # Add virtual CPU flag "virt" for virtualization support
1849 if platform
== "x86":
1851 self
.get_cpus_with_platform_and_flag(platform
, "vmx", when
=when
) + \
1852 self
.get_cpus_with_platform_and_flag(platform
, "svm", when
=when
)))
1854 return sorted(ret
, key
=lambda x
: x
[1], reverse
=True)
1856 def get_common_memory_amounts(self
, when
=None):
1857 amounts
= (128, 256, 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024)
1860 for amount
in amounts
:
1861 ret
.append((amount
, self
.get_memory_amount(amount
* 1024 * 0.95, when
=when
)))
1865 def get_memory_amount(self
, greather_than
, when
=None):
1866 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1867 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles \
1868 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1869 WHERE NOT fireinfo_profiles_memory.amount IS NULL) AS percentage FROM profiles \
1870 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1871 WHERE fireinfo_profiles_memory.amount >= %s", when
, greather_than
)
1874 return res
.percentage
1876 def get_memory_amounts(self
, when
=None):
1877 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1878 SELECT AVG(fireinfo_profiles_memory.amount) AS avg, \
1879 STDDEV(fireinfo_profiles_memory.amount) AS stddev, \
1880 MIN(fireinfo_profiles_memory.amount) AS min, \
1881 MAX(fireinfo_profiles_memory.amount) AS max FROM profiles \
1882 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id", when
)
1885 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1887 def get_arch_map(self
, when
=None):
1888 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1889 SELECT fireinfo_arches.name AS arch, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count \
1891 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1892 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1893 WHERE NOT fireinfo_profiles_arches.profile_id IS NULL \
1894 GROUP BY fireinfo_arches.id ORDER BY count DESC", when
)
1896 return ((r
.arch
, r
.count
) for r
in res
)
1900 def get_hypervisor_map(self
, when
=None):
1901 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1902 virtual_profiles AS (SELECT profiles.id AS profile_id, fireinfo_profiles_virtual.hypervisor_id FROM profiles \
1903 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1904 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL) \
1905 SELECT COALESCE(fireinfo_hypervisors.name, %s) AS name, \
1906 COUNT(*)::float / (SELECT COUNT(*) FROM virtual_profiles) AS count FROM virtual_profiles \
1907 LEFT JOIN fireinfo_hypervisors ON virtual_profiles.hypervisor_id = fireinfo_hypervisors.id \
1908 GROUP BY fireinfo_hypervisors.name ORDER BY count DESC", when
, "unknown")
1910 return ((r
.name
, r
.count
) for r
in res
)
1912 def get_virtual_ratio(self
, when
=None):
1913 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1914 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1915 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1916 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL", when
)
1923 def get_releases_map(self
, when
=None):
1924 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1925 SELECT fireinfo_releases.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1926 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1927 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
1928 GROUP BY fireinfo_releases.name ORDER BY count DESC", when
)
1930 return ((r
.name
, r
.count
) for r
in res
)
1932 def get_kernels_map(self
, when
=None):
1933 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1934 SELECT fireinfo_kernels.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1935 LEFT JOIN fireinfo_profiles_kernels ON profiles.id = fireinfo_profiles_kernels.profile_id \
1936 LEFT JOIN fireinfo_kernels ON fireinfo_profiles_kernels.kernel_id = fireinfo_kernels.id \
1937 GROUP BY fireinfo_kernels.name ORDER BY count DESC", when
)
1939 return ((r
.name
, r
.count
) for r
in res
)
1941 def _process_devices(self
, devices
):
1945 dev
= Device(self
.backend
, dev
.get("id", None), dev
)
1950 def get_driver_map(self
, driver
, when
=None):
1951 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1952 devices AS (SELECT * FROM profiles \
1953 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1954 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1955 WHERE driver = %s) \
1956 SELECT subsystem, model, vendor, driver, deviceclass, \
1957 COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1958 GROUP BY subsystem, model, vendor, driver, deviceclass \
1959 ORDER BY percentage DESC", when
, driver
)
1961 return self
._process
_devices
(res
)
1964 "pci" : hwdata
.PCI(),
1965 "usb" : hwdata
.USB(),
1968 def get_vendor_string(self
, subsystem
, vendor_id
):
1970 cls
= self
.subsystem2class
[subsystem
]
1974 return cls
.get_vendor(vendor_id
)
1976 def get_model_string(self
, subsystem
, vendor_id
, model_id
):
1978 cls
= self
.subsystem2class
[subsystem
]
1982 return cls
.get_device(vendor_id
, model_id
)
1984 def get_vendor_list(self
, when
=None):
1985 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1986 SELECT DISTINCT fireinfo_devices.subsystem AS subsystem, fireinfo_devices.vendor AS vendor FROM profiles \
1987 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1988 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1989 WHERE NOT fireinfo_devices.driver = ANY(%s)", when
, IGNORED_DEVICES
)
1993 vendor
= self
.get_vendor_string(row
.subsystem
, row
.vendor
)
1995 # Drop if vendor could not be determined
2000 vendors
[vendor
].append((row
.subsystem
, row
.vendor
))
2002 vendors
[vendor
] = [(row
.subsystem
, row
.vendor
)]
2004 vendors
= vendors
.items()
2005 return sorted(vendors
)
2007 def get_devices_by_vendor(self
, subsystem
, vendor
, when
=None):
2008 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
2009 devices AS (SELECT * FROM profiles \
2010 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
2011 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
2012 WHERE NOT fireinfo_devices.driver = ANY(%s)), \
2013 vendor_devices AS (SELECT * FROM devices WHERE devices.subsystem = %s AND devices.vendor = %s) \
2014 SELECT subsystem, model, vendor, driver, deviceclass FROM vendor_devices \
2015 GROUP BY subsystem, model, vendor, driver, deviceclass", when
, IGNORED_DEVICES
, subsystem
, vendor
)
2017 return self
._process
_devices
(res
)
2019 def get_device_percentage(self
, subsystem
, vendor
, model
, when
=None):
2020 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
2021 devices AS (SELECT * FROM profiles \
2022 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
2023 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id) \
2024 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
2025 WHERE devices.subsystem = %s AND devices.vendor = %s AND devices.model = %s",
2026 when
, subsystem
, vendor
, model
)
2029 return res
.percentage
2031 def get_device_in_profile(self
, subsystem
, vendor
, model
, limit
=10, when
=None):
2032 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
2033 profiles_with_device AS (SELECT DISTINCT fireinfo_profiles.public_id FROM profiles \
2034 LEFT JOIN fireinfo_profiles ON profiles.id = fireinfo_profiles.id \
2035 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
2036 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
2037 WHERE fireinfo_devices.subsystem = %s AND fireinfo_devices.vendor = %s \
2038 AND fireinfo_devices.model = %s) \
2039 SELECT * FROM profiles_with_device ORDER BY RANDOM() LIMIT %s",
2040 when
, subsystem
, vendor
, model
, limit
)
2042 return (r
.public_id
for r
in res
)
2044 def get_network_zones_map(self
, when
=None):
2045 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
2046 SELECT COUNT(NULLIF(has_red, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_red, \
2047 COUNT(NULLIF(has_green, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_green, \
2048 COUNT(NULLIF(has_orange, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_orange, \
2049 COUNT(NULLIF(has_blue, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_blue FROM profiles \
2050 LEFT JOIN fireinfo_profiles_networks ON profiles.id = fireinfo_profiles_networks.profile_id \
2051 WHERE fireinfo_profiles_networks.profile_id IS NOT NULL", when
)