logstash-output-opsgenie 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e68b7e70a1bd19e7719676111e533006cfb3b9c
4
+ data.tar.gz: 3f7ef7b387ac48afd4816c48d56388c420c47fe3
5
+ SHA512:
6
+ metadata.gz: 809699c36041fbe620f093e6432e559313ae3d0e10ddfd5d67611503ce90f5969c5119f3d4a4d88faa54516c0b2cae45581347924dc666541e45cb73bba390d0
7
+ data.tar.gz: dfa1e80f6c73a9a6e905b03b7e965fdf1747a9657fcdc88e5a08f35f1b8688634683e6134f8aa4adacd346c44652b1ec05e36503abffe454cd9ac7fe31f6aef2
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,223 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+ require 'json'
5
+ require "uri"
6
+ require "net/http"
7
+ require "net/https"
8
+
9
+ # The OpsGenie output is used to Create, Close, Acknowledge Alerts and Add Note to alerts in OpsGenie.
10
+ # For this output to work, your event must contain "opsgenieAction" field and you must configure apiKey field in configuration.
11
+ # If opsgenieAction is "create", event must contain "message" field.
12
+ # For other actions ("close", "acknowledge" or "note"), event must contain "alias" or "alertId" field.
13
+ #
14
+ # If your event have the following fields (If you use default field names).
15
+ #
16
+ # Example JSON-encoded event:
17
+ #
18
+ # {
19
+ # "message": "alert_message",
20
+ # "@version": "1",
21
+ # "@timestamp": "2015-09-22T11:20:00.250Z",
22
+ # "host": "192.168.1.1",
23
+ # "opsgenieAction": "create",
24
+ # "alias": "alert_alias",
25
+ # "teams": ["teams"],
26
+ # "recipients": "the-recipients",
27
+ # "description": "alert_description",
28
+ # "actions": ["actions"],
29
+ # "source": "alert_source",
30
+ # "tags": ["tags"],
31
+ # "entity": "alert_entity",
32
+ # "user": "alert_owner",
33
+ # "note": "additional_alert_note"
34
+ # "details": {
35
+ # "extra_prop1": "value1",
36
+ # "extra_prop2": "value2"
37
+ # }
38
+ # }
39
+ #
40
+ # An alert with following properties will be created.
41
+ #
42
+ # {
43
+ # "message": "alert_message",
44
+ # "alias": "alert_alias",
45
+ # "teams": ["teams"],
46
+ # "description": "alert_description",
47
+ # "source": "alert_source",
48
+ # "tags": [
49
+ # "tags"
50
+ # ],
51
+ # "recipients": [
52
+ # "the-recipients"
53
+ # ],
54
+ # "details": {
55
+ # "extra_prop1": "value1",
56
+ # "extra_prop2": "value2"
57
+ # },
58
+ # "actions": [
59
+ # "actions"
60
+ # ],
61
+ # "entity": "alert_entity",
62
+ # }
63
+ #
64
+ # Fields with prefix "Attribute" are the keys of the fields will be extracted from Logstash event.
65
+ # For more information about the api requests and their contents,
66
+ # please refer to Alert API("https://www.opsgenie.com/docs/web-api/alert-api") support doc.
67
+
68
+ class LogStash::Outputs::OpsGenie < LogStash::Outputs::Base
69
+
70
+ config_name "opsgenie"
71
+
72
+ # OpsGenie Logstash Integration API Key
73
+ config :apiKey, :validate => :string, :required => true
74
+
75
+ # Host of opsgenie api, normally you should not need to change this field.
76
+ config :opsGenieBaseUrl, :validate => :string, :required => false, :default => 'https://api.opsgenie.com'
77
+
78
+ # Url will be used to create alerts in OpsGenie
79
+ config :createActionUrl, :validate => :string, :required => false, :default =>'/v1/json/alert'
80
+
81
+ # Url will be used to close alerts in OpsGenie
82
+ config :closeActionUrl, :validate => :string, :required => false, :default =>'/v1/json/alert/close'
83
+
84
+ # Url will be used to acknowledge alerts in OpsGenie
85
+ config :acknowledgeActionUrl, :validate => :string, :required => false, :default =>'/v1/json/alert/acknowledge'
86
+
87
+ # Url will be used to add notes to alerts in OpsGenie
88
+ config :noteActionUrl, :validate => :string, :required => false, :default =>'/v1/json/alert/note'
89
+
90
+
91
+ # The value of this field holds the name of the action will be executed in OpsGenie.
92
+ # This field must be in Event object. Should be one of "create", "close", "acknowledge" or "note". Other values will be discarded.
93
+ config :actionAttribute, :validate => :string, :required => false, :default => 'opsgenieAction'
94
+
95
+ # The value of this field holds the Id of the alert that actions will be executed.
96
+ # One of "alertId" or "alias" field must be in Event object, except from "create" action
97
+ config :alertIdAttribute, :validate => :string, :required => false, :default => 'alertId'
98
+
99
+ # The value of this field holds the alias of the alert that actions will be executed.
100
+ # One of "alertId" or "alias" field must be in Event object, except from "create" action
101
+ config :aliasAttribute, :validate => :string, :required => false, :default => 'alias'
102
+
103
+ # The value of this field holds the alert text.
104
+ config :messageAttribute, :validate => :string, :required => false, :default => 'message'
105
+
106
+ # The value of this field holds the list of team names which will be responsible for the alert.
107
+ config :teamsAttribute, :validate => :string, :required => false, :default => 'teams'
108
+
109
+ # The value of this field holds the detailed description of the alert.
110
+ config :descriptionAttribute, :validate => :string, :required => false, :default => 'description'
111
+
112
+ # The value of this field holds the optional user, group, schedule or escalation names to calculate which users will receive the notifications of the alert.
113
+ config :recipientsAttribute, :validate => :string, :required => false, :default => 'recipients'
114
+
115
+ # The value of this field holds the comma separated list of actions that can be executed on the alert.
116
+ config :actionsAttribute, :validate => :string, :required => false, :default => 'actions'
117
+
118
+ # The value of this field holds the source of alert. By default, it will be assigned to IP address of incoming request.
119
+ config :sourceAttribute, :validate => :string, :required => false, :default => 'source'
120
+
121
+ # The value of this field holds the comma separated list of labels attached to the alert.
122
+ config :tagsAttribute, :validate => :string, :required => false, :default => 'tags'
123
+
124
+ # The value of this field holds the set of user defined properties. This will be specified as a nested JSON map
125
+ config :detailsAttribute, :validate => :string, :required => false, :default => 'details'
126
+
127
+ # The value of this field holds the entity the alert is related to.
128
+ config :entityAttribute, :validate => :string, :required => false, :default => 'entity'
129
+
130
+ # The value of this field holds the default owner of the execution. If user is not specified, owner of account will be used.
131
+ config :userAttribute, :validate => :string, :required => false, :default => 'user'
132
+
133
+ # The value of this field holds the additional alert note.
134
+ config :noteAttribute, :validate => :string, :required => false, :default => 'note'
135
+
136
+ public
137
+ def register
138
+ end # def register
139
+
140
+ public
141
+ def populateAliasOrId(event, params)
142
+ alertAlias = event[@aliasAttribute] if event[@aliasAttribute]
143
+ if alertAlias == nil then
144
+ alertId = event[@alertIdAttribute] if event[@alertIdAttribute]
145
+ if !(alertId == nil) then
146
+ params['alertId'] = alertId;
147
+ end
148
+ else
149
+ params['alias'] = alertAlias
150
+ end
151
+ end#def populateAliasOrId
152
+
153
+ public
154
+ def executePost(uri, params)
155
+ if not uri == nil then
156
+ @logger.info("Executing url #{uri}")
157
+ url = URI(uri)
158
+ http = Net::HTTP.new(url.host, url.port)
159
+ if url.scheme == 'https'
160
+ http.use_ssl = true
161
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
162
+ end
163
+ request = Net::HTTP::Post.new(url.path)
164
+ request.body = params.to_json
165
+ response = http.request(request)
166
+ body = response.body
167
+ body = JSON.parse(body)
168
+ @logger.warn("Executed [#{uri}]. Response:[#{body}]")
169
+ end
170
+ end#def executePost
171
+
172
+ public
173
+ def receive(event)
174
+ return unless output?(event)
175
+ @logger.info("processing #{event}")
176
+ opsGenieAction = event[@actionAttribute] if event[@actionAttribute]
177
+ if opsGenieAction then
178
+ params = { :apiKey => @apiKey}
179
+ case opsGenieAction.downcase
180
+ when "create"
181
+ uri = "#{@opsGenieBaseUrl}#{@createActionUrl}"
182
+ params = populateCreateAlertContent(params,event)
183
+ when "close"
184
+ uri = "#{@opsGenieBaseUrl}#{@closeActionUrl}"
185
+ when "acknowledge"
186
+ uri = "#{@opsGenieBaseUrl}#{@acknowledgeActionUrl}"
187
+ when "note"
188
+ uri = "#{@opsGenieBaseUrl}#{@noteActionUrl}"
189
+ else
190
+ @logger.warn("Action #{opsGenieAction} does not match any available action, discarding..")
191
+ return
192
+ end
193
+
194
+ populateCommonContent(params, event)
195
+ executePost(uri, params)
196
+ else
197
+ @logger.warn("No opsgenie action defined")
198
+ return
199
+ end
200
+ end # def receive
201
+
202
+ private
203
+ def populateCreateAlertContent(params,event)
204
+ params['message'] = event[@messageAttribute] if event[@messageAttribute]
205
+ params['teams'] = event[@teamsAttribute] if event[@teamsAttribute]
206
+ params['description'] = event[@descriptionAttribute] if event[@descriptionAttribute]
207
+ params['recipients'] = event[@recipientsAttribute] if event[@recipientsAttribute]
208
+ params['actions'] = event[@actionsAttribute] if event[@actionsAttribute]
209
+ params['tags'] = event[@tagsAttribute] if event[@tagsAttribute]
210
+ params['entity'] = event[@entityAttribute] if event[@entityAttribute]
211
+ params['details'] = event[@detailsAttribute] if event[@detailsAttribute]
212
+ return params
213
+ end
214
+
215
+ private
216
+ def populateCommonContent(params,event)
217
+ populateAliasOrId(event, params)
218
+ params['source'] = event[@sourceAttribute] if event[@sourceAttribute]
219
+ params['user'] = event[@userAttribute] if event[@userAttribute]
220
+ params['note'] = event[@noteAttribute] if event[@noteAttribute]
221
+ end
222
+
223
+ end # class LogStash::Outputs::OpsGenie
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-opsgenie'
3
+ s.version = "0.1.0"
4
+ s.licenses = ["Apache License (2.0)"]
5
+ s.summary = "This output Creates, Closes, Acknowledges alerts and Adds Note to alerts in OpsGenie."
6
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
7
+ s.authors = ["Elastic"]
8
+ s.email = "info@elastic.co"
9
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core", ">= 1.4.0", "< 2.0.0"
22
+ s.add_runtime_dependency "logstash-codec-plain"
23
+ s.add_development_dependency "logstash-devutils"
24
+ end
@@ -0,0 +1,52 @@
1
+ require "logstash/devutils/rspec/spec_helper"
2
+ require "logstash/outputs/opsgenie"
3
+ require "logstash/codecs/plain"
4
+ require "logstash/event"
5
+
6
+ describe LogStash::Outputs::OpsGenie do
7
+
8
+ subject {LogStash::Outputs::OpsGenie.new( "apiKey" => "my_api_key" )}
9
+ let(:logger) { subject.logger}
10
+
11
+ describe "receive message" do
12
+
13
+ it "when opsgenieAction is not specified" do
14
+ expect(logger).to receive(:warn).with("No opsgenie action defined").once
15
+ subject.receive({"message" => "test_alert","@version" => "1","@timestamp" => "2015-09-22T11:20:00.250Z"})
16
+ end
17
+
18
+ it "when opsgenieAction is not valid" do
19
+ action = "invalid"
20
+ expect(logger).to receive(:warn).with("Action #{action} does not match any available action, discarding..").once
21
+ subject.receive({"message" => "test_alert","@version" => "1","@timestamp" => "2015-09-22T11:20:00.250Z", "opsgenieAction" => action})
22
+ end
23
+
24
+ it "when opsGenieAction is 'create'" do
25
+ event = {"message" => "test_alert", "@version" => "1", "@timestamp" => "2015-09-22T11:20:00.250Z", "opsgenieAction" => "create"}
26
+ expect(logger).to receive(:info).with("processing #{event}").once
27
+ expect(logger).to receive(:info).with("Executing url #{subject.opsGenieBaseUrl}#{subject.createActionUrl}").once
28
+ subject.receive(event)
29
+ end
30
+
31
+ it "when opsGenieAction is 'close'" do
32
+ event = {"message" => "test_alert", "@version" => "1", "@timestamp" => "2015-09-22T11:20:00.250Z", "opsgenieAction" => "close"}
33
+ expect(logger).to receive(:info).with("processing #{event}").once
34
+ expect(logger).to receive(:info).with("Executing url #{subject.opsGenieBaseUrl}#{subject.closeActionUrl}").once
35
+ subject.receive(event)
36
+ end
37
+
38
+ it "when opsGenieAction is 'acknowledge'" do
39
+ event = {"message" => "test_alert", "@version" => "1", "@timestamp" => "2015-09-22T11:20:00.250Z", "opsgenieAction" => "acknowledge"}
40
+ expect(logger).to receive(:info).with("processing #{event}").once
41
+ expect(logger).to receive(:info).with("Executing url #{subject.opsGenieBaseUrl}#{subject.acknowledgeActionUrl}").once
42
+ subject.receive(event)
43
+ end
44
+
45
+ it "when opsGenieAction is 'note'" do
46
+ event = {"message" => "test_alert", "@version" => "1", "@timestamp" => "2015-09-22T11:20:00.250Z", "opsgenieAction" => "note"}
47
+ expect(logger).to receive(:info).with("processing #{event}").once
48
+ expect(logger).to receive(:info).with("Executing url #{subject.opsGenieBaseUrl}#{subject.noteActionUrl}").once
49
+ subject.receive(event)
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-opsgenie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: 1.4.0
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ name: logstash-core
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-codec-plain
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ name: logstash-devutils
54
+ prerelease: false
55
+ type: :development
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
62
+ email: info@elastic.co
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - CHANGELOG.md
68
+ - Gemfile
69
+ - LICENSE
70
+ - NOTICE.TXT
71
+ - README.md
72
+ - lib/logstash/outputs/opsgenie.rb
73
+ - logstash-output-opsgenie.gemspec
74
+ - spec/outputs/opsgenie_spec.rb
75
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
76
+ licenses:
77
+ - Apache License (2.0)
78
+ metadata:
79
+ logstash_plugin: 'true'
80
+ logstash_group: output
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.6
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: This output Creates, Closes, Acknowledges alerts and Adds Note to alerts in OpsGenie.
101
+ test_files:
102
+ - spec/outputs/opsgenie_spec.rb