-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchEvent.py
More file actions
153 lines (123 loc) · 4.26 KB
/
FetchEvent.py
File metadata and controls
153 lines (123 loc) · 4.26 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python3
import os
import json
from dotenv import load_dotenv
import pandas as pd
import argparse
import time
import pytz
from datetime import datetime
from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v2.api.events_api import EventsApi
def initialize_config():
"""
Sets up configurations, API keys, and Datadog API client.
"""
if not load_dotenv():
raise EnvironmentError(
"Unable to load .env file. Make sure it exists and contains the required keys."
)
DD_API_KEY = os.getenv("DD_API_KEY")
DD_APP_KEY = os.getenv("DD_APP_KEY")
if not DD_API_KEY or not DD_APP_KEY:
raise ValueError("Datadog API Key or App Key is missing in the .env file.")
configuration = Configuration()
configuration.api_key["apiKeyAuth"] = DD_API_KEY
configuration.api_key["appKeyAuth"] = DD_APP_KEY
return configuration
def parse_arguments():
"""
Parses command-line arguments.
"""
parser = argparse.ArgumentParser(description="Fetch events from Datadog API.")
parser.add_argument(
"--query",
type=str,
required=True,
help="The filter query for fetching events, e.g., 'service:trms production service check'.",
)
parser.add_argument(
"--days",
type=int,
default=30,
help="Number of days in the past to fetch events (default: 30).",
)
return parser.parse_args()
def fetch_event_data(events_api, query, days):
"""
Fetches event data from Datadog API.
"""
start_time = int(time.time()) - (86400 * days) # X days ago
end_time = int(time.time())
try:
events_response = events_api.list_events(
filter_from=str(start_time),
filter_to=str(end_time),
filter_query=query,
page_limit=1000,
)
return events_response.data if events_response.data else []
except Exception as e:
handle_errors(f"Error fetching events: {e}")
return []
def process_event_data(events_data):
"""
Processes and transforms the fetched event data.
"""
processed_events = []
target_timezone = pytz.timezone("Asia/Manila")
for event in events_data:
attributes = event.attributes.attributes
event_timestamp = event.attributes.timestamp
# Ensure tz-aware timestamp
if event_timestamp.tzinfo is None:
event_timestamp = event_timestamp.replace(tzinfo=pytz.utc)
event_timestamp_local = event_timestamp.astimezone(target_timezone)
processed_events.append(
{
"event_id": event.id,
"event_type": event.type,
"event_tags": ", ".join(event.attributes.tags) if event.attributes.tags else "",
"event_timestamp_local": event_timestamp_local.strftime("%Y-%m-%d %H:%M:%S"),
"event_title": attributes.get("title", "No Title"),
"event_status": attributes.get("status", "Unknown"),
}
)
return processed_events
def save_to_csv(events_df, output_dir_path="output", output_file="output/events_output.csv"):
"""
Saves the processed event data to a CSV file.
"""
try:
os.makedirs(output_dir_path, exist_ok=True)
print(f"Directory created at: {output_dir_path}")
except OSError as e:
handle_errors(f"Error creating directory: {e}")
return
try:
events_df.to_csv(output_file, index=False)
print(f"Events successfully written to {output_file}.")
except Exception as e:
handle_errors(f"Error saving to CSV: {e}")
def handle_errors(error_message):
"""
Centralized error handling mechanism.
"""
print(error_message)
def main():
"""
Orchestrates the workflow and executes the main logic.
"""
args = parse_arguments()
configuration = initialize_config()
with ApiClient(configuration) as api_client:
events_api = EventsApi(api_client)
events_data = fetch_event_data(events_api, args.query, args.days)
if events_data:
events_df = process_event_data(events_data)
print(events_df)
save_to_csv(events_df)
else:
print("No events found.")
if __name__ == "__main__":
main()