refs #1428 add ogListHardwareInfo
parent
2744273b82
commit
c6735b15ee
|
@ -103,41 +103,6 @@ def ogGetSerialNumber():
|
||||||
def ogIsEfiActive():
|
def ogIsEfiActive():
|
||||||
return os.path.isdir("/sys/firmware/efi")
|
return os.path.isdir("/sys/firmware/efi")
|
||||||
|
|
||||||
def parse_lshw_output():
|
|
||||||
try:
|
|
||||||
# Ejecutar lshw en formato JSON para un fácil procesamiento
|
|
||||||
lshw_output = subprocess.check_output(["lshw", "-json"], text=True)
|
|
||||||
lshw_data = json.loads(lshw_output) # Convertir la salida JSON a un diccionario
|
|
||||||
|
|
||||||
# Extraer información relevante en el formato clave=valor
|
|
||||||
parsed_output = []
|
|
||||||
|
|
||||||
# Ejemplo de datos clave que podríamos extraer
|
|
||||||
if "product" in lshw_data:
|
|
||||||
parsed_output.append(f"product={lshw_data['product']}")
|
|
||||||
|
|
||||||
if "vendor" in lshw_data:
|
|
||||||
parsed_output.append(f"vendor={lshw_data['vendor']}")
|
|
||||||
|
|
||||||
if "configuration" in lshw_data and "memory" in lshw_data["configuration"]:
|
|
||||||
parsed_output.append(f"memory={lshw_data['configuration']['memory']}")
|
|
||||||
|
|
||||||
# Recorrer los dispositivos para obtener información de CPU, almacenamiento, etc.
|
|
||||||
for item in lshw_data.get("children", []):
|
|
||||||
if item["class"] == "processor":
|
|
||||||
parsed_output.append(f"cpu={item.get('product', 'Unknown')}")
|
|
||||||
elif item["class"] == "memory" and "size" in item:
|
|
||||||
parsed_output.append(f"total_memory={item['size']}")
|
|
||||||
elif item["class"] == "disk":
|
|
||||||
parsed_output.append(f"disk={item.get('product', 'Unknown')}")
|
|
||||||
|
|
||||||
# Devolver los datos combinados
|
|
||||||
return "\n".join(parsed_output)
|
|
||||||
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
print(f"Error al ejecutar lshw: {e}")
|
|
||||||
return "Error al obtener información de hardware"
|
|
||||||
|
|
||||||
|
|
||||||
#/**
|
#/**
|
||||||
# ogListHardwareInfo
|
# ogListHardwareInfo
|
||||||
|
@ -148,21 +113,68 @@ def parse_lshw_output():
|
||||||
#@note Requisitos: dmidecode, lshw, awk
|
#@note Requisitos: dmidecode, lshw, awk
|
||||||
#*/ ##
|
#*/ ##
|
||||||
def ogListHardwareInfo():
|
def ogListHardwareInfo():
|
||||||
|
ret = ''
|
||||||
|
|
||||||
|
SystemLib.ogEcho ([], 'info', ogGlobals.lang.MSG_HARDWAREINVENTORY)
|
||||||
# Ejecutar dmidecode y obtener tipo de chasis
|
# Ejecutar dmidecode y obtener tipo de chasis
|
||||||
try:
|
dmi_out = subprocess.run (['dmidecode', '-s', 'chassis-type'], capture_output=True, text=True).stdout
|
||||||
output = subprocess.check_output('echo "cha=$(dmidecode -s chassis-type)" | grep -v "Other"', shell=True).decode().strip()
|
dmi_out = '\n'.join ([ x for x in dmi_out.splitlines() if 'Other' not in x ])
|
||||||
except subprocess.CalledProcessError:
|
ret += f'cha={dmi_out}\n'
|
||||||
output = "cha=Unknown"
|
|
||||||
|
|
||||||
# Detectar BIOS o UEFI
|
if os.path.exists ('/sys/firmware/efi'):
|
||||||
firmware = "boo=UEFI" if os.path.isdir("/sys/firmware/efi") else "boo=BIOS"
|
ret += f'boo=UEFI\n'
|
||||||
print(firmware)
|
else:
|
||||||
|
ret += f'boo=BIOS\n'
|
||||||
|
|
||||||
# Ejecutar y analizar lshw
|
awk_script = r'''
|
||||||
lshw_output = parse_lshw_output()
|
BEGIN {type="mod";}
|
||||||
|
/product:/ {sub(/ *product: */,""); prod=$0;}
|
||||||
|
/vendor:/ {sub(/ *vendor: */,""); vend=$0;}
|
||||||
|
/version:/ {sub(/ *version: */,"v.");vers=$0;}
|
||||||
|
/size:/ {size=$2;}
|
||||||
|
/clock:/ {clock=$2;}
|
||||||
|
/slot:/ {sub(/ *slot: */,""); slot=$0;}
|
||||||
|
/\*-/ {if (type=="mem"){
|
||||||
|
if (size!=""){
|
||||||
|
numbank++;
|
||||||
|
print type"="vend,prod,size,clock" ("slot")";}
|
||||||
|
}else{
|
||||||
|
if (type=="totalmem"){
|
||||||
|
if (size!=""){
|
||||||
|
totalmemory="mem="size;}
|
||||||
|
}else{
|
||||||
|
if (type!="" && prod!=""){
|
||||||
|
if (prod=="v."vers)
|
||||||
|
vers="";
|
||||||
|
print type"="vend,prod,size,vers;} }
|
||||||
|
}
|
||||||
|
type=prod=vend=vers=size=clock=slot="";}
|
||||||
|
$1~/-core/ {type="boa";}
|
||||||
|
$1~/-firmware/ {type="bio";}
|
||||||
|
$1~/-cpu/ {type="cpu";}
|
||||||
|
$1~/-bank/ {type="mem";}
|
||||||
|
$1~/-memory/ {type="totalmem";}
|
||||||
|
$1~/-ide/ {type="ide";}
|
||||||
|
$1~/-storage/ {type="sto";}
|
||||||
|
$1~/-disk/ {type="dis";}
|
||||||
|
$1~/-cdrom/ {type="cdr";}
|
||||||
|
$1~/-display/ {type="vga";}
|
||||||
|
$1~/-network/ {type="net";}
|
||||||
|
$1~/-multimedia/ {type="mul";}
|
||||||
|
$1~/-usb/ {type="usb";}
|
||||||
|
$1~/-firewire/ {type="fir";}
|
||||||
|
$1~/-serial/ {type="bus";}
|
||||||
|
END {if (type!="" && prod!="")
|
||||||
|
print type"="vend,prod,size,vers;
|
||||||
|
if (length(numbank)==0 && length(totalmemory)>=4)
|
||||||
|
print totalmemory; }
|
||||||
|
'''
|
||||||
|
lshw_out = subprocess.run (['lshw'], capture_output=True, text=True).stdout
|
||||||
|
awk_out = subprocess.run (['awk', awk_script], input=lshw_out, capture_output=True, text=True).stdout
|
||||||
|
|
||||||
# Combina y devuelve los resultados
|
ret += awk_out
|
||||||
return f"{output}\n{firmware}\n{lshw_output}"
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
#/**
|
#/**
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from SystemLib import ogHelp
|
||||||
|
from InventoryLib import ogListHardwareInfo
|
||||||
|
|
||||||
|
if 2 == len (sys.argv) and 'help' == sys.argv[1]:
|
||||||
|
#parser.print_help() sale en inglés aunque la locale indique otra cosa
|
||||||
|
ogHelp ('ogListHardwareInfo', 'ogListHardwareInfo', ['ogListHardwareInfo'])
|
||||||
|
sys.exit (0)
|
||||||
|
|
||||||
|
ret = ogListHardwareInfo()
|
||||||
|
|
||||||
|
if ret is not None:
|
||||||
|
if ret == True: sys.exit (0)
|
||||||
|
elif ret == False: sys.exit (1)
|
||||||
|
else: print (ret)
|
Loading…
Reference in New Issue