-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhealth_check.lua
78 lines (69 loc) · 2.29 KB
/
health_check.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
--- HealthCheck module.
-- Register a background task to pull backend and server stats and push them.
--
-- @module health_check
-- TODO check queues for way to hook om email_alert
-- Timer in seconds
local timer = 10
-- Sentry Configuration
-- TODO find a way to extract variable from haproxy conf file
-- TODO configurate different dsn according to backend
local sentry_dsn_url = "http://[email protected]:9000/2"
function get_sentry_raven()
local raven = require "raven"
local rvn = raven.new {
-- multiple senders are available for different networking backends,
-- doing a custom one is also very easy.
sender = require("raven.senders.luasocket").new { dsn = sentry_dsn_url }
}
return rvn
end
function sentry_send_message(rvn, message, conf)
local id, err = rvn:captureMessage(message, conf)
if not id then
core.Alert(err)
end
end
function sentry_level_message(status)
if status == "DOWN" then
return "error"
end
if status ~= "UP" then
return "warning"
end
return "info"
end
--- Main Task
function health_check_task()
local rvn = get_sentry_raven()
while true do
for backend_key, backend in pairs(core.backends) do
-- TODO filter on backend.name
local backend_stats = backend:get_stats()
if backend_stats.status ~= "UP" then
sentry_send_message(
rvn,
"Backend " .. backend.name .. " Status " .. backend_stats.status,
{ extra = backend_stats, tags = { backend = backend_key } }
)
end
-- Backend can be UP and have only 1 healthy server
-- Server in transient state (UP 1/x - DN 1/x) will send a warning instead of an error
for server_key, server in pairs(backend.servers) do
local server_stats = server:get_stats()
if server_stats.status ~= "UP" then
sentry_send_message(
rvn,
"Backend " .. server_stats.pxname .. " Server " .. server_key .. " Status " .. server_stats.status,
{ extra = server_stats,
tags = { backend = server_stats.pxname, server = server_key },
level = sentry_level_message(server_stats.status) }
)
end
end
end
core.sleep(timer)
end
end
-- Register HAProxy background task
core.register_task(health_check_task)