This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatadogEventNotification.groovy
133 lines (122 loc) · 4.7 KB
/
DatadogEventNotification.groovy
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
import com.dtolabs.rundeck.plugins.notification.NotificationPlugin;
import com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants;
import com.dtolabs.rundeck.core.plugins.configuration.ValidationException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode
// See http://docs.datadoghq.com/ja/api/
// curl -X POST -H "Content-type: application/json" \
// -d '{
// "title": "Did you hear the news today?",
// "text": "Oh boy!",
// "priority": "normal",
// "tags": ["environment:test"],
// "alert_type": "info"
// }' \
// 'https://app.datadoghq.com/api/v1/events?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxx'
class DEFAULTS {
static String DATADOG_EVENT_URL = "https://app.datadoghq.com/api/v1/events?api_key="
static String SUBJECT_LINE='Rundeck JOB: ${job.status} [${job.project}] \"${job.name}\" run by ${job.user} (#${job.execid})'
static String API_KEY='Your Datadog API Key'
}
/**
* Expands the Subject string using a predefined set of tokens
*/
def titleString(text,binding) {
//defines the set of tokens usable in the subject configuration property
def tokens=[
'${job.status}': binding.execution.status.toUpperCase(),
'${job.project}': binding.execution.job.project,
'${job.name}': binding.execution.job.name,
'${job.group}': binding.execution.job.group,
'${job.user}': binding.execution.user,
'${job.execid}': binding.execution.id.toString(),
'${job.slugged_name}': binding.execution.job.name.replaceAll("[\\s]", "_").toLowerCase()
]
text.replaceAll(/(\$\{\S+?\})/){
tokens[it[0]]
}
}
/**
* Setting Alert Info
**/
def alertInfo(binding) {
//System.err.println("DEBUG: bindingData="+binding.execution.status)
switch (binding.execution.status) {
case "succeeded" :
alert_info = "info"
break
case "failed" :
alert_info = "error"
break
default:
alert_info = "info"
break
}
return alert_info
}
/**
* @param execution
* @param configuration
*/
def triggerEvent(Map execution, Map configuration) {
//System.err.println("DEBUG: api_key="+configuration.api_key)
//System.err.println("DEBUG: excutionData="+execution)
def expandedTitle = titleString(configuration.subject, [execution:execution])
def expandedAlertinfo = alertInfo([execution:execution])
def tags = configuration.tags ? titleString(configuration.tags, [execution:execution]) : 'rundeck:' + execution.job.name
def job_data = [
title: expandedTitle,
text: "Please see: " + execution.href,
tags: tags,
alert_type: expandedAlertinfo
]
if (configuration.aggregation_key) {
def aggregationKey = titleString(configuration.aggregation_key, [execution:execution])
job_data.put("aggregation_key", aggregationKey)
}
// Send the request.
def url = new URL(DEFAULTS.DATADOG_EVENT_URL+configuration.api_key)
def connection = url.openConnection()
connection.setRequestMethod("POST")
connection.addRequestProperty("Content-type", "application/json")
connection.doOutput = true
def writer = new OutputStreamWriter(connection.outputStream)
def json = new ObjectMapper()
writer.write(json.writeValueAsString(job_data))
writer.flush()
writer.close()
connection.connect()
// process the response.
def response = connection.content.text
//System.err.println("DEBUG: response: "+response)
JsonNode jsnode= json.readTree(response)
def status = jsnode.get("status").asText()
if (! "success".equals(status)) {
System.err.println("ERROR: DatadogEventNotification plugin status: " + status)
}
}
/**
* Main
**/
rundeckPlugin(NotificationPlugin){
title="DataDog_Event"
description="Create a Trigger event."
configuration{
subject title:"Subject", description:"Incident subject line. Can contain \${job.status}, \${job.project}, \${job.name}, \${job.group}, \${job.user}, \${job.execid}", defaultValue:DEFAULTS.SUBJECT_LINE, required:true
api_key title:"API Key", description:"Datadog API key", defaultValue:DEFAULTS.API_KEY, required:true
tags title:"Tags", description:"Datadog tags for this event", defaultValue:"rundeck:\${job.project}:\${job.status}, rundeck:\${job.group}", required:false
aggregation_key title:"Aggregation key", description: "Aggregation key for this event.", defaultValue: "rundeck:\${job.project}:\${job.group}:\${job.slugged_name}", required: false
}
onstart { Map execution, Map configuration ->
triggerEvent(execution, configuration)
true
}
onfailure { Map execution, Map configuration ->
triggerEvent(execution, configuration)
true
}
onsuccess { Map execution, Map configuration ->
triggerEvent(execution, configuration)
true
}
}