refs #1678 add interfaceAdm/InventarioHardware.py
parent
2d0c77b073
commit
e89143bab2
|
@ -1,33 +1,25 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/python3
|
||||||
import subprocess
|
# Script de interfaz para guardar en un fichero el inventario de hardware de un cliente.
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import subprocess
|
||||||
|
|
||||||
def main(output_file):
|
import ogGlobals
|
||||||
# Ejecutar el comando `listHardwareInfo.py` y capturar el resultado
|
from SystemLib import ogRaiseError
|
||||||
try:
|
|
||||||
print(f"------------------------------------------ loading listHardwareInfo.py")
|
|
||||||
result = subprocess.run(
|
|
||||||
["python3", "/opt/opengnsys/scripts/listHardwareInfo.py"],
|
|
||||||
capture_output=True, text=True, check=True
|
|
||||||
)
|
|
||||||
output_lines = result.stdout.strip().split('\n')
|
|
||||||
file_path = output_lines[-1] # Obtener la última línea como la ruta del archivo de salida
|
|
||||||
print(f"------------------------------------------ archivo:{file_path}")
|
|
||||||
|
|
||||||
# Leer desde la segunda línea del archivo y escribir en el archivo de salida especificado
|
|
||||||
with open(file_path, 'r') as input_file, open(output_file, 'w') as output:
|
|
||||||
lines = input_file.readlines()[1:] # Saltar la primera línea
|
|
||||||
output.writelines(lines)
|
|
||||||
|
|
||||||
except subprocess.CalledProcessError as e:
|
prog = sys.argv[0]
|
||||||
print("Error ejecutando listHardwareInfo.py:", e.stderr, file=sys.stderr)
|
if len (sys.argv) != 2:
|
||||||
sys.exit(e.returncode)
|
print (f'Usage: {prog} <output_file>')
|
||||||
except FileNotFoundError as e:
|
sys.exit (1)
|
||||||
print(f"Archivo no encontrado: {e.filename}", file=sys.stderr)
|
output_file = sys.argv[1]
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
listhi_out = subprocess.run ( [f'{ogGlobals.OGSCRIPTS}/listHardwareInfo.py'], capture_output=True, text=True).stdout
|
||||||
if len(sys.argv) != 2:
|
if listhi_out:
|
||||||
print("Uso: python3 InventarioHardware.py <archivo_salida>")
|
inventory_path = listhi_out.splitlines()[0]
|
||||||
sys.exit(1)
|
else:
|
||||||
main(sys.argv[1])
|
ogRaiseError ([], ogGlobals.OG_ERR_GENERIC, 'listHardwareInfo.py failed')
|
||||||
|
sys.exit (1)
|
||||||
|
|
||||||
|
with open (inventory_path, 'r') as fdin, open (output_file, 'w') as fdout:
|
||||||
|
lines = fdin.readlines()[1:] # 'tail -n +2'
|
||||||
|
fdout.writelines (lines)
|
||||||
|
|
|
@ -115,7 +115,6 @@ def ogIsEfiActive():
|
||||||
def ogListHardwareInfo():
|
def ogListHardwareInfo():
|
||||||
ret = ''
|
ret = ''
|
||||||
|
|
||||||
SystemLib.ogEcho ([], 'info', ogGlobals.lang.MSG_HARDWAREINVENTORY)
|
|
||||||
# Ejecutar dmidecode y obtener tipo de chasis
|
# Ejecutar dmidecode y obtener tipo de chasis
|
||||||
dmi_out = subprocess.run (['dmidecode', '-s', 'chassis-type'], capture_output=True, text=True).stdout
|
dmi_out = subprocess.run (['dmidecode', '-s', 'chassis-type'], capture_output=True, text=True).stdout
|
||||||
dmi_out = '\n'.join ([ x for x in dmi_out.splitlines() if 'Other' not in x ])
|
dmi_out = '\n'.join ([ x for x in dmi_out.splitlines() if 'Other' not in x ])
|
||||||
|
|
|
@ -1,43 +1,28 @@
|
||||||
import subprocess
|
#!/usr/bin/python3
|
||||||
|
# Scirpt de ejemplo para almacenear en fichero temporal el listado de hardware.
|
||||||
|
# Nota: se usa como base para el programa de recogida de listado de hardware de OpenGnsys Admin.
|
||||||
|
# Formato: listHardwareInfo
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
sys.path.append('/opt/opengnsys/lib/engine/bin')
|
import subprocess
|
||||||
|
|
||||||
|
import ogGlobals
|
||||||
|
from SystemLib import ogRaiseError
|
||||||
from NetLib import ogGetIpAddress
|
from NetLib import ogGetIpAddress
|
||||||
sys.path.append('/opt/opengnsys/lib/engine/bin')
|
|
||||||
from InventoryLib import ogListHardwareInfo
|
from InventoryLib import ogListHardwareInfo
|
||||||
|
|
||||||
def get_server_log_dir():
|
prog = sys.argv[0]
|
||||||
# Obtener el directorio de logs del servidor
|
if len (sys.argv) > 1:
|
||||||
oglog = os.getenv("OGLOG")
|
ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f'{ogGlobals.lang.MSG_FORMAT} {prog}')
|
||||||
if not oglog:
|
sys.exit (1)
|
||||||
return ""
|
|
||||||
result = subprocess.run(["mount"], capture_output=True, text=True)
|
|
||||||
for line in result.stdout.splitlines():
|
|
||||||
parts = line.split()
|
|
||||||
if len(parts) >= 4 and parts[3] == oglog:
|
|
||||||
return parts[1]
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def list_hardware_info():
|
# Directorio del servidor donde se exportan los ficheros de registro.
|
||||||
oglog = os.getenv("OGLOG", "/tmp") # Usar /tmp como valor por defecto para OGLOG
|
#SERVERLOGDIR = unused
|
||||||
ip_address = ogGetIpAddress()
|
|
||||||
# Fichero de listado de hardware basado en la IP obtenida
|
|
||||||
hardfile = f"hard-{ogGetIpAddress()}"
|
|
||||||
print(f"hardfile:{hardfile}")
|
|
||||||
# Ejecutar ogListHardwareInfo y redirigir al archivo de listado
|
|
||||||
hardware_info_path = os.path.join(oglog, hardfile)
|
|
||||||
with open(hardware_info_path, 'w') as output_file:
|
|
||||||
output_file.write(ogListHardwareInfo())
|
|
||||||
|
|
||||||
return hardware_info_path
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# Obtener el directorio del servidor donde se exportan los ficheros de registro
|
|
||||||
server_log_dir = get_server_log_dir()
|
|
||||||
|
|
||||||
# Generar el archivo de listado de hardware y obtener su ruta
|
|
||||||
hardware_info_path = list_hardware_info()
|
|
||||||
|
|
||||||
# Imprimir la ruta del archivo generado
|
|
||||||
print(hardware_info_path)
|
|
||||||
|
|
||||||
|
# Fichero de listado: hard-IP
|
||||||
|
HARDFILE = f'{ogGlobals.OGLOG}/hard-{ogGetIpAddress()}'
|
||||||
|
out = ogListHardwareInfo()
|
||||||
|
with open (HARDFILE, 'w') as fd:
|
||||||
|
fd.write (out)
|
||||||
|
print (HARDFILE)
|
||||||
|
|
Loading…
Reference in New Issue