]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/fireinfo.py
fireinfo: Fix SQL syntax error
[ipfire.org.git] / src / backend / fireinfo.py
CommitLineData
66862195
MT
1#!/usr/bin/python
2
66862195 3import datetime
e400e37d 4import iso3166
278a2971
MT
5import json
6import jsonschema
66862195
MT
7import logging
8import re
9
6e9f5252 10from . import hwdata
11347e46
MT
11from . import util
12from .misc import Object
278a2971 13from .decorators import *
66862195
MT
14
15N_ = lambda x: x
16
17CPU_VENDORS = {
18 "AMDisbetter!" : "AMD",
19 "AuthenticAMD" : "AMD",
20 "CentaurHauls" : "VIA",
21 "CyrixInstead" : "Cyrix",
22 "GenuineIntel" : "Intel",
23 "TransmetaCPU" : "Transmeta",
24 "GenuineTMx86" : "Transmeta",
25 "Geode by NSC" : "NSC",
26 "NexGenDriven" : "NexGen",
27 "RiseRiseRise" : "Rise",
28 "SiS SiS SiS" : "SiS",
29 "SiS SiS SiS " : "SiS",
30 "UMC UMC UMC " : "UMC",
31 "VIA VIA VIA " : "VIA",
32 "Vortex86 SoC" : "Vortex86",
33}
34
35CPU_STRINGS = (
05bd7298
MT
36 ### AMD ###
37 # APU
3c855735 38 (r"AMD (Sempron)\(tm\) (\d+) APU with Radeon\(tm\) R\d+", r"AMD \1 \2 APU"),
c85ebbc4 39 (r"AMD ([\w\-]+) APU with Radeon\(tm\) HD Graphics", r"AMD \1 APU"),
74638712 40 (r"AMD ([\w\-]+) Radeon R\d+, \d+ Compute Cores \d+C\+\d+G", r"AMD \1 APU"),
05bd7298
MT
41 # Athlon
42 (r"AMD Athlon.* II X2 ([a-z0-9]+).*", r"AMD Athlon X2 \1"),
43 (r"AMD Athlon\(tm\) 64 Processor (\w+)", r"AMD Athlon64 \1"),
44 (r"AMD Athlon\(tm\) 64 X2 Dual Core Processor (\w+)", r"AMD Athlon64 X2 \1"),
66862195
MT
45 (r"(AMD Athlon).*(XP).*", r"\1 \2"),
46 (r"(AMD Phenom).* ([0-9]+) .*", r"\1 \2"),
47 (r"(AMD Phenom).*", r"\1"),
48 (r"(AMD Sempron).*", r"\1"),
05bd7298
MT
49 # Geode
50 (r"Geode\(TM\) Integrated Processor by AMD PCS", r"AMD Geode"),
66862195 51 (r"(Geode).*", r"\1"),
74638712
MT
52 # Mobile
53 (r"Mobile AMD (Athlon|Sempron)\(tm\) Processor (\d+\+?)", r"AMD \1-M \2"),
66862195
MT
54
55 # Intel
56 (r"Intel\(R\) (Atom|Celeron).*CPU\s*([A-Z0-9]+) .*", r"Intel \1 \2"),
57 (r"(Intel).*(Celeron).*", r"\1 \2"),
05bd7298
MT
58 (r"Intel\(R\)? Core\(TM\)?2 Duo *CPU .* ([A-Z0-9]+) .*", r"Intel C2D \1"),
59 (r"Intel\(R\)? Core\(TM\)?2 Duo CPU (\w+)", r"Intel C2D \1"),
60 (r"Intel\(R\)? Core\(TM\)?2 CPU .* ([A-Z0-9]+) .*", r"Intel C2 \1"),
61 (r"Intel\(R\)? Core\(TM\)?2 Quad *CPU .* ([A-Z0-9]+) .*", r"Intel C2Q \1"),
62 (r"Intel\(R\)? Core\(TM\)? (i[753]\-\w+) CPU", r"Intel Core \1"),
63 (r"Intel\(R\)? Xeon\(R\)? CPU (\w+) (0|v\d+)", r"Intel Xeon \1 \2"),
64 (r"Intel\(R\)? Xeon\(R\)? CPU\s+(\w+)", r"Intel Xeon \1"),
66862195
MT
65 (r"(Intel).*(Xeon).*", r"\1 \2"),
66 (r"Intel.* Pentium.* (D|4) .*", r"Intel Pentium \1"),
67 (r"Intel.* Pentium.* Dual .* ([A-Z0-9]+) .*", r"Intel Pentium Dual \1"),
68 (r"Pentium.* Dual-Core .* ([A-Z0-9]+) .*", r"Intel Pentium Dual \1"),
69 (r"(Pentium I{2,3}).*", r"Intel \1"),
70 (r"(Celeron \(Coppermine\))", r"Intel Celeron"),
71
05bd7298
MT
72 # NSC
73 (r"Geode\(TM\) Integrated Processor by National Semi", r"NSC Geode"),
74
66862195
MT
75 # VIA
76 (r"(VIA \w*).*", r"\1"),
77
78 # Qemu
79 (r"QEMU Virtual CPU version .*", r"QEMU CPU"),
80
81 # ARM
82 (r"Feroceon .*", r"ARM Feroceon"),
83)
84
278a2971
MT
85PROFILE_SCHEMA = {
86 "$schema" : "https://json-schema.org/draft/2020-12/schema",
87 "$id" : "https://fireinfo.ipfire.org/profile.schema.json",
88 "title" : "Fireinfo Profile",
89 "description" : "Fireinfo Profile",
90 "type" : "object",
66862195 91
278a2971
MT
92 # Properties
93 "properties" : {
94 # Processor
95 "cpu" : {
96 "type" : "object",
97 "properties" : {
98 "arch" : {
99 "type" : "string",
100 "pattern" : r"^[a-z0-9\_]{,8}$",
101 },
102 "count" : {
103 "type" : "integer",
104 },
105 "family" : {
fd72a507 106 "type" : ["integer", "null"],
278a2971
MT
107 },
108 "flags" : {
109 "type" : "array",
110 "items" : {
111 "type" : "string",
112 "pattern" : r"^.{,24}$",
113 },
114 },
115 "model" : {
116 "type" : "integer",
117 },
118 "model_string" : {
9ab48e8d 119 "type" : ["string", "null"],
278a2971
MT
120 "pattern" : r"^.{,80}$",
121 },
122 "speed" : {
123 "type" : "number",
124 },
125 "stepping" : {
126 "type" : "integer",
127 },
128 "vendor" : {
129 "type" : "string",
130 "pattern" : r"^.{,80}$",
131 },
132 },
133 "additionalProperties" : False,
134 "required" : [
135 "arch",
136 "count",
137 "family",
138 "flags",
139 "model",
140 "model_string",
141 "speed",
142 "stepping",
143 "vendor",
144 ],
145 },
66862195 146
278a2971
MT
147 # Devices
148 "devices" : {
149 "type" : "array",
150 "items" : {
151 "type" : "object",
152 "properties" : {
153 "deviceclass" : {
154 "type" : ["string", "null"],
155 "pattern" : r"^.{,20}$",
156 },
157 "driver" : {
158 "type" : ["string", "null"],
159 "pattern" : r"^.{,24}$",
160 },
161 "model" : {
162 "type" : "string",
163 "pattern" : r"^[a-z0-9]{4}$",
164 },
165 "sub_model" : {
166 "type" : ["string", "null"],
167 "pattern" : r"^[a-z0-9]{4}$",
168 },
169 "sub_vendor" : {
170 "type" : ["string", "null"],
171 "pattern" : r"^[a-z0-9]{4}$",
172 },
173 "subsystem" : {
174 "type" : "string",
175 "pattern" : r"^[a-z]{3}$",
176 },
177 "vendor" : {
178 "type" : "string",
179 "pattern" : r"^[a-z0-9]{4}$",
180 },
181 },
182 "additionalProperties" : False,
183 "required" : [
184 "deviceclass",
185 "driver",
186 "model",
187 "subsystem",
188 "vendor",
189 ],
190 },
191 },
95ae21d1 192
278a2971
MT
193 # Network
194 "network" : {
195 "type" : "object",
196 "properties" : {
197 "blue" : {
198 "type" : "boolean",
199 },
200 "green" : {
201 "type" : "boolean",
202 },
203 "orange" : {
204 "type" : "boolean",
205 },
206 "red" : {
207 "type" : "boolean",
208 },
209 },
210 "additionalProperties" : False,
211 },
66862195 212
278a2971
MT
213 # System
214 "system" : {
215 "type" : "object",
216 "properties" : {
217 "kernel_release" : {
218 "type" : "string",
219 "pattern" : r"^.{,40}$",
220 },
221 "language" : {
222 "type" : "string",
9d038afc 223 "pattern" : r"^[a-z]{2}(\.utf8)?$",
278a2971
MT
224 },
225 "memory" : {
226 "type" : "integer",
227 },
228 "model" : {
bb3bcfe1 229 "type" : ["string", "null"],
278a2971
MT
230 "pattern" : r"^.{,80}$",
231 },
232 "release" : {
233 "type" : "string",
234 "pattern" : r"^.{,80}$",
235 },
236 "root_size" : {
237 "type" : "number",
238 },
239 "vendor" : {
240 "type" : "string",
241 "pattern" : r"^.{,80}$",
242 },
243 "virtual" : {
244 "type" : "boolean"
245 },
246 },
247 "additionalProperties" : False,
248 "required" : [
249 "kernel_release",
250 "language",
251 "memory",
252 "model",
253 "release",
254 "root_size",
255 "vendor",
256 "virtual",
257 ],
258 },
66862195 259
278a2971
MT
260 # Hypervisor
261 "hypervisor" : {
262 "type" : "object",
263 "properties" : {
264 "vendor" : {
265 "type" : "string",
266 "pattern" : r"^.{,40}$",
267 },
268 },
269 "additionalProperties" : False,
270 "required" : [
271 "vendor",
272 ],
273 },
66862195 274
278a2971
MT
275 # Error - BogoMIPS
276 "bogomips" : {
277 "type" : "number",
278 },
279 },
280 "additionalProperties" : False,
281 "required" : [
282 "cpu",
283 "devices",
284 "network",
285 "system",
286 ],
287}
66862195 288
278a2971
MT
289class Network(Object):
290 def init(self, blob):
291 self.blob = blob
66862195
MT
292
293 def __iter__(self):
294 ret = []
295
296 for zone in ("red", "green", "orange", "blue"):
297 if self.has_zone(zone):
298 ret.append(zone)
299
300 return iter(ret)
301
302 def has_zone(self, name):
278a2971 303 return self.blob.get(name, False)
66862195
MT
304
305 @property
306 def has_red(self):
278a2971 307 return self.has_zone("red")
66862195
MT
308
309 @property
310 def has_green(self):
278a2971 311 return self.has_zone("green")
66862195
MT
312
313 @property
314 def has_orange(self):
278a2971 315 return self.has_zone("orange")
66862195
MT
316
317 @property
318 def has_blue(self):
278a2971 319 return self.has_zone("blue")
66862195
MT
320
321
322class Processor(Object):
278a2971
MT
323 def init(self, blob):
324 self.blob = blob
66862195
MT
325
326 def __str__(self):
327 s = []
328
240e9253 329 if self.model_string and not self.model_string.startswith(self.vendor):
66862195
MT
330 s.append(self.vendor)
331 s.append("-")
332
240e9253 333 s.append(self.model_string or "Generic")
66862195
MT
334
335 if self.core_count > 1:
336 s.append("x%s" % self.core_count)
337
338 return " ".join(s)
339
42a41860
MT
340 @property
341 def arch(self):
342 return self.blob.get("arch")
343
66862195
MT
344 @property
345 def vendor(self):
278a2971
MT
346 vendor = self.blob.get("vendor")
347
66862195 348 try:
278a2971 349 return CPU_VENDORS[vendor]
66862195 350 except KeyError:
278a2971 351 return vendor
66862195 352
574ce4e6
MT
353 @property
354 def family(self):
278a2971 355 return self.blob.get("family")
574ce4e6 356
66862195
MT
357 @property
358 def model(self):
278a2971 359 return self.blob.get("model")
66862195 360
574ce4e6
MT
361 @property
362 def stepping(self):
278a2971 363 return self.blob.get("stepping")
574ce4e6 364
66862195
MT
365 @property
366 def model_string(self):
278a2971 367 return self.blob.get("model_string")
66862195
MT
368
369 @property
370 def flags(self):
278a2971 371 return self.blob.get("flags")
66862195
MT
372
373 def has_flag(self, flag):
374 return flag in self.flags
375
376 def uses_ht(self):
8137d982 377 if self.vendor == "Intel" and self.family == 6 and self.model in (15, 55, 76, 77):
574ce4e6
MT
378 return False
379
66862195
MT
380 return self.has_flag("ht")
381
382 @property
383 def core_count(self):
278a2971 384 return self.blob.get("count", 1)
66862195
MT
385
386 @property
387 def count(self):
388 if self.uses_ht():
389 return self.core_count // 2
390
391 return self.core_count
392
393 @property
394 def clock_speed(self):
42a41860 395 return self.blob.get("speed", 0)
66862195
MT
396
397 def format_clock_speed(self):
398 if not self.clock_speed:
399 return
400
401 if self.clock_speed < 1000:
402 return "%dMHz" % self.clock_speed
403
404 return "%.2fGHz" % round(self.clock_speed / 1000, 2)
405
406 @property
407 def bogomips(self):
408 return self.__bogomips
409
410 @property
411 def capabilities(self):
412 caps = [
413 ("64bit", self.has_flag("lm")),
414 ("aes", self.has_flag("aes")),
415 ("nx", self.has_flag("nx")),
416 ("pae", self.has_flag("pae") or self.has_flag("lpae")),
417 ("rdrand", self.has_flag("rdrand")),
418 ]
419
420 # If the system is already running in a virtual environment,
421 # we cannot correctly detect if the CPU supports svm or vmx
422 if self.has_flag("hypervisor"):
423 caps.append(("virt", None))
424 else:
425 caps.append(("virt", self.has_flag("vmx") or self.has_flag("svm")))
426
427 return caps
428
429 def format_model(self):
240e9253 430 s = self.model_string or ""
05bd7298
MT
431
432 # Remove everything after the @: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
433 s, sep, rest = s.partition("@")
434
66862195
MT
435 for pattern, repl in CPU_STRINGS:
436 if re.match(pattern, s) is None:
437 continue
05bd7298
MT
438
439 s = re.sub(pattern, repl, s)
440 break
66862195
MT
441
442 # Otherwise remove the symbols
05bd7298 443 for i in ("C", "R", "TM", "tm"):
66862195
MT
444 s = s.replace("(%s)" % i, "")
445
05bd7298
MT
446 # Replace too long strings with shorter ones
447 pairs = (
448 ("Quad-Core Processor", ""),
449 ("Dual-Core Processor", ""),
450 ("Processor", "CPU"),
451 ("processor", "CPU"),
452 )
453 for k, v in pairs:
454 s = s.replace(k, v)
455
456 # Remove too many spaces
457 s = " ".join((e for e in s.split() if e))
458
66862195
MT
459 return s
460
461 @property
462 def friendly_string(self):
463 s = []
464
465 model = self.format_model()
240e9253
MT
466 if model:
467 s.append(model)
66862195
MT
468
469 clock_speed = self.format_clock_speed()
470 if clock_speed:
471 s.append("@ %s" % clock_speed)
472
473 if self.count > 1:
474 s.append("x%s" % self.count)
475
476 return " ".join(s)
477
478
479class Device(Object):
480 classid2name = {
481 "pci" : {
482 "00" : N_("Unclassified"),
483 "01" : N_("Mass storage"),
484 "02" : N_("Network"),
485 "03" : N_("Display"),
486 "04" : N_("Multimedia"),
487 "05" : N_("Memory controller"),
488 "06" : N_("Bridge"),
489 "07" : N_("Communication"),
490 "08" : N_("Generic system peripheral"),
491 "09" : N_("Input device"),
492 "0a" : N_("Docking station"),
493 "0b" : N_("Processor"),
494 "0c" : N_("Serial bus"),
495 "0d" : N_("Wireless"),
496 "0e" : N_("Intelligent controller"),
497 "0f" : N_("Satellite communications controller"),
498 "10" : N_("Encryption"),
499 "11" : N_("Signal processing controller"),
500 "ff" : N_("Unassigned class"),
501 },
440aba92 502
66862195
MT
503 "usb" : {
504 "00" : N_("Unclassified"),
505 "01" : N_("Multimedia"),
506 "02" : N_("Communication"),
507 "03" : N_("Input device"),
508 "05" : N_("Generic system peripheral"),
509 "06" : N_("Image"),
510 "07" : N_("Printer"),
511 "08" : N_("Mass storage"),
512 "09" : N_("Hub"),
513 "0a" : N_("Communication"),
514 "0b" : N_("Smart card"),
515 "0d" : N_("Encryption"),
516 "0e" : N_("Display"),
517 "0f" : N_("Personal Healthcare"),
518 "dc" : N_("Diagnostic Device"),
519 "e0" : N_("Wireless"),
520 "ef" : N_("Unclassified"),
521 "fe" : N_("Unclassified"),
522 "ff" : N_("Unclassified"),
523 }
524 }
525
278a2971
MT
526 def init(self, blob):
527 self.blob = blob
66862195
MT
528
529 def __repr__(self):
530 return "<%s vendor=%s model=%s>" % (self.__class__.__name__,
531 self.vendor_string, self.model_string)
532
3adfd81b
MT
533 def __eq__(self, other):
534 if isinstance(other, self.__class__):
278a2971
MT
535 return self.blob == other.blob
536
537 return NotImplemented
3adfd81b
MT
538
539 def __lt__(self, other):
540 if isinstance(other, self.__class__):
e2591627 541 return self.cls < other.cls or \
3adfd81b
MT
542 self.vendor_string < other.vendor_string or \
543 self.vendor < other.vendor or \
544 self.model_string < other.model_string or \
e2591627 545 self.model < other.model
66862195 546
278a2971 547 return NotImplemented
66862195
MT
548
549 def is_showable(self):
278a2971 550 if self.driver in ("usb", "pcieport", "hub"):
66862195
MT
551 return False
552
553 return True
554
555 @property
556 def subsystem(self):
278a2971 557 return self.blob.get("subsystem")
66862195
MT
558
559 @property
560 def model(self):
278a2971 561 return self.blob.get("model")
66862195 562
278a2971 563 @lazy_property
66862195 564 def model_string(self):
278a2971 565 return self.fireinfo.get_model_string(self.subsystem, self.vendor, self.model)
66862195
MT
566
567 @property
568 def vendor(self):
278a2971 569 return self.blob.get("vendor")
66862195 570
278a2971 571 @lazy_property
66862195
MT
572 def vendor_string(self):
573 return self.fireinfo.get_vendor_string(self.subsystem, self.vendor)
574
575 @property
576 def driver(self):
278a2971 577 return self.blob.get("driver")
66862195 578
278a2971 579 @lazy_property
66862195 580 def cls(self):
278a2971 581 classid = self.blob.get("deviceclass")
66862195
MT
582
583 if self.subsystem == "pci":
584 classid = classid[:-4]
585 if len(classid) == 1:
586 classid = "0%s" % classid
587
588 elif self.subsystem == "usb" and classid:
589 classid = classid.split("/")[0]
590 classid = "%02x" % int(classid)
591
592 try:
593 return self.classid2name[self.subsystem][classid]
594 except KeyError:
595 return "N/A"
596
66862195 597
278a2971
MT
598class System(Object):
599 def init(self, blob):
600 self.blob = blob
66862195 601
66862195 602 @property
278a2971
MT
603 def language(self):
604 return self.blob.get("language")
66862195
MT
605
606 @property
278a2971
MT
607 def vendor(self):
608 return self.blob.get("vendor")
66862195
MT
609
610 @property
278a2971
MT
611 def model(self):
612 return self.blob.get("model")
66862195
MT
613
614 @property
278a2971
MT
615 def release(self):
616 return self.blob.get("release")
66862195 617
278a2971
MT
618 @property
619 def storage(self):
620 return self.blob.get("storage_size", 0)
66862195 621
278a2971
MT
622 def is_virtual(self):
623 return self.blob.get("virtual", False)
66862195 624
66862195 625
278a2971
MT
626class Hypervisor(Object):
627 def init(self, blob):
628 self.blob = blob
66862195 629
278a2971
MT
630 def __str__(self):
631 return self.vendor
66862195 632
278a2971
MT
633 @property
634 def vendor(self):
635 return self.blob.get("vendor")
66862195 636
66862195 637
278a2971
MT
638class Profile(Object):
639 def init(self, profile_id, private_id, created_at, expired_at, version, blob,
640 last_updated_at, country_code, **kwargs):
641 self.profile_id = profile_id
642 self.private_id = private_id
643 self.created_at = created_at
644 self.expired_at = expired_at
645 self.version = version
646 self.blob = blob
647 self.last_updated_at = last_updated_at
648 self.country_code = country_code
66862195 649
278a2971
MT
650 def __repr__(self):
651 return "<%s %s>" % (self.__class__.__name__, self.profile_id)
66862195 652
278a2971
MT
653 def is_showable(self):
654 return True if self.blob else False
66862195 655
278a2971
MT
656 @property
657 def public_id(self):
658 """
659 An alias for the profile ID
660 """
661 return self.profile_id
66862195
MT
662
663 # Location
664
440aba92
MT
665 @property
666 def location(self):
278a2971 667 return self.country_code
66862195
MT
668
669 @property
670 def location_string(self):
e929ed92 671 return self.backend.get_country_name(self.location) or self.location
66862195
MT
672
673 # Devices
674
278a2971
MT
675 @lazy_property
676 def devices(self):
677 return [Device(self.backend, blob) for blob in self.blob.get("devices", [])]
66862195
MT
678
679 # System
680
278a2971 681 @lazy_property
66862195 682 def system(self):
278a2971 683 return System(self.backend, self.blob.get("system", {}))
66862195 684
278a2971 685 # Processor
66862195
MT
686
687 @property
278a2971
MT
688 def processor(self):
689 return Processor(self.backend, self.blob.get("cpu", {}))
66862195
MT
690
691 # Memory
692
278a2971
MT
693 @property
694 def memory(self):
695 return self.blob.get("memory")
66862195
MT
696
697 @property
698 def friendly_memory(self):
a0c9a14e 699 return util.format_size(self.memory or 0)
66862195 700
66862195
MT
701 # Virtual
702
278a2971
MT
703 def is_virtual(self):
704 return self.system.is_virtual()
66862195
MT
705
706 @property
707 def hypervisor(self):
278a2971 708 return Hypervisor(self.backend, self.blob.get("hypervisor"))
66862195
MT
709
710 # Network
711
278a2971
MT
712 @lazy_property
713 def network(self):
714 return Network(self.backend, self.blob.get("network", {}))
66862195
MT
715
716
717class Fireinfo(Object):
278a2971
MT
718 async def expire(self):
719 """
720 Called to expire any profiles that have not been updated in a fortnight
721 """
722 self.db.execute("UPDATE fireinfo SET expired_at = CURRENT_TIMESTAMP \
723 WHERE last_updated_at <= CURRENT_TIMESTAMP - %s", datetime.timedelta(days=14))
66862195 724
278a2971
MT
725 def _get_profile(self, query, *args, **kwargs):
726 res = self.db.get(query, *args, **kwargs)
d7bebd28
MT
727
728 if res:
278a2971 729 return Profile(self.backend, **res)
d7bebd28 730
278a2971
MT
731 def get_profile_count(self, when=None):
732 if when:
733 res = self.db.get("""
734 SELECT
735 COUNT(*) AS count
736 FROM
737 fireinfo
738 WHERE
739 created_at <= %s
740 AND
741 (
742 expired_at IS NULL
743 OR
744 expired_at > %s
745 )
746 """)
747 else:
748 res = self.db.get("""
749 SELECT
750 COUNT(*) AS count
751 FROM
752 fireinfo
753 WHERE
754 expired_at IS NULL
755 """,
756 )
66862195 757
278a2971 758 return res.count if res else 0
66862195 759
278a2971 760 # Profiles
66862195 761
278a2971
MT
762 def get_profile(self, profile_id, when=None):
763 if when:
764 return self._get_profile("""
765 SELECT
766 *
767 FROM
768 fireinfo
769 WHERE
770 profile_id = %s
771 AND
772 %s BETWEEN created_at AND expired_at
773 """, profile_id,
774 )
66862195 775
278a2971
MT
776 return self._get_profile("""
777 SELECT
778 *
779 FROM
780 fireinfo
781 WHERE
782 profile_id = %s
783 AND
784 expired_at IS NULL
785 """, profile_id,
786 )
66862195 787
278a2971 788 # Handle profile
66862195 789
6ffd6ec1 790 def handle_profile(self, profile_id, blob, country_code=None, asn=None, when=None):
278a2971
MT
791 private_id = blob.get("private_id", None)
792 assert private_id
dd3a5446 793
278a2971 794 now = datetime.datetime.utcnow()
dd3a5446 795
278a2971
MT
796 # Fetch the profile version
797 version = blob.get("profile_version")
e400e37d 798
278a2971
MT
799 # Extract the profile
800 profile = blob.get("profile")
e400e37d 801
2c6cd062
MT
802 if profile:
803 # Validate the profile
804 self._validate(profile_id, version, profile)
66862195 805
2c6cd062
MT
806 # Pre-process the profile
807 profile = self._preprocess(profile)
66862195 808
278a2971
MT
809 # Fetch the previous profile
810 prev = self.get_profile(profile_id)
66862195 811
278a2971
MT
812 if prev:
813 # Check if the private ID matches
814 if not prev.private_id == private_id:
815 logging.error("Private ID for profile %s does not match" % profile_id)
816 return False
66862195 817
278a2971
MT
818 # Check when the last update was
819 elif now - prev.last_updated_at < datetime.timedelta(hours=6):
820 logging.warning("Profile %s has been updated too soon" % profile_id)
821 return False
66862195 822
278a2971
MT
823 # Check if the profile has changed
824 elif prev.version == version and prev.blob == blob:
825 logging.debug("Profile %s has not changed" % profile_id)
826
827 # Update the timestamp
828 self.db.execute("UPDATE fireinfo SET last_updated_at = CURRENT_TIMESTAMP \
829 WHERE profile_id = %s AND expired_at IS NULL", profile_id)
830
831 return True
832
833 # Delete the previous profile
834 self.db.execute("UPDATE fireinfo SET expired_at = CURRENT_TIMESTAMP \
835 WHERE profile_id = %s AND expired_at IS NULL", profile_id)
836
15733ec8
MT
837 # Serialise the profile
838 if profile:
839 profile = json.dumps(profile)
840
278a2971
MT
841 # Store the new profile
842 self.db.execute("""
843 INSERT INTO
844 fireinfo
845 (
846 profile_id,
847 private_id,
848 version,
849 blob,
6ffd6ec1
MT
850 country_code,
851 asn
278a2971
MT
852 )
853 VALUES
854 (
855 %s,
856 %s,
857 %s,
858 %s,
6ffd6ec1 859 %s,
278a2971
MT
860 %s
861 )
15733ec8 862 """, profile_id, private_id, version, profile, country_code, asn,
278a2971 863 )
66862195 864
278a2971
MT
865 def _validate(self, profile_id, version, blob):
866 """
867 Validate the profile
868 """
869 if not version == 0:
870 raise ValueError("Unsupported profile version")
66862195 871
278a2971 872 # Validate the blob
66862195 873 try:
278a2971 874 return jsonschema.validate(blob, schema=PROFILE_SCHEMA)
66862195 875
278a2971
MT
876 # Raise a ValueError instead which is easier to handle later on
877 except jsonschema.exceptions.ValidationError as e:
878 raise ValueError("%s" % e) from e
66862195 879
278a2971
MT
880 def _preprocess(self, blob):
881 """
882 Modifies the profile before storing it
883 """
884 # Remove the architecture from the release string
885 blob["system"]["release"]= self._filter_release(blob["system"]["release"])
66862195 886
278a2971 887 return blob
66862195 888
278a2971
MT
889 def _filter_release(self, release):
890 """
891 Removes the arch part
892 """
893 r = [e for e in release.split() if e]
66862195 894
278a2971
MT
895 for s in ("(x86_64)", "(aarch64)", "(i586)", "(armv6l)", "(armv5tel)", "(riscv64)"):
896 try:
897 r.remove(s)
898 break
899 except ValueError:
900 pass
66862195 901
278a2971 902 return " ".join(r)
66862195
MT
903
904 # Data outputs
905
906 def get_random_profile(self, when=None):
278a2971
MT
907 if when:
908 return self._get_profile("""
909 SELECT
910 *
911 FROM
912 fireinfo
913 WHERE
914 created_at <= %s
915 AND
916 (
917 expired_at IS NULL
918 OR
919 expired_at > %s
920 )
c42a5ef6
MT
921 AND
922 blob IS NOT NULL
278a2971
MT
923 ORDER BY
924 RANDOM()
925 LIMIT
926 1
927 """, when, when,
928 )
66862195 929
278a2971
MT
930 return self._get_profile("""
931 SELECT
932 *
933 FROM
934 fireinfo
935 WHERE
936 expired_at IS NULL
c42a5ef6
MT
937 AND
938 blob IS NOT NULL
278a2971
MT
939 ORDER BY
940 RANDOM()
941 LIMIT
942 1
943 """)
66862195
MT
944
945 def get_active_profiles(self, when=None):
278a2971
MT
946 if when:
947 raise NotImplementedError
66862195 948
278a2971
MT
949 else:
950 res = self.db.get("""
951 SELECT
952 COUNT(*) AS total_profiles,
953 COUNT(*) FILTER (WHERE blob IS NOT NULL) AS active_profiles
954 FROM
955 fireinfo
956 WHERE
957 expired_at IS NULL
958 """)
66862195
MT
959
960 if res:
278a2971
MT
961 return res.active_profiles, res.total_profiles
962
963 def get_geo_location_map(self, when=None):
964 if when:
965 res = self.db.query("""
966 SELECT
967 country_code,
968 fireinfo_percentage(
969 COUNT(*), SUM(COUNT(*)) OVER ()
970 ) AS p
971 FROM
972 fireinfo
973 WHERE
974 created_at <= %s
975 AND
976 (
977 expired_at IS NULL
978 OR
979 expired_at > %s
980 )
981 AND
982 country_code IS NOT NULL
983 GROUP BY
984 country_code
985 """, when, when)
986 else:
987 res = self.db.query("""
988 SELECT
989 country_code,
990 fireinfo_percentage(
991 COUNT(*), SUM(COUNT(*)) OVER ()
992 ) AS p
993 FROM
994 fireinfo
995 WHERE
996 expired_at IS NULL
997 AND
998 country_code IS NOT NULL
999 GROUP BY
1000 country_code
1001 """)
1002
1003 return { row.country_code : row.p for row in res }
66862195 1004
0450fa54
MT
1005 def get_asn_map(self, when=None):
1006 if when:
1007 res = self.db.query("""
1008 SELECT
1009 asn,
1010 fireinfo_percentage(
1011 COUNT(*), SUM(COUNT(*)) OVER ()
1012 ) AS p,
1013 COUNT(*) AS c
1014 FROM
1015 fireinfo
1016 WHERE
1017 created_at <= %s
1018 AND
1019 (
1020 expired_at IS NULL
1021 OR
1022 expired_at > %s
1023 )
1024 AND
1025 asn IS NOT NULL
1026 GROUP BY
1027 asn
1028 """, when, when)
1029 else:
1030 res = self.db.query("""
1031 SELECT
1032 asn,
1033 fireinfo_percentage(
1034 COUNT(*), SUM(COUNT(*)) OVER ()
1035 ) AS p,
1036 COUNT(*) AS c
1037 FROM
1038 fireinfo
1039 WHERE
1040 expired_at IS NULL
1041 AND
1042 asn IS NOT NULL
1043 GROUP BY
1044 asn
1045 """)
1046
1047 return { self.backend.location.get_as(row.asn) : (row.c, row.p) for row in res }
1048
66862195
MT
1049 @property
1050 def cpu_vendors(self):
278a2971
MT
1051 res = self.db.query("""
1052 SELECT DISTINCT
1053 blob->'cpu'->'vendor' AS vendor
1054 FROM
1055 fireinfo
1056 WHERE
1057 blob->'cpu'->'vendor' IS NOT NULL
1058 """,
1059 )
66862195 1060
278a2971 1061 return sorted((CPU_VENDORS.get(row.vendor, row.vendor) for row in res))
66862195
MT
1062
1063 def get_cpu_vendors_map(self, when=None):
278a2971
MT
1064 if when:
1065 raise NotImplementedError
66862195 1066
66862195 1067 else:
278a2971
MT
1068 res = self.db.query("""
1069 SELECT
1070 blob->'cpu'->'vendor' AS vendor,
1071 fireinfo_percentage(
1072 COUNT(*), SUM(COUNT(*)) OVER ()
1073 ) AS p
1074 FROM
1075 fireinfo
1076 WHERE
1077 expired_at IS NULL
1078 AND
1079 blob IS NOT NULL
1080 AND
1081 blob->'cpu'->'vendor' IS NOT NULL
1082 GROUP BY
1083 blob->'cpu'->'vendor'
1084 """)
1085
1086 return { CPU_VENDORS.get(row.vendor, row.vendor) : row.p for row in res }
1087
1088 def get_cpu_flags_map(self, when=None):
1089 if when:
1090 raise NotImplementedError
66862195 1091
278a2971
MT
1092 else:
1093 res = self.db.query("""
1094 WITH arch_flags AS (
1095 SELECT
1096 ROW_NUMBER() OVER (PARTITION BY blob->'cpu'->'arch') AS id,
1097 blob->'cpu'->'arch' AS arch,
1098 blob->'cpu'->'flags' AS flags
1099 FROM
1100 fireinfo
1101 WHERE
1102 expired_at IS NULL
1103 AND
1104 blob->'cpu'->'arch' IS NOT NULL
1105 AND
1106 blob->'cpu'->'flags' IS NOT NULL
1107
1108 -- Filter out virtual systems
1109 AND
1110 CAST((blob->'system'->'virtual') AS boolean) IS FALSE
1111 )
1112
1113 SELECT
1114 arch,
1115 flag,
1116 fireinfo_percentage(
1117 COUNT(*),
1118 (
1119 SELECT
1120 MAX(id)
1121 FROM
1122 arch_flags __arch_flags
1123 WHERE
1124 arch_flags.arch = __arch_flags.arch
1125 )
1126 ) AS p
1127 FROM
1128 arch_flags, jsonb_array_elements(arch_flags.flags) AS flag
1129 GROUP BY
1130 arch, flag
1131 """)
1132
1133 result = {}
66862195 1134
278a2971
MT
1135 for row in res:
1136 try:
1137 result[row.arch][row.flag] = row.p
1138 except KeyError:
1139 result[row.arch] = { row.flag : row.p }
66862195 1140
278a2971 1141 return result
66862195 1142
2a38b034 1143 def get_average_memory_amount(self, when=None):
278a2971
MT
1144 if when:
1145 res = self.db.get("""
1146 SELECT
1147 AVG(
1148 CAST(blob->'system'->'memory' AS numeric)
1149 ) AS memory
1150 FROM
1151 fireinfo
1152 WHERE
1153 created_at <= %s
1154 AND
1155 (
1156 expired_at IS NULL
1157 OR
1158 expired_at > %s
1159 )
1160 """, when)
1161 else:
1162 res = self.db.get("""
1163 SELECT
1164 AVG(
1165 CAST(blob->'system'->'memory' AS numeric)
1166 ) AS memory
1167 FROM
1168 fireinfo
1169 WHERE
1170 expired_at IS NULL
1171 """,)
1172
1173 return res.memory if res else 0
2a38b034 1174
66862195 1175 def get_arch_map(self, when=None):
278a2971
MT
1176 if when:
1177 raise NotImplementedError
66862195 1178
278a2971
MT
1179 else:
1180 res = self.db.query("""
1181 SELECT
1182 blob->'cpu'->'arch' AS arch,
1183 fireinfo_percentage(
1184 COUNT(*), SUM(COUNT(*)) OVER ()
1185 ) AS p
1186 FROM
1187 fireinfo
1188 WHERE
1189 expired_at IS NULL
1190 AND
1191 blob->'cpu'->'arch' IS NOT NULL
1192 GROUP BY
1193 blob->'cpu'->'arch'
1194 """)
1195
1196 return { row.arch : row.p for row in res }
66862195
MT
1197
1198 # Virtual
1199
1200 def get_hypervisor_map(self, when=None):
278a2971
MT
1201 if when:
1202 raise NotImplementedError
1203 else:
1204 res = self.db.query("""
1205 SELECT
1206 blob->'hypervisor'->'vendor' AS vendor,
1207 fireinfo_percentage(
1208 COUNT(*), SUM(COUNT(*)) OVER ()
1209 ) AS p
1210 FROM
1211 fireinfo
1212 WHERE
1213 expired_at IS NULL
1214 AND
1215 CAST((blob->'system'->'virtual') AS boolean) IS TRUE
1216 AND
1217 blob->'hypervisor'->'vendor' IS NOT NULL
1218 GROUP BY
1219 blob->'hypervisor'->'vendor'
1220 """)
1221
1222 return { row.vendor : row.p for row in res }
66862195
MT
1223
1224 def get_virtual_ratio(self, when=None):
278a2971
MT
1225 if when:
1226 raise NotImplementedError
66862195 1227
278a2971
MT
1228 else:
1229 res = self.db.get("""
1230 SELECT
1231 fireinfo_percentage(
1232 COUNT(*) FILTER (
1233 WHERE CAST((blob->'system'->'virtual') AS boolean) IS TRUE
1234 ),
1235 COUNT(*)
1236 ) AS p
1237 FROM
1238 fireinfo
1239 WHERE
1240 expired_at IS NULL
1241 AND
1242 blob IS NOT NULL
1243 """)
1244
1245 return res.p if res else 0
66862195
MT
1246
1247 # Releases
1248
1249 def get_releases_map(self, when=None):
278a2971
MT
1250 if when:
1251 raise NotImplementedError
66862195 1252
278a2971
MT
1253 else:
1254 res = self.db.query("""
1255 SELECT
1256 blob->'system'->'release' AS release,
1257 fireinfo_percentage(
1258 COUNT(*), SUM(COUNT(*)) OVER ()
1259 ) AS p
1260 FROM
1261 fireinfo
1262 WHERE
1263 expired_at IS NULL
1264 AND
1265 blob IS NOT NULL
1266 AND
1267 blob->'system'->'release' IS NOT NULL
1268 GROUP BY
1269 blob->'system'->'release'
1270 """)
1271
1272 return { row.release : row.p for row in res }
1273
1274 # Kernels
66862195
MT
1275
1276 def get_kernels_map(self, when=None):
278a2971
MT
1277 if when:
1278 raise NotImplementedError
66862195 1279
278a2971
MT
1280 else:
1281 res = self.db.query("""
1282 SELECT
1283 blob->'system'->'kernel' AS kernel,
1284 fireinfo_percentage(
1285 COUNT(*), SUM(COUNT(*)) OVER ()
1286 ) AS p
1287 FROM
1288 fireinfo
1289 WHERE
1290 expired_at IS NULL
1291 AND
1292 blob IS NOT NULL
1293 AND
1294 blob->'system'->'kernel' IS NOT NULL
1295 GROUP BY
1296 blob->'system'->'kernel'
1297 """)
1298
1299 return { row.kernel : row.p for row in res }
66862195
MT
1300
1301 subsystem2class = {
1302 "pci" : hwdata.PCI(),
1303 "usb" : hwdata.USB(),
1304 }
1305
1306 def get_vendor_string(self, subsystem, vendor_id):
1307 try:
1308 cls = self.subsystem2class[subsystem]
1309 except KeyError:
3697181e 1310 return ""
66862195 1311
3697181e 1312 return cls.get_vendor(vendor_id) or ""
66862195
MT
1313
1314 def get_model_string(self, subsystem, vendor_id, model_id):
1315 try:
1316 cls = self.subsystem2class[subsystem]
1317 except KeyError:
3697181e 1318 return ""
66862195 1319
3697181e 1320 return cls.get_device(vendor_id, model_id) or ""
66862195
MT
1321
1322 def get_vendor_list(self, when=None):
278a2971
MT
1323 if when:
1324 raise NotImplementedError
1325
1326 else:
1327 res = self.db.query("""
1328 WITH devices AS (
1329 SELECT
1330 jsonb_array_elements(blob->'devices') AS device
1331 FROM
1332 fireinfo
1333 WHERE
1334 expired_at IS NULL
1335 AND
1336 blob IS NOT NULL
1337 AND
1338 blob->'devices' IS NOT NULL
1339 AND
1340 jsonb_typeof(blob->'devices') = 'array'
1341 )
1342
1343 SELECT
1344 devices.device->'subsystem' AS subsystem,
1345 devices.device->'vendor' AS vendor
1346 FROM
1347 devices
1348 WHERE
1349 devices.device->'subsystem' IS NOT NULL
1350 AND
1351 devices.device->'vendor' IS NOT NULL
1352 AND
1353 NOT devices.device->>'driver' = 'usb'
1354 GROUP BY
1355 subsystem, vendor
1356 """)
66862195
MT
1357
1358 vendors = {}
278a2971 1359
66862195 1360 for row in res:
278a2971 1361 vendor = self.get_vendor_string(row.subsystem, row.vendor) or row.vendor
66862195
MT
1362
1363 # Drop if vendor could not be determined
1364 if vendor is None:
1365 continue
1366
1367 try:
1368 vendors[vendor].append((row.subsystem, row.vendor))
1369 except KeyError:
1370 vendors[vendor] = [(row.subsystem, row.vendor)]
1371
278a2971
MT
1372 return vendors
1373
1374 def _get_devices(self, query, *args, **kwargs):
1375 res = self.db.query(query, *args, **kwargs)
1376
1377 return [Device(self.backend, blob) for blob in res]
66862195
MT
1378
1379 def get_devices_by_vendor(self, subsystem, vendor, when=None):
278a2971
MT
1380 if when:
1381 raise NotImplementedError
1382
1383 else:
1384 return self._get_devices("""
1385 WITH devices AS (
1386 SELECT
1387 jsonb_array_elements(blob->'devices') AS device
1388 FROM
1389 fireinfo
1390 WHERE
1391 expired_at IS NULL
1392 AND
1393 blob IS NOT NULL
1394 AND
1395 blob->'devices' IS NOT NULL
1396 AND
1397 jsonb_typeof(blob->'devices') = 'array'
1398 )
1399
1400 SELECT
1401 device.deviceclass,
1402 device.subsystem,
1403 device.vendor,
1404 device.model,
1405 device.driver
1406 FROM
1407 devices,
1408 jsonb_to_record(devices.device) AS device(
1409 deviceclass text,
1410 subsystem text,
1411 vendor text,
1412 sub_vendor text,
1413 model text,
1414 sub_model text,
1415 driver text
1416 )
1417 WHERE
1418 devices.device->>'subsystem' = %s
1419 AND
1420 devices.device->>'vendor' = %s
1421 AND
1422 NOT devices.device->>'driver' = 'usb'
1423 GROUP BY
1424 device.deviceclass,
1425 device.subsystem,
1426 device.vendor,
1427 device.model,
1428 device.driver
1429 """, subsystem, vendor,
1430 )
1431
1432 def get_devices_by_driver(self, driver, when=None):
1433 if when:
1434 raise NotImplementedError
1435
1436 else:
1437 return self._get_devices("""
1438 WITH devices AS (
1439 SELECT
1440 jsonb_array_elements(blob->'devices') AS device
1441 FROM
1442 fireinfo
1443 WHERE
1444 expired_at IS NULL
1445 AND
1446 blob IS NOT NULL
1447 AND
1448 blob->'devices' IS NOT NULL
1449 AND
1450 jsonb_typeof(blob->'devices') = 'array'
1451 )
1452
1453 SELECT
1454 device.deviceclass,
1455 device.subsystem,
1456 device.vendor,
1457 device.model,
1458 device.driver
1459 FROM
1460 devices,
1461 jsonb_to_record(devices.device) AS device(
1462 deviceclass text,
1463 subsystem text,
1464 vendor text,
1465 sub_vendor text,
1466 model text,
1467 sub_model text,
1468 driver text
1469 )
1470 WHERE
12152618 1471 devices.device->>'driver' = %s
278a2971
MT
1472 GROUP BY
1473 device.deviceclass,
1474 device.subsystem,
1475 device.vendor,
1476 device.model,
1477 device.driver
1478 """, driver,
1479 )