Commit 7b9db1df authored by Romain Courteaud's avatar Romain Courteaud

Output json

parent a7423529
......@@ -15,6 +15,9 @@ from urlchecker_http import (
reportHttp,
)
from urlchecker_network import isTcpPortOpen, reportNetwork
import json
import email.utils
from collections import OrderedDict
__version__ = "0.0.3"
......@@ -24,6 +27,10 @@ class BotError(Exception):
pass
def rfc822(date):
return email.utils.format_datetime(date)
class WebBot:
def __init__(self, **kw):
self.config = createConfiguration(**kw)
......@@ -102,10 +109,14 @@ class WebBot:
# XXX Check HTTP Cache
def status(self):
result_dict = OrderedDict()
# Report the bot status
print("# STATUS")
status = reportStatus(self._db).get()
print(" ", status.text, status.timestamp.isoformat(timespec="minutes"))
result_dict["bot_status"] = []
result_dict["bot_status"].append(
{"text": status.text, "date": rfc822(status.timestamp)}
)
# Report the list of DNS server status
query = reportNetwork(
......@@ -114,16 +125,17 @@ class WebBot:
transport="UDP",
ip=self.config["NAMESERVER"].split(),
)
print("# DNS SERVER")
resolver_ip_list = []
result_dict["dns_server"] = []
for network_change in query.dicts().iterator():
if network_change["state"] == "open":
resolver_ip_list.append(network_change["ip"])
print(
" ",
network_change["ip"],
network_change["state"],
network_change["timestamp"].isoformat(timespec="minutes"),
result_dict["dns_server"].append(
{
"ip": network_change["ip"],
"state": network_change["state"],
"date": rfc822(network_change["timestamp"]),
}
)
domain_list = self.calculateFullDomainList()
......@@ -134,15 +146,16 @@ class WebBot:
resolver_ip=resolver_ip_list,
rdtype="A",
)
print("# DNS STATUS")
server_ip_dict = {}
result_dict["dns_query"] = []
for dns_change in query.dicts().iterator():
print(
" ",
dns_change["domain"],
dns_change["resolver_ip"],
dns_change["timestamp"].isoformat(timespec="minutes"),
dns_change["response"],
result_dict["dns_query"].append(
{
"domain": dns_change["domain"],
"resolver_ip": dns_change["resolver_ip"],
"date": rfc822(dns_change["timestamp"]),
"response": dns_change["response"],
}
)
for server_ip in dns_change["response"].split(", "):
if not server_ip:
......@@ -160,16 +173,17 @@ class WebBot:
transport="TCP",
ip=[x for x in server_ip_dict.keys()],
)
print("# HTTP SERVER")
url_dict = {}
result_dict["http_server"] = []
for network_change in query.dicts().iterator():
print(
" ",
network_change["ip"],
network_change["state"],
network_change["port"],
network_change["timestamp"].isoformat(timespec="minutes"),
", ".join(server_ip_dict[network_change["ip"]]),
result_dict["http_server"].append(
{
"ip": network_change["ip"],
"state": network_change["state"],
"port": network_change["port"],
"date": rfc822(network_change["timestamp"]),
"domain": ", ".join(server_ip_dict[network_change["ip"]]),
}
)
if network_change["state"] == "open":
for hostname in server_ip_dict[network_change["ip"]]:
......@@ -194,16 +208,37 @@ class WebBot:
ip=[x for x in server_ip_dict.keys()],
url=[x for x in url_dict.keys()],
)
print("# HTTP")
result_dict["http_query"] = []
for network_change in query.dicts().iterator():
print(
" ",
network_change["status_code"],
network_change["url"],
network_change["ip"],
network_change["timestamp"].isoformat(timespec="minutes"),
result_dict["http_query"].append(
{
"status_code": network_change["status_code"],
"url": network_change["url"],
"ip": network_change["ip"],
"date": rfc822(network_change["timestamp"]),
}
)
if self.config["FORMAT"] == "json":
print(json.dumps(result_dict))
else:
for table_key in result_dict:
print("# %s" % table_key)
print("")
table = result_dict[table_key]
if table:
# Print the header
table_key_list = [x for x in table[0].keys()]
table_key_list.sort()
print(" | ".join(table_key_list))
for line in table:
print(
" | ".join(
["%s" % (line[x]) for x in table_key_list]
)
)
print("")
def stop(self):
self._running = False
logStatus(self._db, "stop")
......
......@@ -21,7 +21,15 @@ from urlchecker_bot import create_bot
@click.option(
"--configuration", "-f", help="The path of the configuration file."
)
def runUrlChecker(run, sqlite, nameserver, url, domain, configuration):
@click.option(
"--output",
"-o",
help="The status output format.",
type=click.Choice(["plain", "json"]),
default="plain",
show_default=True,
)
def runUrlChecker(run, sqlite, nameserver, url, domain, configuration, output):
# click.echo("Running url checker bot")
mapping = {}
......@@ -36,6 +44,7 @@ def runUrlChecker(run, sqlite, nameserver, url, domain, configuration):
mapping["SQLITE"] = sqlite
if nameserver:
mapping["NAMESERVER"] = nameserver
mapping["FORMAT"] = output
bot = create_bot(cfgfile=configuration, mapping=mapping)
return bot.run(run)
......
......@@ -28,6 +28,8 @@ def createConfiguration(
config[CONFIG_SECTION]["NAMESERVER"] = "\n".join(
get_default_resolver().nameservers
)
if "FORMAT" not in config[CONFIG_SECTION]:
config[CONFIG_SECTION]["FORMAT"] = "json"
if config[CONFIG_SECTION]["SQLITE"] == ":memory:":
# Do not loop when using temporary DB
......
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