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.

161 lines
7.0 KiB
Python

from jinja2 import Environment, FileSystemLoader
import subprocess
from datetime import datetime, timedelta
import os
8 months ago
import sys
import re
8 months ago
if len(sys.argv) > 1:
time_ago_arg = int(sys.argv[1])
book_path = f"/home/xpub/www/html/tl-dr/book_{time_ago_arg}.html"
print(f"Going back in time by {time_ago_arg} days")
8 months ago
else:
print(f"Creating a book for today")
book_path = f"/home/xpub/www/html/tl-dr/book.html"
time_ago_arg = False
#create the log directory
8 months ago
path = "/home/xpub/www/html/tl-dr/log-books"
if not os.path.exists(path):
os.makedirs(path)
8 months ago
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")
8 months ago
print(f"making a book called {book_name}")
8 months ago
def get_journalctl_on():
if time_ago_arg:
return ["-S", f"{time_ago_arg + 1} days ago", "-U", f"{time_ago_arg} days ago"]
8 months ago
else:
return ["-S","today"]
def create_all_logs_file():
8 months ago
list_logs = subprocess.run(["ls", "/home/xpub/www/html/tl-dr/log-books"], capture_output=True)
8 months ago
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()
8 months ago
if "No entries" in stripped:
return ""
else:
return stripped
# Run all the commands for getting the logs, and assign to variables
8 months ago
print("Commands: statics")
8 months ago
# 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")
8 months ago
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)
8 months ago
list_active_services = run_command(["sudo", "service", "--status-all"]).splitlines()
8 months ago
else:
since_last_boot = False
8 months ago
device_info = False
ip_address = False
time_since_last_boot = False
8 months ago
list_active_services = False
8 months ago
8 months ago
print("Commands: packages")
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()
8 months ago
print("Commands: journalctl, this sometimes takes longer");
# journal_today = run_command(["sudo", "journalctl","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r", "--utc"] + get_journalctl_on()).splitlines()
journal_today = subprocess.run(["sudo", "journalctl","_COMM=systemd-logind","_COMM=useradd", "_COMM=usermod", "_COMM=userdel","_COMM=groupremove", "_COMM=groupadd","-r", "--utc"] + get_journalctl_on(), check=True, capture_output=True)
def get_last_user(log):
if log:
last = log[-1]
result = re.findall(r"name=([^,]*)", users_created_today[-1])
return result[0]
else:
return False
def get_kitchen_prints():
kitchen_date = now.strftime("%d/%b/%Y")
try:
cup_logs_today = subprocess.run(["sudo","grep","-E", kitchen_date, "/var/log/cups/access_log"], capture_output=True).stdout.decode('UTF-8').splitlines()
return [log for log in cup_logs_today if 'Send-Document' in log]
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
return False
8 months ago
try:
8 months ago
groups_created = subprocess.run(['grep','groupadd'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
groups_removed = subprocess.run(['grep','groupremove'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
users_created_today = subprocess.run(['grep','useradd'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
user_modified = subprocess.run(['grep','usermod'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
user_deleted = subprocess.run(['grep','userdel'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
logins_today = subprocess.run(['grep','New session'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines()
8 months ago
last_user_added_name = get_last_user(users_created_today)
8 months ago
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
8 months ago
print("Got all commands data");
# loading the jinja template environment
8 months ago
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")
8 months ago
8 months ago
print("rendering the template")
# rendering the template and storing the resultant text in variable output
output = template.render(
now = now.strftime("%B %dth %Y"),
last_user_added_name=last_user_added_name if not last_user_added_name else False,
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,
8 months ago
days_since_last_boot = time_since_last_boot,
kitchen_prints = get_kitchen_prints(),
ip_address = ip_address,
8 months ago
list_active_services = list_active_services,
8 months ago
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,
8 months ago
groups_created = groups_created,
groups_removed = groups_removed,
users_created_today = users_created_today,
user_modified = user_modified,
user_deleted = user_deleted,
logins_today = logins_today
)
8 months ago
print("Storing the files");
# Export the html as book.html, which is used as an input for pandoc
with open(book_path, "w") as f:
print(output, file=f)
8 months ago
if not time_ago_arg:
8 months ago
subprocess.run(['sh', '/home/xpub/www/html/tl-dr/create_book.sh', book_name, book_path])
8 months ago
else:
8 months ago
subprocess.run(['sh', '/home/xpub/www/html/tl-dr/create_book.sh', book_name, book_path])
print("Finished");
8 months ago