-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (37 loc) · 1.58 KB
/
main.py
File metadata and controls
42 lines (37 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import logging
logging.basicConfig(level=logging.INFO)
import argparse
import json
import csv
from insightlog.lib import InsightLogAnalyzer
def parse_args():
parser = argparse.ArgumentParser(description="Analyze server log files (nginx, apache2, auth)")
parser.add_argument('--service', required=True, choices=['nginx', 'apache2', 'auth'], help='Type of log to analyze')
parser.add_argument('--logfile', required=True, help='Path to the log file')
parser.add_argument('--filter', required=False, default=None, help='String to filter log lines')
parser.add_argument('--output', choices=['json', 'csv'], help='Export format')
return parser.parse_args()
def main():
args = parse_args()
analyzer = InsightLogAnalyzer(args.service, filepath=args.logfile)
if args.filter:
analyzer.add_filter(args.filter)
requests = analyzer.get_requests()
if args.output == 'json':
with open('output.json', 'w', encoding='utf-8') as f:
json.dump(requests, f, indent=2)
logging.info("Exported results to output.json")
elif args.output == 'csv':
if requests:
with open('output.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=requests[0].keys())
writer.writeheader()
writer.writerows(requests)
logging.info("Exported results to output.csv")
else:
logging.warning("No data to write to CSV.")
else:
for req in requests:
print(json.dumps(req, indent=2))
if __name__ == '__main__':
main()