48 lines
810 B
Python
48 lines
810 B
Python
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import os
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
def record_event(
|
|
evidence_log,
|
|
*,
|
|
service,
|
|
service_token,
|
|
source,
|
|
port,
|
|
result="ok",
|
|
**extra,
|
|
):
|
|
entry = {
|
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
"service": service,
|
|
"service_token": service_token,
|
|
"source": source,
|
|
"port": port,
|
|
"result": result,
|
|
}
|
|
|
|
entry.update(extra)
|
|
|
|
line = (
|
|
json.dumps(
|
|
entry,
|
|
sort_keys=True,
|
|
separators=(",", ":"),
|
|
)
|
|
+ "\n"
|
|
)
|
|
|
|
fd = os.open(
|
|
evidence_log,
|
|
os.O_WRONLY | os.O_CREAT | os.O_APPEND,
|
|
0o600,
|
|
)
|
|
|
|
try:
|
|
os.write(fd, line.encode("utf-8"))
|
|
finally:
|
|
os.close(fd)
|