Skip to content

Commit

Permalink
Initial attempt at slack notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
gsandie committed Apr 6, 2014
1 parent 5527abc commit 432677e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/riemann/slack.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(ns riemann.slack
"Post alerts to slack.com"
(:require [clj-http.client :as client]
[cheshire.core :as json])
(:use [clojure.string :only [escape join upper-case]]))

(defn slack_format [message]
"Format message according to slack formatting spec."
(escape message {\< "&lt;" \> "&gt;"}))

; (client/post hook_url {:form-params
; {:payload (json/write-str {:text "OMG" :channel "#alerts" :username "alerter" :icon_emoji ":warning:"})}})


(defn slack_message
""
[username channel]
(fn [event]
(json/generate-string
(slack_format
{:text (str event)
:channel channel
:username username
:icon_emoji ":warning:"}))))

(defn slack_url
""
[ac_name token]
(str "https://" ac_name ".slack.com/services/hooks/incoming-webhook?token=" token))


(defn slack
""
[account_name token username channel]
(prn (slack_message username channel)))
(client/post (slack_url account_name token)
{:form-params {:payload (str (slack_message username channel ))}}))


29 changes: 29 additions & 0 deletions test/riemann/slack_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns riemann.slack-test
(:use riemann.slack
clojure.test)
(:require [riemann.logging :as logging]))


(def api-key (System/getenv "SLACK_API_KEY"))
(def room (System/getenv "SLACK_ALERT_ROOM"))
(def account (System/getenv "SLACK_ALERT_ACCOUNT"))
(def user "Riemann_Slack_Test")

(when-not api-key
(println "export SLACK_API_KEY=\"...\" to run these tests."))

(when-not room
(println "export SLACK_ALERT_ROOM=\"...\" to run these tests."))

(when-not account
(println "export SLACK_ALERT_ACCOUNT=\"...\" to run these tests."))

(logging/init)

(deftest ^:slack test_event
(let [slack_connect (slack account api-key user room)]
(slack_connect {:host "localhost"
:service "good event test"
:description "Testing slack.com alerts from riemann"
:metric 42
:state "ok"})))

0 comments on commit 432677e

Please sign in to comment.