annotate mod_http_host_status_check/mod_http_host_status_check.lua @ 2219:5fcf9d558250

Three new modules: mod_host_status_check, mod_host_status_heartbeat and mod_http_host_status_check
author Matthew Wild <mwild1@gmail.com>
date Tue, 28 Jun 2016 22:33:13 +0100
parents
children 7356d722e180
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");
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
3
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 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
5 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
6 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
7
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 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
9
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 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
11
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 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
13 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
14 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
15 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
16 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
17 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
18 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
19 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
20 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
21 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
22 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
23
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 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
25 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
26 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
27
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 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
29 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
30 {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
31 }]];
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 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
34 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
35 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
36
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 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
38
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 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
40 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
41
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 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
43
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 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
45
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 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69
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 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
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
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 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
83 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
84 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
85
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 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
87 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
88 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
89 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
90 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
91 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
92 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
93 });
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
94 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
95 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
96
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 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
98 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
99 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
100 };
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 })