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

Output json

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