summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJannik Schönartz2021-06-22 18:01:37 +0200
committerJannik Schönartz2021-06-22 18:01:37 +0200
commit506b976a3c9ec93043aeee7ae65a52f19ee34897 (patch)
tree5cf8fb170ce72e0c1d53c8e08fbfacb661e4c335
parent[bas-python] Make name field optional instead of null (diff)
downloadsystemd-init-506b976a3c9ec93043aeee7ae65a52f19ee34897.tar.gz
systemd-init-506b976a3c9ec93043aeee7ae65a52f19ee34897.tar.xz
systemd-init-506b976a3c9ec93043aeee7ae65a52f19ee34897.zip
[bas-python] Printing in stderr and cleanup
-rwxr-xr-xmodules.d/bas-python/scripts/collect_hw_info_json.py102
1 files changed, 53 insertions, 49 deletions
diff --git a/modules.d/bas-python/scripts/collect_hw_info_json.py b/modules.d/bas-python/scripts/collect_hw_info_json.py
index 23159853..14e3877a 100755
--- a/modules.d/bas-python/scripts/collect_hw_info_json.py
+++ b/modules.d/bas-python/scripts/collect_hw_info_json.py
@@ -1,12 +1,12 @@
from dmiparser import DmiParser
-import json
-import subprocess
from subprocess import PIPE
-import shlex
-import sys
-import argparse
from os import listdir
+import argparse
+import json
import requests
+import shlex
+import subprocess
+import sys
__debug = False
@@ -18,18 +18,18 @@ def run_subprocess(_cmd):
stderr = proc.stderr
if __debug:
- print(_cmd + ':')
- print()
- print('Errors:')
- print(stderr.decode())
- print()
+ eprint(_cmd + ':')
+ eprint()
+ eprint('Errors:')
+ eprint(stderr.decode())
+ eprint()
# stderr len instead of proc.returncode > 0 is used because some have returncode 2 but are still valid
if len(stderr.decode()) > 0:
- print(_cmd + ' Errors:')
- print(stderr.decode())
+ eprint(_cmd + ' Errors:')
+ eprint(stderr.decode())
if proc.returncode != 0:
- print('Error Return Code: ' + str(proc.returncode))
- print()
+ eprint('Error Return Code: ' + str(proc.returncode))
+ eprint()
return False
else:
return stdout.decode()
@@ -67,9 +67,9 @@ def get_smartctl():
disk['readlink'] = get_readlink(diskdir + d).rstrip()
smartctl[d] = disk
except ValueError as e:
- print('smartctl failed with error:')
- print(e)
- print()
+ eprint('smartctl failed with error:')
+ eprint(e)
+ eprint()
return []
return smartctl
@@ -94,15 +94,14 @@ def get_lspci():
# Prepare values positions are in order
if len(values) >= 6:
- lspci_parsed['slot'] = values[0]
+ lspci_parsed['slot'] = values[0]
# The busybox version of lspci has "Class <class>" instead of "<class>"
- lspci_parsed['class'] = values[1].replace("Class ", "")
-
- lspci_parsed['vendor'] = values[2]
- lspci_parsed['device'] = values[3]
- lspci_parsed['subsystem_vendor'] = values[4]
- lspci_parsed['subsystem'] = values[5]
+ lspci_parsed['class'] = values[1].replace("Class ", "")
+ lspci_parsed['vendor'] = values[2]
+ lspci_parsed['device'] = values[3]
+ lspci_parsed['subsystem_vendor'] = values[4]
+ lspci_parsed['subsystem'] = values[5]
# Prepare arguments
if len(arguments) > 0:
@@ -176,16 +175,14 @@ def get_lshw():
result = json.loads(lshw_raw)
return result
-def prepare_location(parent, rack, bay, slot):
+def prepare_location(parent, bay, slot):
location = {}
if parent:
- location['parent'] = parent
- if rack:
- location['rack'] = rack
+ location['parent'] = parent
if bay:
- location['bay'] = bay
+ location['bay'] = int(bay)
if slot:
- location['slot'] = slot
+ location['slot'] = int(slot)
return location
def prepare_contacts(contact_list):
@@ -204,23 +201,27 @@ def send_post(url, payload):
# headers = { 'Content-type': 'application/json', 'Accept': 'text/plain' }
# req = requests.post(url, json=payload, headers=headers)
req = requests.post(url, json=payload)
+ # Print the response
print("POST-Request Response: \n")
print(req.text)
+def eprint(*args, **kwargs):
+ print(*args, file=sys.stderr, **kwargs)
+
def main():
global __debug
# Create and parse arguments
parser = argparse.ArgumentParser(description='Collects hardware data from different tools and returns it as json.')
- parser.add_argument('-d', '--debug', action='store_true', help='Prints all STDERR messages. (Non critical included)')
- parser.add_argument('-u', '--url', action='append', help='If given, a post request with the generated JSON is sent to the given URL')
- parser.add_argument('-p', '--print', action='store_true', help='Prints the generated JSON')
- parser.add_argument('-r', '--rack', action='store', help='<rack>')
- parser.add_argument('-s', '--slot', action='store', help='<slot>')
- parser.add_argument('-b', '--bay', action='store', help='<bay>')
- parser.add_argument('-l', '--location', action='store', help='<location>: Roomname where the client is located')
- parser.add_argument('-c', '--contact', action='append', help='<contact>: <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('-d', '--debug', action='store_true', help='Prints all STDERR messages. (Non critical included)')
+ parser.add_argument('-u', '--url', action='append', help='[multiple] If given, a post request with the generated JSON is sent to the given URLs')
+ parser.add_argument('-p', '--print', action='store_true', help='Prints the generated JSON')
+ 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('-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()
if args.debug:
@@ -228,24 +229,27 @@ def main():
# Run the tools
_collecthw = {}
- _collecthw['version'] = 2.0
+ _collecthw['version'] = 2.0
_collecthw['dmidecode'] = get_dmidecode()
- _collecthw['smartctl'] = get_smartctl()
- _collecthw['lspci'] = get_lspci()
- _collecthw['ip'] = get_ip()
- _collecthw['edid'] = get_edid()
- _collecthw['lshw'] = get_lshw()
- _collecthw['net'] = get_net_fallback()
- _collecthw['location'] = prepare_location(args.location, args.rack, args.bay, args.slot)
- _collecthw['contacts'] = prepare_contacts(args.contact)
+ _collecthw['smartctl'] = get_smartctl()
+ _collecthw['lspci'] = get_lspci()
+ _collecthw['ip'] = get_ip()
+ _collecthw['edid'] = get_edid()
+ _collecthw['lshw'] = get_lshw()
+ _collecthw['net'] = get_net_fallback()
+ _collecthw['location'] = prepare_location(args.location, args.bay, args.slot)
+ _collecthw['contacts'] = prepare_contacts(args.contact)
if args.name:
_collecthw['name'] = args.name
+ if args.SERVER:
+ _collecthw['type'] = 'SERVER'
+
collecthw_json = json.dumps(_collecthw)
if args.url:
for url in args.url:
- send_post(args.url, _collecthw)
+ send_post(url, _collecthw)
# Print out the final json
if args.print: