-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract data from the IntelMQ EventDB and convert it to JSON to use it with intelmqctl
- Loading branch information
Showing
2 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/python3 | ||
|
||
from argparse import ArgumentParser | ||
from datetime import datetime | ||
import json | ||
from sys import exit, stderr | ||
from pprint import pprint | ||
|
||
from psycopg2 import connect | ||
from psycopg2.extras import RealDictCursor | ||
|
||
try: | ||
with open('/etc/intelmq/eventdb-serve.conf') as fody_config: | ||
conninfo = json.load(fody_config)['libpg conninfo'] | ||
except FileNotFoundError as exc: | ||
print(f'Could not load database configuration. {exc}', file=stderr) | ||
exit(2) | ||
|
||
parser = ArgumentParser( | ||
prog='EventDB to JSON', | ||
description='Extract data from the IntelMQ EventDB') | ||
parser.add_argument('-v', '--verbose', action='store_true') | ||
parser.add_argument('-i', '--id', help='Get events by ID') | ||
parser.add_argument('-p', '--pretty', action='store_true', help='Pretty print JSON output') | ||
args = parser.parse_args() | ||
|
||
if args.verbose: | ||
print(f'Using DSN {conninfo!r}.') | ||
db = connect(dsn=conninfo) | ||
cur = db.cursor(cursor_factory=RealDictCursor) | ||
cur.execute ('SELECT * FROM events WHERE id = %s', (args.id, )) | ||
|
||
for row in cur.fetchall(): | ||
del row['id'] | ||
for key in list(row.keys()): | ||
if isinstance(row[key], datetime): | ||
# data from the database has TZ information already included | ||
row[key] = row[key].isoformat() | ||
elif row[key] is None: | ||
del row[key] | ||
if args.pretty: | ||
print(json.dumps(row, indent=2)) | ||
else: | ||
print(json.dumps(row)) |