forked from boundary/boundary-plugin-consul-healthchecks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
166 lines (131 loc) · 7.68 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
-- Copyright 2015 Boundary, Inc.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
local framework = require('framework.lua')
local Plugin = framework.Plugin
local DataSourcePoller = framework.DataSourcePoller
local WebRequestDataSource = framework.WebRequestDataSource
local PollerCollection = framework.PollerCollection
local url = require('url')
local notEmpty = framework.string.notEmpty
local isHttpSuccess = framework.util.isHttpSuccess
local params = framework.params
local os = require('os')
local consul_url = "http://localhost:8500/v1/health/node/" .. os.hostname() .. "?pretty=1"
local string = require('string')
local table = require('table')
local json = require('json')
-- setup plugin poller
local function createPollers(params)
local pollers = PollerCollection:new()
local options = url.parse(consul_url)
options.protocol = 'http'
options.method = "GET"
options.meta = { source = "Consul", ignoreStatusCode = false, debugEnabled = true }
options.wait_for_end = true -- make sure to get all Consul health check output
local data_source = WebRequestDataSource:new(options)
local time_interval = tonumber(notEmpty(params.pollInterval, 1000))
if time_interval < 500 then time_interval = time_interval * 1000 end
local poller = DataSourcePoller:new(time_interval, data_source)
pollers:add(poller)
return pollers
end
local pollers = createPollers(params)
local function logFailure(str)
process.stderr:write(str)
end
local plugin = Plugin:new(params, pollers)
local healthcheck_result = {}
function plugin:onParseValues(body, extra)
if not extra.info.ignoreStatusCode and not isHttpSuccess(extra.status_code) then
self:emitEvent('error', ('%s Returned %d'):format(extra.info.source, extra.status_code), self.source, self.source, ('HTTP Request Returned %d instead of OK'):format(extra.status_code))
if (extra.info.debugEnabled) then
logFailure(extra.info.source .. ' status code: ' .. extra.status_code .. '\n')
logFailure(extra.info.source .. ' body:\n' .. tostring(body) .. '\n')
end
return {}
end
local healthcheck_output_json = json.parse(body)
local event_type = 'info'
local statechange_count = 0
local healthcheck_count = 0
local result = {}
for i,healthcheck_output in next,healthcheck_output_json,nil do
healthcheck_count = healthcheck_count+1
if healthcheck_result[healthcheck_output.CheckID] == nil or not string.find(healthcheck_result[healthcheck_output.CheckID], healthcheck_output.Status) then
healthcheck_result[healthcheck_output.CheckID] = healthcheck_output.Status
statechange_count = statechange_count+1
if string.find(healthcheck_output.Status, 'passing') then
event_type = 'info'
if params.detailedInfo ~= nil and params.detailedInfo == true then
local j,k = string.find(healthcheck_output.Output, " Output: {")
if k ~= nil then -- embedded json?
local preformatted_healthcheck_output_json = string.sub(healthcheck_output.Output, k, -1)
local preformatted_healthcheck_output = json.parse(preformatted_healthcheck_output_json)
local temp_table = {}
for healthcheck_key,healthcheck_value in next,preformatted_healthcheck_output,nil do
if healthcheck_value.message ~= nil then
table.insert(temp_table, healthcheck_key .. ": " .. healthcheck_value.message)
else
table.insert(temp_table, healthcheck_key .. ": passed health check")
end
end
local healthcheck_msg = table.concat(temp_table, "\\n") -- send newline-separated list msg (formatted per event msg spec)
self:emitEvent(event_type, healthcheck_output.Name .. ": " .. healthcheck_output.CheckID, healthcheck_output.Node, "consul", healthcheck_msg)
else
self:emitEvent(event_type, healthcheck_output.Name .. ": " .. healthcheck_output.CheckID, healthcheck_output.Node, "consul", healthcheck_output.Output)
end
else
self:emitEvent(event_type, healthcheck_output.Name .. ": " .. healthcheck_output.CheckID, healthcheck_output.Node, "consul", "") -- send no msg
end
else
if string.find(healthcheck_output.Status, 'warning') then
event_type = 'warning'
elseif string.find(healthcheck_output.Status, 'critical') or string.find(healthcheck_output.Status, 'unknown') then
event_type = 'critical'
else
event_type = 'error'
end
local j,k = string.find(healthcheck_output.Output, " Output: {")
if k ~= nil then -- embedded json?
local preformatted_healthcheck_output_json = string.sub(healthcheck_output.Output, k, -1)
local preformatted_healthcheck_output = json.parse(preformatted_healthcheck_output_json)
local temp_table = {}
for healthcheck_key,healthcheck_value in next,preformatted_healthcheck_output,nil do
if healthcheck_value.healthy == false then
if healthcheck_value.message ~= nil then
table.insert(temp_table, healthcheck_key .. ": " .. healthcheck_value.message)
else
table.insert(temp_table, healthcheck_key .. ": failed health check")
end
elseif params.detailedInfo ~= nil and params.detailedInfo == true then
if healthcheck_value.message ~= nil then
table.insert(temp_table, healthcheck_key .. ": " .. healthcheck_value.message)
else
table.insert(temp_table, healthcheck_key .. ": passed health check")
end
end
end
local healthcheck_msg = table.concat(temp_table, "\\n") -- send newline-separated list msg (formatted per event msg spec)
self:emitEvent(event_type, healthcheck_output.Name .. ": " .. healthcheck_output.CheckID, healthcheck_output.Node, "consul", healthcheck_msg)
else -- just print what's there
self:emitEvent(event_type, healthcheck_output.Name .. ": " .. healthcheck_output.CheckID, healthcheck_output.Node, "consul", healthcheck_output.Output)
end
end
end
end
result['CONSUL_HEALTHCHECK_STATE_CHANGES'] = {value = statechange_count, source = extra.info.source}
result['CONSUL_HEALTHCHECK_COUNT'] = {value = healthcheck_count, source = extra.info.source}
return result
end
plugin:run()