Class: LogStash::Outputs::OpsGenie

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/opsgenie.rb

Overview

The OpsGenie output is used to Create, Close, Acknowledge Alerts and Add Note to alerts in OpsGenie. For this output to work, your event must contain “opsgenieAction” field and you must configure apiKey field in configuration. If opsgenieAction is “create”, event must contain “message” field. For other actions (“close”, “acknowledge” or “note”), event must contain “alias” or “alertId” field.

If your event have the following fields (If you use default field names).

Example event:

"note" => "test note",
"opsgenieAction" => "create",
"teams" => ["teams"],
"description" => "test description",
"source" => "test source",
"message" => "test message",
"priority" => "P4",
"tags" => ["tags"],
"@timestamp" => 2017-09-15T13:32:00.747Z,
"@version" => "1",
"host" => "Neo's-MacBook-Pro.local",
"alias" => "test-alias",
"details" => {
"prop2" => "val2",
"prop1" => "val1"

,

"actions" => ["actions"],
"user" => "test user",
"entity" => "test entity"

}

An alert with following properties will be created.

{
  "message": "test message",
  "alias": "test alias",
  "teams": ["teams"],
  "description": "test description",
  "source": "test source",
  "note": "test note",
  "user": "test user",
  "priority": "P4",
  "tags": [
    "tags"
  ],
  "details": {
    "prop2": "val2",
    "prop1": "val1"
  },
  "actions": [
    "actions"
  ],
  "entity": "test entity",
}

Fields with prefix “Attribute” are the keys of the fields will be extracted from Logstash event. For more information about the api requests and their contents, please refer to Alert API(“docs.opsgenie.com/docs/alert-api”) support doc.

Instance Method Summary collapse

Instance Method Details

#executePost(uri, params) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/logstash/outputs/opsgenie.rb', line 168

def executePost(uri, params)
  unless uri == nil then
    @logger.info("Executing url #{uri}")
    url = URI(uri)
    http = Net::HTTP.new(url.host, url.port, @proxy_address, @proxy_port)
    if url.scheme == 'https'
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    request = Net::HTTP::Post.new(url.request_uri, initheader = { "Content-Type" =>"application/json", "Authorization" => "GenieKey #{@apiKey}" })
    request.body = params.to_json
    response = http.request(request)
    body = response.body
    body = JSON.parse(body)
    @logger.warn("Executed [#{uri}]. Response:[#{body}]")
  end
end

#populateAliasOrId(event, params) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/logstash/outputs/opsgenie.rb', line 153

def populateAliasOrId(event, params)
  alertAlias = event.get(@aliasAttribute) if event.get(@aliasAttribute)
  if alertAlias == nil then
    alertId = event.get(@alertIdAttribute) if event.get(@alertIdAttribute)
    if !(alertId == nil) then
      @identifierType = 'id'
      @identifier = alertId
    end
  else
    @identifierType = 'alias'
    @identifier = alertAlias
  end
end

#receive(event) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/logstash/outputs/opsgenie.rb', line 187

def receive(event)
  return unless output?(event)

  @logger.info("processing #{event}")
  opsGenieAction = event.get(@actionAttribute) if event.get(@actionAttribute)
  if opsGenieAction then
    params = {}
    populateCommonContent(params, event)

    case opsGenieAction.downcase
    when "create"
      uri = "#{@opsGenieBaseUrl}"
      params = populateCreateAlertContent(params, event)
    when "close"
      uri = "#{@opsGenieBaseUrl}#{@identifier}#{@closeActionPath}?identifierType=#{@identifierType}"
    when "acknowledge"
      uri = "#{@opsGenieBaseUrl}#{@identifier}#{@acknowledgeActionPath}?identifierType=#{@identifierType}"
    when "note"
      uri = "#{@opsGenieBaseUrl}#{@identifier}#{@noteActionPath}?identifierType=#{@identifierType}"
    else
      @logger.warn("Action #{opsGenieAction} does not match any available action, discarding..")
        return
    end

    executePost(uri, params)
  else
    @logger.warn("No opsgenie action defined")
    return
  end
end

#registerObject



149
150
# File 'lib/logstash/outputs/opsgenie.rb', line 149

def register
end