This repository was archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.clj
392 lines (345 loc) · 20.5 KB
/
main.clj
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
; -*- mode: clojure; -*-
; vim: filetype=clojure
(require '[clj-http.client :as client]
'[cheshire.core :as json]
'[riemann.query :as query])
(def hostname (.getHostName (java.net.InetAddress/getLocalHost)))
(include "alerta.clj")
; configure the various servers that we listen on
(tcp-server :host "0.0.0.0")
(udp-server :host "0.0.0.0")
(ws-server :host "0.0.0.0")
(repl-server)
(graphite-server :host "0.0.0.0"
:protocol :tcp
:parser-fn (fn [{:keys [service] :as event}]
(if-let [[source metric]
(clojure.string/split service #"\." 2)]
(case source
"fastly" (if-let [[domain service] (clojure.string/split metric #"\." 2)]
{:host domain
:service service
:metric (:metric event)
:origin "Fastly"
:environment "PROD"
:resource domain
:type "graphiteAlert"
:time (:time event)
:tags [source]
})
"cloudwatch" (if-let [[account & metric] (clojure.string/split metric #"\.")]
(let [host (clojure.string/join ":" (butlast metric))]
{:host host
:service (last metric)
:metric (:metric event)
:origin (clojure.string/capitalize account)
:environment "PROD" ; FIXME - use "stage" if available
:resource host
:type "graphiteAlert"
:time (:time event)
:tags (into [source account] metric)
:ttl 600 ; metrics gathered every 5 minutes
}))))))
(defn parse-stream
[& children]
(fn [e] (let [new-event (assoc e
:host (str (:ip e) ":" (:host e))
:resource (:host e)
:type "gangliaAlert")]
(call-rescue new-event children))))
(defn log-info
[e]
(info e))
; reap expired events every 10 seconds
(periodically-expire 10 {:keep-keys [:host :service :environment :resource :grid :cluster :ip :tags :metric :index-time]})
; some helpful functions
(defn now []
(Math/floor (unix-time)))
(defn switch-epoch-to-elapsed
[& children]
(fn [e] ((apply with {:metric (- (now) (:metric e))} children) e)))
(defn state-to-metric
[& children]
(fn [e] ((apply with {:metric (:state e)} children) e)))
(defn event-to-cluster-event
[& children]
(fn [e] ((apply with {:service "cluster heartbeat"
:host (str (:environment e) ":" (:cluster e))
:resource (:cluster e)} children) e)))
(defn event-to-grid-event
[& children]
(fn [e] ((apply with {:service "grid heartbeat"
:host (str (:environment e) ":" (:grid e))
:resource (:grid e)
:cluster "n/a"} children) e)))
(defn lookup-metric
[metricname & children]
(let [metricsymbol (keyword metricname)]
(fn [e]
(let [metricevent (.lookup (:index @core) (:host e) metricname)]
(if-let [metricvalue (:metric metricevent)]
(call-rescue (assoc e metricsymbol metricvalue) children))))))
; set of severity functions
(defn severity
[severity message & children]
(fn [e] ((apply with {:state severity :description message} children) e)))
(def informational (partial severity "informational"))
(def normal (partial severity "normal"))
(def warning (partial severity "warning"))
(def minor (partial severity "minor"))
(def major (partial severity "major"))
(def critical (partial severity "critical"))
(defn edge-detection
[samples & children]
(let [detector (by [:host :service] (runs samples :state (apply changed :state children)))]
(fn [e] (detector e))))
(defn set-resource-from-cluster [e] (assoc e :resource (:cluster e)))
(defn proportion
[events]
(when-let [event (first events)]
(try
(riemann.folds/fold-all (fn [a b] (/ a (+ a b))) events)
(catch NullPointerException ex
(merge event :metric nil)))))
; thresholding
(let [index (default :ttl 900 (index))
alert (async-queue! :alerta {:queue-size 10000}
(alerta {}))
dedup-alert (edge-detection 1 log-info alert)
dedup-2-alert (edge-detection 2 log-info alert)
dedup-4-alert (edge-detection 4 log-info alert)
graph (async-queue! :graphite {:queue-size 1000}
(graphite {:host "graphite"
:path (fn [e] (str "riemann." (riemann.graphite/graphite-path-basic e)))}))]
(streams
(with :index-time (format "%.0f" (now))
(where (service "heartbeat")
(parse-stream
(with :ttl 300 index))
(event-to-cluster-event
(with {:event "ClusterHeartbeat" :group "Ganglia" :type "gangliaAlert" :ttl 180}
(switch-epoch-to-elapsed
(where (< metric 20)
(normal "Heartbeat from Ganglia cluster is OK" dedup-alert))) index))
(event-to-grid-event
(with {:event "GridHeartbeat" :group "Ganglia" :type "gangliaAlert" :ttl 120}
(switch-epoch-to-elapsed
(where (< metric 20)
(normal "Heartbeat from Ganglia grid is OK" dedup-alert))) index))
(else
index))))
(streams
(throttle 1 30 heartbeat))
; (streams
; (let [hosts (atom #{})]
; (fn [event]
; (swap! hosts conj (:host event))
; (index {:service "unique hosts"
; :time (unix-time)
; :metric (count @hosts)})
; ((throttle 1 10 graph) {:service "riemann unique_hosts"
; :host hostname
; :time (unix-time)
; :metric (count @hosts)}))))
; (streams
; (let [metrics (atom #{})]
; (fn [event]
; (swap! metrics conj {:host (:host event) :service (:service event)})
; (index {:service "unique services"
; :time (unix-time)
; :metric (count @metrics)})
; ((throttle 1 10 graph) {:service "riemann unique_services"
; :host hostname
; :time (unix-time)
; :metric (count @metrics)}))))
(streams
(expired
(match :service "heartbeat"
(with {:event "AgentHeartbeat" :group "Ganglia" :type "gangliaAlert"}
(switch-epoch-to-elapsed
(minor "No heartbeat from Ganglia agent" dedup-alert) log-info)))
(match :service "cluster heartbeat"
(with {:event "ClusterHeartbeat" :group "Ganglia" :type "gangliaAlert"}
(switch-epoch-to-elapsed
(minor "No heartbeat from Ganglia cluster" dedup-alert) log-info)))
(match :service "grid heartbeat"
(with {:event "GridHeartbeat" :group "Ganglia" :type "gangliaAlert"}
(switch-epoch-to-elapsed
(major "No heartbeat from Ganglia grid" dedup-alert) log-info)))))
(streams (parse-stream
(let [boot-threshold
(match :service "boottime"
(with {:event "SystemStart" :group "System"}
(switch-epoch-to-elapsed
(where (< metric 7200) (informational "System started less than 2 hours ago" dedup-alert)))))
heartbeat
(match :service "heartbeat"
(with {:event "AgentHeartbeat" :group "Ganglia"}
(switch-epoch-to-elapsed
(splitp < metric
90 (minor "Heartbeat from Ganglia agent is stale" dedup-alert)
(normal "Heartbeat from Ganglia agent is OK" dedup-alert)))))
puppet-last-run
(match :service "pup_last_run"
(where (> metric 0)
(with {:event "PuppetLastRun" :group "Puppet"}
(switch-epoch-to-elapsed
(splitp < metric
86400 (minor "Puppet has not run in more than a day" dedup-alert) ; not run in last day
7200 (warning "Puppet has not run in last 2 hours" dedup-alert) ; not run in last 2 hours
(normal "Puppet agent is running normally" dedup-alert))))
(else (with {:event "PuppetLastRun" :group "Puppet"}
(warning "Puppet metrics are stale or broken" dedup-alert)))))
puppet-resource-failed
(match :service "pup_res_failed"
(with {:event "PuppetResFailed" :group "Puppet"}
(splitp < metric
0 (warning "Puppet resources are failing" dedup-alert)
(normal "Puppet is updating all resources" dedup-alert))))
last-gumetric-collection
(match :service "gu_metric_last"
(where (> metric 0)
(with {:event "GuMgmtMetrics" :group "Ganglia"}
(switch-epoch-to-elapsed
(splitp < metric
300 (minor "Guardian management metrics not updated for 5 minutes or more" dedup-alert)
(normal "Guardian management metrics are reporting OK" dedup-alert))))
(else (with {:event "GuMgmtMetrics" :group "Ganglia"}
(warning "Guardian management metrics are stale or broken" dedup-alert)))))
disk-max-util
(match :service "part_max_used"
(with {:event "DiskMaxUtil" :group "OS"}
(splitp < metric
99 (critical "Disk utilisation for highest filesystem over threshold" dedup-alert)
(normal "Disk utilisation for highest filesystem is under threshold" dedup-alert))))
fs-util
(match :service #"^fs_util-"
(with {:event "FsUtil" :group "OS"}
(splitp < metric
95 (major "File system utilisation is very high" dedup-alert)
90 (minor "File system utilisation is high" dedup-alert)
(normal "File system utilisation is OK" dedup-alert))))
inode-util
(match :service #"^inode_util-"
(with {:event "InodeUtil" :group "OS"}
(splitp < metric
95 (major "File system inode utilisation is very high" dedup-alert)
90 (minor "File system inode utilisation is high" dedup-alert)
(normal "File system inode utilisation is OK" dedup-alert))))
swap-util
(match :service "swap_util"
(with {:event "SwapUtil" :group "OS"}
(splitp < metric
90 (minor "Swap utilisation is very high" dedup-alert)
(normal "Swap utilisation is OK" dedup-alert))))
cpu-load-five
(by [:host]
(match :service "load_five"
(with {:event "SystemLoad" :group "OS"}
(lookup-metric "cpu_num"
(split*
(fn [e] (< (* 10 (:cpu_num e)) (:metric e))) (minor "System 5-minute load average is very high" dedup-alert)
(fn [e] (< (* 6 (:cpu_num e)) (:metric e))) (warning "System 5-minute load average is high" dedup-alert)
(normal "System 5-minute load average is OK" dedup-alert))))))
disk-io-util
(match :service #"^diskio_util-"
(by [:host :service]
(stable 600 :metric
(with {:event "DiskIOUtil" :group "OS"}
(splitp < metric
99 (major "Disk IO utilisation is very high" dedup-alert)
95 (minor "Disk IO utilisation is high" dedup-alert)
(normal "Disk IO utilisation is OK" dedup-alert))))))
r2-frontend-mode
(match :service "gu_currentMode_mode-r2frontend"
(state-to-metric
(with {:event "R2Mode" :service "R2" :group "Application"}
(where (= state "NORMAL")
(normal "R2 frontend mode is OK" dedup-alert)
(else (major "R2 frontend mode is not OK" dedup-alert))))))
mysql-slave-lag
(match :service "mysql_slave_lag"
(with {:event "MySQLlag" :group "MySQL"}
(splitp < metric
14400 (major "MySQL Replication lag is very high" dedup-alert)
7200 (minor "MySQL Replication lag is high" dedup-alert)
(normal "MySQL Replication lag is OK" dedup-alert))))
ntp-offset
(match :service "ntp_offset"
(with {:event "NTPOffset" :group "OS"}
(split
(or (< 10000 metric) (> -10000 metric)) (major "NTP offset is very high (±10s)" dedup-alert)
(or (< 2000 metric) (> -2000 metric)) (minor "NTP offset is high (±2s)" dedup-alert)
(normal "NTP offset is OK" dedup-alert))))
content-api-host-item-request-time
(where* (fn [e] (and (= (:grid e) "EC2")
(= (:environment e) "PROD")
(= (:service e) "gu_item_http_time-Content-API")))
(with {:event "HostItemResponseTime" :group "Application" :grid "ContentAPI"}
(by :resource
(moving-time-window 300
(combine riemann.folds/mean
(splitp < metric
300 (minor "Content API host item response time is slow" dedup-alert)
(normal "Content API host item response time is OK" dedup-alert)))))))
content-api-host-search-request-time
(where* (fn [e] (and (= (:grid e) "EC2")
(= (:environment e) "PROD")
(= (:service e) "gu_search_http_time-Content-API")))
(with {:event "HostSearchResponseTime" :group "Application" :grid "ContentAPI"}
(by :resource
(moving-time-window 300
(combine riemann.folds/mean
(splitp < metric
200 (minor "Content API host search response time is slow" dedup-alert)
(normal "Content API host search response time is OK" dedup-alert)))))))
content-api-request-time
(where* (fn [e] (and (= (:grid e) "EC2")
(= (:environment e) "PROD")
(= (:cluster e) "contentapimq_eu-west-1")
(= (:service e) "gu_httprequests_application_time-Content-API")))
(with {:event "ResponseTime" :group "Application" :grid "ContentAPI"}
(by :cluster
(moving-time-window 30
(combine riemann.folds/mean
(adjust set-resource-from-cluster
(splitp < metric
300 (minor "Content API MQ cluster response time is slow" dedup-2-alert)
(normal "Content API MQ cluster response time is OK" dedup-2-alert))))))))
content-api-request-rate
(where* (fn [e] (and (= (:grid e) "EC2")
(= (:environment e) "PROD")
(= (:cluster e) "contentapimq_eu-west-1")
(= (:service e) "gu_httprequests_application_rate-Content-API")))
(with {:event "MQRequestRate" :group "Application" :grid "ContentAPI"}
(by :cluster
(fixed-time-window 15
(combine riemann.folds/sum
(adjust set-resource-from-cluster
(splitp < metric
70 (normal "Content API MQ total request rate is OK" dedup-2-alert)
(minor "Content API MQ total request rate is low" dedup-2-alert))))))))]
(where (not (state "expired"))
; prn
boot-threshold
heartbeat
disk-max-util
puppet-last-run
puppet-resource-failed
last-gumetric-collection
fs-util
inode-util
swap-util
cpu-load-five
disk-io-util
r2-frontend-mode
mysql-slave-lag
ntp-offset
;content-api-host-item-request-time
;content-api-host-search-request-time
;content-api-request-time
;content-api-request-rate
))))
(streams
(with {:metric 1 :host hostname :state "normal" :service "riemann events_sec"}
(rate 10 index graph))))