check_sys/core/report.py
2023-09-18 14:51:01 +02:00

94 lines
3.5 KiB
Python

#!/usr/bin/env python3
from datetime import datetime
import jinja2
def generateHtmlReport(path, data):
today = datetime.now().isoformat()[0:10]
dataJinja2 = dict()
dataJinja2['title'] = 'Report check system'
dataJinja2['plugins'] = list()
# Env jinja2
env = jinja2.Environment(
loader=jinja2.PackageLoader("reports"),
autoescape=jinja2.select_autoescape()
)
# print(env.list_templates())
tmplIndex = env.get_template("index.html.j2")
body = str()
for plugin in data['system']:
dataJinja2['plugins'].append(f"{plugin}.html.j2")
if 'postfix' in data['system']:
dataJinja2['postfix'] = dict()
dataJinja2['postfix']['filename'] = data["system"]["postfix"]["filename"]
dataJinja2['postfix']['vulnerabilities'] = data['system']['postfix']['postfix']
_generateAccordion(dataJinja2['postfix']['vulnerabilities'], 'postfix')
if 'sysctl' in data['system']:
dataJinja2['sysctl'] = dict()
dataJinja2['sysctl']['file'] = dict()
dataJinja2['sysctl']['file']['filename'] = data['system']['sysctl']['file']['filename']
dataJinja2['sysctl']['file']['sysctl'] = data['system']['sysctl']['file']['sysctl']
_generateAccordion(dataJinja2['sysctl']['file']['sysctl'], 'sysctl')
if 'apache' in data['system']:
dataJinja2['apache'] = data['system']['apache']
if data['system']['apache']['audit']:
if data['system']['apache']['ssl']['audit']:
_generateAccordion(
dataJinja2["apache"]["ssl"]["virtualhost"],
"apache-virtualhost"
)
if data['system']['apache']['signature']['audit']:
print(dataJinja2['apache']['signature'])
_generateAccordion(
dataJinja2["apache"]["signature"]["signature"],
"apache-signature"
)
print("")
if data['system']['apache']['indexes']['audit']:
print(dataJinja2['apache']['indexes'])
_generateAccordion(
dataJinja2["apache"]["indexes"]["indexes"],
"apache-indexes"
)
if 'localaccount' in data['system']:
if 'profile' in data['system']['localaccount']:
dataJinja2['profile'] = dict()
dataJinja2['profile']['filename'] = data['system']['localaccount']['profile']['filename']
dataJinja2['profile']['vulnerabilities'] = data['system']['localaccount']['profile']['vulnerabilities']
_generateAccordion(dataJinja2['profile']['vulnerabilities'], 'profile')
if 'pwd_quality' in data['system']['localaccount']:
pass
if 'grub' in data['system']:
dataJinja2['grub'] = data['system']['grub']
dataJinja2['grub']['accordion-id'] = f"accordion-grub-1"
dataJinja2['year'] = '2023'
dataJinja2['hostname'] = data['hostname']
dataJinja2['kernel'] = data['kernel']
dataJinja2['release'] = data['release']
rdr = tmplIndex.render(data=dataJinja2)
hostname = data['hostname'].lower()
with open(f"{path}/reports/reports_{hostname}_{today}.html", "w") as f:
f.write(rdr)
print("The report is generated at this location: " \
f"reports/reports_{hostname}_{today}.html")
def _generateAccordion(obj, parent):
index = 1
for entry in obj:
obj[entry]['accordion-id'] = f"accordion-{parent}-{index}"
index += 1