Initial commit
This commit is contained in:
27
utils/cache.py
Normal file
27
utils/cache.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from cachetools import TTLCache, cached
|
||||
from config import Config
|
||||
from flask import request
|
||||
|
||||
# Criação do cache com TTL (Time To Live)
|
||||
# O tamanho máximo e o tempo de vida são configurados via config.py
|
||||
ttl_cache = TTLCache(maxsize=Config.CACHE_MAX_SIZE, ttl=Config.CACHE_TTL)
|
||||
|
||||
def get_cache_key(*args, **kwargs):
|
||||
"""
|
||||
Gera uma chave única para o cache baseada na URL completa da requisição.
|
||||
Isso garante que query parameters diferentes (host, driver) gerem entradas diferentes.
|
||||
"""
|
||||
if request:
|
||||
return request.url
|
||||
return str(args) + str(kwargs)
|
||||
|
||||
def cache_response(func):
|
||||
"""
|
||||
Decorador wrapper para aplicar cache nas chamadas de função.
|
||||
A chave do cache será baseada na URL da request.
|
||||
"""
|
||||
# A função wrapper precisa aceitar args/kwargs, mas a chave é gerada por get_cache_key
|
||||
@cached(cache=ttl_cache, key=get_cache_key)
|
||||
def wrapper(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
41
utils/snmp.py
Normal file
41
utils/snmp.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import asyncio
|
||||
from pysnmp.hlapi.v3arch.asyncio import *
|
||||
|
||||
async def _snmp_walk_async(host, community, oid):
|
||||
results = []
|
||||
|
||||
# Configuração do Engine e dos dados de conexão
|
||||
snmp_engine = SnmpEngine()
|
||||
community_data = CommunityData(community, mpModel=1) # mpModel=1 para SNMPv2c
|
||||
transport = await UdpTransportTarget.create((host, 161), timeout=2, retries=1)
|
||||
context = ContextData()
|
||||
|
||||
# Realiza o Walk
|
||||
iterator = walk_cmd(
|
||||
snmp_engine,
|
||||
community_data,
|
||||
transport,
|
||||
context,
|
||||
ObjectType(ObjectIdentity(oid)),
|
||||
lexicographicMode=False
|
||||
)
|
||||
|
||||
async for errorIndication, errorStatus, errorIndex, varBinds in iterator:
|
||||
if errorIndication:
|
||||
print(f"SNMP Error: {errorIndication}")
|
||||
break
|
||||
elif errorStatus:
|
||||
print(f"SNMP Error: {errorStatus.prettyPrint()}")
|
||||
break
|
||||
else:
|
||||
for varBind in varBinds:
|
||||
results.append((str(varBind[0]), str(varBind[1])))
|
||||
|
||||
snmp_engine.closeDispatcher()
|
||||
return results
|
||||
|
||||
def snmp_walk(host, community, oid):
|
||||
"""
|
||||
Wrapper síncrono para o walk assíncrono.
|
||||
"""
|
||||
return asyncio.run(_snmp_walk_async(host, community, oid))
|
||||
Reference in New Issue
Block a user