vaimo-messages-handler 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 943072b1a328bf2fb6c784aa8bf3e18fd11dd573
4
- data.tar.gz: 80374526e30581bb5d7bdbce22e1709fb7173c75
3
+ metadata.gz: 4f9718717b4b77c04dbf01d20dfcd1e0c3d1f580
4
+ data.tar.gz: 6360ab2197887a7a4cbeabcdf14329f3345d2542
5
5
  SHA512:
6
- metadata.gz: 73e3f616aabbe948c607582ffe4a3bccb0b6c5eab4ee5259c8854bec0d23c7f6f2f96c63e9a1fb2a526d7939fe2e00d6b3b2efa62b52b49df27b53d94dd92549
7
- data.tar.gz: 50b7fd10bef5877c4e74d4fef2fbf1c3f24475fa141e998c79713170bb4d9f7ef8ce0330a0a6134deda6e079733fbe396f1a5afe2b6ba673d56fd2305c83e4db
6
+ metadata.gz: 824942302d4dc927decdbc468410d92f93c7706c4c33c03b6a27619c56284aebf42aeb354b7cfd8527f9537d642d427bdedf4ce771b74a0f9610c4f4b3ce6bcc
7
+ data.tar.gz: bc3c7b1bbb2ee1f387fb0652c2c3ca8472c1940773c2d7fea873d53a3d0fef0e8000a249cf4b940f6bf46d1ada9039b424ad86128c6d2d77dc97059e8a13d4c6
data/bin/send-message.rb CHANGED
@@ -26,102 +26,124 @@
26
26
  # See LICENSE_VAIMO.txt for license details.
27
27
  #
28
28
 
29
- require 'date'
30
- require 'sensu-handler'
29
+ require 'json'
31
30
  require 'net/http'
31
+ require 'net/https'
32
32
  require 'uri'
33
- require 'json'
33
+ require 'logger'
34
34
 
35
- # VaimoMessages
36
- class VaimoMessages < Sensu::Handler
37
- option :json_config_name,
38
- description: 'Name of the JSON Configuration block (default: messages)',
39
- short: '-j JsonConfig',
40
- long: '--json_config JsonConfig',
41
- required: false,
42
- default: 'messages'
43
-
44
- def config_settings
45
- settings[config[:json_config_name]]
46
- end
35
+ def sensu_config_files
36
+ ['/etc/sensu/config.json'] + Dir['/etc/sensu/conf.d/**/*.json']
37
+ end
47
38
 
48
- def client_name
49
- @event['client']['name']
50
- end
39
+ def sensu_load_config(filename)
40
+ JSON.parse(File.open(filename, 'r').read)
41
+ rescue
42
+ {}
43
+ end
51
44
 
52
- def check_name
53
- @event['check']['name']
45
+ def sensu_settings
46
+ @sensu_settings ||= sensu_config_files.map { |f| sensu_load_config(f) }.reduce do |a, b|
47
+ sensu_deep_merge(a, b)
54
48
  end
49
+ end
55
50
 
56
- def level
57
- @event['action'].eql?('resolve') ? 'normal' : 'error'
58
- end
51
+ def sensu_setting(key)
52
+ @sensu_settings[key]
53
+ end
59
54
 
60
- def web_hook
61
- config_settings['web_hook'] || nil
55
+ def sensu_deep_merge(hash_one, hash_two)
56
+ merged = hash_one.dup
57
+ hash_two.each do |key, value|
58
+ merged[key] = if hash_one[key].is_a?(Hash) && value.is_a?(Hash)
59
+ deep_merge(hash_one[key], value)
60
+ elsif hash_one[key].is_a?(Array) && value.is_a?(Array)
61
+ hash_one[key].concat(value).uniq
62
+ else
63
+ value
64
+ end
62
65
  end
66
+ merged
67
+ end
63
68
 
64
- def auth_token
65
- config_settings['auth_token'] || nil
66
- end
69
+ def messages_key_value(args, key, default)
70
+ args[key] || default
71
+ end
67
72
 
