diff --git a/script.py b/script.py index a9a5dd5..4b2881d 100644 --- a/script.py +++ b/script.py @@ -7,9 +7,11 @@ import re 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") 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 @@ -79,15 +81,9 @@ list_package_remove = subprocess.run(["grep","-E", dpkg_date + '.*remove|remove. 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() - - -# 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) - -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) +# 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: @@ -96,7 +92,14 @@ def get_last_user(log): 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 try: groups_created = subprocess.run(['grep','groupadd'], capture_output=True, input=journal_today.stdout).stdout.decode('UTF-8').strip().splitlines() @@ -106,15 +109,14 @@ try: 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() - last_user_name = get_last_user(users_created_today) + last_user_added_name = get_last_user(users_created_today) except subprocess.CalledProcessError as e: raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output)) -print("Commands: services, this sometimes takes longer"); -kitchen_services = run_command(["sudo", "journalctl", "-u", "kitchen-stove.service", "-u", "kitchen-bin.service", "-u", "kitchen-fridge.service", "-r", "-n"] + get_journalctl_on()).splitlines() - print("Got all commands data"); + +print(get_kitchen_prints()) # loading the jinja template environment env = Environment(loader=FileSystemLoader("/home/xpub/www/html/tl-dr/templates")) @@ -125,15 +127,14 @@ 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=last_user_added.stdout.decode('UTF-8'), - last_user_added_name=last_user_added_name, + 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, days_since_last_boot = time_since_last_boot, - kitchen_services = kitchen_services, + kitchen_prints = get_kitchen_prints(), ip_address = ip_address, list_active_services = list_active_services, debian_version = run_command(["cat", "/etc/debian_version"]) if not time_ago_arg else False, @@ -143,14 +144,15 @@ output = template.render( users_created_today = users_created_today, user_modified = user_modified, user_deleted = user_deleted, - logins_today = logins_today, - last_user_name = last_user_name if not last_user_name else False + logins_today = logins_today ) print("Storing 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: +with open(book_path, "w") as f: print(output, file=f) if not time_ago_arg: subprocess.run(['sh', '/home/xpub/www/html/tl-dr/create_book.sh', book_name]) diff --git a/templates/book.jinja b/templates/book.jinja index e1b3824..47cd2ca 100644 --- a/templates/book.jinja +++ b/templates/book.jinja @@ -265,10 +265,16 @@ {% endif %} -
tldr@chopchop:~ $sudo journalctl -S today -u kitchen-stove.service -u - kitchen-bin.service -u kitchen-fridge.service -r -n
- - {{ log_output(kitchen_services) }} + {% if kitchen_prints %} + +tldr@chopchop:~ $grep -E {date} /var/log/cups/access_log
+ + {{ log_output(kitchen_prints) }} + + + The kitchen has called for {{kitchen_prints|length}} prints. + + {% endif %}