This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
logtracker/logtracker/agent/journald.py

31 lines
713 B
Python
Executable File

#!/usr/bin/python3
import io
import json
import shlex
import socket
import subprocess
import time
host = socket.getfqdn()
def read_from_journald():
p = subprocess.Popen(shlex.split('journalctl -o json -f'), stdout=subprocess.PIPE)
for line in io.TextIOWrapper(p.stdout):
yield line
def parse_line(line):
data = json.loads(line)
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(data['__REALTIME_TIMESTAMP']) / 1000000))
return {'host': host,
'service': 'journald',
'timestamp': timestamp,
'priority': data['PRIORITY'],
'data': data}
def main():
for line in read_from_journald():
yield parse_line(line)