33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import socket
|
|
import sys
|
|
import os
|
|
from datetime import datetime
|
|
|
|
GRAYLOG_HOST = os.environ.get("GRAYLOG_HOST", "graylog.local")
|
|
GRAYLOG_PORT = int(os.environ.get("GRAYLOG_PORT", 12201))
|
|
LOG_FILE = sys.argv[1]
|
|
|
|
def send_to_graylog(message: dict):
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
sock.sendto(json.dumps(message).encode("utf-8"), (GRAYLOG_HOST, GRAYLOG_PORT))
|
|
|
|
def parse_log(file_path: str):
|
|
with open(file_path, encoding='utf-8') as f:
|
|
blocks = f.read().split('\n--------------------------------------------------\n')
|
|
for block in blocks:
|
|
lines = block.strip().splitlines()
|
|
if not lines:
|
|
continue
|
|
msg = {
|
|
"version": "1.1",
|
|
"host": os.uname().nodename,
|
|
"short_message": lines[0] if lines else "log entry",
|
|
"timestamp": datetime.utcnow().timestamp(),
|
|
"_details": '\n'.join(lines)
|
|
}
|
|
send_to_graylog(msg)
|
|
|
|
if __name__ == "__main__":
|
|
parse_log(LOG_FILE) |