Skip to content

Commit

Permalink
Add env vars (#21)
Browse files Browse the repository at this point in the history
* Updating bonsai config

* Add ability to read env-vars

* Make api_url validation same as routing key

* Updates to readme

* Update changelog

Co-Authored-By: Cameron Johnston <cameron@rootdown.net>
  • Loading branch information
2 people authored and majormoses committed Oct 26, 2019
1 parent 29222ee commit 36d6003
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions .bonsai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ builds:
- "entity.system.arch == 'amd64'"
- "entity.system.platform_family == 'debian'"
- "entity.system.platform_version.split('.')[0] == '9'"

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]
### Added
- Adds ability to use the environment variables `VICTOROPS_API_URL` & `VICTOROPS_ROUTING_KEY` for Sensu Go

## [2.0.0] - 2019-06-06
### Breaking Changes
- Update minimum required ruby version to 2.3. Drop unsupported ruby versions.
Expand Down
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"victorops": {
"api_url": "YOUR_API_URL_WHITOUT_ROUTING_KEY",
"routing_key": "everyone"
},
"handlers": {
"victorops": {
"type": "pipe",
"command": "handler-victorops.rb"
}
}
}
```
Expand All @@ -36,4 +42,32 @@ If you have multiple VictorOps accounts and need to route alerts to different en
"command": "/opt/sensu/embedded/bin/handler-victorops.rb -name victorops_endpoint1"
},
```


### Sensu Go Enabled

This plugin is also Sensu Go enabled. You can map events into ruby by using the flag `--map_go_event_into_ruby` as part of your command. Practically, this might look like:

```
---
type: Handler
spec:
metadata:
name: victorops
namespace: default
labels:
annotations:
type: pipe
command: handler-victorops.rb --map_go_event_into_ruby
timeout: 0
handlers: []
filters:
- is_incident
env_vars:
- KEEPALIVE_SLACK_WEBHOOK=https://alert.victorops.com/integrations/generic/01234567/alert/0123456789101112
- VICTOROPS_ROUTING_KEY=testing
runtime_assets:
- sensu-plugins-victorops
- sensu-ruby-runtime
```

You'll also note that the plugin also supports environment variables. You can use these without having to have a file locally to load in those values.
26 changes: 21 additions & 5 deletions bin/handler-victorops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
# arguments:
# - settingsname: Sensu settings name, defaults to victorops
# - routingkey: VictorOps routing key
#
# NOTE: This plugin is Sensu Go enabled. To use this plugin with Sensu Go, add --map_go_event_into_ruby as part of the command.
# Example:
# handler-victorops.rb --map_go_event_into_ruby

require 'sensu-handler'
require 'uri'
Expand All @@ -15,6 +19,12 @@
require 'json'

class VictorOps < Sensu::Handler
option :api_url,
description: 'VictorOps API URL without routing key',
short: '-a APIURL',
long: '--api-url APIURL',
default: nil

option :settingsname,
description: 'Sensu settings name',
short: '-n NAME',
Expand All @@ -28,17 +38,23 @@ class VictorOps < Sensu::Handler
default: nil

def handle
# validate that we have settings
# validate that we have a settings name
unless defined? settings[config[:settingsname]] && !settings[config[:settingsname]].nil?
raise "victorops.rb sensu setting '#{config[:settingsname]}' not found or empty"
end
unless defined? settings[config[:settingsname]]['api_url'] && !settings[config[:settingsname]]['api_url'].nil?

# validate that we have an api url - environment variables take precedence
api_url = ENV['VICTOROPS_API_URL']
api_url = config[:api_url] if api_url.nil?
api_url = settings[config[:settingsname]]['api_url'] if api_url.nil?

unless defined? api_url && !api_url.nil?
raise "victorops.rb sensu setting '#{config[:settingsname]}.api_url' not found or empty"
end
api_url = settings[config[:settingsname]]['api_url']

# validate that we have a routing key - command arguments take precedence
routing_key = config[:routing_key]
# validate that we have a routing key - environment variables take precedence
routing_key = ENV['VICTOROPS_ROUTING_KEY']
routing_key = config[:routing_key] if routing_key.nil?
routing_key = settings[config[:settingsname]]['routing_key'] if routing_key.nil?

unless defined? routing_key && !routing_key.nil?
Expand Down

0 comments on commit 36d6003

Please sign in to comment.