Moved the project to asyncio
This commit is contained in:
221
cont/main.py
221
cont/main.py
@@ -9,28 +9,15 @@ from funct import *
|
||||
from classes import *
|
||||
|
||||
# Program ENV---------------------------------------
|
||||
ROUND_PREC: Final[int] = os.getenv("ROUND_PREC", 6)
|
||||
IDRAC_HOST_LIST: Final[list] = os.getenv("IDRAC_HOST_LIST").split(";")
|
||||
CISCO_HOST_LIST: Final[list] = os.getenv("CISCO_HOST_LIST").split(";")
|
||||
|
||||
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] = os.getenv("SNMPORT", 161)
|
||||
|
||||
# Flightchecks-------------------------------------------
|
||||
# Check if SNMP ENV are empty
|
||||
if not SNMPUSER or not SNMPPRIVKEY or not SNMPAUTHKEY:
|
||||
raise Exception("No SNMP user or/and PrivAuth passed")
|
||||
|
||||
# if HOST_LIST empty, raise Exception No hosts passed
|
||||
if not IDRAC_HOST_LIST and not CISCO_HOST_LIST:
|
||||
raise Exception("No hosts passed\nExiting...")
|
||||
|
||||
# for host in HOST_LIST check if valid IP and create a list with strings
|
||||
for idracHost in IDRAC_HOST_LIST:
|
||||
try:
|
||||
@@ -48,186 +35,54 @@ for ciscoHost in CISCO_HOST_LIST:
|
||||
|
||||
# Code-------------------------------------------
|
||||
|
||||
# Helper functions
|
||||
def wattageCalc(amperageInp: float, voltageInp: float) -> float:
|
||||
return amperageInp * voltageInp
|
||||
async def main():
|
||||
|
||||
# Queues for storing states that a database needs to insert
|
||||
# idracQueue = asyncio.Queue()
|
||||
# ciscoQueue = asyncio.Queue()
|
||||
# fanQueue = asyncio.Queue()
|
||||
mainQueue(maxsize=225)
|
||||
|
||||
while True:
|
||||
tasks = []
|
||||
# IDRAC part
|
||||
async with asyncio.TaskGroup() as tg:
|
||||
for IdracIP in IDRAC_HOST_LIST:
|
||||
tasks.append(tg.create_task(idracPoolRemote_v3(IdracIP, mainQueue)))
|
||||
# tasks.append(tg.create_task(idracPoolRemoteFAN_v3(IdracIP, mainQueue)))
|
||||
|
||||
|
||||
# for CiscoIP in CISCO_HOST_LIST:
|
||||
# tasks.append(tg.create_task(ciscoPoolRemote(CiscoIP, mainQueue)))
|
||||
|
||||
sleep(20)
|
||||
|
||||
|
||||
async def ciscoPoolRemote(remoteIP: str):
|
||||
snmpEngine = SnmpEngine()
|
||||
|
||||
iterator = get_cmd(
|
||||
snmpEngine,
|
||||
UsmUserData(
|
||||
userName=SNMPUSER,
|
||||
authKey=SNMPPRIVKEY,
|
||||
privKey=SNMPAUTHKEY,
|
||||
authProtocol=usmHMACSHAAuthProtocol,
|
||||
privProtocol=usmHMACSHAAuthProtocol,
|
||||
),
|
||||
await UdpTransportTarget.create((remoteIP, SNMPORT)),
|
||||
ContextData(),
|
||||
# Get Hostname
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.2.1.1.5.0")),
|
||||
#
|
||||
)
|
||||
# yes = asyncio.run(idracPoolRemote_v3("192.168.20.7"))
|
||||
# fan = asyncio.run(idracPoolRemoteFAN_v3("192.168.20.7"))
|
||||
|
||||
print(iterator)
|
||||
# print("fanThingy")
|
||||
# # print(fan)
|
||||
# for thing in fan:
|
||||
# print(fan[thing]["name"], fan[thing]["rpm"])
|
||||
# for thing in fan:
|
||||
# print(thing)
|
||||
|
||||
errorIndication, errorStatus, errorIndex, varBinds = await iterator
|
||||
# print("for loop")
|
||||
# print(yes)
|
||||
|
||||
if errorIndication:
|
||||
print(errorIndication)
|
||||
# print("End of code")
|
||||
|
||||
elif errorStatus:
|
||||
print(
|
||||
"{} at {}".format(
|
||||
errorStatus.prettyPrint(),
|
||||
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
|
||||
)
|
||||
)
|
||||
else:
|
||||
for varBind in varBinds:
|
||||
print(" = ".join([x.prettyPrint() for x in varBind]))
|
||||
|
||||
snmpEngine.close_dispatcher()
|
||||
|
||||
async def idracPoolRemote(remoteIP: str):
|
||||
snmpEngine = SnmpEngine()
|
||||
|
||||
iterator = get_cmd(
|
||||
snmpEngine,
|
||||
UsmUserData(
|
||||
userName=SNMPUSER,
|
||||
authKey=SNMPPRIVKEY,
|
||||
privKey=SNMPAUTHKEY,
|
||||
authProtocol=usmHMACSHAAuthProtocol,
|
||||
privProtocol=usmHMACSHAAuthProtocol,
|
||||
),
|
||||
await UdpTransportTarget.create((remoteIP, SNMPORT)),
|
||||
ContextData(),
|
||||
# Get Hostname
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.2.1.1.5.0")),
|
||||
#
|
||||
)
|
||||
|
||||
print(iterator)
|
||||
|
||||
errorIndication, errorStatus, errorIndex, varBinds = await iterator
|
||||
|
||||
if errorIndication:
|
||||
print(errorIndication)
|
||||
|
||||
elif errorStatus:
|
||||
print(
|
||||
"{} at {}".format(
|
||||
errorStatus.prettyPrint(),
|
||||
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
|
||||
)
|
||||
)
|
||||
else:
|
||||
for varBind in varBinds:
|
||||
print(" = ".join([x.prettyPrint() for x in varBind]))
|
||||
|
||||
snmpEngine.close_dispatcher()
|
||||
if __name__ == "__main__":
|
||||
|
||||
|
||||
async def idracPoolRemote_v1(remoteIP: str):
|
||||
snmpEngine = SnmpEngine()
|
||||
|
||||
iterator = get_cmd(
|
||||
snmpEngine,
|
||||
# SNMPv1 = mpModel=0 (SNMPv2c would be mpModel=1)
|
||||
CommunityData("public", mpModel=0),
|
||||
await UdpTransportTarget.create((remoteIP, SNMPORT)),
|
||||
ContextData(),
|
||||
# Hostname (sysName.0)
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.2.1.1.5.0")), #0
|
||||
|
||||
# PSU1 powerDraw and PSU2 powerDraw (Amps)
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.600.30.1.6.1.1")), #1
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.600.30.1.6.1.2")), #2
|
||||
# (Volts)
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.600.20.1.6.1.31")), #3
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.600.20.1.6.1.32")), #4
|
||||
|
||||
# Get Inlet, Exhaust, CPU1, CPU2
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.700.20.1.6.1.1")), #5
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.700.20.1.6.1.2")), #6
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.700.20.1.6.1.3")), #7
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.4.700.20.1.6.1.4")), #8
|
||||
|
||||
# Uptime in seconds
|
||||
ObjectType(ObjectIdentity(".1.3.6.1.4.1.674.10892.5.2.5.0")), #9
|
||||
)
|
||||
|
||||
errorIndication, errorStatus, errorIndex, varBinds = await iterator
|
||||
|
||||
if errorIndication:
|
||||
print(errorIndication)
|
||||
elif errorStatus:
|
||||
print(
|
||||
"{} at {}".format(
|
||||
errorStatus.prettyPrint(),
|
||||
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
|
||||
)
|
||||
)
|
||||
else:
|
||||
for oid, val in varBinds:
|
||||
print(f"{oid.prettyPrint()} = {val.prettyPrint()}")
|
||||
|
||||
# for element in varBinds:
|
||||
# print(element)
|
||||
|
||||
snmpEngine.close_dispatcher()
|
||||
# print(varBinds[1][-1])
|
||||
# print(type(varBinds[1][-1]))
|
||||
returnObj = snmpPyIDRACData(
|
||||
hostname=varBinds[0][-1],
|
||||
# Hostname
|
||||
|
||||
powerDrawPSU1=round((int(varBinds[1][-1]) / 10) * (int(varBinds[3][-1]) / 1000), ROUND_PREC),
|
||||
powerDrawPSU2=round((int(varBinds[2][-1]) / 10) * (int(varBinds[4][-1]) / 1000), ROUND_PREC),
|
||||
# PSU1 and PSU2 power draw in Watts
|
||||
|
||||
voltagePSU1=round((int(varBinds[3][-1]) / 1000), ROUND_PREC),
|
||||
voltagePSU2=round((int(varBinds[4][-1]) / 1000), ROUND_PREC),
|
||||
# PSU1 and PSU2 voltages
|
||||
|
||||
inletTemp=int(varBinds[5][-1] / 10),
|
||||
exhaustTemp=int(varBinds[6][-1] / 10),
|
||||
# Inlet and Exhaust temp
|
||||
|
||||
cpu1Temp=int(varBinds[7][-1] / 10),
|
||||
cpu2Temp=int(varBinds[8][-1] / 10),
|
||||
# CPU1 and CPU2 temp
|
||||
|
||||
uptimeH=round(((int(varBinds[9][-1]) / 60) / 60), ROUND_PREC),
|
||||
# seconds->minutes->hours
|
||||
uptimeD=round((((int(varBinds[9][-1]) / 60) / 60) / 24), ROUND_PREC)
|
||||
# seconds->minutes->hours->days
|
||||
)
|
||||
loop = asyncio.get_event_loop()
|
||||
task = loop.create_task(main())
|
||||
|
||||
|
||||
return returnObj
|
||||
|
||||
|
||||
yes = asyncio.run(idracPoolRemote_v1("192.168.20.7"))
|
||||
|
||||
print("for loop")
|
||||
print(yes)
|
||||
|
||||
x = idracFanStatus(6)
|
||||
# print(yes.hostname)
|
||||
# print(yes.powerDrawPSU1)
|
||||
# print(yes.powerDrawPSU2)
|
||||
# print(yes.voltagePSU1)
|
||||
# print(yes.voltagePSU2)
|
||||
# print(yes.inletTemp)
|
||||
# print(yes.exhaustTemp)
|
||||
# print(yes.uptimeH)
|
||||
# print(yes.uptimeD)
|
||||
|
||||
|
||||
# poolRemote(HOST_LIST[0])
|
||||
# while True:
|
||||
@@ -239,12 +94,6 @@ x = idracFanStatus(6)
|
||||
# await asyncio.sleep(20)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Create connections for MySQL and InfluxDB
|
||||
|
||||
# functions for inserting data into two DBs
|
||||
|
||||
Reference in New Issue
Block a user