Move threshhold to server, move eventlog to notify

This commit is contained in:
Andreas Wrede
2026-03-29 20:29:33 -04:00
parent 0543266c92
commit ad7178ebcb
7 changed files with 61 additions and 48 deletions
+12 -41
View File
@@ -13,40 +13,16 @@ from . import udp
from . import hbdclass
from . import ws as ws_mod
from . import notify as notify_mod
logger = logging.getLogger(__name__)
msg_to_websockets = ws_mod.broadcast
eventlog = notify_mod.log
logf = None
lastfm = ["", "", ""]
# shared runtime collections and helpers
msgs = []
def initlog(logfile):
try:
return open(logfile, "a+")
except Exception as e:
import sys
print("cannot open loffile %s, using STDERR: %s" % (logfile, e))
return sys.stderr
def log(host, m, service=None):
ts = time.time()
s = f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ts))} {host or ''} {m}"
msgs.append(s)
logger.info(s)
if logf:
try:
logf.write(s + "\n")
logf.flush()
except Exception as e:
logger.warning("failed to write to logfile: %s", e)
msg_to_websockets("message", s)
msgs = notify_mod.msgs
def cleanup_function(config):
"""This function will be executed upon program exit."""
@@ -84,7 +60,7 @@ async def _run_async(config):
from . import notify as notify_mod
from . import monitor as monitor_mod
from . import journal as journal_mod
from ..client import threshold as threshold_mod
from . import threshold as threshold_mod
notify_mod.setup(config)
@@ -125,7 +101,7 @@ async def _run_async(config):
ctx = dict(
config=config,
hbdclass=hbdclass,
log=log,
log=eventlog,
pushmsg=pushmsg,
msg_to_websockets=msg_to_websockets,
msg_journal=msg_journal,
@@ -149,7 +125,7 @@ async def _run_async(config):
config=config,
hbdclass=hbdclass,
msgs_getter=lambda: msgs,
log=log,
log=eventlog,
pushmsg=pushmsg,
msg_to_websockets=msg_to_websockets,
threshold_checker=threshold_checker,
@@ -172,7 +148,7 @@ async def _run_async(config):
dns_task = None
try:
dns_task = dns_mod.start_dns_worker(
hbdclass, config, log=log, pushmsg=pushmsg, loop=loop
hbdclass, config, log=eventlog, pushmsg=pushmsg, loop=loop
)
logger.info("dns update worker started")
except Exception as e:
@@ -211,7 +187,7 @@ async def _run_async(config):
for h in sorted(hbdclass.Host.hosts)
],
get_msgs=lambda: msgs,
verbose=config.get("verbose", False),
config=config,
)
)
logger.info("WebSocket task started")
@@ -224,7 +200,7 @@ async def _run_async(config):
monitor_mod.start(
config=config,
hbdclass=hbdclass,
log=log,
log=eventlog,
pushmsg=pushmsg,
msg_to_websockets=msg_to_websockets,
)
@@ -347,7 +323,6 @@ def run(config):
Manually manages the event loop to ensure clean shutdown.
"""
global logf
import os
logging.basicConfig(
@@ -355,8 +330,8 @@ def run(config):
)
load_pickled_hosts(config, hbdclass)
logf = initlog(logfile=config.get("logfile", "messages.log"))
log(None, f"hbd version {__version__} starting up")
notify_mod.initlog(logfile=config.get("logfile", "messages.log"))
eventlog(None, f"hbd version {__version__} starting up")
# Create and set the event loop manually
loop = asyncio.new_event_loop()
@@ -371,11 +346,7 @@ def run(config):
finally:
cleanup_function(config)
logger.info("hbd shutdown complete")
if logf and logf != sys.stderr:
try:
logf.close()
except Exception:
pass
notify_mod.closelog()
# Explicitly close the loop
try:
# Cancel all remaining tasks