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
, vendor
, model
):
563 for dev
in self
.devices
:
564 if 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 ("lightningwirelabs-eco", self
._appliance
_test
_lightningwirelabs
_eco
),
634 self
._appliance
_id
= None
635 for name
, test_func
in appliances
:
639 self
._appliance
_id
= name
642 return self
._appliance
_id
646 if self
.appliance_id
== "lightningwirelabs-eco":
647 return "Lightning Wire Labs IPFire Eco Appliance"
649 def _appliance_test_lightningwirelabs_eco(self
):
650 if not self
.system
== ("MSI", "MS-9877"):
653 # Must have four Intel network adapters
654 network_adapters_count
= self
.count_device("8086", "10d3")
655 if not network_adapters_count
== 4:
659 if not self
.memory
>= 4230823936 * 0.95:
667 def processor_id(self
):
668 if hasattr(self
, "_processor"):
669 return self
._processor
.id
671 if not hasattr(self
, "_processor_id"):
672 res
= self
.db
.get("SELECT processor_id FROM fireinfo_profiles_processors \
673 WHERE profile_id = %s", self
.id)
676 self
._processor
_id
= res
.processor_id
678 self
._processor
_id
= None
680 return self
._processor
_id
682 def get_processor(self
):
683 if not self
.processor_id
:
686 if not hasattr(self
, "_processor"):
687 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_processors \
688 WHERE profile_id = %s", self
.id)
691 self
._processor
= self
.fireinfo
.get_processor_by_id(res
.processor_id
,
692 clock_speed
=res
.clock_speed
, bogomips
=res
.bogomips
)
694 self
._processor
= None
696 return self
._processor
698 def set_processor(self
, processor
):
699 self
.db
.execute("DELETE FROM fireinfo_profiles_processors \
700 WHERE profile_id = %s", self
.id)
703 self
.db
.execute("INSERT INTO fireinfo_profiles_processors(profile_id, processor_id) \
704 VALUES(%s, %s)", self
.id, processor
.id)
706 self
._processor
= processor
708 processor
= property(get_processor
, set_processor
)
710 def set_processor_speeds(self
, clock_speed
, bogomips
):
711 self
.db
.execute("UPDATE fireinfo_profiles_processors \
712 SET clock_speed = %s, bogomips = %s WHERE profile_id = %s",
713 clock_speed
, bogomips
, self
.id)
718 return self
.processor
722 def get_memory(self
):
723 if not hasattr(self
, "_memory"):
724 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_memory \
725 WHERE profile_id = %s", self
.id)
728 self
._memory
= res
.amount
* 1024
734 def set_memory(self
, amount
):
735 if self
.memory
== amount
:
740 self
.db
.execute("DELETE FROM fireinfo_profiles_memory WHERE profile_id = %s", self
.id)
742 self
.db
.execute("INSERT INTO fireinfo_profiles_memory(profile_id, amount) \
743 VALUES(%s, %s)", self
.id, amount
)
745 self
._memory
= amount
* 1024
747 memory
= property(get_memory
, set_memory
)
750 def friendly_memory(self
):
751 return util
.format_size(self
.memory
)
755 def get_storage(self
):
756 if not hasattr(self
, "_storage"):
757 res
= self
.db
.get("SELECT amount FROM fireinfo_profiles_storage \
758 WHERE profile_id = %s", self
.id)
761 self
._storage
= res
.amount
* 1024
767 def set_storage(self
, amount
):
768 if self
.storage
== amount
:
773 self
.db
.execute("DELETE FROM fireinfo_profiles_storage WHERE profile_id = %s", self
.id)
775 self
.db
.execute("INSERT INTO fireinfo_profiles_storage(profile_id, amount) \
776 VALUES(%s, %s)", self
.id, amount
)
778 self
._storage
= amount
* 1024
780 storage
= property(get_storage
, set_storage
)
783 def friendly_storage(self
):
784 return util
.format_size(self
.storage
)
788 def get_kernel_id(self
):
789 if not hasattr(self
, "_kernel_id"):
790 res
= self
.db
.get("SELECT fireinfo_profiles_kernels.kernel_id AS id FROM fireinfo_profiles \
791 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
792 WHERE fireinfo_profiles.id = %s", self
.id)
795 self
._kernel
_id
= res
.id
797 self
._kernel
_id
= None
799 return self
._kernel
_id
801 def set_kernel_id(self
, kernel_id
):
802 if self
.kernel_id
== kernel_id
:
805 self
.db
.execute("DELETE FROM fireinfo_profiles_kernels WHERE profile_id = %s", self
.id)
807 self
.db
.execute("INSERT INTO fireinfo_profiles_kernels(profile_id, kernel_id) \
808 VALUES(%s, %s)", self
.id, kernel_id
)
810 self
._kernel
_id
= kernel_id
811 if hasattr(self
, "_kernel"):
814 kernel_id
= property(get_kernel_id
, set_kernel_id
)
818 if not hasattr(self
, "_kernel"):
819 res
= self
.db
.get("SELECT fireinfo_kernels.name AS name FROM fireinfo_profiles \
820 LEFT JOIN fireinfo_profiles_kernels ON fireinfo_profiles.id = fireinfo_profiles_kernels.profile_id \
821 LEFT JOIN fireinfo_kernels ON fireinfo_kernels.id = fireinfo_profiles_kernels.kernel_id \
822 WHERE fireinfo_profiles.id = %s", self
.id)
825 self
._kernel
= res
.name
833 def get_arch_id(self
):
834 if not hasattr(self
, "_arch_id"):
835 res
= self
.db
.get("SELECT fireinfo_profiles_arches.arch_id AS id FROM fireinfo_profiles \
836 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
837 WHERE fireinfo_profiles.id = %s", self
.id)
840 self
._arch
_id
= res
.id
846 def set_arch_id(self
, arch_id
):
847 if self
.arch_id
== arch_id
:
850 self
.db
.execute("DELETE FROM fireinfo_profiles_arches WHERE profile_id = %s", self
.id)
852 self
.db
.execute("INSERT INTO fireinfo_profiles_arches(profile_id, arch_id) \
853 VALUES(%s, %s)", self
.id, arch_id
)
856 if hasattr(self
, "_arch"):
859 arch_id
= property(get_arch_id
, set_arch_id
)
863 if not hasattr(self
, "_arch"):
864 res
= self
.db
.get("SELECT fireinfo_arches.name AS name FROM fireinfo_profiles \
865 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
866 LEFT JOIN fireinfo_arches ON fireinfo_arches.id = fireinfo_profiles_arches.arch_id \
867 WHERE fireinfo_profiles.id = %s", self
.id)
870 self
._arch
= res
.name
878 def get_release_id(self
):
879 if not hasattr(self
, "_release_id"):
880 res
= self
.db
.get("SELECT fireinfo_profiles_releases.release_id AS id FROM fireinfo_profiles \
881 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
882 WHERE fireinfo_profiles.id = %s", self
.id)
885 self
._release
_id
= res
.id
887 self
._release
_id
= None
889 return self
._release
_id
891 def set_release_id(self
, release_id
):
892 if self
.release_id
== release_id
:
895 self
.db
.execute("DELETE FROM fireinfo_profiles_releases WHERE profile_id = %s", self
.id)
897 self
.db
.execute("INSERT INTO fireinfo_profiles_releases(profile_id, release_id) \
898 VALUES(%s, %s)", self
.id, release_id
)
900 self
._release
_id
= release_id
901 if hasattr(self
, "_release"):
904 release_id
= property(get_release_id
, set_release_id
)
908 if not hasattr(self
, "_release"):
909 res
= self
.db
.get("SELECT fireinfo_releases.name AS name FROM fireinfo_profiles \
910 LEFT JOIN fireinfo_profiles_releases ON fireinfo_profiles.id = fireinfo_profiles_releases.profile_id \
911 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
912 WHERE fireinfo_profiles.id = %s", self
.id)
915 self
._release
= self
._format
_release
(res
.name
)
922 def _format_release(r
):
926 # Remove the development header
927 r
= r
.replace("Development Build: ", "")
930 ("-beta", " - Beta "),
931 ("-rc", " - Release Candidate "),
932 ("rc", "Release Candidate "),
933 ("core", "Core Update "),
946 if not hasattr(self
, "_virtual"):
947 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles_virtual \
948 WHERE profile_id = %s", self
.id)
953 self
._virtual
= False
957 def get_hypervisor_id(self
):
958 if not hasattr(self
, "_hypervisor_id"):
959 res
= self
.db
.get("SELECT fireinfo_profiles_virtual.hypervisor_id AS id FROM fireinfo_profiles \
960 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
961 WHERE fireinfo_profiles.id = %s", self
.id)
964 self
._hypervisor
_id
= res
.id
966 self
._hypervisor
_id
= None
968 return self
._hypervisor
_id
970 def set_hypervisor_id(self
, hypervisor_id
):
971 self
.db
.execute("DELETE FROM fireinfo_profiles_virtual WHERE profile_id = %s", self
.id)
972 self
.db
.execute("INSERT INTO fireinfo_profiles_virtual(profile_id, hypervisor_id) \
973 VALUES(%s, %s)", self
.id, hypervisor_id
)
975 self
._hypervisor
_id
= hypervisor_id
977 hypervisor_id
= property(get_hypervisor_id
, set_hypervisor_id
)
980 def hypervisor(self
):
981 if not hasattr(self
, "_hypervisor"):
982 res
= self
.db
.get("SELECT fireinfo_hypervisors.name AS hypervisor FROM fireinfo_profiles \
983 LEFT JOIN fireinfo_profiles_virtual ON fireinfo_profiles.id = fireinfo_profiles_virtual.profile_id \
984 LEFT JOIN fireinfo_hypervisors ON fireinfo_profiles_virtual.hypervisor_id = fireinfo_hypervisors.id \
985 WHERE fireinfo_profiles.id = %s", self
.id)
988 self
._hypervisor
= res
.hypervisor
990 self
._hypervisor
= None
992 return self
._hypervisor
996 def get_language(self
):
997 if not hasattr(self
, "_language"):
998 res
= self
.db
.get("SELECT language FROM fireinfo_profiles_languages \
999 WHERE profile_id = %s", self
.id)
1002 self
._language
= res
.language
1004 self
._language
= None
1006 return self
._language
1008 def set_language(self
, language
):
1009 self
.db
.execute("DELETE FROM fireinfo_profiles_languages WHERE profile_id = %s", self
.id)
1012 self
.db
.execute("INSERT INTO fireinfo_profiles_languages(profile_id, language) \
1013 VALUES(%s, %s)", self
.id, language
)
1015 self
._language
= language
1017 language
= property(get_language
, set_language
)
1021 def get_network(self
):
1022 if not hasattr(self
, "_network"):
1023 res
= self
.db
.get("SELECT * FROM fireinfo_profiles_networks \
1024 WHERE profile_id = %s", self
.id)
1029 self
._network
= ProfileNetwork(res
)
1031 return self
._network
1033 def set_network(self
, network
):
1034 self
.db
.execute("DELETE FROM fireinfo_profiles_networks WHERE profile_id = %s", self
.id)
1037 self
.db
.execute("INSERT INTO fireinfo_profiles_networks(profile_id, \
1038 has_red, has_green, has_orange, has_blue) VALUES(%s, %s, %s, %s, %s)",
1039 self
.id, network
.has_red
, network
.has_green
, network
.has_orange
, network
.has_blue
)
1041 self
._network
= network
1043 network
= property(get_network
, set_network
)
1046 class ProfileData(Object
):
1047 def __init__(self
, backend
, id, data
=None, profile
=None):
1048 Object
.__init
__(self
, backend
)
1052 self
._profile
= profile
1056 if self
._data
is None:
1057 self
._data
= self
.db
.get("SELECT * FROM fireinfo_profile_data \
1058 WHERE id = %s", self
.id)
1064 if not self
._profile
:
1065 self
._profile
= self
.fireinfo
.get_profile_by_id(self
.profile_id
)
1067 return self
._profile
1070 def profile_id(self
):
1071 return self
.data
.profile_id
1074 class ProfileParserError(Exception):
1078 class ProfileParser(Object
):
1089 __processor_args
= (
1099 __processor_args_mandatory
= (
1104 def __init__(self
, backend
, public_id
, blob
=None):
1105 Object
.__init
__(self
, backend
)
1107 self
.public_id
= public_id
1108 self
.private_id
= None
1110 self
.processor
= None
1111 self
.processor_clock_speed
= None
1112 self
.processor_bogomips
= None
1113 self
.system_id
= None
1117 self
.kernel_id
= None
1121 self
.release_id
= None
1122 self
.language
= None
1124 self
.hypervisor_id
= None
1127 self
.__parse
_blob
(blob
)
1129 def equals(self
, other
):
1130 if not self
.processor_id
== other
.processor_id
:
1133 if not self
.device_ids
== other
.device_ids
:
1136 if not self
.system_id
== other
.system_id
:
1139 if not self
.memory
== other
.memory
:
1142 if not self
.storage
== other
.storage
:
1145 if not self
.kernel_id
== other
.kernel_id
:
1148 if not self
.arch_id
== other
.arch_id
:
1151 if not self
.release_id
== other
.release_id
:
1154 if not self
.language
== other
.language
:
1157 if not self
.virtual
== other
.virtual
:
1161 if not self
.hypervisor_id
== other
.hypervisor_id
:
1164 if not self
.network
== other
.network
:
1169 def __parse_blob(self
, blob
):
1170 _profile
= blob
.get("profile", {})
1171 self
.private_id
= blob
.get("private_id")
1173 # Do not try to parse an empty profile
1178 _processor
= _profile
.get("cpu", {})
1179 self
.__parse
_processor
(_processor
)
1182 _devices
= _profile
.get("devices", [])
1183 self
.__parse
_devices
(_devices
)
1186 _system
= _profile
.get("system")
1188 self
.__parse
_system
(_system
)
1190 # Memory (convert to bytes)
1191 memory
= _system
.get("memory", None)
1193 self
.memory
= memory
* 1024
1195 # Storage size (convert to bytes)
1196 storage
= _system
.get("root_size", None)
1198 self
.storage
= storage
* 1024
1201 kernel
= _system
.get("kernel_release", None)
1203 self
.__parse
_kernel
(kernel
)
1206 release
= _system
.get("release", None)
1208 self
.__parse
_release
(release
)
1211 language
= _system
.get("language", None)
1213 self
.__parse
_language
(language
)
1216 self
.virtual
= _system
.get("virtual", False)
1218 hypervisor
= _profile
.get("hypervisor")
1219 self
.__parse
_hypervisor
(hypervisor
)
1222 _network
= _profile
.get("network")
1224 self
.__parse
_network
(_network
)
1227 def device_ids(self
):
1228 return sorted([d
.id for d
in self
.devices
])
1230 def __parse_devices(self
, _devices
):
1233 for _device
in _devices
:
1235 for arg
in self
.__device
_args
:
1236 args
[arg
] = _device
.get(arg
, None)
1238 # Find the device or create a new one.
1239 device
= self
.fireinfo
.get_device(**args
)
1241 device
= self
.fireinfo
.create_device(**args
)
1243 self
.devices
.append(device
)
1245 def __parse_system(self
, system
):
1246 vendor
= system
.get("vendor", None)
1250 model
= system
.get("model", None)
1254 self
.system_id
= self
.fireinfo
.get_system(vendor
, model
)
1255 if not self
.system_id
:
1256 self
.system_id
= self
.fireinfo
.create_system(vendor
, model
)
1259 def processor_id(self
):
1260 if not self
.processor
:
1263 return self
.processor
.id
1265 def __parse_processor(self
, _processor
):
1267 for arg
in self
.__processor
_args
:
1268 if arg
== "core_count":
1273 args
[arg
] = _processor
.get(_arg
, None)
1275 for arg
in self
.__processor
_args
_mandatory
:
1276 if not args
.get(arg
, None):
1277 raise ProfileParserError
, "Mandatory argument missing: %s" % arg
1279 self
.processor
= self
.fireinfo
.get_processor(**args
)
1280 if not self
.processor
:
1281 self
.processor
= self
.fireinfo
.create_processor(**args
)
1283 self
.processor_clock_speed
= _processor
.get("speed", None)
1284 self
.processor_bogomips
= _processor
.get("bogomips", None)
1286 arch
= _processor
.get("arch", None)
1288 self
.__parse
_arch
(arch
)
1290 def __parse_kernel(self
, kernel
):
1291 self
.kernel_id
= self
.fireinfo
.get_kernel(kernel
)
1292 if not self
.kernel_id
:
1293 self
.kernel_id
= self
.fireinfo
.create_kernel(kernel
)
1294 assert self
.kernel_id
1296 self
.kernel
= kernel
1298 def __parse_arch(self
, arch
):
1299 self
.arch_id
= self
.fireinfo
.get_arch(arch
)
1300 if not self
.arch_id
:
1301 self
.arch_id
= self
.fireinfo
.create_arch(arch
)
1305 def __parse_release(self
, release
):
1306 # Remove the arch bit
1308 r
= [e
for e
in release
.split() if e
]
1309 for s
in ("(i586)", "(armv5tel)"):
1316 release
= " ".join(r
)
1318 self
.release_id
= self
.fireinfo
.get_release(release
)
1319 if not self
.release_id
:
1320 self
.release_id
= self
.fireinfo
.create_release(release
)
1321 assert self
.release_id
1323 self
.release
= release
1325 def __parse_language(self
, language
):
1326 self
.language
, delim
, rest
= language
.partition(".")
1327 self
.language
, delim
, rest
= language
.partition("_")
1329 def __parse_hypervisor(self
, hypervisor
):
1330 vendor
= hypervisor
.get("vendor", "other")
1332 if vendor
in ("other", "unknown"):
1333 self
.hypervisor_id
= None
1336 self
.hypervisor_id
= self
.fireinfo
.get_hypervisor(vendor
)
1337 if not self
.hypervisor_id
:
1338 self
.hypervisor_id
= self
.fireinfo
.create_hypervisor(vendor
)
1340 def __parse_network(self
, network
):
1341 self
.network
= ProfileNetwork({
1342 "has_red" : network
.get("red", False),
1343 "has_green" : network
.get("green", False),
1344 "has_orange" : network
.get("orange", False),
1345 "has_blue" : network
.get("blue", False),
1349 class Fireinfo(Object
):
1350 def get_profile_count(self
, when
=None):
1351 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1352 WHERE then_or_now(%s) BETWEEN time_created AND time_valid", when
)
1357 def get_archives_count(self
):
1358 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles")
1365 def parse_profile(self
, public_id
, blob
):
1366 return ProfileParser(self
.backend
, public_id
, blob
)
1370 def profile_exists(self
, public_id
):
1371 res
= self
.db
.get("SELECT id FROM fireinfo_profiles \
1372 WHERE public_id = %s LIMIT 1", public_id
)
1379 def can_update_profile(self
, public_id
, private_id
, when
=None):
1380 res
= self
.db
.get("SELECT 1 FROM fireinfo_profiles \
1381 WHERE public_id = %s AND private_id = %s \
1382 AND time_updated + INTERVAL '60 minutes' <= then_or_now(%s) \
1383 AND time_valid >= then_or_now(%s) ORDER BY time_updated DESC LIMIT 1",
1384 public_id
, private_id
, when
, when
)
1391 def get_profile(self
, public_id
, private_id
=None, when
=None):
1392 res
= self
.db
.get("SELECT * FROM fireinfo_profiles \
1393 WHERE public_id = %s AND \
1394 (CASE WHEN %s IS NULL THEN TRUE ELSE private_id = %s END) AND \
1395 (CASE WHEN %s IS NULL THEN TRUE ELSE \
1396 then_or_now(%s) BETWEEN time_created AND time_valid END) \
1397 ORDER BY time_updated DESC LIMIT 1", public_id
,
1398 private_id
, private_id
, when
, when
)
1401 return Profile(self
.backend
, res
.id, res
)
1403 def get_profiles(self
, public_id
):
1404 res
= self
.db
.query("SELECT * FROM fireinfo_profiles \
1405 WHERE public_id = %s ORDER BY time_created DESC", public_id
)
1409 profile
= Profile(self
.backend
, row
.id, row
)
1410 profiles
.append(profile
)
1414 def create_profile(self
, public_id
, private_id
, when
=None):
1415 valid
= self
.settings
.get_int("fireinfo_profile_days_valid", 14)
1417 res
= self
.db
.get("INSERT INTO fireinfo_profiles(public_id, private_id, \
1418 time_created, time_updated, time_valid) VALUES(%s, %s, then_or_now(%s), \
1419 then_or_now(%s), then_or_now(%s) + INTERVAL '%s days') RETURNING id",
1420 public_id
, private_id
, when
, when
, when
, valid
)
1423 return Profile(self
.backend
, res
.id)
1427 def create_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1428 driver
=None, deviceclass
=None):
1429 res
= self
.db
.get("INSERT INTO fireinfo_devices(subsystem, vendor, model, \
1430 sub_vendor, sub_model, driver, deviceclass) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1431 RETURNING id", subsystem
, vendor
, model
, sub_vendor
, sub_model
, driver
, deviceclass
)
1434 return Device(self
.backend
, res
.id)
1436 def get_device(self
, subsystem
, vendor
, model
, sub_vendor
=None, sub_model
=None,
1437 driver
=None, deviceclass
=None):
1438 res
= self
.db
.get("SELECT * FROM fireinfo_devices \
1439 WHERE subsystem = %s AND vendor = %s AND model = %s \
1440 AND sub_vendor IS NOT DISTINCT FROM %s \
1441 AND sub_model IS NOT DISTINCT FROM %s \
1442 AND driver IS NOT DISTINCT FROM %s \
1443 AND deviceclass IS NOT DISTINCT FROM %s \
1444 LIMIT 1", subsystem
, vendor
, model
, sub_vendor
,
1445 sub_model
, driver
, deviceclass
)
1448 return Device(self
.backend
, res
.id, res
)
1452 def create_system(self
, vendor
, model
):
1453 res
= self
.db
.get("INSERT INTO fireinfo_systems(vendor, model) \
1454 VALUES(%s, %s) RETURNING id", vendor
, model
)
1459 def get_system(self
, vendor
, model
):
1460 res
= self
.db
.get("SELECT id FROM fireinfo_systems WHERE vendor IS NOT DISTINCT FROM %s \
1461 AND model IS NOT DISTINCT FROM %s LIMIT 1", vendor
, model
)
1468 def create_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1469 res
= self
.db
.get("INSERT INTO fireinfo_processors(vendor, model_string, \
1470 family, model, stepping, core_count, flags) VALUES(%s, %s, %s, %s, %s, %s, %s) \
1471 RETURNING id", vendor
, model_string
, family
, model
, stepping
, core_count
, flags
)
1474 return Processor(self
.backend
, res
.id)
1476 def get_processor_by_id(self
, processor_id
, **kwargs
):
1477 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1478 WHERE id = %s", processor_id
)
1481 return Processor(self
.backend
, res
.id, data
=res
, **kwargs
)
1483 def get_processor(self
, vendor
, model_string
, family
, model
, stepping
, core_count
, flags
=None):
1487 res
= self
.db
.get("SELECT * FROM fireinfo_processors \
1488 WHERE vendor = %s AND model_string = %s \
1489 AND family IS NOT DISTINCT FROM %s AND model IS NOT DISTINCT FROM %s \
1490 AND stepping IS NOT DISTINCT FROM %s AND core_count = %s \
1491 AND flags <@ %s AND flags @> %s", vendor
, model_string
, family
, model
,
1492 stepping
, core_count
, flags
, flags
)
1495 return Processor(self
.backend
, res
.id, res
)
1499 def create_kernel(self
, kernel
):
1500 res
= self
.db
.get("INSERT INTO fireinfo_kernels(name) VALUES(%s) \
1501 RETURNING id", kernel
)
1506 def get_kernel(self
, kernel
):
1507 res
= self
.db
.get("SELECT id FROM fireinfo_kernels WHERE name = %s", kernel
)
1514 def create_arch(self
, arch
):
1515 res
= self
.db
.get("INSERT INTO fireinfo_arches(name) VALUES(%s) \
1516 RETURNING id", arch
)
1521 def get_arch(self
, arch
):
1522 res
= self
.db
.get("SELECT id FROM fireinfo_arches WHERE name = %s", arch
)
1529 def create_release(self
, release
):
1530 res
= self
.db
.get("INSERT INTO fireinfo_releases(name) VALUES(%s) \
1531 RETURNING id", release
)
1536 def get_release(self
, release
):
1537 res
= self
.db
.get("SELECT id FROM fireinfo_releases WHERE name = %s", release
)
1544 def create_hypervisor(self
, hypervisor
):
1545 res
= self
.db
.get("INSERT INTO fireinfo_hypervisors(name) VALUES(%s) \
1546 RETURNING id", hypervisor
)
1551 def get_hypervisor(self
, hypervisor
):
1552 res
= self
.db
.get("SELECT id FROM fireinfo_hypervisors WHERE name = %s",
1560 def handle_profile(self
, *args
, **kwargs
):
1561 self
.db
.execute("START TRANSACTION")
1563 # Wrap all the handling of the profile in a huge transaction.
1565 self
._handle
_profile
(*args
, **kwargs
)
1568 self
.db
.execute("ROLLBACK")
1572 self
.db
.execute("COMMIT")
1574 def _handle_profile(self
, public_id
, profile_blob
, location
=None, when
=None):
1575 private_id
= profile_blob
.get("private_id", None)
1578 # Check if the profile already exists in the database.
1579 profile
= self
.fireinfo
.get_profile(public_id
, private_id
=private_id
, when
=when
)
1581 # Check if the update can actually be updated
1582 if profile
and not self
.fireinfo
.can_update_profile(public_id
, private_id
, when
=when
):
1583 logging
.warning("Profile not updated because private ID does not"
1584 " match or last update is too recent: %s" % public_id
)
1588 profile_parser
= self
.parse_profile(public_id
, profile_blob
)
1590 # If a profile exists, check if it matches and if so, just update the
1593 # Check if the profile has changed. If so, update the data.
1594 if profile_parser
.equals(profile
):
1595 profile
.updated(profile_parser
, location
=location
, when
=when
)
1598 # If it does not match, we assume that it is expired and
1599 # create a new profile.
1600 profile
.expired(when
=when
)
1602 # Replace the old profile with a new one
1603 profile
= self
.fireinfo
.create_profile(public_id
, private_id
, when
=when
)
1604 profile
.parse(profile_parser
)
1607 profile
.set_location(location
)
1613 def get_random_profile(self
, when
=None):
1614 # Check if the architecture exists so that we pick a profile with some data
1615 res
= self
.db
.get("SELECT public_id FROM fireinfo_profiles \
1616 LEFT JOIN fireinfo_profiles_arches ON fireinfo_profiles.id = fireinfo_profiles_arches.profile_id \
1617 WHERE fireinfo_profiles_arches.profile_id IS NOT NULL \
1618 AND then_or_now(%s) BETWEEN time_created AND time_valid ORDER BY RANDOM() LIMIT 1", when
)
1621 return res
.public_id
1623 def get_active_profiles(self
, when
=None):
1624 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1625 SELECT COUNT(*) AS with_data, (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1626 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1627 WHERE fireinfo_profiles_releases.profile_id IS NOT NULL", when
)
1630 return res
.with_data
, res
.count
1632 def get_archive_size(self
, when
=None):
1633 res
= self
.db
.get("SELECT COUNT(*) AS count FROM fireinfo_profiles \
1634 WHERE time_created <= then_or_now(%s)", when
)
1639 def get_geo_location_map(self
, when
=None):
1640 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1641 SELECT location, COUNT(location)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1642 LEFT JOIN fireinfo_profiles_locations ON profiles.id = fireinfo_profiles_locations.profile_id \
1643 WHERE fireinfo_profiles_locations.location IS NOT NULL GROUP BY location ORDER BY count DESC", when
)
1645 return ((r
.location
, r
.count
) for r
in res
)
1647 def get_language_map(self
, when
=None):
1648 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_at(%s) AS id) \
1649 SELECT language, COUNT(language)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1650 LEFT JOIN fireinfo_profiles_languages ON profiles.id = fireinfo_profiles_languages.profile_id \
1651 WHERE fireinfo_profiles_languages.language IS NOT NULL GROUP BY language ORDER BY count DESC", when
)
1653 return ((r
.language
, r
.count
) for r
in res
)
1656 def cpu_vendors(self
):
1657 res
= self
.db
.query("SELECT DISTINCT vendor FROM fireinfo_processors ORDER BY vendor")
1659 return (CPU_VENDORS
.get(r
.vendor
, r
.vendor
) for r
in res
)
1661 def get_cpu_vendors_map(self
, when
=None):
1662 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1663 SELECT vendor, COUNT(vendor)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1664 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1665 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1666 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL GROUP BY vendor ORDER BY count DESC", when
)
1668 return ((CPU_VENDORS
.get(r
.vendor
, r
.vendor
), r
.count
) for r
in res
)
1670 def get_cpu_clock_speeds(self
, when
=None):
1671 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1672 SELECT AVG(fireinfo_profiles_processors.clock_speed) AS avg, \
1673 STDDEV(fireinfo_profiles_processors.clock_speed) AS stddev, \
1674 MIN(fireinfo_profiles_processors.clock_speed) AS min, \
1675 MAX(fireinfo_profiles_processors.clock_speed) AS max FROM profiles \
1676 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1677 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1678 AND fireinfo_profiles_processors.clock_speed > 0", when
)
1681 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1683 def get_cpus_with_platform_and_flag(self
, platform
, flag
, when
=None):
1684 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1685 processors AS (SELECT fireinfo_processors.id AS id, fireinfo_processors.flags AS flags FROM profiles \
1686 LEFT JOIN fireinfo_profiles_processors ON profiles.id = fireinfo_profiles_processors.profile_id \
1687 LEFT JOIN fireinfo_processors ON fireinfo_profiles_processors.processor_id = fireinfo_processors.id \
1688 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1689 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1690 WHERE NOT fireinfo_profiles_processors.processor_id IS NULL \
1691 AND fireinfo_arches.platform = %s AND NOT 'hypervisor' = ANY(fireinfo_processors.flags)) \
1692 SELECT (COUNT(*)::float / (SELECT NULLIF(COUNT(*), 0) FROM processors)) AS count FROM processors \
1693 WHERE %s = ANY(processors.flags)", when
, platform
, flag
)
1695 return res
.count
or 0
1697 def get_common_cpu_flags_by_platform(self
, platform
, when
=None):
1698 if platform
== "arm":
1700 "lpae", "neon", "thumb", "thumb2", "thumbee", "vfpv3", "vfpv4",
1702 elif platform
== "x86":
1704 "aes", "avx", "avx2", "lm", "mmx", "mmxext", "nx", "pae",
1705 "pni", "popcnt", "sse", "sse2", "rdrand", "ssse3", "sse4a",
1713 ret
.append((flag
, self
.get_cpus_with_platform_and_flag(platform
, flag
, when
=when
)))
1715 # Add virtual CPU flag "virt" for virtualization support
1716 if platform
== "x86":
1718 self
.get_cpus_with_platform_and_flag(platform
, "vmx", when
=when
) + \
1719 self
.get_cpus_with_platform_and_flag(platform
, "svm", when
=when
)))
1721 return sorted(ret
, key
=lambda x
: x
[1], reverse
=True)
1723 def get_common_memory_amounts(self
, when
=None):
1724 amounts
= (128, 256, 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024)
1727 for amount
in amounts
:
1728 ret
.append((amount
, self
.get_memory_amount(amount
* 1024 * 0.95, when
=when
)))
1732 def get_memory_amount(self
, greather_than
, when
=None):
1733 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1734 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles \
1735 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1736 WHERE NOT fireinfo_profiles_memory.amount IS NULL) AS percentage FROM profiles \
1737 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id \
1738 WHERE fireinfo_profiles_memory.amount >= %s", when
, greather_than
)
1741 return res
.percentage
1743 def get_memory_amounts(self
, when
=None):
1744 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1745 SELECT AVG(fireinfo_profiles_memory.amount) AS avg, \
1746 STDDEV(fireinfo_profiles_memory.amount) AS stddev, \
1747 MIN(fireinfo_profiles_memory.amount) AS min, \
1748 MAX(fireinfo_profiles_memory.amount) AS max FROM profiles \
1749 LEFT JOIN fireinfo_profiles_memory ON profiles.id = fireinfo_profiles_memory.profile_id", when
)
1752 return (res
.avg
or 0, res
.stddev
or 0, res
.min or 0, res
.max or 0)
1754 def get_arch_map(self
, when
=None):
1755 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1756 SELECT fireinfo_arches.name AS arch, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count \
1758 LEFT JOIN fireinfo_profiles_arches ON profiles.id = fireinfo_profiles_arches.profile_id \
1759 LEFT JOIN fireinfo_arches ON fireinfo_profiles_arches.arch_id = fireinfo_arches.id \
1760 WHERE NOT fireinfo_profiles_arches.profile_id IS NULL \
1761 GROUP BY fireinfo_arches.id ORDER BY count DESC", when
)
1763 return ((r
.arch
, r
.count
) for r
in res
)
1767 def get_hypervisor_map(self
, when
=None):
1768 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1769 virtual_profiles AS (SELECT profiles.id AS profile_id, fireinfo_profiles_virtual.hypervisor_id FROM profiles \
1770 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1771 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL) \
1772 SELECT COALESCE(fireinfo_hypervisors.name, %s) AS name, \
1773 COUNT(*)::float / (SELECT COUNT(*) FROM virtual_profiles) AS count FROM virtual_profiles \
1774 LEFT JOIN fireinfo_hypervisors ON virtual_profiles.hypervisor_id = fireinfo_hypervisors.id \
1775 GROUP BY fireinfo_hypervisors.name ORDER BY count DESC", when
, "unknown")
1777 return ((r
.name
, r
.count
) for r
in res
)
1779 def get_virtual_ratio(self
, when
=None):
1780 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1781 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1782 LEFT JOIN fireinfo_profiles_virtual ON profiles.id = fireinfo_profiles_virtual.profile_id \
1783 WHERE fireinfo_profiles_virtual.profile_id IS NOT NULL", when
)
1790 def get_releases_map(self
, when
=None):
1791 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1792 SELECT fireinfo_releases.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1793 LEFT JOIN fireinfo_profiles_releases ON profiles.id = fireinfo_profiles_releases.profile_id \
1794 LEFT JOIN fireinfo_releases ON fireinfo_profiles_releases.release_id = fireinfo_releases.id \
1795 GROUP BY fireinfo_releases.name ORDER BY count DESC", when
)
1797 return ((r
.name
, r
.count
) for r
in res
)
1799 def get_kernels_map(self
, when
=None):
1800 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1801 SELECT fireinfo_kernels.name, COUNT(*)::float / (SELECT COUNT(*) FROM profiles) AS count FROM profiles \
1802 LEFT JOIN fireinfo_profiles_kernels ON profiles.id = fireinfo_profiles_kernels.profile_id \
1803 LEFT JOIN fireinfo_kernels ON fireinfo_profiles_kernels.kernel_id = fireinfo_kernels.id \
1804 GROUP BY fireinfo_kernels.name ORDER BY count DESC", when
)
1806 return ((r
.name
, r
.count
) for r
in res
)
1808 def _process_devices(self
, devices
):
1812 dev
= Device(self
.backend
, dev
.get("id", None), dev
)
1817 def get_driver_map(self
, driver
, when
=None):
1818 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1819 devices AS (SELECT * FROM profiles \
1820 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1821 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1822 WHERE driver = %s) \
1823 SELECT subsystem, model, vendor, driver, deviceclass, \
1824 COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1825 GROUP BY subsystem, model, vendor, driver, deviceclass \
1826 ORDER BY percentage DESC", when
, driver
)
1828 return self
._process
_devices
(res
)
1831 "pci" : hwdata
.PCI(),
1832 "usb" : hwdata
.USB(),
1835 def get_vendor_string(self
, subsystem
, vendor_id
):
1837 cls
= self
.subsystem2class
[subsystem
]
1841 return cls
.get_vendor(vendor_id
)
1843 def get_model_string(self
, subsystem
, vendor_id
, model_id
):
1845 cls
= self
.subsystem2class
[subsystem
]
1849 return cls
.get_device(vendor_id
, model_id
)
1851 def get_vendor_list(self
, when
=None):
1852 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1853 SELECT DISTINCT fireinfo_devices.subsystem AS subsystem, fireinfo_devices.vendor AS vendor FROM profiles \
1854 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1855 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1856 WHERE NOT fireinfo_devices.driver = ANY(%s)", when
, IGNORED_DEVICES
)
1860 vendor
= self
.get_vendor_string(row
.subsystem
, row
.vendor
)
1862 # Drop if vendor could not be determined
1867 vendors
[vendor
].append((row
.subsystem
, row
.vendor
))
1869 vendors
[vendor
] = [(row
.subsystem
, row
.vendor
)]
1871 vendors
= vendors
.items()
1872 return sorted(vendors
)
1874 def get_devices_by_vendor(self
, subsystem
, vendor
, when
=None):
1875 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1876 devices AS (SELECT * FROM profiles \
1877 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1878 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1879 WHERE NOT fireinfo_devices.driver = ANY(%s)), \
1880 vendor_devices AS (SELECT * FROM devices WHERE devices.subsystem = %s AND devices.vendor = %s) \
1881 SELECT subsystem, model, vendor, driver, deviceclass FROM vendor_devices \
1882 GROUP BY subsystem, model, vendor, driver, deviceclass", when
, IGNORED_DEVICES
, subsystem
, vendor
)
1884 return self
._process
_devices
(res
)
1886 def get_device_percentage(self
, subsystem
, vendor
, model
, when
=None):
1887 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1888 devices AS (SELECT * FROM profiles \
1889 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1890 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id) \
1891 SELECT COUNT(*)::float / (SELECT COUNT(*) FROM devices) AS percentage FROM devices \
1892 WHERE devices.subsystem = %s AND devices.vendor = %s AND devices.model = %s",
1893 when
, subsystem
, vendor
, model
)
1896 return res
.percentage
1898 def get_device_in_profile(self
, subsystem
, vendor
, model
, limit
=10, when
=None):
1899 res
= self
.db
.query("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id), \
1900 profiles_with_device AS (SELECT DISTINCT fireinfo_profiles.public_id FROM profiles \
1901 LEFT JOIN fireinfo_profiles ON profiles.id = fireinfo_profiles.id \
1902 LEFT JOIN fireinfo_profiles_devices ON profiles.id = fireinfo_profiles_devices.profile_id \
1903 LEFT JOIN fireinfo_devices ON fireinfo_profiles_devices.device_id = fireinfo_devices.id \
1904 WHERE fireinfo_devices.subsystem = %s AND fireinfo_devices.vendor = %s \
1905 AND fireinfo_devices.model = %s) \
1906 SELECT * FROM profiles_with_device ORDER BY RANDOM() LIMIT %s",
1907 when
, subsystem
, vendor
, model
, limit
)
1909 return (r
.public_id
for r
in res
)
1911 def get_network_zones_map(self
, when
=None):
1912 res
= self
.db
.get("WITH profiles AS (SELECT fireinfo_profiles_with_data_at(%s) AS id) \
1913 SELECT COUNT(NULLIF(has_red, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_red, \
1914 COUNT(NULLIF(has_green, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_green, \
1915 COUNT(NULLIF(has_orange, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_orange, \
1916 COUNT(NULLIF(has_blue, FALSE))::float / (SELECT COUNT(*) FROM profiles) AS has_blue FROM profiles \
1917 LEFT JOIN fireinfo_profiles_networks ON profiles.id = fireinfo_profiles_networks.profile_id \
1918 WHERE fireinfo_profiles_networks.profile_id IS NOT NULL", when
)