import subprocess def snmp_get(ip, community, oid): command = f"snmpget -v2c -O v -O q -c {community} {ip} {oid}" result = subprocess.run(command, shell=True, capture_output=True, text=True) return result.stdout.strip().strip('"') def interpret_flags(state_string): if state_string == "UNKNOWN": return "NMC имеет внутреннюю проблему коммуникации с ИБП." flags_description = { 1: "Abnormal Condition Present", 2: "On Battery", 3: "Low Battery", 4: "On Line", 5: "Replace Battery", 6: "Serial Communication Established", 7: "AVR Boost Active", 8: "AVR Trim Active", 9: "Overload", 10: "Runtime Calibration", 11: "Batteries Discharged", 12: "Manual Bypass", 13: "Software Bypass", 14: "In Bypass due to Internal Fault", 15: "In Bypass due to Supply Failure", 16: "In Bypass due to Fan Failure", 17: "Sleeping on a Timer", 18: "Sleeping until Utility Power Returns", 19: "On", 20: "Rebooting", 21: "Battery Communication Lost", 22: "Graceful Shutdown Initiated", 23: "Smart Boost or Smart Trim Fault", 24: "Bad Output Voltage", 25: "Battery Charger Failure", 26: "High Battery Temperature", 27: "Warning Battery Temperature", 28: "Critical Battery Temperature", 29: "Self Test In Progress", 30: "Low Battery / On Battery", 31: "Graceful Shutdown Issued by Upstream Device", 32: "Graceful Shutdown Issued by Downstream Device", 33: "No Batteries Attached", 34: "Synchronized Command is in Progress", 35: "Synchronized Sleeping Command is in Progress", 36: "Synchronized Rebooting Command is in Progress", 37: "Inverter DC Imbalance", 38: "Transfer Relay Failure", 39: "Shutdown or Unable to Transfer", 40: "Low Battery Shutdown", 41: "Electronic Unit Fan Failure", 42: "Main Relay Failure", 43: "Bypass Relay Failure", 44: "Temporary Bypass", 45: "High Internal Temperature", 46: "Battery Temperature Sensor Fault", 47: "Input Out of Range for Bypass", 48: "DC Bus Overvoltage", 49: "PFC Failure", 50: "Critical Hardware Fault", 51: "Green Mode/ECO Mode", 52: "Hot Standby", 53: "Emergency Power Off (EPO) Activated", 54: "Load Alarm Violation", 55: "Bypass Phase Fault", 56: "UPS Internal Communication Failure", 57: "Efficiency Booster Mode", 58: "Off", 59: "Standby", 60: "Minor or Environment Alarm", } positions = [pos + 1 for pos, char in enumerate(state_string) if char == '1'] flags = [flags_description.get(pos, f"Flag {pos} not defined") for pos in positions] return ", ".join(flags) oids = { "upsAdvBatteryCapacity": "1.3.6.1.4.1.318.1.1.1.2.2.1.0", "upsAdvBatteryRunTimeRemaining": "1.3.6.1.4.1.318.1.1.1.2.2.3.0", "upsAdvBatteryTemperature": "1.3.6.1.4.1.318.1.1.1.2.2.2.0", "upsAdvOutputLoad": "1.3.6.1.4.1.318.1.1.1.4.2.3.0", "upsBasicStateOutputState": "1.3.6.1.4.1.318.1.1.1.11.1.1.0", "upsBasicIdentModel": "1.3.6.1.4.1.318.1.1.1.1.1.1.0" } def parse_time(time_str): time_parts = time_str.split(":")[-4:] days = int(time_parts[0]) hours = int(time_parts[1]) minutes = int(time_parts[2]) seconds = float(time_parts[3]) total_minutes = (days * 24 * 60) + (hours * 60) + minutes + (int(seconds // 60)) return total_minutes for name, oid in oids.items(): response = snmp_get('10.31.41.46', 'public', oid) if name == "upsBasicStateOutputState": print(f"{name}: {interpret_flags(response)}") elif name == "upsAdvBatteryRunTimeRemaining": print(f"{name}: {parse_time(response)} minutes") else: print(f"{name}: {response}")