68
- def recipient
69
- config_settings['recipient'] || nil
70
- end
73
+ def messages_post_args(args, settings)
74
+ {
75
+ auth_token: settings['auth_token'],
76
+ receiver: settings['recipient'],
77
+ from: settings['from'],
78
+ server: messages_key_value(args, :server, nil),
79
+ level: messages_key_value(args, :level, nil),
80
+ message: messages_key_value(args, :message, nil),
81
+ component: messages_key_value(args, :component, nil)
82
+ }
83
+ end
84
+
85
+ def messages_parse_response(response)
86
+ data = JSON.parse(response)
71
87
 
72
- def from
73
- config_settings['from'] || 'SensuAlert'
88
+ hash = {}
89
+ data.each do |key, value|
90
+ hash[key] = value
74
91
  end
75
92
 
76
- def post_args
77
- {
78
- auth_token: auth_token,
79
- receiver: recipient,
80
- from: from,
81
- server: client_name,
82
- level: level,
83
- message: nil,
84
- component: check_name
85
- }
93
+ raise "Messages WebHook: #{hash['message']}" if hash['error']
94
+
95
+ hash
96
+ end
97
+
98
+ def messages_level(status)
99
+ case status
100
+ when 0
101
+ 'normal'
102
+ when 1
103
+ 'warning'
104
+ when 2
105
+ 'error'
86
106
  end
107
+ end
87
108
 
88
- def parse_response(response)
89
- data = JSON.parse(response)
109
+ def messages_send(post_args, settings)
110
+ header = { 'Content-Type': 'text/json' }
90
111
 
91
- hash = {}
92
- data.each do |key, value|
93
- hash[key] = value
94
- end
112
+ return unless settings['webhook_url']
95
113
 
96
- raise ErrorHandler "Messages WebHook: #{hash['message']}" if hash['error']
114
+ uri = URI.parse(settings['webhook_url'])
97
115
 
98
- hash
99
- end
116
+ http = Net::HTTP.new(uri.host, uri.port)
117
+ http.use_ssl = true
100
118
 
101
- def send_message
102
- header = ['Content-Type' => 'text/json']
119
+ request = Net::HTTP::Post.new(uri.request_uri, header)
120
+ request.body = post_args.to_json
103
121
 
104
- uri = URI.parse(web_hook)
122
+ response = http.request(request)
105
123
 
106
- http = Net::HTTP.new(uri.host, uri.port)
107
- http.use_ssl = true
124
+ messages_parse_response(response.body)
125
+ end
108
126
 
109
- request = Net::HTTP::Post.new(uri.request_uri, header)
110
- request.body = post_args.to_json
127
+ log = Logger.new('/tmp/sensu-messages-handler.log')
128
+ log.level = Logger::INFO
111
129
 
112
- response = http.request(request)
130
+ begin
131
+ event = JSON.parse(STDIN.read)
113
132
 
114
- parse_response(response.body)
115
- end
133
+ sensu_settings
134
+ messages_settings = sensu_setting('messages')
116
135
 
117
- def handle
118
- raise 'No web_hook configured' if web_hook.nil?
119
- raise 'No auth_token configured' if auth_token.nil?
120
- raise 'No recipient configured' if recipient.nil?
136
+ args = {
137
+ server: event[0]['client']['name'],
138
+ component: event[0]['check']['name'],
139
+ level: messages_level(event[0]['check']['status'])
140
+ }
121
141
 
122
- send_message
123
- end
142
+ post_args = messages_post_args(args, messages_settings)
124
143
 
125
- class ErrorHandler < StandardError
126
- end
144
+ messages_send(post_args, messages_settings)
145
+ rescue => e
146
+ log.error "Messages Error: #{e}"
147
+ raise e
127
148
  end
149
+
@@ -1,15 +1,6 @@
1
1
  require 'vaimo-messages-handler/version'
