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.

145 lines
6.7 KiB
Python

from jinja2 import Environment, FileSystemLoader
import subprocess
from datetime import datetime, timedelta
import os
import sys
if len(sys.argv) > 1:
time_ago_arg = int(sys.argv[1])
print(f"Going back in time by {time_ago_arg} days")
else:
time_ago_arg = False
#create the log directory
path = "/home/xpub/www/html/tl-dr/log-books"
if not os.path.exists(path):
os.makedirs(path)
if(time_ago_arg):
now = datetime.today() - timedelta(days=time_ago_arg)
else:
now = datetime.today()
book_name = "TL;DR_" + now.strftime("%B %dth %Y")
print(f"making a book called {book_name}")
def get_journalctl_on():
if time_ago_arg:
return ["-S", f"{time_ago_arg + 1} days ago", "-U", f"{time_ago_arg} days ago"]
else:
return ["-S","today"]
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");
# These vars cannot go back in time
if not time_ago_arg:
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).days
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)
else:
since_last_boot = False
device_info = False
ip_address = False
time_since_last_boot = False
dpkg_date = now.strftime("%Y-%m-%d")
list_package_installs = subprocess.run(["grep","-E", dpkg_date + '.*install|install.*' + dpkg_date, "/var/log/dpkg.log"], capture_output=True).stdout.decode('UTF-8').splitlines()
list_package_upgrade = subprocess.run(["grep","-E", dpkg_date + '.*upgrade|upgrade.*' + dpkg_date, "/var/log/dpkg.log"], capture_output=True).stdout.decode('UTF-8').splitlines()
list_package_remove = subprocess.run(["grep","-E", dpkg_date + '.*remove|remove.*' + dpkg_date, "/var/log/dpkg.log"], capture_output=True).stdout.decode('UTF-8').splitlines()
print("start running the service commands, this sometimes takes longer");
# journalctl --utc
journal_today = run_command(["sudo", "journalctl","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r"] + get_journalctl_on()).splitlines()
# TODO this is not working with the time ago. name should be grepped from user_created_today
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","_COMM=useradd","-r"] + get_journalctl_on()).splitlines()
journal_today = subprocess.run(["sudo", "journalctl","_COMM=systemd-logind","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r"] + get_journalctl_on(), check=True, capture_output=True)
kitchen_services = run_command(["sudo", "journalctl", "-u", "kitchen-stove.service", "-u", "kitchen-bin.service", "-u", "kitchen-fridge.service", "-r", "-n"] + get_journalctl_on()).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")
fallback = list_package_installs
# rendering the template and storing the resultant text in variable output
output = template.render(
now = now.strftime("%B %dth %Y"),
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,
list_package_upgrade=list_package_upgrade,
list_package_remove=list_package_remove,
device_info=device_info,
days_since_last_boot = time_since_last_boot,
kitchen_services = kitchen_services,
ip_address = ip_address,
list_active_services = run_command(["sudo", "service", "--status-all"]).splitlines() if not time_ago_arg else False,
debian_version = run_command(["cat", "/etc/debian_version"]) if not time_ago_arg else False,
hostname = run_command(["hostname","-i"]) if not time_ago_arg else False,
groups_created = run_command(['grep', '-v','groupadd'], based=journal_today.stdout).splitlines(),
groups_removed = run_command(['grep', '-v','groupremove'], based=journal_today.stdout).splitlines(),
users_created_today = run_command(['grep', '-v','useradd'], based=journal_today.stdout).splitlines(),
user_modified = run_command(['grep', '-v','usermod'], based=journal_today.stdout).splitlines(),
user_deleted = run_command(['grep', '-v','userdel'], based=journal_today.stdout).splitlines(),
logins_today = run_command(['grep', '-v','New session'], based=journal_today.stdout).splitlines(),
logins_today = run_command(['grep', '-v', "Boot"], based=logins_today_log.stdout).splitlines(),
groups_created = run_command(['grep', '-v', "Boot"], based=groupadd_today_log.stdout).splitlines(),
groups_removed = run_command(['grep', '-v', "Boot"], based=groupremove_today_log.stdout).splitlines(),
user_modified = run_command(['grep', '-v', "Boot"], based=usermod_today_log.stdout).splitlines(),
user_deleted = run_command(['grep', '-v', "Boot"], based=userdel_today_log.stdout).splitlines()
)
print("Output the files");
# Export the html as book.html, which is used as an input for pandoc
with open(f"/home/xpub/www/html/tl-dr/book_{time_ago_arg}.html", "w") as f:
print(output, file=f)
# subprocess.run(['sh', '/home/xpub/www/html/tl-dr/create_book.sh', book_name])
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)