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",
35 (r
"(AMD Athlon).*(XP).*", r
"\1 \2"),
36 (r
"(AMD Phenom).* ([0-9]+) .*", r
"\1 \2"),
37 (r
"(AMD Phenom).*", r
"\1"),
38 (r
"(AMD Sempron).*", r
"\1"),
39 (r
"AMD Athlon.* II X2 ([a-z0-9]+).*", r
"AMD Athlon X2 \1"),
40 (r
"(Geode).*", r
"\1"),
43 (r
"Intel\(R\) (Atom|Celeron).*CPU\s*([A-Z0-9]+) .*", r
"Intel \1 \2"),
44 (r
"(Intel).*(Celeron).*", r
"\1 \2"),
45 (r
"Intel.* Core.*2 Duo *CPU .* ([A-Z0-9]+) .*", r
"Intel C2D \1"),
46 (r
"Intel.* Core.*2 CPU .* ([A-Z0-9]+) .*", r
"Intel C2 \1"),
47 (r
"Intel.* Core.*2 Quad *CPU .* ([A-Z0-9]+) .*", r
"Intel C2Q \1"),
48 (r
"Intel.* Xeon.* CPU .* ([A-Z0-9]+) .*", r
"Intel Xeon \1"),
49 (r
"(Intel).*(Xeon).*", r
"\1 \2"),
50 (r
"Intel.* Pentium.* (D|4) .*", r
"Intel Pentium \1"),
51 (r
"Intel.* Pentium.* Dual .* ([A-Z0-9]+) .*", r
"Intel Pentium Dual \1"),
52 (r
"Pentium.* Dual-Core .* ([A-Z0-9]+) .*", r
"Intel Pentium Dual \1"),
53 (r
"(Pentium I{2,3}).*", r
"Intel \1"),
54 (r
"(Celeron \(Coppermine\))", r
"Intel Celeron"),
57 (r
"(VIA \w*).*", r
"\1"),
60 (r
"QEMU Virtual CPU version .*", r
"QEMU CPU"),
63 (r
"Feroceon .*", r
"ARM Feroceon"),
66 IGNORED_DEVICES
= ["usb",]
68 class ProfileDict(object):
69 def __init__(self
, data
):
73 class ProfileNetwork(ProfileDict
):
74 def __eq__(self
, other
):
75 if not self
.has_red
== other
.has_red
:
78 if not self
.has_green
== other
.has_green
:
81 if not self
.has_orange
== other
.has_orange
:
84 if not self
.has_blue
== other
.has_blue
:
92 for zone
in ("red", "green", "orange", "blue"):
93 if self
.has_zone(zone
):
98 def has_zone(self
, name
):
99 return self
._data
.get("has_%s" % name
)
103 return self
._data
.get("has_red", False)
107 return self
._data
.get("has_green", False)
110 def has_orange(self
):
111 return self
._data
.get("has_orange", False)
115 return self
._data
.get("has_blue", False)
118 class Processor(Object
):
119 def __init__(self
, backend
, id, data
=None, clock_speed
=None, bogomips
=None):
120 Object
.__init
__(self
, backend
)
124 self
.__clock
_speed
= clock_speed
125 self
.__bogomips
= bogomips
130 if not self
.model_string
.startswith(self
.vendor
):
131 s
.append(self
.vendor
)
134 s
.append(self
.model_string
)
136 if self
.core_count
> 1:
137 s
.append("x%s" % self
.core_count
)
143 if self
.__data
is None:
144 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_processors \
145 WHERE id = %s", self
.id)
152 return CPU_VENDORS
[self
.data
.vendor
]
154 return self
.data
.vendor
158 return self
.data
.model
161 def model_string(self
):
162 s
= self
.data
.model_string
.split()
164 return " ".join((e
for e
in s
if e
))
168 return self
.data
.flags
170 def has_flag(self
, flag
):
171 return flag
in self
.flags
174 return self
.has_flag("ht")
177 def core_count(self
):
178 return self
.data
.core_count
183 return self
.core_count
// 2
185 return self
.core_count
188 def clock_speed(self
):
189 return self
.__clock
_speed
191 def format_clock_speed(self
):
192 if not self
.clock_speed
:
195 if self
.clock_speed
< 1000:
196 return "%dMHz" % self
.clock_speed
198 return "%.2fGHz" % round(self
.clock_speed
/ 1000, 2)
202 return self
.__bogomips
205 def capabilities(self
):
207 ("64bit", self
.has_flag("lm")),
208 ("aes", self
.has_flag("aes")),
209 ("nx", self
.has_flag("nx")),
210 ("pae", self
.has_flag("pae") or self
.has_flag("lpae")),
211 ("rdrand", self
.has_flag("rdrand")),
214 # If the system is already running in a virtual environment,
215 # we cannot correctly detect if the CPU supports svm or vmx
216 if self
.has_flag("hypervisor"):
217 caps
.append(("virt", None))
219 caps
.append(("virt", self
.has_flag("vmx") or self
.has_flag("svm")))
223 def format_model(self
):
224 s
= self
.model_string
225 for pattern
, repl
in CPU_STRINGS
:
226 if re
.match(pattern
, s
) is None:
228 return re
.sub(pattern
, repl
, s
)
230 # Otherwise remove the symbols
231 for i
in ("C", "R", "TM"):
232 s
= s
.replace("(%s)" % i
, "")
237 def friendly_string(self
):
240 model
= self
.format_model()
243 clock_speed
= self
.format_clock_speed()
245 s
.append("@ %s" % clock_speed
)
248 s
.append("x%s" % self
.count
)
253 class Device(Object
):
256 "00" : N_("Unclassified"),
257 "01" : N_("Mass storage"),
258 "02" : N_("Network"),
259 "03" : N_("Display"),
260 "04" : N_("Multimedia"),
261 "05" : N_("Memory controller"),
263 "07" : N_("Communication"),
264 "08" : N_("Generic system peripheral"),
265 "09" : N_("Input device"),
266 "0a" : N_("Docking station"),
267 "0b" : N_("Processor"),
268 "0c" : N_("Serial bus"),
269 "0d" : N_("Wireless"),
270 "0e" : N_("Intelligent controller"),
271 "0f" : N_("Satellite communications controller"),
272 "10" : N_("Encryption"),
273 "11" : N_("Signal processing controller"),
274 "ff" : N_("Unassigned class"),
278 "00" : N_("Unclassified"),
279 "01" : N_("Multimedia"),
280 "02" : N_("Communication"),
281 "03" : N_("Input device"),
282 "05" : N_("Generic system peripheral"),
284 "07" : N_("Printer"),
285 "08" : N_("Mass storage"),
287 "0a" : N_("Communication"),
288 "0b" : N_("Smart card"),
289 "0d" : N_("Encryption"),
290 "0e" : N_("Display"),
291 "0f" : N_("Personal Healthcare"),
292 "dc" : N_("Diagnostic Device"),
293 "e0" : N_("Wireless"),
294 "ef" : N_("Unclassified"),
295 "fe" : N_("Unclassified"),
296 "ff" : N_("Unclassified"),
300 def __init__(self
, backend
, id, data
=None):
301 Object
.__init
__(self
, backend
)
307 return "<%s vendor=%s model=%s>" % (self
.__class
__.__name
__,
308 self
.vendor_string
, self
.model_string
)
310 def __cmp__(self
, other
):
311 if self
.id and self
.id == other
.id:
315 cmp(self
.subsystem
, other
.subsystem
) or \
316 cmp(self
.vendor_string
, other
.vendor_string
) or \
317 cmp(self
.vendor
, other
.vendor
) or \
318 cmp(self
.model_string
, other
.model_string
) or \
319 cmp(self
.model
, other
.model
) or \
320 cmp(self
.driver
, other
.driver
)
324 if self
.__data
is None:
327 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_devices \
328 WHERE id = %s", self
.id)
332 def is_showable(self
):
333 if self
.driver
in IGNORED_DEVICES
:
336 if self
.driver
in ("pcieport", "hub"):
343 return self
.data
.subsystem
347 return self
.data
.model
350 def model_string(self
):
351 return self
.fireinfo
.get_model_string(self
.subsystem
,
352 self
.vendor
, self
.model
)
356 return self
.data
.vendor
359 def vendor_string(self
):
360 return self
.fireinfo
.get_vendor_string(self
.subsystem
, self
.vendor
)
364 return self
.data
.driver
368 classid
= self
.data
.deviceclass
370 if self
.subsystem
== "pci":
371 classid
= classid
[:-4]
372 if len(classid
) == 1:
373 classid
= "0%s" % classid
375 elif self
.subsystem
== "usb" and classid
:
376 classid
= classid
.split("/")[0]
377 classid
= "%02x" % int(classid
)
380 return self
.classid2name
[self
.subsystem
][classid
]
385 def percentage(self
):
386 return self
.data
.get("percentage", None)
389 class Profile(Object
):
390 def __init__(self
, backend
, id, data
=None):
391 Object
.__init
__(self
, backend
)
397 return "<%s %s>" % (self
.__class
__.__name
__, self
.public_id
)
399 def __cmp__(self
, other
):
400 return cmp(self
.id, other
.id)
402 def is_showable(self
):
410 if self
.__data
is None:
411 self
.__data
= self
.db
.get("SELECT * FROM fireinfo_profiles \
412 WHERE id = %s", self
.id)
418 return self
.data
.public_id
421 def private_id(self
):
422 raise NotImplementedError
425 def time_created(self
):
426 return self
.data
.time_created
429 def time_updated(self
):
430 return self
.data
.time_updated
432 def updated(self
, profile_parser
=None, location
=None, when
=None):
433 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
435 self
.db
.execute("UPDATE fireinfo_profiles \
437 time_updated = then_or_now(%s), \
438 time_valid = then_or_now(%s) + INTERVAL '%s days', \
439 updates = updates + 1 \
440 WHERE id = %s", when
, when
, valid
, self
.id)
443 self
.set_processor_speeds(
444 profile_parser
.processor_clock_speed
,
445 profile_parser
.processor_bogomips
,
449 self
.set_location(location
)
451 def expired(self
, when
=None):
452 self
.db
.execute("UPDATE fireinfo_profiles \
453 SET time_valid = then_or_now(%s) WHERE id = %s", when
, self
.id)
455 def parse(self
, parser
):
457 self
.processor
= parser
.processor
458 self
.set_processor_speeds(parser
.processor_clock_speed
, parser
.processor_bogomips
)
461 self
.devices
= parser
.devices
464 self
.system_id
= parser
.system_id
467 self
.memory
= parser
.memory
470 self
.storage
= parser
.storage
473 self
.kernel_id
= parser
.kernel_id
476 self
.arch_id
= parser
.arch_id
479 self
.release_id
= parser
.release_id
482 self
.language
= parser
.language
486 self
.hypervisor_id
= parser
.hypervisor_id
489 self
.network
= parser
.network
493 def get_location(self
):
494 if not hasattr(self
, "_location"):
495 res
= self
.db
.get("SELECT location FROM fireinfo_profiles_locations \
496 WHERE profile_id = %s", self
.id)
499 self
._location
= res
.location
501 self
._location
= None
503 return self
._location
505 def set_location(self
, location
):
506 if self
.location
== location
:
509 self
.db
.execute("DELETE FROM fireinfo_profiles_locations \
510 WHERE profile_id = %s", self
.id)
511 self
.db
.execute("INSERT INTO fireinfo_profiles_locations(profile_id, location) \
512 VALUES(%s, %s)", self
.id, location
)
514 self
._location
= location
516 location
= property(get_location
, set_location
)
519 def location_string(self
):
520 return self
.geoip
.get_country_name(self
.location
)
525 def device_ids(self
):
526 if not hasattr(self
, "_device_ids"):
527 res
= self
.db
.query("SELECT device_id FROM fireinfo_profiles_devices \
528 WHERE profile_id = %s", self
.id)
530 self
._device
_ids
= sorted([r
.device_id
for r
in res
])
532 return self
._device
_ids
534 def get_devices(self
):
535 if not hasattr(self
, "_devices"):
536 res
= self
.db
.query("SELECT * FROM fireinfo_devices \
537 LEFT JOIN fireinfo_profiles_devices ON \
538 fireinfo_devices.id = fireinfo_profiles_devices.device_id \
539 WHERE fireinfo_profiles_devices.profile_id = %s", self
.id)
543 device
= Device(self
.backend
, row
.id, row
)
544 self
._devices
.append(device
)
548 def set_devices(self
, devices
):
549 device_ids
= [d
.id for d
in devices
]
551 self
.db
.execute("DELETE FROM fireinfo_profiles_devices WHERE profile_id = %s", self
.id)
552 self
.db
.executemany("INSERT INTO fireinfo_profiles_devices(profile_id, device_id) \
553 VALUES(%s, %s)", ((self
.id, d
) for d
in device_ids
))
555 self
._devices
= devices
556 self
._device
_ids
= device_ids
558 devices
= property(get_devices
, set_devices
)
560 def count_device(self
, subsystem
, vendor
, model
):
563 for dev
in self
.devices
:
564 if dev
.subsystem
== subsystem
and dev
.vendor
== vendor
and dev
.model
== model
:
571 def get_system_id(self
):
572 if not hasattr(self
, "_system_id"):
573 res
= self
.db
.get("SELECT system_id AS id FROM fireinfo_profiles_systems \
574 WHERE profile_id = %s", self
.id)
577 self
._system
_id
= res
.id
579 self
._system
_id
= None
581 return self
._system
_id
583 def set_system_id(self
, system_id
):
584 self
.db
.execute("DELETE FROM fireinfo_profiles_systems WHERE profile_id = %s", self
.id)
587 self
.db
.execute("INSERT INTO fireinfo_profiles_systems(profile_id, system_id) \
588 VALUES(%s, %s)", self
.id, system_id
)
590 self
._system
_id
= None
591 if hasattr(self
, "_system"):
594 system_id
= property(get_system_id
, set_system_id
)
598 if not hasattr(self
, "_system"):
599 res
= self
.db
.get("SELECT fireinfo_systems.vendor AS vendor, fireinfo_systems.model AS model \
600 FROM fireinfo_profiles_systems \
601 LEFT JOIN fireinfo_systems ON fireinfo_profiles_systems.system_id = fireinfo_systems.id \
602 WHERE fireinfo_profiles_systems.profile_id = %s", self
.id)
605 self
._system
= (res
.vendor
, res
.model
)
607 self
._system
= (None, None)
612 def system_vendor(self
):
620 def system_model(self
):
628 def appliance_id(self
):
629 if not hasattr(self
, "_appliance_id"):
631 ("fountainnetworks-prime", self
._appliance
_test
_fountainnetworks
_prime
),
632 ("lightningwirelabs-eco", self
._appliance
_test
_lightningwirelabs
_eco
),
635 self
._appliance
_id
= None
636 for name
, test_func
in appliances
:
640 self
._appliance
_id
= name
643 return self
._appliance
_id
647 if self
.appliance_id
== "fountainnetworks-prime":
648 return "Fountain Networks - IPFire Prime Box"
650 elif self
.appliance_id
== "lightningwirelabs-eco":
651 return "Lightning Wire Labs - IPFire Eco Appliance"
653 def _appliance_test_fountainnetworks_prime(self
):
654 if not self
.system
== ("SECO", "0949"):
657 # Must have a wireless device
658 if self
.count_device("usb", "148f", "5572") < 1:
663 def _appliance_test_lightningwirelabs_eco(self
):
664 if not self
.system
== ("MSI", "MS-9877"):
667 # Must have four Intel network adapters
668 network_adapters_count
= self
.count_device("pci", "8086", "10d3")
669 if not network_adapters_count
== 4:
673 if not self
.memory
>= 4230823936 * 0.95:
681 def processor_id(self
):
682 if hasattr(self
, "_processor"):
683 return self
._processor
.id
685 if not hasattr(self
, "_processor_id"):
686 res
= self
.db
.get("SELECT processor_id FROM fireinfo_profiles_processors \
687 WHERE profile_id = %s", self
.id)
690 self
._processor
_id
= res
.processor_id
692 self
._processor
_id
= None
694 return self
._processor
_id
696 def get_processor(self
):
697 if not self
.processor_id
:
700 if not hasattr(self
, "_processor"):
701 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_processors \
702 WHERE profile_id = %s", self
.id)
705 self
._processor
= self
.fireinfo
.get_processor_by_id(res
.processor_id
,
706 clock_speed
=res
.clock_speed
, bogomips
=res
.bogomips
)
708 self
._processor
= None
710 return self
._processor
712 def set_processor(self
, processor
):
713 self
.db
.execute("DELETE FROM fireinfo_profiles_processors \
714 WHERE profile_id = %s", self
.id)
717 self
.db
.execute("INSERT INTO fireinfo_profiles_processors(profile_id, processor_id) \
718 VALUES(%s, %s)", self
.id, processor
.id)
720 self
._processor
= processor
722 processor
= property(get_processor
, set_processor
)
724 def set_processor_speeds(self
, clock_speed
, bogomips
):
725 self
.db
.execute("UPDATE fireinfo_profiles_processors \
726 SET clock_speed = %s, bogomips = %s WHERE profile_id = %s",
727 clock_speed
, bogomips
, self
.id)
732 return self
.processor
736 def get_memory(self
):
737 if not hasattr(self
, "_memory"):
738 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_memory \
739 WHERE profile_id = %s", self
.id)
742 self
._memory
= res
.amount
* 1024
748 def set_memory(self
, amount
):
749 if self
.memory
== amount
:
754 self
.db
.execute("DELETE FROM fireinfo_profiles_memory WHERE profile_id = %s", self
.id)
756 self
.db
.execute("INSERT INTO fireinfo_profiles_memory(profile_id, amount) \
757 VALUES(%s, %s)", self
.id, amount
)
759 self
._memory
= amount
* 1024
761 memory
= property(get_memory
, set_memory
)
764 def friendly_memory(self
):
765 return util
.format_size(self
.memory
)
769 def get_storage(self
):
770 if not hasattr(self
, "_storage"):
771 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_storage \
772 WHERE profile_id = %s", self
.id)
775 self
._storage
= res
.amount
* 1024
781 def set_storage(self
, amount
):
782 if self
.storage
== amount
:
787 self
.db
.execute("DELETE FROM fireinfo_profiles_storage WHERE profile_id = %s", self
.id)
789 self
.db
.execute("INSERT INTO fireinfo_profiles_storage(profile_id, amount) \
790 VALUES(%s, %s)", self
.id, amount
)
792 self
._storage
= amount
* 1024
794 storage
= property(get_storage
, set_storage
)
797 def friendly_storage(self
):
798 return util
.format_size(self
.storage
)
802 def get_kernel_id(self
):
803 if not hasattr(self
, "_kernel_id"):
804 res
= self
.db
.get("SELECT fireinfo_profiles_kernels.kernel_id AS id FROM fireinfo_profiles \
805 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
806 WHERE fireinfo_profiles.id = %s", self
.id)
809 self
._kernel
_id
= res
.id
811 self
._kernel
_id
= None
813 return self
._kernel
_id
815 def set_kernel_id(self
, kernel_id
):
816 if self
.kernel_id
== kernel_id
:
819 self
.db
.execute("DELETE FROM fireinfo_profiles_kernels WHERE profile_id = %s", self
.id)
821 self
.db
.execute("INSERT INTO fireinfo_profiles_kernels(profile_id, kernel_id) \
822 VALUES(%s, %s)", self
.id, kernel_id
)
824 self
._kernel
_id
= kernel_id
825 if hasattr(self
, "_kernel"):
828 kernel_id
= property(get_kernel_id
, set_kernel_id
)
832 if not hasattr(self
, "_kernel"):
833 res
= self
.db
.get("SELECT fireinfo_kernels.name AS name FROM fireinfo_profiles \
834 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
835 LEFT JOIN fireinfo_kernels ON fireinfo_kernels.id = fireinfo_profiles_kernels.kernel_id \
836 WHERE fireinfo_profiles.id = %s", self
.id)
839 self
._kernel
= res
.name
847 def get_arch_id(self
):
848 if not hasattr(self
, "_arch_id"):
849 res
= self
.db
.get("SELECT fireinfo_profiles_arches.arch_id AS id FROM fireinfo_profiles \
850 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
851 WHERE fireinfo_profiles.id = %s", self
.id)
854 self
._arch
_id
= res
.id
860 def set_arch_id(self
, arch_id
):
861 if self
.arch_id
== arch_id
:
864 self
.db
.execute("DELETE FROM fireinfo_profiles_arches WHERE profile_id = %s", self
.id)
866 self
.db
.execute("INSERT INTO fireinfo_profiles_arches(profile_id, arch_id) \
867 VALUES(%s, %s)", self
.id, arch_id
)
870 if hasattr(self
, "_arch"):
873 arch_id
= property(get_arch_id
, set_arch_id
)
877 if not hasattr(self
, "_arch"):
878 res
= self
.db
.get("SELECT fireinfo_arches.name AS name FROM fireinfo_profiles \
879 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
880 LEFT JOIN fireinfo_arches ON fireinfo_arches.id = fireinfo_profiles_arches.arch_id \
881 WHERE fireinfo_profiles.id = %s", self
.id)
884 self
._arch
= res
.name
892 def get_release_id(self
):
893 if not hasattr(self
, "_release_id"):
894 res
= self
.db
.get("SELECT fireinfo_profiles_releases.release_id AS id FROM fireinfo_profiles \
895 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
896 WHERE fireinfo_profiles.id = %s", self
.id)
899 self
._release
_id
= res
.id
901 self
._release
_id
= None
903 return self
._release
_id
905 def set_release_id(self
, release_id
):
906 if self
.release_id
== release_id
:
909 self
.db
.execute("DELETE FROM fireinfo_profiles_releases WHERE profile_id = %s", self
.id)
911 self
.db
.execute("INSERT INTO fireinfo_profiles_releases(profile_id, release_id) \
912 VALUES(%s, %s)", self
.id, release_id
)
914 self
._release
_id
= release_id
915 if hasattr(self
, "_release"):
918 release_id
= property(get_release_id
, set_release_id
)
922 if not hasattr(self
, "_release"):
923 res
= self
.db
.get("SELECT fireinfo_releases.name AS name FROM fireinfo_profiles \
924 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
925 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
926 WHERE fireinfo_profiles.id = %s", self
.id)
929 self
._release
= self
._format
_release
(res
.name
)
936 def _format_release(r
):
940 # Remove the development header
941 r
= r
.replace("Development Build: ", "")
944 ("-beta", " - Beta "),
945 ("-rc", " - Release Candidate "),
946 ("rc", "Release Candidate "),
947 ("core", "Core Update "),
960 if not hasattr(self
, "_virtual"):
961 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles_virtual \
962 WHERE profile_id = %s", self
.id)
967 self
._virtual
= False
971 def get_hypervisor_id(self
):
972 if not hasattr(self
, "_hypervisor_id"):
973 res
= self
.db
.get("SELECT fireinfo_profiles_virtual.hypervisor_id AS id FROM fireinfo_profiles \
974 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
975 WHERE fireinfo_profiles.id = %s", self
.id)
978 self
._hypervisor
_id
= res
.id
980 self
._hypervisor
_id
= None
982 return self
._hypervisor
_id
984 def set_hypervisor_id(self
, hypervisor_id
):
985 self
.db
.execute("DELETE FROM fireinfo_profiles_virtual WHERE profile_id = %s", self
.id)
986 self
.db
.execute("INSERT INTO fireinfo_profiles_virtual(profile_id, hypervisor_id) \
987 VALUES(%s, %s)", self
.id, hypervisor_id
)
989 self
._hypervisor
_id
= hypervisor_id
991 hypervisor_id
= property(get_hypervisor_id
, set_hypervisor_id
)
994 def hypervisor(self
):
995 if not hasattr(self
, "_hypervisor"):
996 res
= self
.db
.get("SELECT fireinfo_hypervisors.name AS hypervisor FROM fireinfo_profiles \
997 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
998 LEFT JOIN fireinfo_hypervisors ON fireinfo_profiles_virtual.hypervisor_id = fireinfo_hypervisors.id \
999 WHERE fireinfo_profiles.id = %s", self
.id)
1002 self
._hypervisor
= res
.hypervisor
1004 self
._hypervisor
= None
1006 return self
._hypervisor
1010 def get_language(self
):
1011 if not hasattr(self
, "_language"):
1012 res
= self
.db
.get("SELECT language FROM fireinfo_profiles_languages \
1013 WHERE profile_id = %s", self
.id)
1016 self
._language
= res
.language
1018 self
._language
= None
1020 return self
._language
1022 def set_language(self
, language
):
1023 self
.db
.execute("DELETE FROM fireinfo_profiles_languages WHERE profile_id = %s", self
.id)
1026 self
.db
.execute("INSERT INTO fireinfo_profiles_languages(profile_id, language) \
1027 VALUES(%s, %s)", self
.id, language
)
1029 self
._language
= language
1031 language
= property(get_language
, set_language
)
1035 def get_network(self
):
1036 if not hasattr(self
, "_network"):
1037 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_networks \
1038 WHERE profile_id = %s", self
.id)
1043 self
._network
= ProfileNetwork(res
)
1045 return self
._network
1047 def set_network(self
, network
):
1048 self
.db
.execute("DELETE FROM fireinfo_profiles_networks WHERE profile_id = %s", self
.id)
1051 self
.db
.execute("INSERT INTO fireinfo_profiles_networks(profile_id, \
1052 has_red, has_green, has_orange, has_blue) VALUES(%s, %s, %s, %s, %s)",
1053 self
.id, network
.has_red
, network
.has_green
, network
.has_orange
, network
.has_blue
)
1055 self
._network
= network
1057 network
= property(get_network
, set_network
)
1060 class ProfileData(Object
):
1061 def __init__(self
, backend
, id, data
=None, profile
=None):
1062 Object
.__init
__(self
, backend
)
1066 self
._profile
= profile
1070 if self
._data
is None:
1071 self
._data
= self
.db
.get("SELECT * FROM fireinfo_profile_data \
1072 WHERE id = %s", self
.id)
1078 if not self
._profile
:
1079 self
._profile
= self
.fireinfo
.get_profile_by_id(self
.profile_id
)
1081 return self
._profile
1084 def profile_id(self
):
1085 return self
.data
.profile_id
1088 class ProfileParserError(Exception):
1092 class ProfileParser(Object
):
1103 __processor_args
= (
1113 __processor_args_mandatory
= (
1118 def __init__(self
, backend
, public_id
, blob
=None):
1119 Object
.__init
__(self
, backend
)
1121 self
.public_id
= public_id
1122 self
.private_id
= None
1124 self
.processor
= None
1125 self
.processor_clock_speed
= None
1126 self
.processor_bogomips
= None
1127 self
.system_id
= None
1131 self
.kernel_id
= None
1135 self
.release_id
= None
1136 self
.language
= None
1138 self
.hypervisor_id
= None
1141 self
.__parse
_blob
(blob
)
1143 def equals(self
, other
):
1144 if not self
.processor_id
== other
.processor_id
:
1147 if not self
.device_ids
== other
.device_ids
:
1150 if not self
.system_id
== other
.system_id
:
1153 if not self
.memory
== other
.memory
:
1156 if not self
.storage
== other
.storage
:
1159 if not self
.kernel_id
== other
.kernel_id
:
1162 if not self
.arch_id
== other
.arch_id
:
1165 if not self
.release_id
== other
.release_id
:
1168 if not self
.language
== other
.language
:
1171 if not self
.virtual
== other
.virtual
:
1175 if not self
.hypervisor_id
== other
.hypervisor_id
:
1178 if not self
.network
== other
.network
:
1183 def __parse_blob(self
, blob
):
1184 _profile
= blob
.get("profile", {})
1185 self
.private_id
= blob
.get("private_id")
1187 # Do not try to parse an empty profile
1192 _processor
= _profile
.get("cpu", {})
1193 self
.__parse
_processor
(_processor
)
1196 _devices
= _profile
.get("devices", [])
1197 self
.__parse
_devices
(_devices
)
1200 _system
= _profile
.get("system")
1202 self
.__parse
_system
(_system
)
1204 # Memory (convert to bytes)
1205 memory
= _system
.get("memory", None)
1207 self
.memory
= memory
* 1024
1209 # Storage size (convert to bytes)
1210 storage
= _system
.get("root_size", None)
1212 self
.storage
= storage
* 1024
1215 kernel
= _system
.get("kernel_release", None)
1217 self
.__parse
_kernel
(kernel
)
1220 release
= _system
.get("release", None)
1222 self
.__parse
_release
(release
)
1225 language
= _system
.get("language", None)
1227 self
.__parse
_language
(language
)
1230 self
.virtual
= _system
.get("virtual", False)
1232 hypervisor
= _profile
.get("hypervisor")
1233 self
.__parse
_hypervisor
(hypervisor
)
1236 _network
= _profile
.get("network")
1238 self
.__parse
_network
(_network
)
1241 def device_ids(self
):
1242 return sorted([d
.id for d
in self
.devices
])
1244 def __parse_devices(self
, _devices
):
1247 for _device
in _devices
:
1249 for arg
in self
.__device
_args
:
1250 args
[arg
] = _device
.get(arg
, None)
1252 # Find the device or create a new one.
1253 device
= self
.fireinfo
.get_device(**args
)
1255 device
= self
.fireinfo
.create_device(**args
)
1257 self
.devices
.append(device
)
1259 def __parse_system(self
, system
):
1260 vendor
= system
.get("vendor", None)
1264 model
= system
.get("model", None)
1268 self
.system_id
= self
.fireinfo
.get_system(vendor
, model
)
1269 if not self
.system_id
:
1270 self
.system_id
= self
.fireinfo
.create_system(vendor
, model
)
1273 def processor_id(self
):
1274 if not self
.processor
:
1277 return self
.processor
.id
1279 def __parse_processor(self
, _processor
):
1281 for arg
in self
.__processor
_args
:
1282 if arg
== "core_count":
1287 args
[arg
] = _processor
.get(_arg
, None)
1289 for arg
in self
.__processor
_args
_mandatory
:
1290 if not args
.get(arg
, None):
1291 raise ProfileParserError
, "Mandatory argument missing: %s" % arg
1293 self
.processor
= self
.fireinfo
.get_processor(**args
)
1294 if not self
.processor
:
1295 self
.processor
= self
.fireinfo
.create_processor(**args
)
1297 self
.processor_clock_speed
= _processor
.get("speed", None)
1298 self
.processor_bogomips
= _processor
.get("bogomips", None)
1300 arch
= _processor
.get("arch", None)
1302 self
.__parse
_arch
(arch
)
1304 def __parse_kernel(self
, kernel
):
1305 self
.kernel_id
= self
.fireinfo
.get_kernel(kernel
)
1306 if not self
.kernel_id
:
1307 self
.kernel_id
= self
.fireinfo
.create_kernel(kernel
)
1308 assert self
.kernel_id
1310 self
.kernel
= kernel
1312 def __parse_arch(self
, arch
):
1313 self
.arch_id
= self
.fireinfo
.get_arch(arch
)
1314 if not self
.arch_id
:
1315 self
.arch_id
= self
.fireinfo
.create_arch(arch
)
1319 def __parse_release(self
, release
):
1320 # Remove the arch bit
1322 r
= [e
for e
in release
.split() if e
]
1323 for s
in ("(i586)", "(armv5tel)"):
1330 release
= " ".join(r
)
1332 self
.release_id
= self
.fireinfo
.get_release(release
)
1333 if not self
.release_id
:
1334 self
.release_id
= self
.fireinfo
.create_release(release
)
1335 assert self
.release_id
1337 self
.release
= release
1339 def __parse_language(self
, language
):
1340 self
.language
, delim
, rest
= language
.partition(".")
1341 self
.language
, delim
, rest
= language
.partition("_")
1343 def __parse_hypervisor(self
, hypervisor
):
1344 vendor
= hypervisor
.get("vendor", "other")
1346 if vendor
in ("other", "unknown"):
1347 self
.hypervisor_id
= None
1350 self
.hypervisor_id
= self
.fireinfo
.get_hypervisor(vendor
)
1351 if not self
.hypervisor_id
:
1352 self
.hypervisor_id
= self
.fireinfo
.create_hypervisor(vendor
)
1354 def __parse_network(self
, network
):
1355 self
.network
= ProfileNetwork({
1356 "has_red" : network
.get("red", False),
1357 "has_green" : network
.get("green", False),
1358 "has_orange" : network
.get("orange", False),
1359 "has_blue" : network
.get("blue", False),
1363 class Fireinfo(Object
):
1364 def get_profile_count(self
, when
=None):
1365 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1366 WHERE then_or_now(%s) BETWEEN time_created AND time_valid", when
)
1371 def get_total_updates_count(self
, when
=None):
1372 res
= self
.db
.get("SELECT COUNT(*) + SUM(updates) AS count \
1373 FROM fireinfo_profiles WHERE time_created <= then_or_now(%s)", when
)
1380 def parse_profile(self
, public_id
, blob
):
1381 return ProfileParser(self
.backend
, public_id
, blob
)
1385 def profile_exists(self
, public_id
):
1386 res
= self
.db
.get("SELECT id FROM fireinfo_profiles \
1387 WHERE public_id = %s LIMIT 1", public_id
)
1394 def can_update_profile(self
, public_id
, private_id
, when
=None):
1395 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles \
1396 WHERE public_id = %s AND private_id = %s \
1397 AND time_updated + INTERVAL '60 minutes' <= then_or_now(%s) \
1398 AND time_valid >= then_or_now(%s) ORDER BY time_updated DESC LIMIT 1",
1399 public_id
, private_id
, when
, when
)
1406 def get_profile(self
, public_id
, private_id
=None, when
=None):
1407 res
= self
.db
.get("SELECT * FROM fireinfo_profiles \
1408 WHERE public_id = %s AND \
1409 (CASE WHEN %s IS NULL THEN TRUE ELSE private_id = %s END) AND \
1410 (CASE WHEN %s IS NULL THEN TRUE ELSE \
1411 then_or_now(%s) BETWEEN time_created AND time_valid END) \
1412 ORDER BY time_updated DESC LIMIT 1", public_id
,
1413 private_id
, private_id
, when
, when
)
1416 return Profile(self
.backend
, res
.id, res
)
1418 def get_profiles(self
, public_id
):
1419 res
= self
.db
.query("SELECT * FROM fireinfo_profiles \
1420 WHERE public_id = %s ORDER BY time_created DESC", public_id
)
1424 profile
= Profile(self
.backend
, row
.id, row
)
1425 profiles
.append(profile
)
1429 def create_profile(self
, public_id
, private_id
, when
=None):
1430 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
1432 res
= self
.db
.get("INSERT INTO fireinfo_profiles(public_id, private_id, \
1433 time_created, time_updated, time_valid) VALUES(%s, %s, then_or_now(%s), \
1434 then_or_now(%s), then_or_now(%s) + INTERVAL '%s days') RETURNING id",
1435 public_id
, private_id
, when
, when
, when
, valid
)
1438 return Profile(self
.backend
, res
.id)
1442 def create_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1443 driver
=None, deviceclass
=None):
1444 res
= self
.db
.get("INSERT INTO fireinfo_devices(subsystem, vendor, model, \
1445 sub_vendor, sub_model, driver, deviceclass) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1446 RETURNING id", subsystem
, vendor
, model
, sub_vendor
, sub_model
, driver
, deviceclass
)
1449 return Device(self
.backend
, res
.id)
1451 def get_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1452 driver
=None, deviceclass
=None):
1453 res
= self
.db
.get("SELECT * FROM fireinfo_devices \
1454 WHERE subsystem = %s AND vendor = %s AND model = %s \
1455 AND sub_vendor IS NOT DISTINCT FROM %s \
1456 AND sub_model IS NOT DISTINCT FROM %s \
1457 AND driver IS NOT DISTINCT FROM %s \
1458 AND deviceclass IS NOT DISTINCT FROM %s \
1459 LIMIT 1", subsystem
, vendor
, model
, sub_vendor
,
1460 sub_model
, driver
, deviceclass
)
1463 return Device(self
.backend
, res
.id, res
)
1467 def create_system(self
, vendor
, model
):
1468 res
= self
.db
.get("INSERT INTO fireinfo_systems(vendor, model) \
1469 VALUES(%s, %s) RETURNING id", vendor
, model
)
1474 def get_system(self
, vendor
, model
):
1475 res
= self
.db
.get("SELECT id FROM fireinfo_systems WHERE vendor IS NOT DISTINCT FROM %s \
1476 AND model IS NOT DISTINCT FROM %s LIMIT 1", vendor
, model
)
1483 def create_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1484 res
= self
.db
.get("INSERT INTO fireinfo_processors(vendor, model_string, \
1485 family, model, stepping, core_count, flags) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1486 RETURNING id", vendor
, model_string
, family
, model
, stepping
, core_count
, flags
)
1489 return Processor(self
.backend
, res
.id)
1491 def get_processor_by_id(self
, processor_id
, **kwargs
):
1492 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1493 WHERE id = %s", processor_id
)
1496 return Processor(self
.backend
, res
.id, data
=res
, **kwargs
)
1498 def get_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1502 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1503 WHERE vendor = %s AND model_string = %s \
1504 AND family IS NOT DISTINCT FROM %s AND model IS NOT DISTINCT FROM %s \
1505 AND stepping IS NOT DISTINCT FROM %s AND core_count = %s \
1506 AND flags <@ %s AND flags @> %s", vendor
, model_string
, family
, model
,
1507 stepping
, core_count
, flags
, flags
)
1510 return Processor(self
.backend
, res
.id, res
)
1514 def create_kernel(self
, kernel
):
1515 res
= self
.db
.get("INSERT INTO fireinfo_kernels(name) VALUES(%s) \
1516 RETURNING id", kernel
)
1521 def get_kernel(self
, kernel
):
1522 res
= self
.db
.get("SELECT id FROM fireinfo_kernels WHERE name = %s", kernel
)
1529 def create_arch(self
, arch
):
1530 res
= self
.db
.get("INSERT INTO fireinfo_arches(name) VALUES(%s) \
1531 RETURNING id", arch
)
1536 def get_arch(self
, arch
):
1537 res
= self
.db
.get("SELECT id FROM fireinfo_arches WHERE name = %s", arch
)
1544 def create_release(self
, release
):
1545 res
= self
.db
.get("INSERT INTO fireinfo_releases(name) VALUES(%s) \
1546 RETURNING id", release
)
1551 def get_release(self
, release
):
1552 res
= self
.db
.get("SELECT id FROM fireinfo_releases WHERE name = %s", release
)
1559 def create_hypervisor(self
, hypervisor
):
1560 res
= self
.db
.get("INSERT INTO fireinfo_hypervisors(name) VALUES(%s) \
1561 RETURNING id", hypervisor
)
1566 def get_hypervisor(self
, hypervisor
):
1567 res
= self
.db
.get("SELECT id FROM fireinfo_hypervisors WHERE name = %s",
1575 def handle_profile(self
, *args
, **kwargs
):
1576 self
.db
.execute("START TRANSACTION")
1578 # Wrap all the handling of the profile in a huge transaction.
1580 self
._handle
_profile
(*args
, **kwargs
)
1583 self
.db
.execute("ROLLBACK")
1587 self
.db
.execute("COMMIT")
1589 def _handle_profile(self
, public_id
, profile_blob
, location
=None, when
=None):
1590 private_id
= profile_blob
.get("private_id", None)
1593 # Check if the profile already exists in the database.
1594 profile
= self
.fireinfo
.get_profile(public_id
, private_id
=private_id
, when
=when
)
1596 # Check if the update can actually be updated
1597 if profile
and not self
.fireinfo
.can_update_profile(public_id
, private_id
, when
=when
):
1598 logging
.warning("Profile not updated because private ID does not"
1599 " match or last update is too recent: %s" % public_id
)
1603 profile_parser
= self
.parse_profile(public_id
, profile_blob
)
1605 # If a profile exists, check if it matches and if so, just update the
1608 # Check if the profile has changed. If so, update the data.
1609 if profile_parser
.equals(profile
):
1610 profile
.updated(profile_parser
, location
=location
, when
=when
)
1613 # If it does not match, we assume that it is expired and
1614 # create a new profile.
1615 profile
.expired(when
=when
)
1617 # Replace the old profile with a new one
1618 profile
= self
.fireinfo
.create_profile(public_id
, private_id
, when
=when
)
1619 profile
.parse(profile_parser
)
1622 profile
.set_location(location
)
1628 def get_random_profile(self
, when
=None):
1629 # Check if the architecture exists so that we pick a profile with some data
1630 res
= self
.db
.get("SELECT public_id FROM fireinfo_profiles \
1631 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
1632 WHERE fireinfo_profiles_arches.profile_id IS NOT NULL \
1633 AND then_or_now(%s) BETWEEN time_created AND time_valid ORDER BY RANDOM() LIMIT 1", when
)
1636 return res
.public_id
1638 def get_active_profiles(self
, when
=None):
1639 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1640 SELECT COUNT(*) AS with_data, (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1641 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1642 WHERE fireinfo_profiles_releases.profile_id IS NOT NULL", when
)
1645 return res
.with_data
, res
.count
1647 def get_archive_size(self
, when
=None):
1648 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1649 WHERE time_created <= then_or_now(%s)", when
)
1654 def get_geo_location_map(self
, when
=None):
1655 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1656 SELECT location, COUNT(location)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1657 LEFT JOIN fireinfo_profiles_locations ON profiles.id = fireinfo_profiles_locations.profile_id \
1658 WHERE fireinfo_profiles_locations.location IS NOT NULL GROUP BY location ORDER BY count DESC", when
)
1660 return ((r
.location
, r
.count
) for r
in res
)
1662 def get_language_map(self
, when
=None):
1663 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1664 SELECT language, COUNT(language)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1665 LEFT JOIN fireinfo_profiles_languages ON profiles.id = fireinfo_profiles_languages.profile_id \
1666 WHERE fireinfo_profiles_languages.language IS NOT NULL GROUP BY language ORDER BY count DESC", when
)
1668 return ((r
.language
, r
.count
) for r
in res
)
1671 def cpu_vendors(self
):
1672 res
= self
.db
.query("SELECT DISTINCT vendor FROM fireinfo_processors ORDER BY vendor")
1674 return (CPU_VENDORS
.get(r
.vendor
, r
.vendor
) for r
in res
)
1676 def get_cpu_vendors_map(self
, when
=None):
1677 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1678 SELECT vendor, COUNT(vendor)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1679 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1680 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1681 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL GROUP BY vendor ORDER BY count DESC", when
)
1683 return ((CPU_VENDORS
.get(r
.vendor
, r
.vendor
), r
.count
) for r
in res
)
1685 def get_cpu_clock_speeds(self
, when
=None):
1686 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1687 SELECT AVG(fireinfo_profiles_processors.clock_speed) AS avg, \
1688 STDDEV(fireinfo_profiles_processors.clock_speed) AS stddev, \
1689 MIN(fireinfo_profiles_processors.clock_speed) AS min, \
1690 MAX(fireinfo_profiles_processors.clock_speed) AS max FROM profiles \
1691 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1692 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1693 AND fireinfo_profiles_processors.clock_speed > 0", when
)
1696 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1698 def get_cpus_with_platform_and_flag(self
, platform
, flag
, when
=None):
1699 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1700 processors AS (SELECT fireinfo_processors.id AS id, fireinfo_processors.flags AS flags FROM profiles \
1701 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1702 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1703 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1704 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1705 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1706 AND fireinfo_arches.platform = %s AND NOT 'hypervisor' = ANY(fireinfo_processors.flags)) \
1707 SELECT (COUNT(*)::float / (SELECT NULLIF(COUNT(*), 0) FROM processors)) AS count FROM processors \
1708 WHERE %s = ANY(processors.flags)", when
, platform
, flag
)
1710 return res
.count
or 0
1712 def get_common_cpu_flags_by_platform(self
, platform
, when
=None):
1713 if platform
== "arm":
1715 "lpae", "neon", "thumb", "thumb2", "thumbee", "vfpv3", "vfpv4",
1717 elif platform
== "x86":
1719 "aes", "avx", "avx2", "lm", "mmx", "mmxext", "nx", "pae",
1720 "pni", "popcnt", "sse", "sse2", "rdrand", "ssse3", "sse4a",
1728 ret
.append((flag
, self
.get_cpus_with_platform_and_flag(platform
, flag
, when
=when
)))
1730 # Add virtual CPU flag "virt" for virtualization support
1731 if platform
== "x86":
1733 self
.get_cpus_with_platform_and_flag(platform
, "vmx", when
=when
) + \
1734 self
.get_cpus_with_platform_and_flag(platform
, "svm", when
=when
)))
1736 return sorted(ret
, key
=lambda x
: x
[1], reverse
=True)
1738 def get_common_memory_amounts(self
, when
=None):
1739 amounts
= (128, 256, 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024)
1742 for amount
in amounts
:
1743 ret
.append((amount
, self
.get_memory_amount(amount
* 1024 * 0.95, when
=when
)))
1747 def get_memory_amount(self
, greather_than
, when
=None):
1748 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1749 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles \
1750 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1751 WHERE NOT fireinfo_profiles_memory.amount IS NULL) AS percentage FROM profiles \
1752 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1753 WHERE fireinfo_profiles_memory.amount >= %s", when
, greather_than
)
1756 return res
.percentage
1758 def get_memory_amounts(self
, when
=None):
1759 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1760 SELECT AVG(fireinfo_profiles_memory.amount) AS avg, \
1761 STDDEV(fireinfo_profiles_memory.amount) AS stddev, \
1762 MIN(fireinfo_profiles_memory.amount) AS min, \
1763 MAX(fireinfo_profiles_memory.amount) AS max FROM profiles \
1764 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id", when
)
1767 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1769 def get_arch_map(self
, when
=None):
1770 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1771 SELECT fireinfo_arches.name AS arch, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count \
1773 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1774 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1775 WHERE NOT fireinfo_profiles_arches.profile_id IS NULL \
1776 GROUP BY fireinfo_arches.id ORDER BY count DESC", when
)
1778 return ((r
.arch
, r
.count
) for r
in res
)
1782 def get_hypervisor_map(self
, when
=None):
1783 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1784 virtual_profiles AS (SELECT profiles.id AS profile_id, fireinfo_profiles_virtual.hypervisor_id FROM profiles \
1785 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1786 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL) \
1787 SELECT COALESCE(fireinfo_hypervisors.name, %s) AS name, \
1788 COUNT(*)::float / (SELECT COUNT(*) FROM virtual_profiles) AS count FROM virtual_profiles \
1789 LEFT JOIN fireinfo_hypervisors ON virtual_profiles.hypervisor_id = fireinfo_hypervisors.id \
1790 GROUP BY fireinfo_hypervisors.name ORDER BY count DESC", when
, "unknown")
1792 return ((r
.name
, r
.count
) for r
in res
)
1794 def get_virtual_ratio(self
, when
=None):
1795 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1796 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1797 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1798 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL", when
)
1805 def get_releases_map(self
, when
=None):
1806 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1807 SELECT fireinfo_releases.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1808 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1809 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
1810 GROUP BY fireinfo_releases.name ORDER BY count DESC", when
)
1812 return ((r
.name
, r
.count
) for r
in res
)
1814 def get_kernels_map(self
, when
=None):
1815 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1816 SELECT fireinfo_kernels.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1817 LEFT JOIN fireinfo_profiles_kernels ON profiles.id = fireinfo_profiles_kernels.profile_id \
1818 LEFT JOIN fireinfo_kernels ON fireinfo_profiles_kernels.kernel_id = fireinfo_kernels.id \
1819 GROUP BY fireinfo_kernels.name ORDER BY count DESC", when
)
1821 return ((r
.name
, r
.count
) for r
in res
)
1823 def _process_devices(self
, devices
):
1827 dev
= Device(self
.backend
, dev
.get("id", None), dev
)
1832 def get_driver_map(self
, driver
, when
=None):
1833 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1834 devices AS (SELECT * FROM profiles \
1835 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1836 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1837 WHERE driver = %s) \
1838 SELECT subsystem, model, vendor, driver, deviceclass, \
1839 COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1840 GROUP BY subsystem, model, vendor, driver, deviceclass \
1841 ORDER BY percentage DESC", when
, driver
)
1843 return self
._process
_devices
(res
)
1846 "pci" : hwdata
.PCI(),
1847 "usb" : hwdata
.USB(),
1850 def get_vendor_string(self
, subsystem
, vendor_id
):
1852 cls
= self
.subsystem2class
[subsystem
]
1856 return cls
.get_vendor(vendor_id
)
1858 def get_model_string(self
, subsystem
, vendor_id
, model_id
):
1860 cls
= self
.subsystem2class
[subsystem
]
1864 return cls
.get_device(vendor_id
, model_id
)
1866 def get_vendor_list(self
, when
=None):
1867 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1868 SELECT DISTINCT fireinfo_devices.subsystem AS subsystem, fireinfo_devices.vendor AS vendor FROM profiles \
1869 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1870 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1871 WHERE NOT fireinfo_devices.driver = ANY(%s)", when
, IGNORED_DEVICES
)
1875 vendor
= self
.get_vendor_string(row
.subsystem
, row
.vendor
)
1877 # Drop if vendor could not be determined
1882 vendors
[vendor
].append((row
.subsystem
, row
.vendor
))
1884 vendors
[vendor
] = [(row
.subsystem
, row
.vendor
)]
1886 vendors
= vendors
.items()
1887 return sorted(vendors
)
1889 def get_devices_by_vendor(self
, subsystem
, vendor
, when
=None):
1890 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1891 devices AS (SELECT * FROM profiles \
1892 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1893 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1894 WHERE NOT fireinfo_devices.driver = ANY(%s)), \
1895 vendor_devices AS (SELECT * FROM devices WHERE devices.subsystem = %s AND devices.vendor = %s) \
1896 SELECT subsystem, model, vendor, driver, deviceclass FROM vendor_devices \
1897 GROUP BY subsystem, model, vendor, driver, deviceclass", when
, IGNORED_DEVICES
, subsystem
, vendor
)
1899 return self
._process
_devices
(res
)
1901 def get_device_percentage(self
, subsystem
, vendor
, model
, when
=None):
1902 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1903 devices AS (SELECT * FROM profiles \
1904 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1905 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id) \
1906 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1907 WHERE devices.subsystem = %s AND devices.vendor = %s AND devices.model = %s",
1908 when
, subsystem
, vendor
, model
)
1911 return res
.percentage
1913 def get_device_in_profile(self
, subsystem
, vendor
, model
, limit
=10, when
=None):
1914 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1915 profiles_with_device AS (SELECT DISTINCT fireinfo_profiles.public_id FROM profiles \
1916 LEFT JOIN fireinfo_profiles ON profiles.id = fireinfo_profiles.id \
1917 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1918 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1919 WHERE fireinfo_devices.subsystem = %s AND fireinfo_devices.vendor = %s \
1920 AND fireinfo_devices.model = %s) \
1921 SELECT * FROM profiles_with_device ORDER BY RANDOM() LIMIT %s",
1922 when
, subsystem
, vendor
, model
, limit
)
1924 return (r
.public_id
for r
in res
)
1926 def get_network_zones_map(self
, when
=None):
1927 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1928 SELECT COUNT(NULLIF(has_red, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_red, \
1929 COUNT(NULLIF(has_green, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_green, \
1930 COUNT(NULLIF(has_orange, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_orange, \
1931 COUNT(NULLIF(has_blue, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_blue FROM profiles \
1932 LEFT JOIN fireinfo_profiles_networks ON profiles.id = fireinfo_profiles_networks.profile_id \
1933 WHERE fireinfo_profiles_networks.profile_id IS NOT NULL", when
)