Skip to content

Instantly share code, notes, and snippets.

@dch
Created May 24, 2017 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dch/4dfef897c3b8e136ad159f26e5f75357 to your computer and use it in GitHub Desktop.
Save dch/4dfef897c3b8e136ad159f26e5f75357 to your computer and use it in GitHub Desktop.
slack with rollups
(defn format-slack-attachments
"Format attachments list from events."
[events mentions]
(let [event-count (count events)
event (last events)
description (:description event)
event-text (format-event-text event)
color (get-event-color event)]
(if (= event-count 1)
[{:text event-text
:color color
:mrkdwn_in ["text"]}])
(let [{ok "ok"
warning "warning"
critical "critical"
none nil} (group-by :state events)
state-counts {:ok (count ok)
:warning (count warning)
:critical (count critical)
:none (count none)}
; rollup-text becomes list of non-zero counts like '("ok: 3" "critical: 2")
rollup-text
(filter identity (map (fn [[state-keyword state-count]]
(if (> state-count 0)
(str (name state-keyword) ": " state-count)))
state-counts))]
[{:text (str "Current: " event-text
"\nrollup = "
(string/join ", " rollup-text))
:color color
:mrkdwn_in ["text"]}]))))
(defn format-slack-rollup
"Format rollup events into Slack message."
[args]
(let [mentions (:mentions args)
mention-string (if (and (not (nil? mentions)) (not (empty? mentions)))
(str " " (string/join " " mentions))
"")]
(fn [events]
{:attachments (format-slack-attachments events mention-string)
:link_names 1
:icon (:slack-icon @settings)})))
(defn send-slack-alerts
"Send alerts to Slack channels."
[events]
(let [username (:slack-username @settings)
credentials {:account (env :riemann-slack-account)
:token (env :riemann-slack-token)}
event (last events)
event-count (count events)
slack-args (get-slack-args event)
channels (:channels slack-args)
channels-string (string/join ", " channels)
formatter (format-slack-rollup slack-args)]
(doseq [channel channels]
((riemann.slack/slack credentials {:username username
:channel channel
:formatter formatter}) events))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment