-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial attempt at slack notifications
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
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 {\< "<" \> ">"})) | ||
|
||
; (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 ))}})) | ||
|
||
|
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
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"}))) |