vaimo-messages-handler 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/send-message.rb +92 -3
- data/lib/vaimo-messages-handler/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 943072b1a328bf2fb6c784aa8bf3e18fd11dd573
|
4
|
+
data.tar.gz: 80374526e30581bb5d7bdbce22e1709fb7173c75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 '
|
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
|
-
|
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
|