1. Home
  2. Integrations
  3. Sensu Integration Guide – VictorOps

Sensu Integration Guide – VictorOps

Requirements

VictorOps Version Required: Standard or Enterprise

The Sensu integration uses a dedicated Sensu endpoint key to send alerts into the VictorOps timeline. Below are the necessary parts and some relevant Sensu docs.


In VictorOps

Navigate to your VO instance’s Integrations page by going to Settings >> Alert Behavior >> Integrations. Then please locate the Sensu integration tile within the alphabetized list (quickly find it by leveraging the search bar).

Once you have entered the Sensu Integration tile, please click the blue ‘Enable Integration’ button to have your Sensu API Endpoint generated for you. Please copy this Service API Endpoint to your clipboard once you have modified the ‘$routing_key’ piece at the end of the API Endpoint to a valid routing_key in which you wish to send these Sensu alerts to. For more information on routing_keys, please see this Knowledge Base article.


VictorOps.json

Edit Line 18 in the following config file so that it contains your Sensu API Endpoint you just copied instead of “https://alert.victorops.com/integrations/generic/SOMETHIHNG/alert/UUID” and place it in /etc/sensu/conf.d then, download the VictorOps handler and the integration is complete!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"handlers": {
   "victorops": {
     "type": "pipe",
     "command": "/etc/sensu/handlers/victorops.rb"
   }
 },
 "checks": {
   "tmp_check": {
     "description": "check that /tmp exists ",
     "handler": "victorops",
     "command": "ls /tmp",
     "interval": 30,
     "subscribers": [ "all_servers" ]
   }
 },
 "victorops" : {
   "api_url": "https://alert.victorops.com/integrations/generic/SOMETHIHNG/alert/UUID",
   "routing_key" : "everyone"
 }
}

VictorOps Handler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby
#!/usr/bin/env ruby
# This handler creates and resolves victorops incidents
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
# Downloaded from:
# https://help.victorops.com/knowledge-base/victorops-sensu-integration/#
 
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
 
class VictorOps < Sensu::Handler
 def handle
   config = settings['victorops']
   incident_key = @event['client']['name'] + '/' + @event['check']['name']
 
   description = @event['check']['notification']
   description ||= [@event['client']['name'], @event['check']['name'], @event['check']['output']].join(' : ')
   host = @event['client']['name']
   entity_id = incident_key
   state_message  = description
   begin
     Timeout.timeout(10) do
 
       case @event['action']
       when 'create'
         case @event['check']['status']
         when 1
           message_type = 'WARNING'
         else
           message_type = 'CRITICAL'
         end
       when 'resolve'
          message_type = 'RECOVERY'
       end
 
       payload = Hash.new
       payload[:message_type] = message_type
       payload[:state_message] = state_message.chomp
       payload[:entity_id] = entity_id
       payload[:host_name] = host
       payload[:monitoring_tool] = 'sensu'
 
       # Add in client data
       payload[:check] = @event['check']
       payload[:client] = @event['client']
 
       uri   = URI("#{config['api_url'].chomp('/')}/#{config['routing_key']}")
       https = Net::HTTP.new(uri.host, uri.port)
 
       https.use_ssl = true
 
       request      = Net::HTTP::Post.new(uri.path)
       request.body = payload.to_json
       response     = https.request(request)
 
       if response.code == '200'
         puts "victorops -- #{@event['action'].capitalize}'d incident -- #{incident_key}"
       else
         puts "victorops -- failed to #{@event['action']} incident -- #{incident_key}"
         puts "victorops -- response: #{response.inspect}"
       end
     end
   rescue Timeout::Error
     puts 'victorops -- timed out while attempting to ' + @event['action'] + ' a incident -- ' + incident_key
   end
 end
end

Sensu Documentation

Sensu documentation on Handlers

Updated on November 9, 2017

Was this article helpful?

Related Articles