Last active
August 29, 2015 14:01
-
-
Save smalltown/e5093bb054c11e1d2e96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; -*- mode: clojure; -*- | |
; vim: filetype=clojure | |
(logging/init :file "/var/log/riemann/riemann.log") | |
; Listen on the local interface over TCP (5555), UDP (5555), and websockets | |
; (5556) | |
(let [host "127.0.0.1"] | |
(tcp-server :host host) | |
(udp-server :host host) | |
(ws-server :host host)) | |
; Expire old events from the index every 5 seconds. | |
(periodically-expire 5) | |
; Keep events in the index for 5 minutes by default. | |
(let [index (default :ttl 300 (update-index (index)))] | |
; Inbound events will be passed to these streams: | |
(streams | |
; Index all events immediately. | |
index | |
; Calculate an overall rate of events. | |
(with {:metric 1 :host nil :state "ok" :service "events/sec"} | |
(rate 5 index)) | |
; Log expired events. | |
;(expired | |
; (fn [event] (info "expired" event))) | |
)) | |
; Email related | |
(def email (mailer {:host "smtp server" | |
:port 25 | |
:from "email adress" | |
})) | |
(def tellServiceOwner (rollup 5 3600 (email "emaill address"))) | |
(defn emailByEvent [events] | |
(email-event {:host "smtp server" :port 25 :starttls true} | |
{:from "email adress" :to "email adress"} | |
events | |
) | |
) | |
; Math related | |
(defn abs "(abs n) is the absolute value of n" [n] | |
(cond | |
(not (number? n)) (throw (IllegalArgumentException. | |
"abs requires a number")) | |
(neg? n) (- n) | |
:else n)) | |
(defn minVal "(maxVal x y) is the min value between x y" [x y] | |
(cond | |
(not (number? x)) (throw (IllegalArgumentException. | |
"minVal requires a number")) | |
(not (number? y)) (throw (IllegalArgumentException. | |
"minVal requires a number")) | |
(< x y) x | |
:else y)) | |
;Main Function | |
; For alert notification | |
(streams | |
(by [:host :service] | |
(where (service #"POC") | |
(where (>= metric 2000) | |
#(info (:service %) (:host %) (:state %) (:metric %)) | |
tellServiceOwner)))) | |
; For abnormal scenario | |
(def previous 0) | |
(streams | |
(by [:host :service] | |
(where (service #"POC01") | |
(rate 10 | |
(fn [event] | |
(def current (get event :metric)) | |
(if (<= previous 0) (def previous 1)) | |
(if (<= current 0) (def current 1)) | |
(def changeRate (/ (abs (- previous current)) (minVal previous current))) | |
(println "previous:" previous ",current:" current ",change rate:" changeRate) | |
(def previous (get event :metric)) | |
(if (> changeRate 1) | |
;(println "Sent out mail to notify service owner.") | |
(emailByEvent event) | |
) | |
) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment