You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
6.0 KiB
Python

from jinja2 import Environment, FileSystemLoader
import subprocess
from datetime import datetime, timedelta
import os
import fnmatch
#create the log directory
path = "/home/xpub/www/html/tl-dr/logs"
if not os.path.exists(path):
os.makedirs(path)
# TODO: check sorting of these files (should be chronological)
# Getting the dynamic variables
log_files = fnmatch.filter(os.listdir(path), '*.html')
now = datetime.today()
next_report = now + timedelta(hours=3)
log_file_name = "log_" + now.strftime("%Y-%m-%d_%H:%M:%S") + ".html"
def create_all_logs_file():
list_logs = subprocess.run(["ls", "/home/xpub/www/html/tl-dr/log-books"], capture_output=True)
with open("/home/xpub/www/html/tl-dr/log-books/all.txt", "w") as f:
print(list_logs.stdout.decode('UTF-8').strip(), file=f)
# Execute a command on the command line. Based = used for piped commands
def run_command(command, based = False):
result = subprocess.run(command, capture_output=True, input=based)
result.check_returncode()
stripped = result.stdout.decode('UTF-8').strip()
if "No entries" in stripped:
return ""
else:
return stripped
# Run all the commands for getting the logs, and assign to variables
print("start running the log commands");
last_user_added = subprocess.run(["sudo", "journalctl","_COMM=useradd","-r","-n", "1" , "--output-fields=MESSAGE"], capture_output=True)
last_user_added_name = run_command(['grep', '-Po', "(?<=name)\W*\K[^ ]*"], based=last_user_added.stdout)
users_created_today = run_command(["sudo", "journalctl", "-S","today","_COMM=useradd","-r","--output-fields=MESSAGE"]).splitlines()
since_last_boot = run_command(["uptime","-s"])
slb_date = datetime.strptime(since_last_boot, "%Y-%m-%d %H:%M:%S")
time_since_last_boot = now - slb_date
list_package_installs_init = subprocess.run(["grep", 'install', "/var/log/dpkg.log"], capture_output=True)
list_package_installs = run_command(['grep', '-v', "status"], based=list_package_installs_init.stdout)
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 = run_command(['awk', '-F:', '{ print $2}'], based=device_info_grab.stdout)
ip_address_show = subprocess.run(["ip", "addr", "show", "end0"], check=True, capture_output=True)
ip_address = run_command(['awk', '$1 == "inet" {gsub(/\/.*$/, "", $2); print $2}' ], based=ip_address_show.stdout)
print("start running the service commands, this sometimes takes longer");
logins_today_log = subprocess.run(["sudo", "journalctl","_COMM=systemd-logind","-r" , "--output-fields=MESSAGE", "-S", "today", "-g", "'New session'" ], capture_output=True)
logins_today = run_command(['grep', '-v', "'Boot'"], based=logins_today_log.stdout)
kitchen_services = run_command(["sudo", "journalctl", "-S", "today", "-u", "kitchen-stove.service", "-u", "kitchen-bin.service", "-u", "kitchen-fridge.service", "-r", "-n"]).splitlines()
# loading the jinja template environment
print("Filling the template");
env = Environment(loader=FileSystemLoader("/home/xpub/www/html/tl-dr/templates"))
# loading the template (use template.jinja when generating the html webview)
template = env.get_template("book.jinja")
# rendering the template and storing the resultant text in variable output
output = template.render(
now = now,
next_report = next_report,
log_files = log_files,
last_user_added=last_user_added.stdout.decode('UTF-8'),
last_user_added_name=last_user_added_name,
users_created_today=users_created_today,
since_last_boot=since_last_boot,
list_package_installs=list_package_installs.splitlines(),
list_package_upgrade=list_package_upgrade.splitlines(),
list_package_remove=list_package_remove.splitlines(),
device_info=device_info,
days_since_last_boot = time_since_last_boot.days,
kitchen_services = kitchen_services,
ip_address = ip_address,
logins_today = logins_today,
list_groups = run_command(["getent","group"]).splitlines(),
list_active_services = run_command(["sudo", "service", "--status-all"]).splitlines(),
debian_version = run_command(["cat", "/etc/debian_version"]),
kernel_version = run_command(["uname","-a"]),
hostname = run_command(["hostname","-i"]),
groups_created = run_command(["sudo", "journalctl", "-S","today","_COMM=groupadd","-r"]).splitlines(),
groups_removed = run_command(["sudo", "journalctl", "-S","today","_COMM=groupremove","-r"]).splitlines(),
user_modified = run_command(["sudo", "journalctl", "-S","today","_COMM=usermod","-r"]).splitlines(),
user_deleted = run_command(["sudo", "journalctl", "-S","today","_COMM=userdel","-r"]).splitlines(),
)
print("Output the files");
# Export the html as book.html, which is used as an input for pandoc
with open("/home/xpub/www/html/tl-dr/book.html", "w") as f:
print(output, file=f)
print("execute the bash script now to create the books");
subprocess.run(["pandoc", "/home/xpub/www/html/tl-dr/book.html",
"-o", "/home/xpub/www/html/tl-dr/logged_book.epub",
"-c", "print.css", "--metadata",
"title="+log_file_name,
"--epub-cover-image=cover.jpg" ,
"--epub-embed-font='/home/xpub/www/html/tl-dr/fonts/Cascadia/ttf/CascadiaCode-*.ttf'"
"--epub-embed-font='/home/xpub/www/html/tl-dr/fonts/Ductus/Ductus*.woff'"], capture_output=True)
print("just generated a new book, also put it in the backlog of logs")
subprocess.run(["cp", "/home/xpub/www/html/tl-dr/logged_book.epub", "/home/xpub/www/html/tl-dr/log-books/log_" + now.strftime("%Y-%m-%d_%H%M%S") + ".epub"], capture_output=True)
create_all_logs_file();
print("Finished");
# At the moment, we are not storing logs in the log folder anymore
# with open(log_file_name, "w") as f:
# print(output, file=f)