-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplunk-alert.groovy
171 lines (154 loc) · 8.75 KB
/
splunk-alert.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
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
pipeline {
agent any
environment {
SPLUNK_URL = 'https://my-splunk.com:8089/servicesNS/splunk/search/saved/searches'
GIT_REPO_URL = 'https://my-git.example.com/sigmahq'
}
parameters {
string(name: 'SPL_DIRECTORY', defaultValue: 'path/to/your/dir', description: 'Path to the directory containing .spl files')
}
stages {
stage('Clone Source Repo') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '*/spl']],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: 'git-credential-id']]
])
}
}
stage('Apply .spl Rules to Splunk') {
steps {
script {
def splDirectory = params.SPL_DIRECTORY
def splFiles = findFiles(glob: "${splDirectory}/*.spl")
if (splFiles.length == 0) {
echo "No .spl files found in the directory: ${splDirectory}"
currentBuild.result = 'FAILURE'
error("No .spl files found. Aborting.")
}
splFiles.each { file ->
def fileName = file.name
def fileContent = readFile(file.path)
def alertName = fileName.replaceAll('.spl$', '')
def alert_comparator = "equal to"
def alert_threshold = "0"
def cron = "0 6 * * 1"
def alert_message = "The alert condition for '\$name\$' was triggered."
def alertExists = false
try {
def checkResponse = httpRequest(
url: "${SPLUNK_URL}/${alertName}",
httpMode: 'GET',
authentication: 'splunk-credential-id',
validResponseCodes: '200:299',
ignoreSslErrors: true
)
if (checkResponse.status == 200) {
echo "Alert ${alertName} already exists. Skipping creation."
alertExists = true
}
} catch (Exception e) {
echo "Alert ${alertName} does not exist. Proceeding with creation."
}
if (!alertExists) {
def headers = ["Content-Type": "application/x-www-form-urlencoded"]
def requestData = [
"output_mode" : "json",
"action.email.to" : EMAIL_RECIPIENT,
"action.email.sendresults": "1",
"action.email.inline" : "1",
"action.email.format" : "csv",
"action.email.allow_empty_attachment" : "0",
"action.email.message.alert": alert_message,
"actions" : "email",
"alert.digest_mode" : "1",
"alert.expires" : "24h",
"alert.managedBy" : "",
"alert.severity" : "3",
"alert.suppress" : "0",
"alert.suppress.fields": "",
"alert.suppress.period": "",
"alert.track" : "0",
"alert_comparator" : alert_comparator,
"alert_condition" : "",
"alert_threshold" : alert_threshold,
"alert_type" : "number of events",
"allow_skew" : "0",
"cron_schedule" : cron,
"description" : "",
"disabled" : "0",
"displayview" : "",
"is_scheduled" : "1",
"is_visible" : "1",
"max_concurrent" : "1",
"name" : alertName,
"realtime_schedule" : "1",
"restart_on_searchpeer_add" : "1",
"run_n_times" : "0",
"run_on_startup" : "0",
"schedule_priority" : "default",
"schedule_window" : "0",
"search" : fileContent,
"action.email" : "1"
]
try {
def response = httpRequest(
url: SPLUNK_URL,
httpMode: 'POST',
authentication: 'splunk-credential-id-jenkins',
contentType: 'APPLICATION_FORM',
customHeaders: headers.collect { key, value -> [name: key, value: value] },
requestBody: requestData.collect { key, value -> "${URLEncoder.encode(key, 'UTF-8')}=${URLEncoder.encode(value.toString(), 'UTF-8')}" }.join("&"),
validResponseCodes: '200:299',
ignoreSslErrors: true
)
echo "Successfully applied alert for ${fileName}: ${response.content}"
// Set ACL Permissions
def aclRequestData = [
"modifiable": "1",
"owner": "splunk",
"sharing": "global",
"app": "search",
"perms.read": "*",
"perms.write": "admin"
]
try {
def aclResponse = httpRequest(
url: "${SPLUNK_URL}/${alertName}/acl",
httpMode: 'POST',
authentication: 'splunk-credential-id',
contentType: 'APPLICATION_FORM',
customHeaders: headers.collect { key, value -> [name: key, value: value] },
requestBody: aclRequestData.collect { key, value -> "${URLEncoder.encode(key, 'UTF-8')}=${URLEncoder.encode(value.toString(), 'UTF-8')}" }.join("&"),
validResponseCodes: '200:299',
ignoreSslErrors: true
)
echo "Successfully updated permissions for ${fileName}: ${aclResponse.content}"
} catch (Exception aclException) {
echo "Failed to set permissions for ${fileName}: ${aclException.message}"
currentBuild.result = 'FAILURE'
error("ACL update failed for alert: ${alertName}")
}
} catch (Exception e) {
echo "Failed to apply alert for ${fileName}: ${e.message}"
currentBuild.result = 'FAILURE'
error("Alert creation failed for ${alertName}")
}
}
}
}
}
}
}
post {
success {
echo 'Process completed successfully'
}
failure {
echo 'Build failed due to an error in the pipeline'
}
always {
echo 'Pipeline execution completed'
}
}
}