Skip to content

Commit

Permalink
Merge pull request riemann#614 from VideoAmp/victorops
Browse files Browse the repository at this point in the history
VictorOps integration
  • Loading branch information
aphyr committed Oct 16, 2015
2 parents 4143cfb + 0f1fa94 commit 48e9f61
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/riemann/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
[test :as test :refer [tap io tests]]
[time :refer [unix-time linear-time once! every!]]
[twilio :refer [twilio]]
[victorops :refer [victorops]]
[xymon :refer [xymon]]]
[riemann.transport [tcp :as tcp]
[udp :as udp]
Expand Down
54 changes: 54 additions & 0 deletions src/riemann/victorops.clj
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)))})
23 changes: 23 additions & 0 deletions test/riemann/victorops_test.clj
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}])))))

0 comments on commit 48e9f61

Please sign in to comment.