Repository: jamesoff/simplemonitor

Issue:
Offer warning if no alerter config is found
If a config is loaded with no alerters and no remote logger, and maybe other configurations, warn at startup that alerts will not be sent.

Discussion:

Patch:
diff --git a/simplemonitor/monitor.py b/simplemonitor/monitor.py
index 8b859a4..e1b264b 100644
--- a/simplemonitor/monitor.py
+++ b/simplemonitor/monitor.py
@@ -502,6 +502,9 @@ def main() -> None:
             remote_port = int(config.get("monitor", "remote_port"))
     key = config.get("monitor", "key", fallback=None)
 
+    if not m.verify_alerting():
+        main_logger.critical("No alerters defined and no remote logger found")
+
     if options.test:
         main_logger.warning("Config test complete. Exiting.")
         sys.exit(0)
diff --git a/simplemonitor/simplemonitor.py b/simplemonitor/simplemonitor.py
index b522874..fbdb3dd 100644
--- a/simplemonitor/simplemonitor.py
+++ b/simplemonitor/simplemonitor.py
@@ -71,6 +71,17 @@ class SimpleMonitor:
                     ok = False
         return ok
 
+    def verify_alerting(self) -> bool:
+        """Sanity check the configuration to see if we have at least an alerter, or network logging."""
+        sane = True
+        if len(self.alerters) == 0:
+            for _, logger in self.loggers.items():
+                if logger._type == "network":
+                    break
+            else:
+                sane = False
+        return sane
+
     def sort_joblist(self, joblist: List[str]) -> List[str]:
         """Order a list of monitors so that compound monitors are at the end"""
         new_list = []  # type: List[str]


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
index a3e3255..4527da5 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -8,7 +8,8 @@ import time
 import unittest
 from unittest.mock import patch
 
-from simplemonitor import monitor
+from simplemonitor import Alerters, monitor, simplemonitor
+from simplemonitor.Loggers import network
 
 
 class TestMonitor(unittest.TestCase):
@@ -40,3 +41,19 @@ class TestMonitor(unittest.TestCase):
             "check_hup_file should not have triggered",
         )
         os.unlink(temp_file_name)
+
+
+class TestSanity(unittest.TestCase):
+    def test_config_has_alerting(self):
+        m = simplemonitor.SimpleMonitor()
+        self.assertFalse(m.verify_alerting())
+
+        m.add_alerter("testing", Alerters.alerter.Alerter({}))
+        self.assertTrue(m.verify_alerting())
+
+        m = simplemonitor.SimpleMonitor()
+        m.add_logger(
+            "testing",
+            network.NetworkLogger({"host": "localhost", "port": 1234, "key": "hello"}),
+        )
+        self.assertTrue(m.verify_alerting())