]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/fireinfo.py
e5099599d0b67a417de01d7f421ed61b54284aea
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 self
.model_string
and not self
.model_string
.startswith(self
.vendor
):
151 s
.append(self
.vendor
)
154 s
.append(self
.model_string
or "Generic")
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 if self
.data
.model_string
:
191 s
= self
.data
.model_string
.split()
193 return " ".join((e
for e
in s
if e
))
197 return self
.data
.flags
199 def has_flag(self
, flag
):
200 return flag
in self
.flags
203 if self
.vendor
== "Intel" and self
.family
== 6 and self
.model
in (15, 55, 76, 77):
206 return self
.has_flag("ht")
209 def core_count(self
):
210 return self
.data
.core_count
215 return self
.core_count
// 2
217 return self
.core_count
220 def clock_speed(self
):
221 return self
.__clock
_speed
223 def format_clock_speed(self
):
224 if not self
.clock_speed
:
227 if self
.clock_speed
< 1000:
228 return "%dMHz" % self
.clock_speed
230 return "%.2fGHz" % round(self
.clock_speed
/ 1000, 2)
234 return self
.__bogomips
237 def capabilities(self
):
239 ("64bit", self
.has_flag("lm")),
240 ("aes", self
.has_flag("aes")),
241 ("nx", self
.has_flag("nx")),
242 ("pae", self
.has_flag("pae") or self
.has_flag("lpae")),
243 ("rdrand", self
.has_flag("rdrand")),
246 # If the system is already running in a virtual environment,
247 # we cannot correctly detect if the CPU supports svm or vmx
248 if self
.has_flag("hypervisor"):
249 caps
.append(("virt", None))
251 caps
.append(("virt", self
.has_flag("vmx") or self
.has_flag("svm")))
255 def format_model(self
):
256 s
= self
.model_string
or ""
258 # Remove everything after the @: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
259 s
, sep
, rest
= s
.partition("@")
261 for pattern
, repl
in CPU_STRINGS
:
262 if re
.match(pattern
, s
) is None:
265 s
= re
.sub(pattern
, repl
, s
)
268 # Otherwise remove the symbols
269 for i
in ("C", "R", "TM", "tm"):
270 s
= s
.replace("(%s)" % i
, "")
272 # Replace too long strings with shorter ones
274 ("Quad-Core Processor", ""),
275 ("Dual-Core Processor", ""),
276 ("Processor", "CPU"),
277 ("processor", "CPU"),
282 # Remove too many spaces
283 s
= " ".join((e
for e
in s
.split() if e
))
288 def friendly_string(self
):
291 model
= self
.format_model()
295 clock_speed
= self
.format_clock_speed()
297 s
.append("@ %s" % clock_speed
)
300 s
.append("x%s" % self
.count
)
305 class Device(Object
):
308 "00" : N_("Unclassified"),
309 "01" : N_("Mass storage"),
310 "02" : N_("Network"),
311 "03" : N_("Display"),
312 "04" : N_("Multimedia"),
313 "05" : N_("Memory controller"),
315 "07" : N_("Communication"),
316 "08" : N_("Generic system peripheral"),
317 "09" : N_("Input device"),
318 "0a" : N_("Docking station"),
319 "0b" : N_("Processor"),
320 "0c" : N_("Serial bus"),
321 "0d" : N_("Wireless"),
322 "0e" : N_("Intelligent controller"),
323 "0f" : N_("Satellite communications controller"),
324 "10" : N_("Encryption"),
325 "11" : N_("Signal processing controller"),
326 "ff" : N_("Unassigned class"),
330 "00" : N_("Unclassified"),
331 "01" : N_("Multimedia"),
332 "02" : N_("Communication"),
333 "03" : N_("Input device"),
334 "05" : N_("Generic system peripheral"),
336 "07" : N_("Printer"),
337 "08" : N_("Mass storage"),
339 "0a" : N_("Communication"),
340 "0b" : N_("Smart card"),
341 "0d" : N_("Encryption"),
342 "0e" : N_("Display"),
343 "0f" : N_("Personal Healthcare"),
344 "dc" : N_("Diagnostic Device"),
345 "e0" : N_("Wireless"),
346 "ef" : N_("Unclassified"),
347 "fe" : N_("Unclassified"),
348 "ff" : N_("Unclassified"),
352 def __init__(self
, backend
, id, data
=None):
353 Object
.__init
__(self
, backend
)
359 return "<%s vendor=%s model=%s>" % (self
.__class
__.__name
__,
360 self
.vendor_string
, self
.model_string
)
362 def __eq__(self
, other
):
363 if isinstance(other
, self
.__class
__):
364 return self
.id == other
.id
366 def __lt__(self
, other
):
367 if isinstance(other
, self
.__class
__):
368 return self
.cls
< other
.cls
or \
369 self
.vendor_string
< other
.vendor_string
or \
370 self
.vendor
< other
.vendor
or \
371 self
.model_string
< other
.model_string
or \
372 self
.model
< other
.model
376 if self
.__data
is None:
379 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_devices \
380 WHERE id = %s", self
.id)
384 def is_showable(self
):
385 if self
.driver
in IGNORED_DEVICES
:
388 if self
.driver
in ("pcieport", "hub"):
395 return self
.data
.subsystem
399 return self
.data
.model
402 def model_string(self
):
403 return self
.fireinfo
.get_model_string(self
.subsystem
,
404 self
.vendor
, self
.model
)
408 return self
.data
.vendor
411 def vendor_string(self
):
412 return self
.fireinfo
.get_vendor_string(self
.subsystem
, self
.vendor
)
416 return self
.data
.driver
420 classid
= self
.data
.deviceclass
422 if self
.subsystem
== "pci":
423 classid
= classid
[:-4]
424 if len(classid
) == 1:
425 classid
= "0%s" % classid
427 elif self
.subsystem
== "usb" and classid
:
428 classid
= classid
.split("/")[0]
429 classid
= "%02x" % int(classid
)
432 return self
.classid2name
[self
.subsystem
][classid
]
437 def percentage(self
):
438 return self
.data
.get("percentage", None)
441 class Profile(Object
):
442 def __init__(self
, backend
, id, data
=None):
443 Object
.__init
__(self
, backend
)
449 return "<%s %s>" % (self
.__class
__.__name
__, self
.public_id
)
451 def __cmp__(self
, other
):
452 return cmp(self
.id, other
.id)
454 def is_showable(self
):
462 if self
.__data
is None:
463 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_profiles \
464 WHERE id = %s", self
.id)
470 return self
.data
.public_id
473 def private_id(self
):
474 raise NotImplementedError
477 def time_created(self
):
478 return self
.data
.time_created
481 def time_updated(self
):
482 return self
.data
.time_updated
484 def updated(self
, profile_parser
=None, location
=None, when
=None):
485 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
487 self
.db
.execute("UPDATE fireinfo_profiles \
489 time_updated = then_or_now(%s), \
490 time_valid = then_or_now(%s) + INTERVAL '%s days', \
491 updates = updates + 1 \
492 WHERE id = %s", when
, when
, valid
, self
.id)
495 self
.set_processor_speeds(
496 profile_parser
.processor_clock_speed
,
497 profile_parser
.processor_bogomips
,
501 self
.set_location(location
)
503 self
.log_profile_update()
505 def log_profile_update(self
):
506 # Log that an update was performed for this profile id
507 self
.db
.execute("INSERT INTO fireinfo_profiles_log(public_id) \
508 VALUES(%s)", self
.public_id
)
510 def expired(self
, when
=None):
511 self
.db
.execute("UPDATE fireinfo_profiles \
512 SET time_valid = then_or_now(%s) WHERE id = %s", when
, self
.id)
514 def parse(self
, parser
):
516 self
.processor
= parser
.processor
517 self
.set_processor_speeds(parser
.processor_clock_speed
, parser
.processor_bogomips
)
520 self
.devices
= parser
.devices
523 self
.system_id
= parser
.system_id
526 self
.memory
= parser
.memory
529 self
.storage
= parser
.storage
532 self
.kernel_id
= parser
.kernel_id
535 self
.arch_id
= parser
.arch_id
538 self
.release_id
= parser
.release_id
541 self
.language
= parser
.language
545 self
.hypervisor_id
= parser
.hypervisor_id
548 self
.network
= parser
.network
552 def get_location(self
):
553 if not hasattr(self
, "_location"):
554 res
= self
.db
.get("SELECT location FROM fireinfo_profiles_locations \
555 WHERE profile_id = %s", self
.id)
558 self
._location
= res
.location
560 self
._location
= None
562 return self
._location
564 def set_location(self
, location
):
565 if self
.location
== location
:
568 self
.db
.execute("DELETE FROM fireinfo_profiles_locations \
569 WHERE profile_id = %s", self
.id)
570 self
.db
.execute("INSERT INTO fireinfo_profiles_locations(profile_id, location) \
571 VALUES(%s, %s)", self
.id, location
)
573 self
._location
= location
575 location
= property(get_location
, set_location
)
578 def location_string(self
):
579 return self
.geoip
.get_country_name(self
.location
)
584 def device_ids(self
):
585 if not hasattr(self
, "_device_ids"):
586 res
= self
.db
.query("SELECT device_id FROM fireinfo_profiles_devices \
587 WHERE profile_id = %s", self
.id)
589 self
._device
_ids
= sorted([r
.device_id
for r
in res
])
591 return self
._device
_ids
593 def get_devices(self
):
594 if not hasattr(self
, "_devices"):
595 res
= self
.db
.query("SELECT * FROM fireinfo_devices \
596 LEFT JOIN fireinfo_profiles_devices ON \
597 fireinfo_devices.id = fireinfo_profiles_devices.device_id \
598 WHERE fireinfo_profiles_devices.profile_id = %s", self
.id)
602 device
= Device(self
.backend
, row
.id, row
)
603 self
._devices
.append(device
)
607 def set_devices(self
, devices
):
608 device_ids
= [d
.id for d
in devices
]
610 self
.db
.execute("DELETE FROM fireinfo_profiles_devices WHERE profile_id = %s", self
.id)
611 self
.db
.executemany("INSERT INTO fireinfo_profiles_devices(profile_id, device_id) \
612 VALUES(%s, %s)", ((self
.id, d
) for d
in device_ids
))
614 self
._devices
= devices
615 self
._device
_ids
= device_ids
617 devices
= property(get_devices
, set_devices
)
619 def count_device(self
, subsystem
, vendor
, model
):
622 for dev
in self
.devices
:
623 if dev
.subsystem
== subsystem
and dev
.vendor
== vendor
and dev
.model
== model
:
630 def get_system_id(self
):
631 if not hasattr(self
, "_system_id"):
632 res
= self
.db
.get("SELECT system_id AS id FROM fireinfo_profiles_systems \
633 WHERE profile_id = %s", self
.id)
636 self
._system
_id
= res
.id
638 self
._system
_id
= None
640 return self
._system
_id
642 def set_system_id(self
, system_id
):
643 self
.db
.execute("DELETE FROM fireinfo_profiles_systems WHERE profile_id = %s", self
.id)
646 self
.db
.execute("INSERT INTO fireinfo_profiles_systems(profile_id, system_id) \
647 VALUES(%s, %s)", self
.id, system_id
)
649 self
._system
_id
= None
650 if hasattr(self
, "_system"):
653 system_id
= property(get_system_id
, set_system_id
)
657 if not hasattr(self
, "_system"):
658 res
= self
.db
.get("SELECT fireinfo_systems.vendor AS vendor, fireinfo_systems.model AS model \
659 FROM fireinfo_profiles_systems \
660 LEFT JOIN fireinfo_systems ON fireinfo_profiles_systems.system_id = fireinfo_systems.id \
661 WHERE fireinfo_profiles_systems.profile_id = %s", self
.id)
664 self
._system
= (res
.vendor
, res
.model
)
666 self
._system
= (None, None)
671 def system_vendor(self
):
679 def system_model(self
):
687 def appliance_id(self
):
688 if not hasattr(self
, "_appliance_id"):
690 ("fountainnetworks-duo-box", self
._appliance
_test
_fountainnetworks
_duo
_box
),
691 ("fountainnetworks-prime", self
._appliance
_test
_fountainnetworks
_prime
),
692 ("lightningwirelabs-eco-plus", self
._appliance
_test
_lightningwirelabs
_eco
_plus
),
693 ("lightningwirelabs-eco", self
._appliance
_test
_lightningwirelabs
_eco
),
696 self
._appliance
_id
= None
697 for name
, test_func
in appliances
:
701 self
._appliance
_id
= name
704 return self
._appliance
_id
708 if self
.appliance_id
== "fountainnetworks-duo-box":
709 return "Fountain Networks - IPFire Duo Box"
711 elif self
.appliance_id
== "fountainnetworks-prime":
712 return "Fountain Networks - IPFire Prime Box"
714 elif self
.appliance_id
== "lightningwirelabs-eco-plus":
715 return "Lightning Wire Labs - IPFire Eco Plus Appliance"
717 elif self
.appliance_id
== "lightningwirelabs-eco":
718 return "Lightning Wire Labs - IPFire Eco Appliance"
720 def _appliance_test_fountainnetworks_duo_box(self
):
721 if not self
.processor
.vendor
== "Intel":
724 if not self
.processor
.model_string
== "Intel(R) Celeron(R) 2957U @ 1.40GHz":
727 if not self
.count_device("pci", "10ec", "8168") == 2:
731 #if self.count_device("usb", "148f", "5572") < 1:
736 def _appliance_test_fountainnetworks_prime(self
):
737 if not self
.system
in (("SECO", None), ("SECO", "0949")):
740 # Must have a wireless device
741 if self
.count_device("usb", "148f", "5572") < 1:
746 def _appliance_test_lightningwirelabs_eco(self
):
747 if not self
.system
== ("MSI", "MS-9877"):
750 # Must have four Intel network adapters
751 network_adapters_count
= self
.count_device("pci", "8086", "10d3")
752 if not network_adapters_count
== 4:
757 def _appliance_test_lightningwirelabs_eco_plus(self
):
758 if not self
.system_vendor
== "ASUS":
761 if not self
.system_model
.startswith("P9A-I/2550"):
764 # Must have four Intel network adapters
765 network_adapters_count
= self
.count_device("pci", "8086", "1f41")
766 if not network_adapters_count
== 4:
774 def processor_id(self
):
775 if hasattr(self
, "_processor"):
776 return self
._processor
.id
778 if not hasattr(self
, "_processor_id"):
779 res
= self
.db
.get("SELECT processor_id FROM fireinfo_profiles_processors \
780 WHERE profile_id = %s", self
.id)
783 self
._processor
_id
= res
.processor_id
785 self
._processor
_id
= None
787 return self
._processor
_id
789 def get_processor(self
):
790 if not self
.processor_id
:
793 if not hasattr(self
, "_processor"):
794 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_processors \
795 WHERE profile_id = %s", self
.id)
798 self
._processor
= self
.fireinfo
.get_processor_by_id(res
.processor_id
,
799 clock_speed
=res
.clock_speed
, bogomips
=res
.bogomips
)
801 self
._processor
= None
803 return self
._processor
805 def set_processor(self
, processor
):
806 self
.db
.execute("DELETE FROM fireinfo_profiles_processors \
807 WHERE profile_id = %s", self
.id)
810 self
.db
.execute("INSERT INTO fireinfo_profiles_processors(profile_id, processor_id) \
811 VALUES(%s, %s)", self
.id, processor
.id)
813 self
._processor
= processor
815 processor
= property(get_processor
, set_processor
)
817 def set_processor_speeds(self
, clock_speed
, bogomips
):
818 self
.db
.execute("UPDATE fireinfo_profiles_processors \
819 SET clock_speed = %s, bogomips = %s WHERE profile_id = %s",
820 clock_speed
, bogomips
, self
.id)
825 return self
.processor
829 def get_memory(self
):
830 if not hasattr(self
, "_memory"):
831 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_memory \
832 WHERE profile_id = %s", self
.id)
835 self
._memory
= res
.amount
* 1024
841 def set_memory(self
, amount
):
842 if self
.memory
== amount
:
847 self
.db
.execute("DELETE FROM fireinfo_profiles_memory WHERE profile_id = %s", self
.id)
849 self
.db
.execute("INSERT INTO fireinfo_profiles_memory(profile_id, amount) \
850 VALUES(%s, %s)", self
.id, amount
)
852 self
._memory
= amount
* 1024
854 memory
= property(get_memory
, set_memory
)
857 def friendly_memory(self
):
858 return util
.format_size(self
.memory
or 0)
862 def get_storage(self
):
863 if not hasattr(self
, "_storage"):
864 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_storage \
865 WHERE profile_id = %s", self
.id)
868 self
._storage
= res
.amount
* 1024
874 def set_storage(self
, amount
):
875 if self
.storage
== amount
:
880 self
.db
.execute("DELETE FROM fireinfo_profiles_storage WHERE profile_id = %s", self
.id)
882 self
.db
.execute("INSERT INTO fireinfo_profiles_storage(profile_id, amount) \
883 VALUES(%s, %s)", self
.id, amount
)
885 self
._storage
= amount
* 1024
887 storage
= property(get_storage
, set_storage
)
890 def friendly_storage(self
):
891 return util
.format_size(self
.storage
)
895 def get_kernel_id(self
):
896 if not hasattr(self
, "_kernel_id"):
897 res
= self
.db
.get("SELECT fireinfo_profiles_kernels.kernel_id AS id FROM fireinfo_profiles \
898 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
899 WHERE fireinfo_profiles.id = %s", self
.id)
902 self
._kernel
_id
= res
.id
904 self
._kernel
_id
= None
906 return self
._kernel
_id
908 def set_kernel_id(self
, kernel_id
):
909 if self
.kernel_id
== kernel_id
:
912 self
.db
.execute("DELETE FROM fireinfo_profiles_kernels WHERE profile_id = %s", self
.id)
914 self
.db
.execute("INSERT INTO fireinfo_profiles_kernels(profile_id, kernel_id) \
915 VALUES(%s, %s)", self
.id, kernel_id
)
917 self
._kernel
_id
= kernel_id
918 if hasattr(self
, "_kernel"):
921 kernel_id
= property(get_kernel_id
, set_kernel_id
)
925 if not hasattr(self
, "_kernel"):
926 res
= self
.db
.get("SELECT fireinfo_kernels.name AS name FROM fireinfo_profiles \
927 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
928 LEFT JOIN fireinfo_kernels ON fireinfo_kernels.id = fireinfo_profiles_kernels.kernel_id \
929 WHERE fireinfo_profiles.id = %s", self
.id)
932 self
._kernel
= res
.name
940 def get_arch_id(self
):
941 if not hasattr(self
, "_arch_id"):
942 res
= self
.db
.get("SELECT fireinfo_profiles_arches.arch_id AS id FROM fireinfo_profiles \
943 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
944 WHERE fireinfo_profiles.id = %s", self
.id)
947 self
._arch
_id
= res
.id
953 def set_arch_id(self
, arch_id
):
954 if self
.arch_id
== arch_id
:
957 self
.db
.execute("DELETE FROM fireinfo_profiles_arches WHERE profile_id = %s", self
.id)
959 self
.db
.execute("INSERT INTO fireinfo_profiles_arches(profile_id, arch_id) \
960 VALUES(%s, %s)", self
.id, arch_id
)
963 if hasattr(self
, "_arch"):
966 arch_id
= property(get_arch_id
, set_arch_id
)
970 if not hasattr(self
, "_arch"):
971 res
= self
.db
.get("SELECT fireinfo_arches.name AS name FROM fireinfo_profiles \
972 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
973 LEFT JOIN fireinfo_arches ON fireinfo_arches.id = fireinfo_profiles_arches.arch_id \
974 WHERE fireinfo_profiles.id = %s", self
.id)
977 self
._arch
= res
.name
985 def get_release_id(self
):
986 if not hasattr(self
, "_release_id"):
987 res
= self
.db
.get("SELECT fireinfo_profiles_releases.release_id AS id FROM fireinfo_profiles \
988 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
989 WHERE fireinfo_profiles.id = %s", self
.id)
992 self
._release
_id
= res
.id
994 self
._release
_id
= None
996 return self
._release
_id
998 def set_release_id(self
, release_id
):
999 if self
.release_id
== release_id
:
1002 self
.db
.execute("DELETE FROM fireinfo_profiles_releases WHERE profile_id = %s", self
.id)
1004 self
.db
.execute("INSERT INTO fireinfo_profiles_releases(profile_id, release_id) \
1005 VALUES(%s, %s)", self
.id, release_id
)
1007 self
._release
_id
= release_id
1008 if hasattr(self
, "_release"):
1011 release_id
= property(get_release_id
, set_release_id
)
1015 if not hasattr(self
, "_release"):
1016 res
= self
.db
.get("SELECT fireinfo_releases.name AS name FROM fireinfo_profiles \
1017 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
1018 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
1019 WHERE fireinfo_profiles.id = %s", self
.id)
1022 self
._release
= self
._format
_release
(res
.name
)
1024 self
._release
= None
1026 return self
._release
1029 def _format_release(r
):
1033 # Remove the development header
1034 r
= r
.replace("Development Build: ", "")
1037 ("-beta", " - Beta "),
1038 ("-rc", " - Release Candidate "),
1039 ("core", "Core Update "),
1049 def release_short(self
):
1051 (r
"Release Candidate (\d+)", r
"RC\1"),
1055 for pattern
, repl
in pairs
:
1056 if re
.search(pattern
, s
) is None:
1059 s
= re
.sub(pattern
, repl
, s
)
1067 if not hasattr(self
, "_virtual"):
1068 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles_virtual \
1069 WHERE profile_id = %s", self
.id)
1072 self
._virtual
= True
1074 self
._virtual
= False
1076 return self
._virtual
1078 def get_hypervisor_id(self
):
1079 if not hasattr(self
, "_hypervisor_id"):
1080 res
= self
.db
.get("SELECT fireinfo_profiles_virtual.hypervisor_id AS id FROM fireinfo_profiles \
1081 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
1082 WHERE fireinfo_profiles.id = %s", self
.id)
1085 self
._hypervisor
_id
= res
.id
1087 self
._hypervisor
_id
= None
1089 return self
._hypervisor
_id
1091 def set_hypervisor_id(self
, hypervisor_id
):
1092 self
.db
.execute("DELETE FROM fireinfo_profiles_virtual WHERE profile_id = %s", self
.id)
1093 self
.db
.execute("INSERT INTO fireinfo_profiles_virtual(profile_id, hypervisor_id) \
1094 VALUES(%s, %s)", self
.id, hypervisor_id
)
1096 self
._hypervisor
_id
= hypervisor_id
1098 hypervisor_id
= property(get_hypervisor_id
, set_hypervisor_id
)
1101 def hypervisor(self
):
1102 if not hasattr(self
, "_hypervisor"):
1103 res
= self
.db
.get("SELECT fireinfo_hypervisors.name AS hypervisor FROM fireinfo_profiles \
1104 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
1105 LEFT JOIN fireinfo_hypervisors ON fireinfo_profiles_virtual.hypervisor_id = fireinfo_hypervisors.id \
1106 WHERE fireinfo_profiles.id = %s", self
.id)
1109 self
._hypervisor
= res
.hypervisor
1111 self
._hypervisor
= None
1113 return self
._hypervisor
1117 def get_language(self
):
1118 if not hasattr(self
, "_language"):
1119 res
= self
.db
.get("SELECT language FROM fireinfo_profiles_languages \
1120 WHERE profile_id = %s", self
.id)
1123 self
._language
= res
.language
1125 self
._language
= None
1127 return self
._language
1129 def set_language(self
, language
):
1130 self
.db
.execute("DELETE FROM fireinfo_profiles_languages WHERE profile_id = %s", self
.id)
1133 self
.db
.execute("INSERT INTO fireinfo_profiles_languages(profile_id, language) \
1134 VALUES(%s, %s)", self
.id, language
)
1136 self
._language
= language
1138 language
= property(get_language
, set_language
)
1142 def get_network(self
):
1143 if not hasattr(self
, "_network"):
1144 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_networks \
1145 WHERE profile_id = %s", self
.id)
1150 self
._network
= ProfileNetwork(res
)
1152 return self
._network
1154 def set_network(self
, network
):
1155 self
.db
.execute("DELETE FROM fireinfo_profiles_networks WHERE profile_id = %s", self
.id)
1158 self
.db
.execute("INSERT INTO fireinfo_profiles_networks(profile_id, \
1159 has_red, has_green, has_orange, has_blue) VALUES(%s, %s, %s, %s, %s)",
1160 self
.id, network
.has_red
, network
.has_green
, network
.has_orange
, network
.has_blue
)
1162 self
._network
= network
1164 network
= property(get_network
, set_network
)
1167 class ProfileData(Object
):
1168 def __init__(self
, backend
, id, data
=None, profile
=None):
1169 Object
.__init
__(self
, backend
)
1173 self
._profile
= profile
1177 if self
._data
is None:
1178 self
._data
= self
.db
.get("SELECT * FROM fireinfo_profile_data \
1179 WHERE id = %s", self
.id)
1185 if not self
._profile
:
1186 self
._profile
= self
.fireinfo
.get_profile_by_id(self
.profile_id
)
1188 return self
._profile
1191 def profile_id(self
):
1192 return self
.data
.profile_id
1195 class ProfileParserError(Exception):
1199 class ProfileParser(Object
):
1210 __processor_args
= (
1220 def __init__(self
, backend
, public_id
, blob
=None):
1221 Object
.__init
__(self
, backend
)
1223 self
.public_id
= public_id
1224 self
.private_id
= None
1226 self
.processor
= None
1227 self
.processor_clock_speed
= None
1228 self
.processor_bogomips
= None
1229 self
.system_id
= None
1233 self
.kernel_id
= None
1237 self
.release_id
= None
1238 self
.language
= None
1240 self
.hypervisor_id
= None
1243 self
.__parse
_blob
(blob
)
1245 def equals(self
, other
):
1246 if not self
.processor_id
== other
.processor_id
:
1249 if not self
.device_ids
== other
.device_ids
:
1252 if not self
.system_id
== other
.system_id
:
1255 if not self
.memory
== other
.memory
:
1258 if not self
.storage
== other
.storage
:
1261 if not self
.kernel_id
== other
.kernel_id
:
1264 if not self
.arch_id
== other
.arch_id
:
1267 if not self
.release_id
== other
.release_id
:
1270 if not self
.language
== other
.language
:
1273 if not self
.virtual
== other
.virtual
:
1277 if not self
.hypervisor_id
== other
.hypervisor_id
:
1280 if not self
.network
== other
.network
:
1285 def __parse_blob(self
, blob
):
1286 _profile
= blob
.get("profile", {})
1287 self
.private_id
= blob
.get("private_id")
1289 # Do not try to parse an empty profile
1294 _processor
= _profile
.get("cpu", {})
1295 self
.__parse
_processor
(_processor
)
1298 _devices
= _profile
.get("devices", [])
1299 self
.__parse
_devices
(_devices
)
1302 _system
= _profile
.get("system")
1304 self
.__parse
_system
(_system
)
1306 # Memory (convert to bytes)
1307 memory
= _system
.get("memory", None)
1309 self
.memory
= memory
* 1024
1311 # Storage size (convert to bytes)
1312 storage
= _system
.get("root_size", None)
1314 self
.storage
= storage
* 1024
1317 kernel
= _system
.get("kernel_release", None)
1319 self
.__parse
_kernel
(kernel
)
1322 release
= _system
.get("release", None)
1324 self
.__parse
_release
(release
)
1327 language
= _system
.get("language", None)
1329 self
.__parse
_language
(language
)
1332 self
.virtual
= _system
.get("virtual", False)
1334 hypervisor
= _profile
.get("hypervisor")
1335 self
.__parse
_hypervisor
(hypervisor
)
1338 _network
= _profile
.get("network")
1340 self
.__parse
_network
(_network
)
1343 def device_ids(self
):
1344 return sorted([d
.id for d
in self
.devices
])
1346 def __parse_devices(self
, _devices
):
1349 for _device
in _devices
:
1351 for arg
in self
.__device
_args
:
1352 args
[arg
] = _device
.get(arg
, None)
1354 # Skip if the subsystem is not set
1355 if not args
.get("subsystem", None):
1358 # Find the device or create a new one.
1359 device
= self
.fireinfo
.get_device(**args
)
1361 device
= self
.fireinfo
.create_device(**args
)
1363 self
.devices
.append(device
)
1365 def __parse_system(self
, system
):
1366 vendor
= system
.get("vendor", None)
1370 model
= system
.get("model", None)
1374 self
.system_id
= self
.fireinfo
.get_system(vendor
, model
)
1375 if not self
.system_id
:
1376 self
.system_id
= self
.fireinfo
.create_system(vendor
, model
)
1379 def processor_id(self
):
1380 if not self
.processor
:
1383 return self
.processor
.id
1385 def __parse_processor(self
, _processor
):
1387 for arg
in self
.__processor
_args
:
1388 if arg
== "core_count":
1393 args
[arg
] = _processor
.get(_arg
, None)
1395 self
.processor
= self
.fireinfo
.get_processor(**args
)
1396 if not self
.processor
:
1397 self
.processor
= self
.fireinfo
.create_processor(**args
)
1399 self
.processor_clock_speed
= _processor
.get("speed", None)
1400 self
.processor_bogomips
= _processor
.get("bogomips", None)
1402 arch
= _processor
.get("arch", None)
1404 self
.__parse
_arch
(arch
)
1406 def __parse_kernel(self
, kernel
):
1407 self
.kernel_id
= self
.fireinfo
.get_kernel(kernel
)
1408 if not self
.kernel_id
:
1409 self
.kernel_id
= self
.fireinfo
.create_kernel(kernel
)
1410 assert self
.kernel_id
1412 self
.kernel
= kernel
1414 def __parse_arch(self
, arch
):
1415 self
.arch_id
= self
.fireinfo
.get_arch(arch
)
1416 if not self
.arch_id
:
1417 self
.arch_id
= self
.fireinfo
.create_arch(arch
)
1421 def __parse_release(self
, release
):
1422 # Remove the arch bit
1424 r
= [e
for e
in release
.split() if e
]
1425 for s
in ("(x86_64)", "(i586)", "(armv5tel)"):
1432 release
= " ".join(r
)
1434 self
.release_id
= self
.fireinfo
.get_release(release
)
1435 if not self
.release_id
:
1436 self
.release_id
= self
.fireinfo
.create_release(release
)
1437 assert self
.release_id
1439 self
.release
= release
1441 def __parse_language(self
, language
):
1442 self
.language
= language
1443 self
.language
, delim
, rest
= self
.language
.partition(".")
1444 self
.language
, delim
, rest
= self
.language
.partition("_")
1446 def __parse_hypervisor(self
, hypervisor
):
1447 vendor
= hypervisor
.get("vendor", "other")
1449 if vendor
in ("other", "unknown"):
1450 self
.hypervisor_id
= None
1453 self
.hypervisor_id
= self
.fireinfo
.get_hypervisor(vendor
)
1454 if not self
.hypervisor_id
:
1455 self
.hypervisor_id
= self
.fireinfo
.create_hypervisor(vendor
)
1457 def __parse_network(self
, network
):
1458 self
.network
= ProfileNetwork({
1459 "has_red" : network
.get("red", False),
1460 "has_green" : network
.get("green", False),
1461 "has_orange" : network
.get("orange", False),
1462 "has_blue" : network
.get("blue", False),
1466 class Fireinfo(Object
):
1467 def get_profile_count(self
, when
=None):
1468 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1469 WHERE then_or_now(%s) BETWEEN time_created AND time_valid", when
)
1474 def get_total_updates_count(self
, when
=None):
1475 res
= self
.db
.get("SELECT COUNT(*) + SUM(updates) AS count \
1476 FROM fireinfo_profiles WHERE time_created <= then_or_now(%s)", when
)
1483 def parse_profile(self
, public_id
, blob
):
1484 return ProfileParser(self
.backend
, public_id
, blob
)
1488 def profile_exists(self
, public_id
):
1489 res
= self
.db
.get("SELECT id FROM fireinfo_profiles \
1490 WHERE public_id = %s LIMIT 1", public_id
)
1497 def profile_rate_limit_active(self
, public_id
, when
=None):
1498 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles_log \
1499 WHERE public_id = %s AND ts >= then_or_now(%s) - INTERVAL '60 minutes'",
1502 if res
and res
.count
>= 10:
1507 def is_private_id_change_permitted(self
, public_id
, private_id
, when
=None):
1508 # Check if a profile exists with a different private id that is still valid
1509 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles \
1510 WHERE public_id = %s AND NOT private_id = %s \
1511 AND time_valid >= then_or_now(%s) LIMIT 1", public_id
, private_id
, when
)
1518 def get_profile(self
, public_id
, private_id
=None, when
=None):
1519 res
= self
.db
.get("SELECT * FROM fireinfo_profiles \
1520 WHERE public_id = %s AND \
1521 (CASE WHEN %s IS NULL THEN TRUE ELSE private_id = %s END) AND \
1522 then_or_now(%s) BETWEEN time_created AND time_valid \
1523 ORDER BY time_updated DESC LIMIT 1",
1524 public_id
, private_id
, private_id
, when
)
1527 return Profile(self
.backend
, res
.id, res
)
1529 def get_profile_with_data(self
, public_id
, when
=None):
1530 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1531 SELECT * FROM profiles JOIN fireinfo_profiles ON profiles.id = fireinfo_profiles.id \
1532 WHERE public_id = %s ORDER BY time_updated DESC LIMIT 1", when
, public_id
)
1535 return Profile(self
.backend
, res
.id, res
)
1537 def get_profiles(self
, public_id
):
1538 res
= self
.db
.query("SELECT * FROM fireinfo_profiles \
1539 WHERE public_id = %s ORDER BY time_created DESC", public_id
)
1543 profile
= Profile(self
.backend
, row
.id, row
)
1544 profiles
.append(profile
)
1548 def create_profile(self
, public_id
, private_id
, when
=None):
1549 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
1551 res
= self
.db
.get("INSERT INTO fireinfo_profiles(public_id, private_id, \
1552 time_created, time_updated, time_valid) VALUES(%s, %s, then_or_now(%s), \
1553 then_or_now(%s), then_or_now(%s) + INTERVAL '%s days') RETURNING id",
1554 public_id
, private_id
, when
, when
, when
, valid
)
1557 p
= Profile(self
.backend
, res
.id)
1558 p
.log_profile_update()
1564 def create_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1565 driver
=None, deviceclass
=None):
1566 res
= self
.db
.get("INSERT INTO fireinfo_devices(subsystem, vendor, model, \
1567 sub_vendor, sub_model, driver, deviceclass) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1568 RETURNING id", subsystem
, vendor
, model
, sub_vendor
, sub_model
, driver
, deviceclass
)
1571 return Device(self
.backend
, res
.id)
1573 def get_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1574 driver
=None, deviceclass
=None):
1575 res
= self
.db
.get("SELECT * FROM fireinfo_devices \
1576 WHERE subsystem = %s AND vendor = %s AND model = %s \
1577 AND sub_vendor IS NOT DISTINCT FROM %s \
1578 AND sub_model IS NOT DISTINCT FROM %s \
1579 AND driver IS NOT DISTINCT FROM %s \
1580 AND deviceclass IS NOT DISTINCT FROM %s \
1581 LIMIT 1", subsystem
, vendor
, model
, sub_vendor
,
1582 sub_model
, driver
, deviceclass
)
1585 return Device(self
.backend
, res
.id, res
)
1589 def create_system(self
, vendor
, model
):
1590 res
= self
.db
.get("INSERT INTO fireinfo_systems(vendor, model) \
1591 VALUES(%s, %s) RETURNING id", vendor
, model
)
1596 def get_system(self
, vendor
, model
):
1597 res
= self
.db
.get("SELECT id FROM fireinfo_systems WHERE vendor IS NOT DISTINCT FROM %s \
1598 AND model IS NOT DISTINCT FROM %s LIMIT 1", vendor
, model
)
1605 def create_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1606 res
= self
.db
.get("INSERT INTO fireinfo_processors(vendor, model_string, \
1607 family, model, stepping, core_count, flags) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1608 RETURNING id", vendor
or None, model_string
or None, family
, model
, stepping
, core_count
, flags
)
1611 return Processor(self
.backend
, res
.id)
1613 def get_processor_by_id(self
, processor_id
, **kwargs
):
1614 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1615 WHERE id = %s", processor_id
)
1618 return Processor(self
.backend
, res
.id, data
=res
, **kwargs
)
1620 def get_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1624 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1625 WHERE vendor IS NOT DISTINCT FROM %s AND model_string IS NOT DISTINCT FROM %s \
1626 AND family IS NOT DISTINCT FROM %s AND model IS NOT DISTINCT FROM %s \
1627 AND stepping IS NOT DISTINCT FROM %s AND core_count = %s \
1628 AND flags <@ %s AND flags @> %s", vendor
or None, model_string
or None,
1629 family
, model
, stepping
, core_count
, flags
, flags
)
1632 return Processor(self
.backend
, res
.id, res
)
1636 def create_kernel(self
, kernel
):
1637 res
= self
.db
.get("INSERT INTO fireinfo_kernels(name) VALUES(%s) \
1638 RETURNING id", kernel
)
1643 def get_kernel(self
, kernel
):
1644 res
= self
.db
.get("SELECT id FROM fireinfo_kernels WHERE name = %s", kernel
)
1651 def create_arch(self
, arch
):
1652 res
= self
.db
.get("INSERT INTO fireinfo_arches(name) VALUES(%s) \
1653 RETURNING id", arch
)
1658 def get_arch(self
, arch
):
1659 res
= self
.db
.get("SELECT id FROM fireinfo_arches WHERE name = %s", arch
)
1666 def create_release(self
, release
):
1667 res
= self
.db
.get("INSERT INTO fireinfo_releases(name) VALUES(%s) \
1668 RETURNING id", release
)
1673 def get_release(self
, release
):
1674 res
= self
.db
.get("SELECT id FROM fireinfo_releases WHERE name = %s", release
)
1679 def get_release_penetration(self
, release
, when
=None):
1680 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1681 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS penetration FROM profiles \
1682 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1683 WHERE fireinfo_profiles_releases.release_id = %s", when
, release
.fireinfo_id
)
1686 return res
.penetration
1688 def get_random_country_penetration(self
):
1689 res
= self
.db
.get("SELECT * FROM fireinfo_country_percentages \
1690 ORDER BY RANDOM() LIMIT 1")
1693 return database
.Row({
1694 "country" : iso3166
.countries
.get(res
.location
),
1695 "percentage" : res
.count
,
1700 def create_hypervisor(self
, hypervisor
):
1701 res
= self
.db
.get("INSERT INTO fireinfo_hypervisors(name) VALUES(%s) \
1702 RETURNING id", hypervisor
)
1707 def get_hypervisor(self
, hypervisor
):
1708 res
= self
.db
.get("SELECT id FROM fireinfo_hypervisors WHERE name = %s",
1716 def handle_profile(self
, *args
, **kwargs
):
1717 self
.db
.execute("START TRANSACTION")
1719 # Wrap all the handling of the profile in a huge transaction.
1721 self
._handle
_profile
(*args
, **kwargs
)
1724 self
.db
.execute("ROLLBACK")
1728 self
.db
.execute("COMMIT")
1730 def _handle_profile(self
, public_id
, profile_blob
, location
=None, when
=None):
1731 private_id
= profile_blob
.get("private_id", None)
1734 # Check if the profile already exists in the database.
1735 profile
= self
.fireinfo
.get_profile(public_id
, private_id
=private_id
, when
=when
)
1737 # Check if the update can actually be updated
1738 if profile
and self
.fireinfo
.profile_rate_limit_active(public_id
, when
=when
):
1739 logging
.warning("There were too many updates for this profile in the last hour: %s" % public_id
)
1742 elif not self
.is_private_id_change_permitted(public_id
, private_id
, when
=when
):
1743 logging
.warning("Changing private id is not permitted for profile: %s" % public_id
)
1747 profile_parser
= self
.parse_profile(public_id
, profile_blob
)
1749 # If a profile exists, check if it matches and if so, just update the
1752 # Check if the profile has changed. If so, update the data.
1753 if profile_parser
.equals(profile
):
1754 profile
.updated(profile_parser
, location
=location
, when
=when
)
1757 # If it does not match, we assume that it is expired and
1758 # create a new profile.
1759 profile
.expired(when
=when
)
1761 # Replace the old profile with a new one
1762 profile
= self
.fireinfo
.create_profile(public_id
, private_id
, when
=when
)
1763 profile
.parse(profile_parser
)
1766 profile
.set_location(location
)
1772 def get_random_profile(self
, when
=None):
1773 # Check if the architecture exists so that we pick a profile with some data
1774 res
= self
.db
.get("SELECT public_id FROM fireinfo_profiles \
1775 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
1776 WHERE fireinfo_profiles_arches.profile_id IS NOT NULL \
1777 AND then_or_now(%s) BETWEEN time_created AND time_valid ORDER BY RANDOM() LIMIT 1", when
)
1780 return res
.public_id
1782 def get_active_profiles(self
, when
=None):
1783 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1784 SELECT COUNT(*) AS with_data, (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1785 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1786 WHERE fireinfo_profiles_releases.profile_id IS NOT NULL", when
)
1789 return res
.with_data
, res
.count
1791 def get_archive_size(self
, when
=None):
1792 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1793 WHERE time_created <= then_or_now(%s)", when
)
1798 def get_geo_location_map(self
, when
=None, minimum_percentage
=0):
1799 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1800 SELECT location, COUNT(location)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1801 LEFT JOIN fireinfo_profiles_locations ON profiles.id = fireinfo_profiles_locations.profile_id \
1802 WHERE fireinfo_profiles_locations.location IS NOT NULL GROUP BY location \
1803 HAVING COUNT(location)::float / (SELECT COUNT(*) FROM profiles) >= %s ORDER BY count DESC",
1804 when
, minimum_percentage
)
1806 return list(((r
.location
, r
.count
) for r
in res
))
1809 def cpu_vendors(self
):
1810 res
= self
.db
.query("SELECT DISTINCT vendor FROM fireinfo_processors ORDER BY vendor")
1812 return (CPU_VENDORS
.get(r
.vendor
, r
.vendor
) for r
in res
)
1814 def get_cpu_vendors_map(self
, when
=None):
1815 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1816 SELECT COALESCE(vendor, %s) AS vendor, COUNT(vendor)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1817 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1818 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1819 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL GROUP BY vendor ORDER BY count DESC", when
, "Unknown")
1821 return ((CPU_VENDORS
.get(r
.vendor
, r
.vendor
), r
.count
) for r
in res
)
1823 def get_cpu_clock_speeds(self
, when
=None):
1824 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1825 SELECT AVG(fireinfo_profiles_processors.clock_speed) AS avg, \
1826 STDDEV(fireinfo_profiles_processors.clock_speed) AS stddev, \
1827 MIN(fireinfo_profiles_processors.clock_speed) AS min, \
1828 MAX(fireinfo_profiles_processors.clock_speed) AS max FROM profiles \
1829 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1830 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1831 AND fireinfo_profiles_processors.clock_speed > 0 \
1832 AND fireinfo_profiles_processors.clock_speed < fireinfo_profiles_processors.bogomips \
1833 AND fireinfo_profiles_processors.bogomips <= %s", when
, 10000)
1836 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1838 def get_cpus_with_platform_and_flag(self
, platform
, flag
, when
=None):
1839 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1840 processors AS (SELECT fireinfo_processors.id AS id, fireinfo_processors.flags AS flags FROM profiles \
1841 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1842 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1843 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1844 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1845 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1846 AND fireinfo_arches.platform = %s AND NOT 'hypervisor' = ANY(fireinfo_processors.flags)) \
1847 SELECT (COUNT(*)::float / (SELECT NULLIF(COUNT(*), 0) FROM processors)) AS count FROM processors \
1848 WHERE %s = ANY(processors.flags)", when
, platform
, flag
)
1850 return res
.count
or 0
1852 def get_common_cpu_flags_by_platform(self
, platform
, when
=None):
1853 if platform
== "arm":
1855 "lpae", "neon", "thumb", "thumbee", "vfpv3", "vfpv4",
1857 elif platform
== "x86":
1859 "aes", "avx", "avx2", "lm", "mmx", "mmxext", "nx", "pae",
1860 "pni", "popcnt", "sse", "sse2", "rdrand", "ssse3", "sse4a",
1861 "sse4_1", "sse4_2", "pclmulqdq", "rdseed",
1868 ret
.append((flag
, self
.get_cpus_with_platform_and_flag(platform
, flag
, when
=when
)))
1870 # Add virtual CPU flag "virt" for virtualization support
1871 if platform
== "x86":
1873 self
.get_cpus_with_platform_and_flag(platform
, "vmx", when
=when
) + \
1874 self
.get_cpus_with_platform_and_flag(platform
, "svm", when
=when
)))
1876 return sorted(ret
, key
=lambda x
: x
[1], reverse
=True)
1878 def get_average_memory_amount(self
, when
=None):
1879 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1880 SELECT AVG(fireinfo_profiles_memory.amount) AS avg FROM profiles \
1881 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id", when
)
1886 def get_arch_map(self
, when
=None):
1887 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1888 SELECT fireinfo_arches.name AS arch, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count \
1890 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1891 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1892 WHERE NOT fireinfo_profiles_arches.profile_id IS NULL \
1893 GROUP BY fireinfo_arches.id ORDER BY count DESC", when
)
1895 return ((r
.arch
, r
.count
) for r
in res
)
1899 def get_hypervisor_map(self
, when
=None):
1900 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1901 virtual_profiles AS (SELECT profiles.id AS profile_id, fireinfo_profiles_virtual.hypervisor_id FROM profiles \
1902 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1903 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL) \
1904 SELECT COALESCE(fireinfo_hypervisors.name, %s) AS name, \
1905 COUNT(*)::float / (SELECT COUNT(*) FROM virtual_profiles) AS count FROM virtual_profiles \
1906 LEFT JOIN fireinfo_hypervisors ON virtual_profiles.hypervisor_id = fireinfo_hypervisors.id \
1907 GROUP BY fireinfo_hypervisors.name ORDER BY count DESC", when
, "unknown")
1909 return ((r
.name
, r
.count
) for r
in res
)
1911 def get_virtual_ratio(self
, when
=None):
1912 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1913 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1914 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1915 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL", when
)
1922 def get_releases_map(self
, when
=None):
1923 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1924 SELECT fireinfo_releases.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1925 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1926 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
1927 GROUP BY fireinfo_releases.name ORDER BY count DESC", when
)
1929 return ((r
.name
, r
.count
) for r
in res
)
1931 def get_kernels_map(self
, when
=None):
1932 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1933 SELECT fireinfo_kernels.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1934 LEFT JOIN fireinfo_profiles_kernels ON profiles.id = fireinfo_profiles_kernels.profile_id \
1935 LEFT JOIN fireinfo_kernels ON fireinfo_profiles_kernels.kernel_id = fireinfo_kernels.id \
1936 GROUP BY fireinfo_kernels.name ORDER BY count DESC", when
)
1938 return ((r
.name
, r
.count
) for r
in res
)
1940 def _process_devices(self
, devices
):
1944 dev
= Device(self
.backend
, dev
.get("id", None), dev
)
1949 def get_driver_map(self
, driver
, when
=None):
1950 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1951 devices AS (SELECT * FROM profiles \
1952 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1953 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1954 WHERE driver = %s) \
1955 SELECT subsystem, model, vendor, driver, deviceclass, \
1956 COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1957 GROUP BY subsystem, model, vendor, driver, deviceclass \
1958 ORDER BY percentage DESC", when
, driver
)
1960 return self
._process
_devices
(res
)
1963 "pci" : hwdata
.PCI(),
1964 "usb" : hwdata
.USB(),
1967 def get_vendor_string(self
, subsystem
, vendor_id
):
1969 cls
= self
.subsystem2class
[subsystem
]
1973 return cls
.get_vendor(vendor_id
) or ""
1975 def get_model_string(self
, subsystem
, vendor_id
, model_id
):
1977 cls
= self
.subsystem2class
[subsystem
]
1981 return cls
.get_device(vendor_id
, model_id
) or ""
1983 def get_vendor_list(self
, when
=None):
1984 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1985 SELECT DISTINCT fireinfo_devices.subsystem AS subsystem, fireinfo_devices.vendor AS vendor FROM profiles \
1986 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1987 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1988 WHERE NOT fireinfo_devices.driver = ANY(%s)", when
, IGNORED_DEVICES
)
1992 vendor
= self
.get_vendor_string(row
.subsystem
, row
.vendor
)
1994 # Drop if vendor could not be determined
1999 vendors
[vendor
].append((row
.subsystem
, row
.vendor
))
2001 vendors
[vendor
] = [(row
.subsystem
, row
.vendor
)]
2003 vendors
= list(vendors
.items())
2004 return sorted(vendors
)
2006 def get_devices_by_vendor(self
, subsystem
, vendor
, when
=None):
2007 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
2008 devices AS (SELECT * FROM profiles \
2009 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
2010 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
2011 WHERE NOT fireinfo_devices.driver = ANY(%s)), \
2012 vendor_devices AS (SELECT * FROM devices WHERE devices.subsystem = %s AND devices.vendor = %s) \
2013 SELECT subsystem, model, vendor, driver, deviceclass FROM vendor_devices \
2014 GROUP BY subsystem, model, vendor, driver, deviceclass", when
, IGNORED_DEVICES
, subsystem
, vendor
)
2016 return self
._process
_devices
(res
)