Debian Codegraph @

lualib/rspamadm/fuzzy_ping.lua

raw
1 --[[
2 Copyright (c) 2023, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 local argparse = require "argparse"
18 local ansicolors = require "ansicolors"
19 local rspamd_logger = require "rspamd_logger"
20 local lua_util = require "lua_util"
21
22 local E = {}
23
24 local parser = argparse()
25 :name 'rspamadm fuzzy_ping'
26 :description 'Pings fuzzy storage'
27 :help_description_margin(30)
28 parser:option "-c --config"
29 :description "Path to config file"
30 :argname("<cfg>")
31 :default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf")
32 parser:option "-r --rule"
33 :description "Storage to ping (must be configured in Rspamd configuration)"
34 :argname("<name>")
35 :default("rspamd.com")
36 parser:flag "-f --flood"
37 :description "Flood mode (no waiting for replies)"
38 parser:flag "-S --silent"
39 :description "Silent mode (statistics only)"
40 parser:option "-t --timeout"
41 :description "Timeout for requests"
42 :argname("<timeout>")
43 :convert(tonumber)
44 :default(5)
45 parser:option "-s --server"
46 :description "Override server to ping"
47 :argname("<name>")
48 parser:option "-n --number"
49 :description "Timeout for requests"
50 :argname("<number>")
51 :convert(tonumber)
52 :default(5)
53 parser:flag "-l --list"
54 :description "List configured storages"
55
56 local function load_config(opts)
57 local _r, err = rspamd_config:load_ucl(opts['config'])
58
59 if not _r then
60 rspamd_logger.errx('cannot parse %s: %s', opts['config'], err)
61 os.exit(1)
62 end
63
64 -- Init the real structure excluding logging and workers
65 _r, err = rspamd_config:parse_rcl({ 'logging', 'worker' })
66 if not _r then
67 rspamd_logger.errx('cannot process %s: %s', opts['config'], err)
68 os.exit(1)
69 end
70
71 _r, err = rspamd_config:init_modules()
72 if not _r then
73 rspamd_logger.errx('cannot init modules from %s: %s', opts['config'], err)
74 os.exit(1)
75 end
76 end
77
78 local function highlight(fmt, ...)
79 return ansicolors.white .. string.format(fmt, ...) .. ansicolors.reset
80 end
81
82 local function highlight_err(fmt, ...)
83 return ansicolors.red .. string.format(fmt, ...) .. ansicolors.reset
84 end
85
86 local function print_storages(rules)
87 for n, rule in pairs(rules) do
88 print(highlight('Rule: %s', n))
89 print(string.format("\tRead only: %s", rule.read_only))
90 -- Handle both unified servers and separate read/write servers
91 if rule.servers then
92 print(string.format("\tServers: %s", table.concat(lua_util.values(rule.servers), ',')))
93 else
94 if rule.read_servers then
95 print(string.format("\tRead servers: %s", table.concat(lua_util.values(rule.read_servers), ',')))
96 end
97 if rule.write_servers then
98 print(string.format("\tWrite servers: %s", table.concat(lua_util.values(rule.write_servers), ',')))
99 end
100 end
101 print("\tFlags:")
102
103 for fl, id in pairs(rule.flags or E) do
104 print(string.format("\t\t%s: %s", fl, id))
105 end
106 end
107 end
108
109 local function std_mean(tbl)
110 local function mean()
111 local sum = 0
112 local count = 0
113
114 for _, v in ipairs(tbl) do
115 sum = sum + v
116 count = count + 1
117 end
118
119 return (sum / count)
120 end
121
122 local m
123 local vm
124 local sum = 0
125 local count = 0
126 local result
127
128 m = mean(tbl)
129
130 for _, v in ipairs(tbl) do
131 vm = v - m
132 sum = sum + (vm * vm)
133 count = count + 1
134 end
135
136 result = math.sqrt(sum / (count - 1))
137
138 return result, m
139 end
140
141 local function maxmin(tbl)
142 local max = -math.huge
143 local min = math.huge
144
145 for _, v in ipairs(tbl) do
146 max = math.max(max, v)
147 min = math.min(min, v)
148 end
149
150 return max, min
151 end
152
153 local function print_results(results)
154 local servers = {}
155 local err_servers = {}
156 for _, res in ipairs(results) do
157 if res.success then
158 if servers[res.server] then
159 table.insert(servers[res.server], res.latency)
160 else
161 servers[res.server] = { res.latency }
162 end
163 else
164 if err_servers[res.server] then
165 err_servers[res.server] = err_servers[res.server] + 1
166 else
167 err_servers[res.server] = 1
168 end
169 -- For the case if no successful replies are detected
170 if not servers[res.server] then
171 servers[res.server] = {}
172 end
173 end
174 end
175
176 for s, l in pairs(servers) do
177 local total = #l + (err_servers[s] or 0)
178 print(highlight('Summary for %s: %d packets transmitted, %d packets received, %.1f%% packet loss',
179 s, total, #l, (total - #l) * 100.0 / total))
180 local std, mean = std_mean(l)
181 local max, min = maxmin(l)
182 print(string.format('round-trip min/avg/max/std-dev = %.2f/%.2f/%.2f/%.2f ms',
183 min, mean,
184 max, std))
185 end
186 end
187
188 local function handler(args)
189 local opts = parser:parse(args)
190
191 load_config(opts)
192
193 if opts.list then
194 print_storages(rspamd_plugins.fuzzy_check.list_storages(rspamd_config))
195 os.exit(0)
196 end
197
198 -- Perform ping using a fake task from async stuff provided by rspamadm
199 local rspamd_task = require "rspamd_task"
200
201 -- TODO: this task is not cleared at the end, do something about it some day
202 local task = rspamd_task.create(rspamd_config, rspamadm_ev_base)
203 task:set_session(rspamadm_session)
204 task:set_resolver(rspamadm_dns_resolver)
205
206 local replied = 0
207 local results = {}
208 local ping_fuzzy
209
210 local function gen_ping_fuzzy_cb(num)
211 return function(success, server, latency_or_err)
212 if not success then
213 if not opts.silent then
214 print(highlight_err('error from %s: %s', server, latency_or_err))
215 end
216 results[num] = {
217 success = false,
218 error = latency_or_err,
219 server = tostring(server),
220 }
221 else
222 if not opts.silent then
223 local adjusted_latency = math.floor(latency_or_err * 1000) * 1.0 / 1000;
224 print(highlight('reply from %s: %s ms', server, adjusted_latency))
225
226 end
227 results[num] = {
228 success = true,
229 latency = latency_or_err,
230 server = tostring(server),
231 }
232 end
233
234 if replied == opts.number - 1 then
235 print_results(results)
236 else
237 replied = replied + 1
238 if not opts.flood then
239 ping_fuzzy(replied + 1)
240 end
241 end
242 end
243 end
244
245 ping_fuzzy = function(num)
246 local ret, err = rspamd_plugins.fuzzy_check.ping_storage(task, gen_ping_fuzzy_cb(num),
247 opts.rule, opts.timeout, opts.server)
248
249 if not ret then
250 print(highlight_err('error from %s: %s', opts.server, err))
251 opts.number = opts.number - 1 -- To avoid issues with waiting for other replies
252 end
253 end
254
255 if opts.flood then
256 for i = 1, opts.number do
257 ping_fuzzy(i)
258 end
259 else
260 ping_fuzzy(1)
261 end
262 end
263
264 return {
265 name = 'fuzzy_ping',
266 aliases = { 'fuzzyping' },
267 handler = handler,
268 description = parser._description
269 }