Complete restructure to modules.

Using modules and imports makes it much more modular
and managable. There are no 600 row files now
This commit is contained in:
2026-03-02 21:46:51 +01:00
parent fcf3643ff8
commit 10e6ae2347
24 changed files with 930 additions and 29537 deletions

71
cont/db/Influx.py Normal file
View File

@@ -0,0 +1,71 @@
import influxdb_client, os, asyncio
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS, WriteOptions
from typing import Annotated, Final
from models.idracModel import snmpPyIDRACData
# FluxQL ENV----------------------------------------
USEINFLUX: Final[int] = int(os.getenv("USEINFLUX", 1))
if USEINFLUX:
INFLXDBTOKEN: Final[str] = os.getenv("INFLXDBTOKEN", "123" )
INFLUXBCKT: Final[str] = os.getenv("INFLUXBCKT", "SNMPyth")
INFLUXORG: Final[str] = os.getenv("INFLUXORG", "staging")
INFLXDBURL: Final[str] = os.getenv("INFLXDBURL", "http://localhost:8086")
INFXLUXDB_MEASUEREMENT: Final[str] = os.getenv("INFXLUXDB_MEASUEREMENT", "SNMPyth-containrr")
INFLX_SEPARATE_POINTS: Final[float] = float(os.getenv("INFLX_SEPARATE_POINTS", 0.1))
# Prepare-------------------------------------------
# INFLUX
if USEINFLUX:
fluxdb_client = influxdb_client.InfluxDBClient(url=INFLXDBURL, token=INFLXDBTOKEN, org=INFLUXORG)
write_fluxdb_api = fluxdb_client.write_api(write_options=ASYNCHRONOUS)
query_fluxdb_api = fluxdb_client.query_api()
else:
fluxdb_client = write_fluxdb_api = query_fluxdb_api = None
async def fluxIDRACWriter(
inpDict: dict
) -> int:
"""
Insert temperature data into InfluxDB
:inputQueue: Asyncio Queue that has what is needed to be sent
"""
# inputQueue have multiple such Dicts
# {
# "source": "IDRAC",
# "value": returnObj,
# "type": "snmpPyIDRACData"
# }
if write_fluxdb_api is None:
return 2
# Prep InfluxDB data
inflxdb_Data_To_Send = (
influxdb_client.Point(INFXLUXDB_MEASUEREMENT)
.tag("SOURCE", inpDict["source"])
.tag("TYPE", inpDict["type"])
.tag("HOSTNAME", inpDict["value"].hostname)
.field("PowerDrawPSU1", inpDict["value"].powerDrawPSU1)
.field("PowerDrawPSU2", inpDict["value"].powerDrawPSU2)
.field("TotalBoardPower", inpDict["value"].powerDrawBoard)
.field("VoltagePSU1", inpDict["value"].voltagePSU1)
.field("VoltagePSU2", inpDict["value"].voltagePSU2)
.field("InletTemperature", inpDict["value"].inletTemp)
.field("ExhaustTemperature", inpDict["value"].exhaustTemp)
.field("TemperatureCPU1", inpDict["value"].cpu1Temp)
.field("TemperatureCPU2", inpDict["value"].cpu2Temp)
.field("UptimeInSeconds", inpDict["value"].uptimeS)
.field("UptimeInHours", inpDict["value"].uptimeH)
.field("UptimeInDays", inpDict["value"].uptimeD)
)
try:
write_fluxdb_api.write(bucket=INFLUXBCKT, org=INFLUXORG, record=inflxdb_Data_To_Send)
except Exception as e:
print(e)
return 1
return 0
# return {"STATUS": "succesfully inserted to InfluxDB"}

76
cont/db/MariaDB.py Normal file
View File

@@ -0,0 +1,76 @@
import sqlalchemy, os, asyncio
from sqlalchemy.ext.asyncio import create_async_engine
from typing import Annotated, Final
from dataclasses import asdict
from models.idracModel import snmpPyIDRACData
from models.sqlTable import idracMeasurement
# SQL ENV-------------------------------------------
USESQL: Final[int] = int(os.getenv("USESQL", 0))
if USESQL:
DBENGINE: Final[str] = os.getenv("DBENGINE", "mysql+asyncmy")
DBADDR: Final[str] = os.getenv("DBADDR", "127.0.0.1")
DBUSR: Final[str] = os.getenv("DBUSR", "root")
DBPWD: Final[str] = os.getenv("DBPWD", "6767")
DBNAME: Final[str] = os.getenv("DBNAME", "TEMP_SENSR")
# SQL
if USESQL:
engine = create_async_engine(
f"{DBENGINE}://{DBUSR}:{DBPWD}@{DBADDR}/{DBNAME}",
# pool_pre_ping=True, # Check connection liveness before using and if needed, recconect
# pool_recycle=3600, # recycle connections older than N (3600 in this case) seconds
echo=True,
)
else:
engine = None
async def sqlIDRACDataWriter(
inpDict: dict
) -> int:
# inputQueue have multiple such Dicts
# {
# "source": "IDRAC",
# "value": returnObj,
# "type": "snmpPyIDRACData"
# }
if engine is None:
return 3
try:
async with engine.begin() as eng:
match inpDict["source"]:
case "CISCO":
await eng.execute(
# changeMeLater
t1.insert(), [{"name": "some name 1"}, {"name": "some name 2"}]
)
case "IDRAC":
payload = inpDict["value"] # snmpPyIDRACData
row = asdict(payload)
await eng.execute(
idracMeasurement.insert(),
[row]
)
return 0
case "FANS":
await eng.execute(
# changeMeLater
t1.insert(), [{"name": "some name 1"}, {"name": "some name 2"}]
)
case _:
print(f"No such source of data as {inpDict['source']}")
return 1
except Exception as e:
print(e)
return 2
return 0
# asyncio.sleep(5)

0
cont/db/__init__.py Normal file
View File

View File

@@ -0,0 +1,40 @@
import asyncio, os
from typing import Annotated, Final
from db.Influx import fluxIDRACWriter
from db.MariaDB import sqlIDRACDataWriter
USEINFLUX: Final[int] = int(os.getenv("USEINFLUX", 1))
USESQL: Final[int] = int(os.getenv("USESQL", 0))
async def ALLdbIDRACWriter(inputQueue: asyncio.Queue) -> None:
while True:
# print(inputQueue)
qu = await inputQueue.get()
try:
if USESQL:
sqlResult = await sqlIDRACDataWriter(qu)
match sqlResult:
case 1:
print("Wrong source")
case 2:
print("Error inserting to database")
case 3:
print("No engine defined")
case _:
print("Inserted in SQL")
if USEINFLUX:
fluxResult = await fluxIDRACWriter(qu)
match fluxResult:
case 1:
print("could not insert")
case 2:
print("Error with initializing Inxlux variables")
case _:
print("Inserted to InfluxDB")
except Exception as e:
print(e)
finally:
inputQueue.task_done()