#!/usr/bin/env python3 from optparse import OptionParser from requests.packages.urllib3.exceptions import InsecureRequestWarning import urllib3 from requests.auth import HTTPBasicAuth import requests from sys import exc_info import sys user = "" password = "" Ok = 0 Warning = 1 Critical = 2 Unknown = 3 requests.packages.urllib3.disable_warnings(InsecureRequestWarning) headers = { 'Content-type': 'application/json', } # Parse some Arguments parser = OptionParser() parser.add_option("-m", "--mode", dest="mode", help="Which check mode is in use (system-health, power,cpu,storage,temperature,fans,voltage,memory)", default="system-health") parser.add_option("-H", "--host", dest="host", help="Hostname or IP address of the host to check") (opts, args) = parser.parse_args() if opts.host == None: parser.error("Hostname (-H) is required.") def check_temperature(): response = requests.get(f"https://{opts.host}/redfish/v1/Chassis/1/Thermal", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r = response.json() status = 'OK' informacionFans = "" informacionCpus = "" fans=r['Fans'] cpus=r['Temperatures'] #rr['Status']['Health'] for fan in fans: informacionFans = informacionFans + (f"{fan['Name']} -> {fan['Status']['Health']}\n") if status != fan['Status']['Health'] and fan['Status']['Health'] != 'OK' and status != 'Critical': status = fan['Status']['Health'] for cpu in cpus: informacionCpus = informacionCpus + (f"{cpu['Name']} -> {cpu['Status']['State']} -> Temperature: {cpu['ReadingCelsius']} \n") if cpu['Status']['State'] == 'Enabled': enable = 'OK' else: enable = 'Warning' if status != enable and enable != 'OK' and status != 'Critical': status = enable datos=(f"-------Temperature - {status}-------\n{informacionFans}Temperature: {r['Status']['HealthRollup']}\n{informacionCpus}") output(datos, status) def check_storage(): response = requests.get(f"https://{opts.host}/redfish/v1/Systems/1/Storage", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r = response.json() response.close() #print(r['Members'][0]['@odata.id']) status='OK' informacionDiscos='' response1 = requests.get(f"https://{opts.host+r['Members'][0]['@odata.id']}", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r1 = response1.json() response1.close() if status != r1['Status']['Health']: status = r1['Status']['Health'] if status != r1['StorageControllers'][0]['Status']['Health'] and r1['StorageControllers'][0]['Status']['Health'] != 'OK' and status != 'Critical': status = r1['StorageControllers'][0]['Status']['Health'] for drive in r1['Drives']: response2 = requests.get(f"https://{opts.host+drive['@odata.id']}", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r2 = response2.json() response2.close() if status != r2['StatusIndicator'] and r2['StatusIndicator'] != 'OK' and status != 'Critical': status = r2['StatusIndicator'] if status != r2['Status']['Health'] and r2['Status']['Health'] != 'OK' and status != 'Critical': status = r2['Status']['Health'] if r1['Status']['State'] != 'Enabled' and status != 'Critical': status = 'Warning' informacionDiscos = informacionDiscos + (f"{r2['PhysicalLocation']['Info']} - SN: {r2['SerialNumber']} - Indicador: {r2['StatusIndicator']} - {r2['Status']['State']} - {r2['Status']['Health']}\n") output(f"-------Storage - {status}-------\nEstado General: {r1['Status']['Health']}\nStorageControllers --> {r1['StorageControllers'][0]['Status']['Health']}\n{informacionDiscos}", status) def check_memory(): response = requests.get(f"https://{opts.host}/redfish/v1/Systems/1/Memory", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r = response.json() response.close() status = 'OK' informacionMemorias='' for memoria in r['Members']: response1 = requests.get(f"https://{opts.host+memoria['@odata.id']}", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r1 = response1.json() response1.close() if r1['Status']['State'] != 'Absent': informacionMemorias = informacionMemorias + (f"{r1['Name']} - {r1['Status']['State']} - {r1['Status']['Health']}\n") if status != r1['Status']['Health'] and r1['Status']['Health'] != 'OK' and status != 'Critical': status = r1['Status']['Health'] if r1['Status']['State'] != 'Enabled' and status != 'Critical': status = 'Warning' output((f"-------Memory - {status}-------\n{r['Name']}\n{informacionMemorias}"),status) def check_power(): response = requests.get(f"https://{opts.host}/redfish/v1/Chassis/1/Power", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r = response.json() response.close() status='OK' informacionPower='' informacionPower='PowerControl -->\n' for power in r['PowerControl']: if status != power['Status']['Health'] and power['Status']['Health'] != 'OK' and status != 'Critical': status = power['Status']['Health'] if power['Status']['State'] != 'Enabled' and status != 'Critical': status = 'Warning' informacionPower = informacionPower + (f"{power['Name']} - {power['Status']['State']} - {power['Status']['Health']}\n") informacionPower = informacionPower + 'Voltages -->\n' for voltage in r['Voltages']: if voltage['Status']['State'] != 'Enabled' and status != 'Critical': status = 'Warning' informacionPower = informacionPower + (f"{voltage['Name']} - {voltage['Status']['State']}\n") informacionPower = informacionPower + 'Redundancy -->\n' for redundant in r['Redundancy']: if status != redundant['Status']['Health'] and redundant['Status']['Health'] != 'OK' and status != 'Critical': status = redundant['Status']['Health'] informacionPower = informacionPower + (f"{redundant['Name']} - {redundant['Status']['State']} - {redundant['Status']['Health']}\n") informacionPower = informacionPower + 'PowerSupplies -->\n' for powersupplies in r['PowerSupplies']: if powersupplies['Status']['State'] != 'Absent': if status != powersupplies['Status']['Health'] and powersupplies['Status']['Health'] != 'OK' and status != 'Critical': status = powersupplies['Status']['Health'] if powersupplies['Status']['State'] != 'Enabled' and status != 'Critical': status = 'Warning' informacionPower = informacionPower + (f"{powersupplies['Name']} - {powersupplies['Status']['State']} - {powersupplies['Status']['Health']}\n") output((f"-------Power - {status}-------\n{informacionPower}"),status) def check_system(): response = requests.get(f"https://{opts.host}/redfish/v1/Systems/1", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r = response.json() status = 'OK' if status != r['ProcessorSummary']['Status']['Health']: status = r['ProcessorSummary']['Status']['Health'] if status != r['MemorySummary']['Status']['Health'] and r['MemorySummary']['Status']['Health'] != 'OK' and status != 'Critical': status = r['MemorySummary']['Status']['Health'] if status != r['Status']['Health'] and r['Status']['Health'] != 'OK' and status != 'Critical': status = r['Status']['Health'] datos=(f"-------System - {status}-------\nSN: {r['SerialNumber']}\nProcessorSummary: {r['ProcessorSummary']['Status']['Health']}\nMemorySummary: {r['MemorySummary']['Status']['Health']}\nGeneral Alerts: {r['Status']['Health']}") output(datos, status) def check_network(): response = requests.get(f"https://{opts.host}/redfish/v1/Chassis/1/NetworkAdapters", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r = response.json() response.close() informacionNetworkPorts='' informacionNetworkDevice='' status = 'OK' networks=r['Members'] informacionNetworks=r['Name'] for network in networks: informacionNetworks = network['@odata.id'] response1 = requests.get(f"https://{opts.host+informacionNetworks}", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r1 = response1.json() response1.close() links = r1['Controllers'][0] if status != r1['Status']['Health']: status = r1['Status']['Health'] for networkPort in links['Links']['NetworkPorts']: response2 = requests.get(f"https://{opts.host+networkPort['@odata.id']}", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r2 = response2.json() response2.close() informacionNetworkPorts = informacionNetworkPorts + (f"{r2['Name']} - {r2['Status']['State']} - {r2['Status']['Health']}\n") if status != r2['Status']['Health'] and r2['Status']['Health'] != 'OK' and status != 'Critical': status = r2['Status']['Health'] if r2['Status']['State'] != 'Enabled' and status != 'Critical': status = 'Warning' for networkDevice in links['Links']['NetworkDeviceFunctions']: response3 = requests.get(f"https://{opts.host+networkDevice['@odata.id']}", headers=headers, verify=False, auth=HTTPBasicAuth(user, password)) r3 = response3.json() response3.close() informacionNetworkDevice = informacionNetworkDevice + (f"{r3['Name']} - {r3['Ethernet']['MACAddress']} - {r2['Status']['State']} - {r2['Status']['Health']}\n") if status != r3['Status']['Health'] and r3['Status']['Health'] != 'OK' and status != 'Critical': status = r3['Status']['Health'] if r3['Status']['State'] != 'Enabled' and status != 'Critical': status = 'Warning' output((f"-------Network - {status}-------\n{r['Name']} --> {informacionNetworks}\nAdaptador --> {r1['Name']} - {r1['Status']['Health']}\nNetworkPorts -->\n{informacionNetworkPorts}NetworkDeviceFunctions -->\n{informacionNetworkDevice}"),status) def output(info, status): if status == 'OK': status = Ok elif status == 'Warning': status = Warning elif status == 'Critical': status = Critical else: status = Unknown print(info) sys.exit(status) if __name__ == '__main__': try: if opts.mode == 'temperature': check_temperature() elif opts.mode == 'storage': check_storage() elif opts.mode == 'memory': check_memory() elif opts.mode == 'power': check_power() elif opts.mode == 'system': check_system() elif opts.mode == 'network': check_network() else: output(f"{opts.mode} is not a valid option for --mode", Unknown) except Exception as e: output(f"Error: {str(e)}", Unknown) # by Raúl Cebreiro