Class: Messhy::HealthChecker
Constant Summary
collapse
- HANDSHAKE_STALENESS_LIMIT =
180
WireguardStatusParser::TIME_UNITS_IN_SECONDS
Instance Attribute Summary collapse
Instance Method Summary
collapse
extract_allowed_ips, extract_endpoint, extract_handshake_time, extract_peer_block, extract_transfer_stats, integer_token?, line_value, parse_handshake_seconds
Constructor Details
Returns a new instance of HealthChecker.
14
15
16
17
|
# File 'lib/messhy/health_checker.rb', line 14
def initialize(config)
@config = config
@ssh_executor = SSHExecutor.new(config)
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
12
13
14
|
# File 'lib/messhy/health_checker.rb', line 12
def config
@config
end
|
Instance Method Details
#ping_node(node_or_ip) ⇒ Object
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
|
# File 'lib/messhy/health_checker.rb', line 64
def ping_node(node_or_ip)
target_node = nil
target_ip = nil
if node_or_ip =~ /^\d+\.\d+\.\d+\.\d+$/
target_ip = node_or_ip
target_node = config.nodes.find { |_, cfg| cfg['private_ip'] == target_ip }&.first
else
target_node = node_or_ip
node_config = config.node_config(target_node)
target_ip = node_config['private_ip'] if node_config
end
unless target_ip
puts "Node or IP not found: #{node_or_ip}"
return
end
puts "Pinging #{target_node || target_ip} (#{target_ip})..."
config.each_node do |source_node, _|
next if source_node == target_node
success = @ssh_executor.ping_node_from(source_node, target_ip)
status = success ? '✓' : '✗'
puts " #{status} from #{source_node}"
end
end
|
#show_dns_status ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/messhy/health_checker.rb', line 162
def show_dns_status
puts '==> Mesh DNS Status'
puts "Domain: #{config.dns_domain}"
puts "Servers: #{config.dns_server_nodes.join(', ')}"
puts
config.dns_server_nodes.each do |node_name|
node_config = config.node_config(node_name)
next unless node_config
label = node_config['label']
label_display = label.to_s.strip.empty? ? '' : " (#{label})"
begin
@ssh_executor.execute_on_node(node_name) do
service = capture(:systemctl, 'is-active', 'dnsmasq', raise_on_non_zero_exit: false).strip
messhy_records = capture(:bash, '-c',
"sudo awk 'BEGIN{c=0} /^address=\\//{c++} END{print c}' " \
'/etc/dnsmasq.d/messhy.conf 2>/dev/null || true').strip
ap_records = capture(:bash, '-c',
"sudo awk 'BEGIN{c=0} /^address=\\//{c++} END{print c}' " \
'/etc/dnsmasq.d/active_postgres.conf 2>/dev/null || true').strip
status_icon = service == 'active' ? '✓' : '✗'
puts "#{status_icon} #{node_name} (#{node_config['private_ip']})#{label_display} - dnsmasq #{service}"
puts " └─ records: messhy=#{messhy_records.to_i} active_postgres=#{ap_records.to_i}"
end
rescue StandardError => e
puts "✗ #{node_name} (#{node_config['private_ip']})#{label_display} - DNS check failed: #{e.message}"
end
end
puts
end
|
#show_node_status(node_name) ⇒ Object
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
|
# File 'lib/messhy/health_checker.rb', line 34
def show_node_status(node_name)
node_config = config.node_config(node_name)
label = node_config['label']
label_display = label.to_s.strip.empty? ? '' : " (#{label})"
begin
status = @ssh_executor.get_wireguard_status(node_name)
peers = status.scan(/peer: (.+?)$/).flatten
if peers.any?
puts "✓ #{node_name} (#{node_config['private_ip']})#{label_display} - connected to #{peers.size} peers"
status.split('peer:').drop(1).each do |peer_block|
endpoint = (peer_block)
next unless endpoint
stats = (peer_block)
puts " └─ Peer: #{endpoint} - #{stats[:received]} rx, #{stats[:sent]} tx"
end
else
puts "✗ #{node_name} (#{node_config['private_ip']})#{label_display} - 0 peers (DOWN)"
end
rescue StandardError => e
puts "✗ #{node_name} (#{node_config['private_ip']})#{label_display} - Error: #{e.message}"
end
end
|
#show_stats(node: nil) ⇒ Object
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/messhy/health_checker.rb', line 151
def show_stats(node: nil)
if node
show_node_stats(node)
else
config.each_node do |node_name, _|
show_node_stats(node_name)
puts
end
end
end
|
#show_status ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/messhy/health_checker.rb', line 19
def show_status
puts '==> WireGuard Mesh Status'
puts "Environment: #{config.environment}"
puts
config.each_node do |node_name, _node_config|
show_node_status(node_name)
puts
end
show_dns_status if config.dns_enabled?
show_latency_matrix
end
|
#test_all ⇒ Object
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
|
# File 'lib/messhy/health_checker.rb', line 97
def test_all
puts '==> Testing mesh connectivity...'
puts
puts 'Note: This test may take a while. WireGuard status shows all peers connected.'
puts
all_ok = true
tested_pairs = Set.new
test_count = 0
total_tests = config.node_names.size * (config.node_names.size - 1) / 2
status_cache = {}
config.each_node do |source_name, _source_config|
config.each_node do |target_name, target_config|
next if source_name == target_name
pair_key = [source_name, target_name].sort.join('-')
next if tested_pairs.include?(pair_key)
tested_pairs.add(pair_key)
test_count += 1
target_ip = target_config['private_ip']
print "[#{test_count}/#{total_tests}] Testing #{source_name} → #{target_name} (#{target_ip})... "
$stdout.flush
success = false
begin
Timeout.timeout(3) do
success = @ssh_executor.ping_node_from(source_name, target_ip) ||
@ssh_executor.test_tcp_connectivity(source_name, target_ip, 22)
end
rescue StandardError
success = false
end
if success
puts '✓'
elsif handshake_recent?(source_name, target_config['private_ip'], status_cache)
puts '✓ (handshake)'
else
puts '✗ (ICMP/TCP may be blocked, and no recent WireGuard handshake)'
all_ok = false
end
$stdout.flush
end
end
puts
puts 'Note: When ICMP/TCP probes fail, we fall back to recent WireGuard handshakes.'
puts 'If a pair still reports a failure, there has been no recent handshake—check UDP 51820 and ' \
'keepalive/route settings.'
end
|