2
- require 'net/http'
3
- require 'uri'
4
- require 'json'
5
2
 
6
3
  # Messages for Vaimo System
7
4
  module VaimoMessagesHandler
8
- # Client for Sending data to WebHook
9
- class Client
10
- end
11
5
 
12
- # Error Handler
13
- class ErrorHandler < StandardError
14
- end
15
6
  end
@@ -1,8 +1,8 @@
1
1
  module VaimoMessagesHandler
2
2
  module Version
3
- MAJOR = 1
3
+ MAJOR = 2
4
4
  MINOR = 0
5
- PATCH = 2
5
+ PATCH = 0
6
6
 
7
7
  VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vaimo-messages-handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Margus Heinmaa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-04 00:00:00.000000000 Z
11
+ date: 2018-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sensu-plugin
@@ -153,7 +153,6 @@ dependencies:
153
153
  description: Sensu Handler Messages
154
154
  email: "<margus.heinmaa@vaimo.com>"
155
155
  executables:
156
- - copy-send-message.rb
157
156
  - send-message.rb
158
157
  extensions: []
159
158
  extra_rdoc_files: []
@@ -161,7 +160,6 @@ files:
161
160
  - CHANGELOG.md
162
161
  - LICENSE_VAIMO.txt
163
162
  - README.md
164
- - bin/copy-send-message.rb
165
163
  - bin/send-message.rb
166
164
  - lib/vaimo-messages-handler.rb
167
165
  - lib/vaimo-messages-handler/version.rb
@@ -177,7 +175,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
175
  requirements:
178
176
  - - ">="
179
177
  - !ruby/object:Gem::Version
180
- version: 2.3.0
178
+ version: 2.2.0
181
179
  required_rubygems_version: !ruby/object:Gem::Requirement
182
180
  requirements:
183
181
  - - ">="
@@ -1,81 +0,0 @@
1
- #! /usr/bin/env ruby
2
- #
3
- # send-message.rb
4
- #
5
- # DESCRIPTION:
6
- # Sensu Messages Handler For Custom Messages gateway
7
- #
8
- # OUTPUT:
9
- # None unless there is an error
10
- #
11
- # PLATFORMS:
12
- # Linux
13
- #
14
- # DEPENDENCIES:
15
- # gem: sensu-handler
16
- #
17
- # USAGE:
18
- #
19
- # Configure your vaimo-messages web_hook, auth_token, recipient and from here:
20
- # vaimo-messages.json
21
- #
22
- # NOTES:
23
- #
24
- # LICENSE:
25
- # Copyright (c) Vaimo Group. All rights reserved.
26
- # See LICENSE_VAIMO.txt for license details.
27
- #
28
-
29
- require 'date'
30
- require 'sensu-handler'
31
- require 'timeout'
32
-
33
- # VaimoMessages Handler
34
- class VaimoMessagesHandler < Sensu::Handler
35
- def send_message
36
- client = VaimoMessages::Client.new(
37
- web_hook: web_hook,
38
- auth_token: auth_token,
39
- receiver: recipient,
40
- from: from
41
- )
42
-
43
- client.send_message(server: server, level: 'error', component: component)
44
- end
45
-
46
- def web_hook
47
- settings['vaimo-messages']['web_hook']
48
- end
49
-
50
- def auth_token
51
- settings['vaimo-messages']['auth_token']
52
- end
53
-
54
- def recipient
55
- settings['vaimo-messages']['recipient']
56
- end
57
-
58
- def from
59
- if settings['vaimo-messages']['from'].is_a?(String)
60
- return settings['vaimo-messages']['from']
61
- end
62
-
63
- Null
64
- end
65
-
66
- def server
67
- @event['client']['name']
68
- end
69
-
70
- def component
71
- @event['check']['name']
72
- end
73
-
74
- def handle
75
- Timeout.timeout 10 do
76
- send_message
77
- end
78
- rescue Timeout::Error
79
- puts 'timed out while attempting to send message'
80
- end
81
- end