Got 3750-X monitoring working
This commit is contained in:
@@ -1,61 +1,52 @@
|
||||
import os, asyncio
|
||||
from models.ciscoModel import snmpPyCiscoData
|
||||
from snmp.walker import walk_column_v3
|
||||
from models.ciscoModel import C3750XciscoData
|
||||
from models.snmpTimeOut import SNMPTimeoutError
|
||||
from snmp.walker import walk_column_v3, walk_lobotomy_column_v3
|
||||
from typing import Annotated, Final
|
||||
|
||||
# SNMP
|
||||
from pysnmp.hlapi.v3arch.asyncio import *
|
||||
|
||||
# SNMP ENV-------------------------------------------
|
||||
ROUND_PREC: Final[int] = int(os.getenv("ROUND_PREC", 2))
|
||||
|
||||
SNMPUSER: Final[str] = os.getenv("SNMPUSER", None)
|
||||
SNMPPRIVKEY: Final[str] = os.getenv("SNMPPRIVKEY", None)
|
||||
SNMPAUTHKEY: Final[str] = os.getenv("SNMPAUTHKEY", None)
|
||||
# Right now I'll only use SHA
|
||||
# SNMPAUTHPROTO: Final[str] = os.getenv("SNMPAUTHPROTO", "SHA")
|
||||
# SNMPPRIVPROTO: Final[str] = os.getenv("SNMPPRIVPROTO", "SHA")
|
||||
SNMPORT: Final[int] = int(os.getenv("SNMPORT", 161))
|
||||
|
||||
|
||||
# Check if SNMP ENV are empty
|
||||
if not SNMPUSER or not SNMPPRIVKEY or not SNMPAUTHKEY:
|
||||
raise Exception("No SNMP user or/and PrivAuth passed")
|
||||
|
||||
|
||||
# SNMP
|
||||
USMUSRDATA = UsmUserData(
|
||||
userName=SNMPUSER,
|
||||
authKey=SNMPAUTHKEY,
|
||||
privKey=SNMPPRIVKEY,
|
||||
authProtocol=usmHMACSHAAuthProtocol,
|
||||
privProtocol=usmAesCfb128Protocol,
|
||||
)
|
||||
|
||||
|
||||
|
||||
# Cisco
|
||||
async def ciscoPoolRemote(remoteIP: str, queueToInsrt: asyncio.Queue):
|
||||
async def lobGetRemote(
|
||||
remoteIP: str,
|
||||
queueToInsrt: asyncio.Queue,
|
||||
SNMPUSER: str,
|
||||
SNMPAUTHKEY: str,
|
||||
SNMPPRIVKEY: str,
|
||||
ROUND_PREC: int = 2,
|
||||
SNMPORT: int = 161
|
||||
):
|
||||
print("starting work on ", remoteIP)
|
||||
|
||||
|
||||
# SNMPUSRDATA
|
||||
USMUSRDATA = UsmUserData(
|
||||
userName=SNMPUSER,
|
||||
authKey=SNMPAUTHKEY,
|
||||
privKey=SNMPPRIVKEY,
|
||||
authProtocol=usmHMACSHAAuthProtocol,
|
||||
privProtocol=usmAesCfb128Protocol,
|
||||
)
|
||||
|
||||
snmpEngine = SnmpEngine()
|
||||
|
||||
|
||||
iterator = get_cmd(
|
||||
snmpEngine,
|
||||
USMUSRDATA,
|
||||
await UdpTransportTarget.create((remoteIP, SNMPORT)),
|
||||
ContextData(),
|
||||
# Get Hostname
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.2.1.1.5.0")),
|
||||
#
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.2.1.1.5.0")), # 0
|
||||
)
|
||||
|
||||
print(iterator)
|
||||
|
||||
errorIndication, errorStatus, errorIndex, varBinds = await iterator
|
||||
|
||||
|
||||
if errorIndication:
|
||||
print(errorIndication)
|
||||
|
||||
print(f"\n\n{errorIndication}\n\n")
|
||||
if "No SNMP response received before timeout" in str(errorIndication):
|
||||
raise SNMPTimeoutError(f"Host {remoteIP} timed out while walking")
|
||||
raise RuntimeError(errorIndication)
|
||||
elif errorStatus:
|
||||
print(
|
||||
"{} at {}".format(
|
||||
@@ -67,18 +58,56 @@ async def ciscoPoolRemote(remoteIP: str, queueToInsrt: asyncio.Queue):
|
||||
for varBind in varBinds:
|
||||
print(" = ".join([x.prettyPrint() for x in varBind]))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try:
|
||||
# System data
|
||||
systemData = await walk_lobotomy_column_v3(snmpEngine, remoteIP, ".1.3.6.1.4.1.9.9.13.1.3", SNMPUSER, SNMPAUTHKEY, SNMPPRIVKEY)
|
||||
|
||||
# Uptime in seconds
|
||||
uptimeSeconds = await walk_column_v3(snmpEngine, remoteIP, ".1.3.6.1.6.3.10.2.1.3", SNMPUSER, SNMPAUTHKEY, SNMPPRIVKEY)
|
||||
|
||||
# Last 5 second CPU usage
|
||||
last5SecCPUsage = await walk_column_v3(snmpEngine, remoteIP, ".1.3.6.1.4.1.9.2.1.56", SNMPUSER, SNMPAUTHKEY, SNMPPRIVKEY)
|
||||
|
||||
# Last minute CPU usage
|
||||
last1MinCPUsage = await walk_column_v3(snmpEngine, remoteIP, ".1.3.6.1.4.1.9.2.1.58", SNMPUSER, SNMPAUTHKEY, SNMPPRIVKEY)
|
||||
|
||||
# Last 5 minutes CPU usage
|
||||
last5MinCPUsage = await walk_column_v3(snmpEngine, remoteIP, ".1.3.6.1.4.1.9.2.1.58", SNMPUSER, SNMPAUTHKEY, SNMPPRIVKEY)
|
||||
except SNMPTimeoutError as e:
|
||||
print(f"{e}\nContinuing regardless...")
|
||||
return 1
|
||||
except RuntimeError as e:
|
||||
print(f"SNMP error on {remoteIP}: {e}")
|
||||
return 1
|
||||
|
||||
snmpEngine.close_dispatcher()
|
||||
|
||||
returObj = snmpPyCiscoData()
|
||||
returObj = C3750XciscoData(
|
||||
hostname=varBinds[0][-1],
|
||||
|
||||
systemStatus=systemData[0].split(" ")[2], #[2],
|
||||
|
||||
systemTemp=systemData[1], #[2][-1],
|
||||
|
||||
last5SecUsage=last5SecCPUsage[0],
|
||||
|
||||
last1MinUsage=last1MinCPUsage[0],
|
||||
|
||||
last5MinUsage=last5MinCPUsage[0],
|
||||
|
||||
uptimeS=int(uptimeSeconds[0])
|
||||
|
||||
returObj(
|
||||
hostname="X"
|
||||
)
|
||||
|
||||
returnDict = {
|
||||
"source": "CISCO",
|
||||
"device": "3750X",
|
||||
"value": returObj,
|
||||
"type": "snmpPyCiscoData"
|
||||
"type": "C3750XciscoData"
|
||||
}
|
||||
# return returnObj
|
||||
await queueToInsrt.put(returnDict)
|
||||
await queueToInsrt.put(returnDict)
|
||||
|
||||
Reference in New Issue
Block a user