vaimo-messages-handler 1.0.1 → 1.0.2

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: 89b17c68d79f1ef94b034d9f753b5696d2386e14
4
- data.tar.gz: cf312e17e699da7f6d0c250c6495e3a701c45ec9
3
+ metadata.gz: 943072b1a328bf2fb6c784aa8bf3e18fd11dd573
4
+ data.tar.gz: 80374526e30581bb5d7bdbce22e1709fb7173c75
5
5
  SHA512:
6
- metadata.gz: 3122e0f06f520700be49bdca317b42581711dbe29292af81cfc9378cea492771a0799b47773e10855f4703401159ffb3240aef744cabab46413cc414d1d6dfd0
7
- data.tar.gz: d5475a8688ffed4ee848bd3de2b3345eda88c10eec7acae77d006358d4ece8d873dc7c1d0f9ba6b1416c540a76f655ad0c543325d67e114df3607d08bba02f35
6
+ metadata.gz: 73e3f616aabbe948c607582ffe4a3bccb0b6c5eab4ee5259c8854bec0d23c7f6f2f96c63e9a1fb2a526d7939fe2e00d6b3b2efa62b52b49df27b53d94dd92549
7
+ data.tar.gz: 50b7fd10bef5877c4e74d4fef2fbf1c3f24475fa141e998c79713170bb4d9f7ef8ce0330a0a6134deda6e079733fbe396f1a5afe2b6ba673d56fd2305c83e4db
data/bin/send-message.rb CHANGED
@@ -28,11 +28,100 @@
28
28
 
29
29
  require 'date'
30
30
  require 'sensu-handler'
31
- require 'timeout'
31
+ require 'net/http'
32
+ require 'uri'
33
+ require 'json'
32
34
 
33
35
  # VaimoMessages
34
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
47
+
48
+ def client_name
49
+ @event['client']['name']
50
+ end
51
+
52
+ def check_name
53
+ @event['check']['name']
54
+ end
55
+
56
+ def level
57
+ @event['action'].eql?('resolve') ? 'normal' : 'error'
58
+ end
59
+
60
+ def web_hook
61
+ config_settings['web_hook'] || nil
62
+ end
63
+
64
+ def auth_token
65
+ config_settings['auth_token'] || nil
66
+ end
67
+
68
+ def recipient
69
+ config_settings['recipient'] || nil
70
+ end
71
+
72
+ def from
73
+ config_settings['from'] || 'SensuAlert'
74
+ end
75
+
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
+ }
86
+ end
87
+
88
+ def parse_response(response)
89
+ data = JSON.parse(response)
90
+
91
+ hash = {}
92
+ data.each do |key, value|
93
+ hash[key] = value
94
+ end
95
+
96
+ raise ErrorHandler "Messages WebHook: #{hash['message']}" if hash['error']
97
+
98
+ hash
99
+ end
100
+
101
+ def send_message
102
+ header = ['Content-Type' => 'text/json']
103
+
104
+ uri = URI.parse(web_hook)
105
+
106
+ http = Net::HTTP.new(uri.host, uri.port)
107
+ http.use_ssl = true
108
+
109
+ request = Net::HTTP::Post.new(uri.request_uri, header)
110
+ request.body = post_args.to_json
111
+
112
+ response = http.request(request)
113
+
114
+ parse_response(response.body)
115
+ end
116
+
35
117
  def handle
36
- File.open('/tmp/ruby-sensu-data', 'w') { |file| file.write(gets) }
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?
121
+
122
+ send_message
123
+ end
124
+
125
+ class ErrorHandler < StandardError
37
126
  end
38
- end
127
+ end
@@ -2,7 +2,7 @@ module VaimoMessagesHandler
2
2
  module Version
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 1
5
+ PATCH = 2
6
6
 
7
7
  VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vaimo-messages-handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Margus Heinmaa