annotate mod_http_host_status_check/mod_http_host_status_check.lua @ 2227:7356d722e180

mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
author Matthew Wild <mwild1@gmail.com>
date Sat, 02 Jul 2016 16:04:06 +0100
parents 5fcf9d558250
children 03a4c3209f21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2219
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local heartbeats = module:shared("/*/host_status_check/heartbeats");
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local events = module:shared("/*/host_status_check/connection_events");
2227
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
3 local host_status_ok = module:shared("host_status_ok");
2219
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local time = require "socket".gettime;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local template = require "util.interpolation".new("%b{}", function (s) return s end)
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local st = require "util.stanza";
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 module:depends "http"
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local threshold = module:get_option_number("status_check_heartbeat_threshold", 5);
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local function status_string(status, duration, comment)
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local string_timestamp = "";
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 if duration then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 string_timestamp = ("(%0.2fs%s)"):format(duration, comment or "");
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 elseif comment then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 string_timestamp = ("(%s)"):format(comment);
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 else
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return status and "UP" or "DOWN";
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 return (status and "UP " or "DOWN ")..string_timestamp;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local function string_pad(s, len)
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return s..(" "):rep(len-#s);
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local status_page_template = [[
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 STATUS {status}
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 {host_statuses%HOST {item} {idx}
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 }]];
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 function status_page()
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local host_statuses = {};
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local current_time = time();
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local all_ok = true;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 for host in pairs(hosts) do
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local last_heartbeat_time = heartbeats[host];
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local ok, status_text = true, "OK";
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local is_component = hosts[host].type == "component" and hosts[host].modules.component;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if is_component then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local current_status = hosts[host].modules.component.connected;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if events[host] then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local tracked_status = events[host].connected;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if tracked_status == current_status then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 status_text = status_string(current_status, time() - events[host].timestamp);
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 else
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 status_text = status_string(current_status, nil, "!");
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 else
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 status_text = status_string(current_status, nil, "?");
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if not current_status then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 ok = false;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 else
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local event_info = events[host];
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local connected = true;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if event_info then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 connected = event_info.connected;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 status_text = status_string(connected, event_info and (time() - events[host].timestamp), not event_info and "?");
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if last_heartbeat_time then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local time_since_heartbeat = current_time - last_heartbeat_time;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if ok then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 if time_since_heartbeat > threshold then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 status_text = ("TIMEOUT (%0.2fs)"):format(time_since_heartbeat);
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 ok = false;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 else
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 status_text = status_text:gsub("^%S+", "GOOD");
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if not ok then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 all_ok = false;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if not ok or is_component or last_heartbeat_time then
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 host_statuses[host] = string_pad(status_text, 20);
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
2227
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
90 local last_ok = host_status_ok[host];
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
91 if last_ok ~= ok then
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
92 if last_ok ~= nil then
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
93 module:log("warn", "Host status check %s (%s)", ok and "OK" or "FAILED", status_text);
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
94 end
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
95 host_status_ok[host] = ok;
7356d722e180 mod_http_host_status_check: Emit log message when host status is not the same as last time we looked
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
96 end
2219
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local page = template(status_page_template, {
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 status = all_ok and "OK" or "FAIL";
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 host_statuses = host_statuses;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 });
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 return page;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 module:provides("http", {
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 route = {
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 GET = status_page;
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 };
5fcf9d558250 Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 })