From f65b035b6b6121be680914f6ffb4db2960102860 Mon Sep 17 00:00:00 2001 From: Danny de Kooker Date: Sun, 17 Sep 2023 19:31:46 +0200 Subject: [PATCH] first attempt. not live yet --- Check_scripts/HPE - ILO SNMP monitor.py | 93 +++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Check_scripts/HPE - ILO SNMP monitor.py diff --git a/Check_scripts/HPE - ILO SNMP monitor.py b/Check_scripts/HPE - ILO SNMP monitor.py new file mode 100644 index 0000000..1177548 --- /dev/null +++ b/Check_scripts/HPE - ILO SNMP monitor.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +__license__ = "MIT" +__authors__ = "D.de Kooker " + +""" +This script is designed to monitor a HPE ilo using the python pysnmp module + +Returncodes: + 0 - All OK + 1 - There is an error message retrieved by the SNMP monitor + 2 - There is an warning message retrieved by the SNMP monitor + +Command line arguments: + python Check_scripts/HPE - ILO SNMP monitor.py {ILO IP} {SNMP Community string} + +Changelog: + 17-09-2023 - Initial creation of the script using openAI. Script doesn't work yet, returns errors + +""" + +import argparse +import importlib +import subprocess +import sys + +# Check if pysnmp is installed +try: + importlib.import_module('pysnmp') +except ImportError: + print("pysnmp is not installed. Installing...") + + # Specify the full path to pip based on the Python executable + python_exe = sys.executable + pip_cmd = f'"{python_exe}" -m pip' + + # Use subprocess to install pysnmp using pip + subprocess.run(f'{pip_cmd} install pysnmp', shell=True) + +from pysnmp.hlapi import * + +def snmp_query(target_ip, community_string, oids_to_query): + error_occurred = False # Track if any errors occurred + + for name, oid in oids_to_query.items(): + errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + CommunityData(community_string), + UdpTransportTarget((target_ip, 161), timeout=5, retries=5), + ContextData(), + ObjectType(ObjectIdentity(oid))) + ) + + if errorIndication: + error_occurred = True + print(f"Error querying {name}: {errorIndication}") + else: + for varBind in varBinds: + print(f"{name}: {varBind.prettyPrint()}") + + return error_occurred + +def main(): + parser = argparse.ArgumentParser(description='Monitor HPE server and iLO via SNMP.') + parser.add_argument('target_ip', help='iLO IP address') + parser.add_argument('community_string', help='SNMP community string') + + args = parser.parse_args() + + hardware_and_health = { + 'System Model': '1.3.6.1.2.1.1.1.0', + 'Serial Number': '1.3.6.1.4.1.232.2.2.2.1.9.1.3.2.0', + 'Asset Tag': '1.3.6.1.4.1.232.2.2.2.1.9.1.3.3.0', + # 'CPU Usage': '1.3.6.1.4.1.232.11.2.4.1.0', + # 'Memory Usage': '1.3.6.1.4.1.232.11.2.11.1.0', + 'Network Interfaces': '1.3.6.1.2.1.2.2.1', + 'Storage': '1.3.6.1.2.1.25.2', + 'Fans': '1.3.6.1.4.1.232.6.2.9.3.6.0', + 'Temperature': '1.3.6.1.4.1.232.6.2.9.2.6.0', + 'Power Supplies': '1.3.6.1.4.1.232.6.2.9.4.6.0', + 'Memory Errors': '1.3.6.1.4.1.232.6.2.6.8.1.0', + 'Drive Predictive Failures': '1.3.6.1.4.1.232.3.2.3.1.1.7.0', + } + + error_occurred = snmp_query(args.target_ip, args.community_string, hardware_and_health) + + if error_occurred: + exit(1) # Exit with code 1 (indicating error) + else: + exit(0) # Exit with code 0 (indicating success) + +if __name__ == '__main__': + main()