Compare commits

...

3 commits

Author SHA1 Message Date
pedro 6ba62b3cea remove requests dependency, use vanilla urllib 2024-10-15 13:32:36 +02:00
pedro 8c9b7983fe add lspci command
which was required by legacy server
2024-10-10 19:10:46 +02:00
pedro 3f4a23645d allow more response codes 2024-10-10 19:09:44 +02:00

View file

@ -6,9 +6,7 @@ import uuid
import hashlib import hashlib
import argparse import argparse
import configparser import configparser
import urllib.request
import requests
from datetime import datetime from datetime import datetime
@ -249,11 +247,13 @@ def get_data(all_disks):
lshw = 'sudo lshw -json' lshw = 'sudo lshw -json'
hwinfo = 'sudo hwinfo --reallyall' hwinfo = 'sudo hwinfo --reallyall'
dmidecode = 'sudo dmidecode' dmidecode = 'sudo dmidecode'
lspci = 'sudo lspci -vv'
data = { data = {
'lshw': json.loads(exec_cmd(lshw) or "{}"), 'lshw': json.loads(exec_cmd(lshw) or "{}"),
'disks': smartctl(all_disks), 'disks': smartctl(all_disks),
'hwinfo': exec_cmd(hwinfo), 'hwinfo': exec_cmd(hwinfo),
'dmidecode': exec_cmd(dmidecode) 'dmidecode': exec_cmd(dmidecode),
'lspci': exec_cmd(lspci)
} }
return data return data
@ -302,25 +302,32 @@ def send_snapshot_to_devicehub(snapshot, token, url):
"Content-Type": "application/json" "Content-Type": "application/json"
} }
try: try:
response = requests.post(url, data=json.dumps(snapshot), headers=headers) data = json.dumps(snapshot).encode('utf-8')
if response.status_code == 200: request = urllib.request.Request(url, data=data, headers=headers)
with urllib.request.urlopen(request) as response:
status_code = response.getcode()
response_text = response.read().decode('utf-8')
if 200 <= status_code < 300:
print(f"workbench: INFO: Snapshot successfully sent to '{url}'") print(f"workbench: INFO: Snapshot successfully sent to '{url}'")
else: else:
txt = "workbench: ERROR: Failed to send snapshot. HTTP {}: {}".format( txt = "workbench: ERROR: Failed to send snapshot. HTTP {}: {}".format(
response.status_code, status_code,
response.text response_text
) )
raise Exception(txt) raise Exception(txt)
try: try:
res = json.loads(response.text) response = json.loads(response_text)
if res.get('public_url'): if response.get('public_url'):
qr = "echo {} | qrencode -t ANSI".format(res['public_url']) # apt install qrencode
qr = "echo {} | qrencode -t ANSI".format(response['public_url'])
print(exec_cmd(qr)) print(exec_cmd(qr))
if res.get("dhid"): print("public_url: {}".format(response['public_url']))
print(res['dhid']) if response.get("dhid"):
print("dhid: {}".format(response['dhid']))
except Exception: except Exception:
print(response.text) print(response_text)
except Exception as e: except Exception as e:
print(f"workbench: ERROR: Snapshot not remotely sent. URL '{url}' is unreachable. Do you have internet? Is your server up & running?\n {e}") print(f"workbench: ERROR: Snapshot not remotely sent. URL '{url}' is unreachable. Do you have internet? Is your server up & running?\n {e}")