from jinja2 import Environment, FileSystemLoader import subprocess from datetime import datetime, timedelta import os import fnmatch amount_of_logs = len(fnmatch.filter(os.listdir("./logs"), '*.html')) now = datetime.today() next_report = now + timedelta(hours=3) last_user_added = subprocess.run(["sudo", "journalctl","_COMM=useradd","-r","-n", "1" , "--output-fields=MESSAGE"], capture_output=True) last_user_added_name = subprocess.run(['grep', '-Po', "(?<=name)\W*\K[^ ]*"], input=last_user_added.stdout, capture_output=True).stdout.decode('UTF-8').strip() users_created_today = subprocess.run(["sudo", "journalctl", "-S","today","_COMM=useradd","-r","-n","1","--output-fields=MESSAGE"], capture_output=True).stdout.decode('UTF-8') list_active_services = subprocess.run(["sudo", "service", "--status-all"], capture_output=True).stdout.decode('UTF-8') list_groups = subprocess.run(["getent","group"], capture_output=True).stdout.decode('UTF-8') since_last_boot = subprocess.run(["uptime","-s"], capture_output=True).stdout.decode('UTF-8').strip() slb_date = datetime.strptime(since_last_boot, "%Y-%m-%d %H:%M:%S") time_since_last_boot = now - slb_date list_package_installs = subprocess.run(["grep", 'install', "/var/log/dpkg.log"], capture_output=True).stdout.decode('UTF-8') list_package_upgrade = subprocess.run(["grep", 'upgrade', "/var/log/dpkg.log"], capture_output=True).stdout.decode('UTF-8') list_package_remove = subprocess.run(["grep", 'remove', "/var/log/dpkg.log"], capture_output=True).stdout.decode('UTF-8') device_info_grab = subprocess.run(["grep", 'Model' , "/proc/cpuinfo"], check=True, capture_output=True) device_info = subprocess.run(['awk', '-F:', '{ print $2}'], input=device_info_grab.stdout, capture_output=True).stdout.decode('utf-8').strip() ip_address_show = subprocess.run(["ip", "addr", "show", "end0"], check=True, capture_output=True) ip_address = subprocess.run(['awk', '$1 == "inet" {gsub(/\/.*$/, "", $2); print $2}' ], input=ip_address_show.stdout, capture_output=True).stdout.decode('utf-8').strip() debian_version = subprocess.run(["cat", "/etc/debian_version"], capture_output=True).stdout.decode('UTF-8') kernel_version = subprocess.run(["uname","-a"], capture_output=True).stdout.decode('UTF-8') hostname = subprocess.run(["hostname","-i"], capture_output=True).stdout.decode('UTF-8') kitchen_services = subprocess.run(["sudo", "journalctl", "-S", "today", "-u", "kitchen-stove.service", "-u", "kitchen-bin.service", "-u", "kitchen-fridge.service", "-r", "-n"], capture_output=True).stdout.decode('UTF-8') # loading the environment env = Environment(loader=FileSystemLoader("templates")) # # loading the template template = env.get_template("template.jinja") # rendering the template and storing the resultant text in variable output output = template.render( now = now, next_report = next_report, amount_of_logs = amount_of_logs, last_user_added=last_user_added.stdout.decode('UTF-8'), last_user_added_name=last_user_added_name, users_created_today=users_created_today, list_active_services=list_active_services, list_groups=list_groups, since_last_boot=since_last_boot, list_package_installs=list_package_installs, list_package_upgrade=list_package_upgrade, list_package_remove=list_package_remove, device_info=device_info, debian_version=debian_version, kernel_version=kernel_version, hostname=hostname, days_since_last_boot = time_since_last_boot.days, kitchen_services = kitchen_services, ip_address = ip_address ) log_file_name = "logs/log_" + now.strftime("%Y-%m-%d_%H:%M:%S") + ".html" with open("index.html", "w") as f: print(output, file=f) with open(log_file_name, "w") as f: print(output, file=f)