From b59fb50a965f82a96fbf4f3622c9cd36b2a85f38 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Fri, 24 Sep 2021 20:13:07 +0200 Subject: [bas-hw-collect] Add some more checks for existing paths --- .../bas-hw-collect/scripts/collect_hw_info_json.py | 77 ++++++++++++---------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/modules.d/bas-hw-collect/scripts/collect_hw_info_json.py b/modules.d/bas-hw-collect/scripts/collect_hw_info_json.py index 6512394c..18326b2b 100755 --- a/modules.d/bas-hw-collect/scripts/collect_hw_info_json.py +++ b/modules.d/bas-hw-collect/scripts/collect_hw_info_json.py @@ -1,6 +1,6 @@ from dmiparser import DmiParser from subprocess import PIPE -from os import listdir +from os import listdir, path import argparse import json import requests @@ -88,21 +88,22 @@ def get_lsblk(disk_path, disk_name): # Get informations about the disks def get_disk_info(): diskdir = '/dev/disk/by-path/' + disk_informations = {} # Get and filter all disks - disks = listdir(diskdir) - filtered_disks = [i for i in disks if (not "-part" in i) and (not "-usb-" in i)] - - # Call all disk specific tools - disk_informations = {} - for d in filtered_disks: - disk_path = diskdir + d - disk_info = {} - disk_info['readlink'] = get_readlink(disk_path).rstrip() - disk_info['smartctl'] = get_smartctl(diskdir, d) - disk_info['sfdisk'] = get_sfdisk(diskdir, d) - disk_info['lsblk'] = get_lsblk(diskdir, d) - disk_informations[d] = disk_info + if path.exists(diskdir): + disks = listdir(diskdir) + filtered_disks = [i for i in disks if (not "-part" in i) and (not "-usb-" in i)] + + # Call all disk specific tools + for d in filtered_disks: + disk_path = diskdir + d + disk_info = {} + disk_info['readlink'] = get_readlink(disk_path).rstrip() + disk_info['smartctl'] = get_smartctl(diskdir, d) + disk_info['sfdisk'] = get_sfdisk(diskdir, d) + disk_info['lsblk'] = get_lsblk(diskdir, d) + disk_informations[d] = disk_info return disk_informations # Get and process "lspci -mn" output @@ -172,19 +173,21 @@ def get_ip(): return result def get_net_fallback(): + netdir = '/sys/class/net' result = {} # Get MAC address - interfaces = run_subprocess('ls /sys/class/net').split('\n') - for interface in interfaces: - # Skip local stuff - if interface == 'lo' or interface == '': - continue - net = {} - net['mac'] = run_subprocess('cat /sys/class/net/' + interface + '/address') - if net['mac'].endswith('\n'): - net['mac'] = net['mac'][:-1] - result[interface] = net + if path.exists(netdir): + interfaces = run_subprocess('ls ' + netdir).split('\n') + for interface in interfaces: + # Skip local stuff + if interface == 'lo' or interface == '': + continue + net = {} + net['mac'] = run_subprocess('cat /sys/class/net/' + interface + '/address') + if net['mac'].endswith('\n'): + net['mac'] = net['mac'][:-1] + result[interface] = net # Get IP address interfaces = run_subprocess('ip -o addr show | awk \'/inet/ {print $2, $3, $4}\'').split('\n') @@ -195,6 +198,8 @@ def get_net_fallback(): if interf[0] == 'lo': continue else: + if interf[0] not in result: + result[interf[0]] = {} if interf[1] == 'inet': result[interf[0]]['ipv4'] = interf[2] elif interf[1] == 'inet6': @@ -207,14 +212,12 @@ def get_edid(): display_paths = run_subprocess('ls /sys/class/drm/*/edid') if display_paths: display_paths = display_paths.split('\n') - else: - return edid - for dp in display_paths: - if dp == '': continue - edid_hex = open(dp, 'rb').read().hex() - if len(edid_hex) > 0: - # The path is always /sys/class/drm/[..]/edid, so cut the first 15 chars and the last 5 chars - edid[dp[15:-5]] = edid_hex + for dp in display_paths: + if dp == '': continue + edid_hex = open(dp, 'rb').read().hex() + if len(edid_hex) > 0: + # The path is always /sys/class/drm/[..]/edid, so cut the first 15 chars and the last 5 chars + edid[dp[15:-5]] = edid_hex return edid def get_lshw(): @@ -225,8 +228,14 @@ def get_lshw(): return result def get_uuid(): - uuid = run_subprocess('cat /sys/class/dmi/id/product_uuid') - return uuid.rstrip() + uuid_path = '/sys/class/dmi/id/product_uuid' + uuid = 'N/A' + if path.exists(uuid_path): + uuid_raw = run_subprocess('cat ' + uuid_path) + # uuid_raw = False if no sudo permission: + if uuid_raw: + uuid = uuid_raw.rstrip() + return uuid def prepare_location(parent, bay, slot): location = {} -- cgit v1.2.3-55-g7522