Fetching contributors…
Cannot retrieve contributors at this time
64 lines (53 sloc) 2.37 KB
(ns riemann.pagerduty
"Forwards events to Pagerduty"
(:require [clj-http.client :as client])
(:require [cheshire.core :as json]))
(def ^:private event-url
(defn- post
"POST to the PagerDuty events API."
[request options]
(client/post event-url
{: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 PagerDuty"
{:incident_key (str (:host event) " " (:service event))
:description (str (:host event) " "
(:service event) " is "
(:state event) " ("
(:metric event) ")")
:details event})
(defn send-event
"Sends an event to PD. event-type is one of :trigger, :acknowledge,
[service-key event-type formatter event]
{:service_key service-key, :event_type event-type} (formatter event)))
(defn pagerduty
"Creates a PagerDuty adapter. Takes your PD service key, and returns a map of
functions which trigger, acknowledge, and resolve events. By default, event
service will be used as the incident key. The PD description will be the service,
state, and metric. The full event will be attached as the details.
You can override this by specifying a formatter. The formatter must be a function that accepts an event and emits a hash.
You can also pass more http options (like proxy) using the `:options` key.
(defn pd-format-event
{:incident_key 'Incident key', :description 'Incident Description',
:details 'Incident details'})
The :formatter is an optional argument.
(let [pd (pagerduty { :service-key \"my-service-key\" :formatter pd-format-event})]
(where (state \"ok\") (:resolve pd))
(where (state \"critical\") (:trigger pd))))"
[{:keys [service-key formatter options] :or {formatter format-event options {}}}]
{:trigger (fn [e] (post (send-event service-key :trigger formatter e) options))
:acknowledge (fn [e] (post (send-event service-key :acknowledge formatter e) options))
:resolve (fn [e] (post (send-event service-key :resolve formatter e) options))})