-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
355 lines (311 loc) · 12.4 KB
/
index.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const fs = require('fs');
const axios = require('axios');
require('dotenv').config();
// Validate environment variables
const requiredEnvVars = ['DATADOG_API_KEY', 'DATADOG_APP_KEY', 'OPSGENIE_API_TOKEN', 'ROOTLY_API_TOKEN', 'ROOTLY_ALERT_SOURCE_SECRET'];
requiredEnvVars.forEach(envVar => {
if (!process.env[envVar]) {
console.error(`Missing environment variable: ${envVar}`);
process.exit(1);
}
});
const DATADOG_API_KEY = process.env.DATADOG_API_KEY;
const DATADOG_APP_KEY = process.env.DATADOG_APP_KEY;
const OPSGENIE_API_TOKEN = process.env.OPSGENIE_API_TOKEN;
const ROOTLY_API_TOKEN = process.env.ROOTLY_API_TOKEN;
const ROOTLY_ALERT_SOURCE_SECRET = process.env.ROOTLY_ALERT_SOURCE_SECRET
const DATADOG_API_URL = 'https://api.datadoghq.com/api/v1';
const OPSGENIE_API_URL = 'https://api.opsgenie.com';
const ROOTLY_API_URL = process.env.ROOTLY_API_URL || 'https://api.rootly.com/v1';
const TIMEOUT = 5000; // 5 seconds timeout for requests
const DRY_RUN = process.env.DRY_RUN;
const results = [];
const axiosInstance = axios.create({ timeout: TIMEOUT });
let opsgenieServicesCache = [];
let rootlyServicesCache = [];
async function main() {
try {
console.log('Starting process...');
await fetchAndCacheRootlyServices();
await fetchAndCacheOpsgenieServices();
const monitors = await fetchDatadogMonitors();
console.log(`Found ${monitors.length} monitors.`);
for (const monitor of monitors) {
await processMonitor(monitor);
}
console.log('Process completed.');
fs.writeFileSync(`run-${Date.now()}.csv`, resultsCSV());
} catch (err) {
console.error('An error occurred:', err);
}
}
main();
async function fetchAndCacheOpsgenieServices() {
console.log('Fetching and caching Opsgenie services...');
let services = [];
let offset = 0;
const limit = 25; // Number of services to fetch per page
while (true) {
try {
const response = await axiosInstance.get(`${OPSGENIE_API_URL}/v1/services`, {
headers: {
'Authorization': `GenieKey ${OPSGENIE_API_TOKEN}`,
'Accept': 'application/json'
},
params: {
limit,
offset
}
});
const fetchedServices = response.data.data;
if (fetchedServices.length === 0) {
console.log('No more Opsgenie services found.');
break;
}
console.log(`Fetched ${fetchedServices.length} Opsgenie services from offset ${offset}.`);
services.push(...fetchedServices);
offset += limit;
} catch (error) {
console.error('Error fetching Opsgenie services:', error.message);
break;
}
}
opsgenieServicesCache = services;
console.log(`Cached ${opsgenieServicesCache.length} Opsgenie services.`);
}
async function fetchAndCacheRootlyServices() {
console.log('Fetching Rootly services...');
const services = [];
let page = 1;
const page_size = 100; // Number of monitors to fetch per page
while (true) {
try {
const response = await axiosInstance.get(`${ROOTLY_API_URL}/services`, {
headers: {
'Authorization': `Bearer ${ROOTLY_API_TOKEN}`
},
params: {
page: {
number: page,
size: page_size
}
}
});
const fetchedServices = response.data.data;
if (fetchedServices.length === 0) {
console.log('No more services found.');
break;
}
console.log(`Fetched ${fetchedServices.length} services from page ${page}.`);
services.push(...fetchedServices);
page += 1;
} catch (error) {
console.error('Error fetching Rootly services:', error.message);
break;
}
}
rootlyServicesCache = services;
return services;
}
async function fetchDatadogMonitors() {
console.log('Fetching Datadog monitors...');
const monitors = [];
let page = 0;
const page_size = 100; // Number of monitors to fetch per page
while (true) {
try {
const response = await axiosInstance.get(`${DATADOG_API_URL}/monitor`, {
headers: {
'DD-API-KEY': DATADOG_API_KEY,
'DD-APPLICATION-KEY': DATADOG_APP_KEY
},
params: {
page: page,
page_size: page_size
}
});
const fetchedMonitors = response.data;
if (fetchedMonitors.length === 0) {
console.log('No more monitors found.');
break;
}
console.log(`Fetched ${fetchedMonitors.length} monitors from page ${page}.`);
monitors.push(...fetchedMonitors);
page += 1;
} catch (error) {
console.error('Error fetching Datadog monitors:', error.message);
break;
}
}
return monitors;
}
function normalizedServiceName(serviceName) {
return serviceName.replace(/[^\w_-]+/g, "_").toLowerCase().replace(/^[_-]/, "")
}
function getOpsgenieServiceId(serviceName) {
console.log(`Looking up Opsgenie service ID for service name: ${serviceName}`);
const service = opsgenieServicesCache.find(s => normalizedServiceName(s.name) === normalizedServiceName(serviceName));
const serviceId = service ? service.id : null;
if (serviceId) {
console.log(`Found Opsgenie service ID: ${serviceId}`);
} else {
console.log(`Opsgenie service ID not found for service name: ${serviceName}`);
}
return serviceId;
}
async function fetchRootlyServiceId(opsgenieId) {
console.log(`Looking up Rootly service ID for Opsgenie ID: ${opsgenieId}`);
const service = rootlyServicesCache.find(s => s.attributes.opsgenie_id === opsgenieId);
const serviceId = service ? service.id : null;
if (serviceId) {
console.log(`Found Rootly service ID: ${serviceId}`);
} else {
console.log(`Rootly service ID not found for Opsgenie ID: ${opsgenieId}`);
}
return serviceId;
}
async function createDatadogWebhook(monitor, serviceName, serviceId) {
try {
if (DRY_RUN) {
console.log(`Dry run - skipping creation of Datadog webhook for service: ${serviceName} with ID: ${serviceId}`)
} else {
console.log(`Creating Datadog webhook for service: ${serviceName} with ID: ${serviceId}`);
await axiosInstance.post(`${DATADOG_API_URL}/integration/webhooks/configuration/webhooks`, {
name: `rootly-${normalizedServiceName(serviceName)}`,
url: `https://webhooks.rootly.com/webhooks/incoming/datadog_webhooks`,
payload: JSON.stringify({
"id":"$ID",
"body":"$EVENT_MSG",
"last_updated":"$LAST_UPDATED",
"event_type":"$EVENT_TYPE",
"title":"$EVENT_TITLE",
"alert_id":"$ALERT_ID",
"alert_metric":"$ALERT_METRIC",
"alert_priority":"$ALERT_PRIORITY",
"alert_query":"$ALERT_QUERY",
"alert_scope":"$ALERT_SCOPE",
"alert_status":"$ALERT_STATUS",
"alert_title":"$ALERT_TITLE",
"alert_transition":"$ALERT_TRANSITION",
"alert_type":"$ALERT_TYPE",
"alert_cycle_key": "$ALERT_CYCLE_KEY",
"date":"$DATE",
"org":{"id":"$ORG_ID","name":"$ORG_NAME"},
"rootly": {
"notification_target": {
"type": "Service",
"id": serviceId
}
}
}),
custom_headers: JSON.stringify({secret: ROOTLY_ALERT_SOURCE_SECRET})
}, {
headers: {
'DD-API-KEY': DATADOG_API_KEY,
'DD-APPLICATION-KEY': DATADOG_APP_KEY
}
});
console.log(`Datadog webhook created for service: ${serviceName}`);
}
} catch (error) {
if (error.response.data.errors[0] === "Webhook already exists") {
console.log(`Webhook already exists: @webhook-rootly-${normalizedServiceName(serviceName)}`)
} else {
results.push({monitor: monitor, new: `@webhook-rootly-${normalizedServiceName(serviceName)}`, error: error.response.data.errors[0]})
console.error('Error creating Datadog webhook:', error, error.response.data);
}
}
}
async function updateDatadogMonitor(monitorId, patches) {
try {
const response = await axiosInstance.get(`${DATADOG_API_URL}/monitor/${monitorId}`, {
headers: {
'DD-API-KEY': DATADOG_API_KEY,
'DD-APPLICATION-KEY': DATADOG_APP_KEY
}
});
const monitor = response.data;
if (monitor) {
const oldMessage = monitor.message;
patches.forEach(([oldNotification, newNotification]) => {
monitor.message = monitor.message.replace(oldNotification, `${oldNotification} ${newNotification}`);
});
results.push({monitor: monitor, oldMessage: oldMessage, newMessage: monitor.message});
if (DRY_RUN) {
console.log(`Dry run enabled, skipping update of Datadog monitor ID: ${monitorId}`);
} else {
console.log(`Updating Datadog monitor ID: ${monitorId}`);
// Synthetics monitors have to be updated using Synthetics API
if (monitor.type === 'synthetics alert' && monitor.options.synthetics_check_id) {
await axiosInstance.patch(`${DATADOG_API_URL}/synthetics/tests/${monitor.options.synthetics_check_id}`, {data: [{path: "/message", op: "replace", value: monitor.message}]}, {
headers: {
'DD-API-KEY': DATADOG_API_KEY,
'DD-APPLICATION-KEY': DATADOG_APP_KEY
}
});
} else {
await axiosInstance.put(`${DATADOG_API_URL}/monitor/${monitorId}`, monitor, {
headers: {
'DD-API-KEY': DATADOG_API_KEY,
'DD-APPLICATION-KEY': DATADOG_APP_KEY
}
});
}
console.log(`Updated monitor ID: ${monitorId}: ${patches}`);
}
} else {
console.log(`Monitor ID: ${monitorId} not found.`);
}
} catch (error) {
results.push({monitor: monitor, error: error.response.data.errors[0]})
console.error('Error updating Datadog monitor:', error, error.response.data);
}
}
async function processMonitor(monitor) {
try {
if (monitor.message.match(/@webhook-rootly-[^\s]+/)) {
console.log(`Skipping monitor ID: ${monitor.id} as it already contains @webhook-rootly.`);
results.push({monitor: monitor, old: null, new: null, error: `Skipping monitor ID: ${monitor.id} as it already contains @webhook-rootly.`});
return;
}
const notifications = monitor.message.match(/@opsgenie-([^\s]+)/g);
if (notifications) {
console.log(`Processing monitor ID: ${monitor.id} with ${notifications.length} Opsgenie notifications.`);
const patches = (await Promise.all(notifications.map(async function(notification) {
const serviceName = notification.split('@opsgenie-')[1];
const opsgenieId = getOpsgenieServiceId(serviceName);
if (opsgenieId) {
const rootlyId = await fetchRootlyServiceId(opsgenieId);
if (rootlyId) {
await createDatadogWebhook(monitor, serviceName, rootlyId);
const newNotification = `@webhook-rootly-${normalizedServiceName(serviceName)}`;
results.push({monitor: monitor, old: notification, new: newNotification, error: null});
return [notification, newNotification];
} else {
results.push({monitor: monitor, old: notification, error: `Rootly ID not found for Opsgenie ID: ${opsgenieId}`});
console.error(`Rootly ID not found for Opsgenie ID: ${opsgenieId}`);
}
} else {
results.push({monitor: monitor, old: notification, error: `Opsgenie ID not found for service name: ${serviceName}`});
console.error(`Opsgenie ID not found for service name: ${serviceName}`);
}
}))).filter((patch) => (!!patch))
if (patches.length) {
await updateDatadogMonitor(monitor.id, patches);
}
} else {
results.push({monitor: monitor, old: null, new: null, error: `No Opsgenie notifications found in monitor ID: ${monitor.id}`})
console.log(`No Opsgenie notifications found in monitor ID: ${monitor.id}`);
}
} catch (error) {
console.error(`Error processing monitor ID: ${monitor.id}`, error);
}
}
function resultsCSV() {
const headers = ['Monitor', 'Monitor Name', 'Monitor JSON', 'Notification', 'Notification to append', 'Monitor message', 'New monitor message', 'Error'];
const csv = [headers];
results.forEach((result) => {
csv.push([result.monitor?.id?.toString() || "", result.monitor?.name || "", result.monitor ? JSON.stringify(result.monitor) : "", result.old || "", result.new || "", result.oldMessage || "", result.newMessage || "", result.error || ""])
})
return csv.map((row) => row.map((cell) => `"${cell.replace(/"/g, '""')}"`).join(',')).join('\n');
}