Skip to content

Instantly share code, notes, and snippets.

@jamtur01
Created June 7, 2015 19:54
Show Gist options
  • Save jamtur01/521c5862fbdd838d0ee2 to your computer and use it in GitHub Desktop.
Save jamtur01/521c5862fbdd838d0ee2 to your computer and use it in GitHub Desktop.
(ns examplecom.email
"Riemann email alerting"
(:require [riemann.email :refer :all]
[riemann.common :refer :all]
[clojure.string :as str]
[clojure.pprint :as pp])
)
(def header "Monitoring alert from Riemann\n\n")
(def footer "This is an automated Riemann notification. Please do not reply.")
(defn lookup
"Lookup events in the index"
[host service]
(riemann.index/lookup (:index @riemann.config/core) host service)
)
(defn round
"Round numbers to 2 decimal places"
[metric]
(pp/cl-format nil "~,2f" metric)
)
(defn context
"Add some contextual event data"
[event]
(str
"Host context:\n"
" CPU\n"
" System: " (round (:metric (lookup (:host event) "cpu-0/cpu-system"))) "%\n"
" User: " (round (:metric (lookup (:host event) "cpu-0/cpu-user"))) "%\n"
" IOWait: " (round (:metric (lookup (:host event) "cpu-0/cpu-wait"))) "%\n"
" Memory\n"
" Used: " (:metric (lookup (:host event) "memory/memory-used")) " bytes.\n"
"\n\n")
)
(defn format-subject
"Format the email subject"
[events]
(apply format "Service %s in state %s on host %s" (str/join ", " (map :service events)) (str/join ", " (map :state events)) (map :host events))
)
(defn format-body
"Format the email body"
[events]
(str/join "\n\n\n"
(map
(fn [event]
(str
header
"At " (time-at (:time event)) "\n"
"Host: " (:host event) "\n"
"Service: " (:service event) "\n"
"State: " (:state event) "\n"
"Metric: " (if (ratio? (:metric event))
(double (:metric event))
(:metric event)) "\n"
"Tags: [" (str/join ", " (:tags event)) "]"
"\n\n"
"Description: " (:description event)
"\n\n"
(if-not (riemann.streams/expired? event)
(context event)
footer)))
events))
)
(def email (mailer {:from "[email protected]"
:subject (fn [events] (format-subject events))
:body (fn [events] (format-body events))
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment