forked from riemann/riemann
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request riemann#614 from VideoAmp/victorops
VictorOps integration
- Loading branch information
Showing
3 changed files
with
78 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
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,54 @@ | ||
(ns riemann.victorops | ||
"Forwards events to VictorOps" | ||
(:require [clj-http.client :as client]) | ||
(:require [cheshire.core :as json]) | ||
(:require [clojure.string :as cstr])) | ||
|
||
(def ^:private event-url | ||
"https://alert.victorops.com/integrations/generic/20131114/alert") | ||
|
||
(defn- post | ||
"POST to the VictorOps API" | ||
[api-key routing-key request] | ||
(client/post (cstr/join "/" [event-url api-key routing-key]) | ||
{:body (json/generate-string request) | ||
:socket-timeout 5000 | ||
:conn-timeout 5000 | ||
:content-type :json | ||
:accept :json | ||
:throw-entire-message? true})) | ||
|
||
(defn- format-event | ||
"Formats an event for VO. message-type is one of :INFO, :WARNING, | ||
:ACKNOWLEDGEMENT, :CRITICAL, :RECOVERY" | ||
[message-type event] | ||
{:message_type message-type | ||
:entity_id (cstr/join "/" [(:host event) (:service event)]) | ||
:timestamp (:time event) | ||
:state_start_time (:time event) | ||
:state_message (str (:host event) " " | ||
(:service event) " is " | ||
(:state event) " (" | ||
(:metric event) ")") | ||
:entity_is_host false | ||
:monitoring_tool "riemann"}) | ||
|
||
(defn victorops | ||
"Creates a VictorOps adapter. Takes your API key and routing key and returns a map of | ||
functions :info, :warning, :critical, :acknowledgement and :recovery corresponding to | ||
the related VictorOps API message types. Each message's entity id will be | ||
\"<event host>/<event service>\". The state message will be the host, service, state | ||
and metric. | ||
(let [vo (victorops \"my-api-key\" \"my-routing-key\")] | ||
(changed-state | ||
(where (state \"info\") (:info vo)) | ||
(where (state \"warning\") (:warning vo)) | ||
(where (state \"critical\") (:critical vo)) | ||
(where (state \"ok\") (:recovery vo))))" | ||
[api-key routing-key] | ||
{:info (fn [e] (post api-key routing-key (format-event :INFO e))) | ||
:warning (fn [e] (post api-key routing-key (format-event :WARNING e))) | ||
:critical (fn [e] (post api-key routing-key (format-event :CRITICAL e))) | ||
:acknowledgement (fn [e] (post api-key routing-key (format-event :ACKNOWLEDGEMENT e))) | ||
:recovery (fn [e] (post api-key routing-key (format-event :RECOVERY e)))}) |
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,23 @@ | ||
(ns riemann.victorops-test | ||
(:require [clj-http.client :as client] | ||
[clojure.test :refer :all] | ||
[riemann.victorops :as vo] | ||
[riemann.test-utils :refer [with-mock]])) | ||
|
||
(deftest victorops-test | ||
(with-mock [calls client/post] | ||
(let [vo (vo/victorops "my-dumb-api-key" "my-dumb-routing-key")] | ||
((:info vo) {:host "my-dumb-host" | ||
:service "victorops info notification" | ||
:metric 42 | ||
:time 12345678 | ||
:state "info"}) | ||
(is (= 1 (count @calls))) | ||
(is (= (last @calls) | ||
["https://alert.victorops.com/integrations/generic/20131114/alert/my-dumb-api-key/my-dumb-routing-key" | ||
{:body "{\"message_type\":\"INFO\",\"entity_id\":\"my-dumb-host/victorops info notification\",\"timestamp\":12345678,\"state_start_time\":12345678,\"state_message\":\"my-dumb-host victorops info notification is info (42)\",\"entity_is_host\":false,\"monitoring_tool\":\"riemann\"}" | ||
:socket-timeout 5000 | ||
:conn-timeout 5000 | ||
:content-type :json | ||
:accept :json | ||
:throw-entire-message? true}]))))) |