diff options
author | Jannik Schönartz | 2021-06-29 15:37:05 +0200 |
---|---|---|
committer | Jannik Schönartz | 2021-06-29 15:37:05 +0200 |
commit | c2680aafabc6c7abfca291dd47faf0a63f083450 (patch) | |
tree | 9b3fc949efca7b8dd7ed7929c4bc1e8d6129cd10 | |
parent | [bas-hw-collect] remove unnecessary printing (diff) | |
download | systemd-init-c2680aafabc6c7abfca291dd47faf0a63f083450.tar.gz systemd-init-c2680aafabc6c7abfca291dd47faf0a63f083450.tar.xz systemd-init-c2680aafabc6c7abfca291dd47faf0a63f083450.zip |
[bas-hw-collect] Add sfdisk and blkid information and group it with smartctl by drives
-rwxr-xr-x | modules.d/bas-hw-collect/scripts/collect_hw_info_json.py | 69 |
1 files changed, 51 insertions, 18 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 53050976..f9d6994e 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 @@ -52,26 +52,58 @@ def get_readlink(link): _readlink = run_subprocess('readlink -f ' + link) return _readlink +# Try parsing string to json +def parse_to_json(program_name, raw_string): + parsed = {} + if isinstance(raw_string, str): + try: + parsed = json.loads(raw_string) + except ValueError as e: + eprint(program_name + ' output couldn\'t be parsed') + eprint(e) + eprint('Output of ' + program_name + 'was:') + eprint(raw_string) + eprint() + return parsed + # Get smartctl output in json format -def get_smartctl(): +def get_smartctl(disk_path, disk_name): + disk_path_full = disk_path + disk_name + output = run_subprocess('smartctl -x --json ' + disk_path_full) + smartctl = parse_to_json('smartctl', output) + return smartctl + +# Get sfdisk info in json format +def get_sfdisk(disk_path, disk_name): + output = run_subprocess('sfdisk --json ' + disk_path + disk_name) + sfdisk = parse_to_json('sfdisk', output) + return sfdisk + +# Get lsblk info in json format +def get_lsblk(disk_path, disk_name): + output = run_subprocess('lsblk --json -b --output-all ' + disk_path + disk_name) + lsblk = parse_to_json('lsblk', output) + return lsblk + +# Get informations about the disks +def get_disk_info(): diskdir = '/dev/disk/by-path/' + # Get and filter all disks disks = listdir(diskdir) - filteredDisks = [i for i in disks if (not "-part" in i) and (not "-usb-" in i)] - smartctl = {} - for d in filteredDisks: - output = run_subprocess('smartctl -x --json ' + diskdir + d) - if isinstance(output, str): - try: - disk = json.loads(output) - disk['readlink'] = get_readlink(diskdir + d).rstrip() - smartctl[d] = disk - except ValueError as e: - eprint('smartctl failed with error:') - eprint(e) - eprint() - return [] - return smartctl + 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 + return disk_informations # Get and process "lspci -mn" output def get_lspci(): @@ -218,7 +250,7 @@ def main(): parser.add_argument('-l', '--location', action='store', help='<location>: Room-/Rackname where the client is located') parser.add_argument('-s', '--slot', action='store', help='<slot> The slot number (int) where the client is located in the rack') parser.add_argument('-b', '--bay', action='store', help='<bay> The bay number (int) where the client is located in the slot (segment)') - parser.add_argument('-c', '--contact', action='append', help='[multiple] <contact>: <firstname> <lastname> of the person responsible for this machine', nargs=2) + parser.add_argument('-c', '--contact', action='append', help='[multiple] <firstname> <lastname> of the person responsible for this machine', nargs=2) parser.add_argument('-n', '--name', action='store', help='<name>: Name of the client') parser.add_argument('-S', '--SERVER', action='store_true', help='Defines the type of the client to be a server') args = parser.parse_args() @@ -230,7 +262,8 @@ def main(): _collecthw = {} _collecthw['version'] = 2.0 _collecthw['dmidecode'] = get_dmidecode() - _collecthw['smartctl'] = get_smartctl() + #_collecthw['smartctl'] = get_smartctl() + _collecthw['drives'] = get_disk_info() _collecthw['lspci'] = get_lspci() _collecthw['ip'] = get_ip() _collecthw['edid'] = get_edid() |