From 59f6ac705ca752684fa9dc93292e0181d19fae02 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 25 Sep 2024 19:04:29 +0200 Subject: [PATCH] fix components --- device/models.py | 5 +-- device/templates/details.html | 9 ++++-- device/views.py | 5 ++- evidence/parse.py | 6 +++- evidence/parse_details.py | 57 ++++++++++++++++++----------------- 5 files changed, 47 insertions(+), 35 deletions(-) diff --git a/device/models.py b/device/models.py index e2378c2..c9e04ec 100644 --- a/device/models.py +++ b/device/models.py @@ -102,8 +102,9 @@ class Device: def get_last_evidence(self): annotations = self.get_annotations() - if annotations: - annotation = annotations.first() + if not annotations.count(): + return + annotation = annotations.first() self.last_evidence = Evidence(annotation.uuid) def last_uuid(self): diff --git a/device/templates/details.html b/device/templates/details.html index de4ae85..b757b1f 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -180,9 +180,12 @@ {{ evidence.created }}

- {{ c.manufacturer }}
- {{ c.model }}
- {{ c.serialNumber }}
+ {% for k, v in c.items %} + {% if k not in "actions,type" %} + {{ k }}: {{ v }}
+ {% endif %} + {% endfor %} +

diff --git a/device/views.py b/device/views.py index 0fd9365..8d4c024 100644 --- a/device/views.py +++ b/device/views.py @@ -1,7 +1,7 @@ import json from django.urls import reverse_lazy -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, Http404 from django.utils.translation import gettext_lazy as _ from django.views.generic.edit import ( CreateView, @@ -87,6 +87,9 @@ class DetailsView(DashboardView, TemplateView): def get(self, request, *args, **kwargs): self.pk = kwargs['pk'] self.object = Device(id=self.pk) + if not self.object.last_evidence: + raise Http404 + return super().get(request, *args, **kwargs) def get_context_data(self, **kwargs): diff --git a/evidence/parse.py b/evidence/parse.py index 6de5ca4..d07f313 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -19,8 +19,12 @@ def get_network_cards(child, nets): def get_mac(lshw): nets = [] + try: + get_network_cards(json.loads(lshw), nets) + except Exception as ss: + print("WARNING!! {}".format(ss)) + return - get_network_cards(json.loads(lshw), nets) nets_sorted = sorted(nets, key=lambda x: x['businfo']) # This funcion get the network card integrated in motherboard # integrate = [x for x in nets if "pci@0000:00:" in x.get('businfo', '')] diff --git a/evidence/parse_details.py b/evidence/parse_details.py index cddd293..8465403 100644 --- a/evidence/parse_details.py +++ b/evidence/parse_details.py @@ -3,7 +3,6 @@ import numpy as np from datetime import datetime from dmidecode import DMIParse -from evidence import base2, unit from utils.constants import CHASSIS_DH, DATASTORAGEINTERFACE @@ -81,9 +80,9 @@ class ParseSnapshot: "threads": int(cpu.get('Thread Count', 1)), "manufacturer": cpu.get('Manufacturer'), "serialNumber": serial, - "generation": None, "brand": cpu.get('Family'), "address": self.get_cpu_address(cpu), + "bogomips": self.get_bogomips(), } ) @@ -182,16 +181,26 @@ class ParseSnapshot: def sanitize(self, action): return [] + def get_bogomips(self): + if not self.hwinfo: + return self.default + + bogomips = 0 + for row in self.hwinfo: + for cel in row: + if 'BogoMips' in cel: + try: + bogomips += float(cel.split(":")[-1]) + except: + pass + return bogomips + def get_networks(self): networks = [] get_lshw_child(self.lshw, networks, 'network') for c in networks: capacity = c.get('capacity') - units = c.get('units') - speed = None - if capacity and units: - speed = unit.Quantity(capacity, units).to('Mbit/s').m wireless = bool(c.get('configuration', {}).get('wireless', False)) self.components.append( { @@ -200,9 +209,10 @@ class ParseSnapshot: "model": c.get('product'), "manufacturer": c.get('vendor'), "serialNumber": c.get('serial'), - "speed": speed, + "speed": capacity, "variant": c.get('version', 1), - "wireless": wireless, + "wireless": wireless or False, + "integrated": "PCI:0000:00" in c.get("businfo", ""), } ) @@ -300,7 +310,7 @@ class ParseSnapshot: if 'x' not in sizes: return 0 w, h = [int(x) for x in sizes.split('x')] - return np.sqrt(w**2 + h**2) * i + return "{:.2f}".format(np.sqrt(w**2 + h**2) * i) def get_cpu_address(self, cpu): default = 64 @@ -367,25 +377,16 @@ class ParseSnapshot: return slots def get_ram_size(self, ram): - try: - memory = ram.get("Size", "0") - memory = memory.split(' ') - if len(memory) > 1: - size = int(memory[0]) - units = memory[1] - return base2.Quantity(size, units).to('MiB').m - return int(size.split(" ")[0]) - except Exception as err: - logger.error("get_ram_size error: {}".format(err)) - return 0 + memory = ram.get("Size", "0") + return memory def get_ram_speed(self, ram): size = ram.get("Speed", "0") - return int(size.split(" ")[0]) + return size def get_cpu_speed(self, cpu): speed = cpu.get('Max Speed', "0") - return float(speed.split(" ")[0]) / 1024 + return speed def get_sku(self): return self.dmi.get("System")[0].get("SKU Number", self.default).strip() @@ -468,11 +469,7 @@ class ParseSnapshot: self.errors("{}".format(err)) def get_data_storage_size(self, x): - total_capacity = x.get('user_capacity', {}).get('bytes') - if not total_capacity: - return 1 - # convert bytes to Mb - return total_capacity / 1024**2 + return x.get('user_capacity', {}).get('bytes') def parse_hwinfo(self): hw_blocks = self.hwinfo_raw.split("\n\n") @@ -480,7 +477,11 @@ class ParseSnapshot: def loads(self, x): if isinstance(x, str): - return json.loads(x) + try: + return json.loads(x) + except Exception as ss: + print("WARNING!! {}".format(ss)) + return {} return x def errors(self, txt=None):