Created
May 7, 2020 12:05
-
-
Save waveform80/0f9dade900316ca4d9137f8eb8d30be2 to your computer and use it in GitHub Desktop.
A rough script for mux'ing the main cloud-init log and systemd's journal output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import io | |
import re | |
import datetime as dt | |
from subprocess import run, PIPE | |
from itertools import chain | |
def cloud_init_log(): | |
with io.open('/var/log/cloud-init.log', 'r', encoding='utf-8') as log: | |
ts = None | |
for line in log: | |
try: | |
new_ts = dt.datetime.strptime(line[:23], '%Y-%m-%d %H:%M:%S,%f') | |
new_ts = new_ts.replace(tzinfo=dt.timezone.utc) | |
except ValueError as e: | |
message += '\n' + line | |
else: | |
if ts is not None: | |
yield ts, 'ci', message | |
ts = new_ts | |
message = line[26:].rstrip() | |
yield ts, 'ci', message | |
def journal(): | |
out = run(['journalctl', '-o', 'short-iso-precise'], check=True, stdout=PIPE, | |
stderr=PIPE, encoding='utf-8') | |
ts = None | |
for line in out.stdout.splitlines()[1:]: | |
try: | |
new_ts = dt.datetime.strptime(line[:31], '%Y-%m-%dT%H:%M:%S.%f%z') | |
except ValueError: | |
message += '\n' + line | |
else: | |
if ts is not None: | |
yield ts, 'sysd', message | |
ts = new_ts | |
message = line[32:].rstrip() | |
yield ts, 'sysd', message | |
for ts, source, msg in sorted(chain(journal(), cloud_init_log())): | |
print('{ts:%Y-%m-%d %H:%M:%S.%f} {source:4s} {msg}'.format( | |
ts=ts, source=source, msg=msg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment