Files
SNMP-Influx-collector/cont/main.py

117 lines
3.0 KiB
Python
Raw Normal View History

2026-02-26 19:49:10 +01:00
import ipaddress, os, re, time, funct, classes, asyncio
# SNMP
from pysnmp.hlapi.v3arch.asyncio import *
2026-02-25 23:16:46 +01:00
# QoL
2026-02-26 17:09:56 +01:00
from typing import Annotated, Final
2026-02-26 19:49:10 +01:00
# functions
from funct import *
# additional classes
from classes import *
2026-02-25 23:16:46 +01:00
# Program ENV---------------------------------------
try:
IDRAC_HOST_LIST: Final[list] = os.getenv("IDRAC_HOST_LIST", None).split(";")
CISCO_HOST_LIST: Final[list] = os.getenv("CISCO_HOST_LIST", None).split(";")
except:
raise Exception("No IDRAC hosts variable")
2026-02-25 23:16:46 +01:00
2026-02-26 17:09:56 +01:00
2026-02-25 23:16:46 +01:00
# Flightchecks-------------------------------------------
# if HOST_LIST empty, raise Exception No hosts passed
2026-02-26 19:49:10 +01:00
if not IDRAC_HOST_LIST and not CISCO_HOST_LIST:
2026-02-25 23:16:46 +01:00
raise Exception("No hosts passed\nExiting...")
# for host in HOST_LIST check if valid IP and create a list with strings
2026-02-26 19:49:10 +01:00
for idracHost in IDRAC_HOST_LIST:
2026-02-25 23:16:46 +01:00
try:
2026-02-26 19:49:10 +01:00
ip = str(ipaddress.IPv4Address(idracHost))
2026-02-25 23:16:46 +01:00
except ValueError:
2026-02-26 19:49:10 +01:00
raise Exception(f" IP {ip} for IDRAC devices is invalid.\nExiting...")
for ciscoHost in CISCO_HOST_LIST:
try:
ip = str(ipaddress.IPv4Address(ciscoHost))
except ValueError:
raise Exception(f" IP {ip} for Cisco devices is invalid.\nExiting...")
2026-02-25 23:16:46 +01:00
# Done under "Program ENV" on line ~18
2026-02-26 17:09:56 +01:00
# Code-------------------------------------------
2026-02-28 17:48:01 +01:00
async def main():
2026-02-26 19:49:10 +01:00
2026-02-28 17:48:01 +01:00
# Queues for storing states that a database needs to insert
# idracQueue = asyncio.Queue()
# ciscoQueue = asyncio.Queue()
# fanQueue = asyncio.Queue()
mainQueue = asyncio.Queue(maxsize=225)
asyncio.create_task(ALLdbWriter(mainQueue))
2026-02-26 19:49:10 +01:00
2026-02-28 17:48:01 +01:00
while True:
2026-02-28 17:48:01 +01:00
# IDRAC part
async with asyncio.TaskGroup() as tg:
for IdracIP in IDRAC_HOST_LIST:
tg.create_task(idracPoolRemote_v3(IdracIP, mainQueue))
# FANS
# tg.create_task(idracPoolRemoteFAN_v3(IdracIP, mainQueue))
# CISCO devices
2026-02-28 17:48:01 +01:00
# for CiscoIP in CISCO_HOST_LIST:
# tg.create_task(ciscoPoolRemote(CiscoIP, mainQueue))
2026-02-26 19:49:10 +01:00
await asyncio.sleep(20)
2026-02-26 19:49:10 +01:00
2026-02-26 17:09:56 +01:00
2026-02-28 17:48:01 +01:00
# yes = asyncio.run(idracPoolRemote_v3("192.168.20.7"))
# fan = asyncio.run(idracPoolRemoteFAN_v3("192.168.20.7"))
# print("fanThingy")
# # print(fan)
# for thing in fan:
# print(fan[thing]["name"], fan[thing]["rpm"])
# for thing in fan:
# print(thing)
# print("for loop")
# print(yes)
# print("End of code")
if __name__ == "__main__":
# qu = asyncio.Queue()
# asyncio.run(idracPoolRemote_v3("192.168.20.7", qu))
2026-02-26 17:09:56 +01:00
asyncio.run(main())
# loop = asyncio.get_event_loop()
# task = loop.create_task(main())
2026-02-25 23:16:46 +01:00
2026-02-28 17:48:01 +01:00
# poolRemote(HOST_LIST[0])
# while True:
# tasks = [poolRemote(ip) for ip in HOST_LIST]
# results = await asyncio.gather(*tasks)
# print(results)
# await asyncio.sleep(20)
2026-02-25 23:16:46 +01:00
# Create connections for MySQL and InfluxDB
# functions for inserting data into two DBs
# DNS lookup if the user passed a hostname
# if not given a specific IP for DNS server, fallback to 1.1.1.1
# maybe a class?
# store last value and check if it wasn't sent already to the database
2026-02-26 19:49:10 +01:00