Commit e5e0512b authored by Romain Courteaud's avatar Romain Courteaud

Add warning section to report various wrong configuration

parent bb6fb890
...@@ -67,7 +67,11 @@ def rfc822(date): ...@@ -67,7 +67,11 @@ def rfc822(date):
def filterWarningStatus( def filterWarningStatus(
status_dict, interval, not_critical_url_list, warning_period_duration status_dict,
interval,
not_critical_url_list,
warning_period_duration,
keep_warning=True,
): ):
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
if interval < 60: if interval < 60:
...@@ -82,6 +86,9 @@ def filterWarningStatus( ...@@ -82,6 +86,9 @@ def filterWarningStatus(
if not status_dict["bot_status"]: if not status_dict["bot_status"]:
del status_dict["bot_status"] del status_dict["bot_status"]
if (not status_dict["warning"]) or (not keep_warning):
del status_dict["warning"]
for i in range(len(status_dict["whois"]) - 1, -1, -1): for i in range(len(status_dict["whois"]) - 1, -1, -1):
expiration_date = status_dict["whois"][i]["expiration_date"] expiration_date = status_dict["whois"][i]["expiration_date"]
if (expiration_date is None) or ( if (expiration_date is None) or (
...@@ -484,6 +491,7 @@ class WebBot: ...@@ -484,6 +491,7 @@ class WebBot:
) )
server_ip_dict = {} server_ip_dict = {}
result_dict["dns_query"] = [] result_dict["dns_query"] = []
result_dict["warning"] = []
for dns_change in query.dicts().iterator(): for dns_change in query.dicts().iterator():
if dns_change["domain"] not in checked_domain_dict: if dns_change["domain"] not in checked_domain_dict:
...@@ -713,22 +721,31 @@ class WebBot: ...@@ -713,22 +721,31 @@ class WebBot:
def run(self, mode): def run(self, mode):
status_dict = None status_dict = None
if mode not in ["crawl", "pack", "status", "warning"]: if mode not in ["crawl", "pack", "status", "warning", "error"]:
raise NotImplementedError("Unexpected mode: %s" % mode) raise NotImplementedError("Unexpected mode: %s" % mode)
if self.config["SQLITE"] == ":memory:": if self.config["SQLITE"] == ":memory:":
# Crawl/report are mandatory when using memory # Crawl/report are mandatory when using memory
if mode == "warning": if mode == "warning":
mode = "wallwarning" mode = "wallwarning"
elif mode == "error":
mode = "wallerror"
else: else:
mode = "all" mode = "all"
self.initDB() self.initDB()
try: try:
if mode in ["crawl", "wallwarning", "all"]: if mode in ["crawl", "wallwarning", "wallerror", "all"]:
self.crawl() self.crawl()
if mode in ["status", "all", "wallwarning", "warning"]: if mode in [
"status",
"all",
"wallwarning",
"warning",
"wallerror",
"error",
]:
status_dict = self.status() status_dict = self.status()
if mode == "pack": if mode == "pack":
self.pack() self.pack()
...@@ -739,12 +756,13 @@ class WebBot: ...@@ -739,12 +756,13 @@ class WebBot:
self.closeDB() self.closeDB()
if status_dict is not None: if status_dict is not None:
if mode in ("wallwarning", "warning"): if mode in ("wallwarning", "warning", "wallerror", "error"):
filterWarningStatus( filterWarningStatus(
status_dict, status_dict,
int(self.config.get("INTERVAL")), int(self.config.get("INTERVAL")),
self.calculateNotCriticalUrlList(), self.calculateNotCriticalUrlList(),
float(self.config["WARNING_PERIOD"]), float(self.config["WARNING_PERIOD"]),
keep_warning=("warning" in mode),
) )
if self.config["FORMAT"] == "json": if self.config["FORMAT"] == "json":
status_output = json.dumps(status_dict) status_output = json.dumps(status_dict)
......
...@@ -29,7 +29,7 @@ from .bot import create_bot ...@@ -29,7 +29,7 @@ from .bot import create_bot
help="The bot operation mode to run.", help="The bot operation mode to run.",
show_default=True, show_default=True,
default="status", default="status",
type=click.Choice(["crawl", "pack", "status", "warning"]), type=click.Choice(["crawl", "pack", "status", "warning", "error"]),
) )
@click.option( @click.option(
"--sqlite", "-s", help="The path of the sqlite DB. (default: :memory:)" "--sqlite", "-s", help="The path of the sqlite DB. (default: :memory:)"
......
...@@ -974,6 +974,7 @@ class SurykatkaBotStatusTestCase(unittest.TestCase): ...@@ -974,6 +974,7 @@ class SurykatkaBotStatusTestCase(unittest.TestCase):
"tcp_server": [], "tcp_server": [],
"ssl_certificate": [], "ssl_certificate": [],
"http_query": [], "http_query": [],
"warning": [],
} }
def test_status_sqliteMaxVariableNumber(self): def test_status_sqliteMaxVariableNumber(self):
...@@ -1009,6 +1010,7 @@ class SurykatkaBotStatusTestCase(unittest.TestCase): ...@@ -1009,6 +1010,7 @@ class SurykatkaBotStatusTestCase(unittest.TestCase):
assert len(result["tcp_server"]) == 0 assert len(result["tcp_server"]) == 0
assert len(result["ssl_certificate"]) == 0 assert len(result["ssl_certificate"]) == 0
assert len(result["http_query"]) == 0 assert len(result["http_query"]) == 0
assert len(result["warning"]) == 0
# +1 for example.org # +1 for example.org
assert len(result["missing_data"]) == more_than_sqlite_max * 2 + 1 assert len(result["missing_data"]) == more_than_sqlite_max * 2 + 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment