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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
150
151
152
153
154
155
156
157
158
159
160
161
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
--[[
Copyright (c) 2023, Vsevolod Stakhov <vsevolod@rspamd.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
local argparse = require "argparse"
local ansicolors = require "ansicolors"
local rspamd_logger = require "rspamd_logger"
local lua_util = require "lua_util"
local E = {}
local parser = argparse()
:name 'rspamadm fuzzy_ping'
:description 'Pings fuzzy storage'
:help_description_margin(30)
parser:option "-c --config"
:description "Path to config file"
:argname("<cfg>")
:default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf")
parser:option "-r --rule"
:description "Storage to ping (must be configured in Rspamd configuration)"
:argname("<name>")
:default("rspamd.com")
parser:flag "-f --flood"
:description "Flood mode (no waiting for replies)"
parser:flag "-S --silent"
:description "Silent mode (statistics only)"
parser:option "-t --timeout"
:description "Timeout for requests"
:argname("<timeout>")
:convert(tonumber)
:default(5)
parser:option "-s --server"
:description "Override server to ping"
:argname("<name>")
parser:option "-n --number"
:description "Timeout for requests"
:argname("<number>")
:convert(tonumber)
:default(5)
parser:flag "-l --list"
:description "List configured storages"
local function load_config(opts)
local _r, err = rspamd_config:load_ucl(opts['config'])
if not _r then
rspamd_logger.errx('cannot parse %s: %s', opts['config'], err)
os.exit(1)
end
-- Init the real structure excluding logging and workers
_r, err = rspamd_config:parse_rcl({ 'logging', 'worker' })
if not _r then
rspamd_logger.errx('cannot process %s: %s', opts['config'], err)
os.exit(1)
end
_r, err = rspamd_config:init_modules()
if not _r then
rspamd_logger.errx('cannot init modules from %s: %s', opts['config'], err)
os.exit(1)
end
end
local function highlight(fmt, ...)
return ansicolors.white .. string.format(fmt, ...) .. ansicolors.reset
end
local function highlight_err(fmt, ...)
return ansicolors.red .. string.format(fmt, ...) .. ansicolors.reset
end
local function print_storages(rules)
for n, rule in pairs(rules) do
print(highlight('Rule: %s', n))
print(string.format("\tRead only: %s", rule.read_only))
print(string.format("\tServers: %s", table.concat(lua_util.values(rule.servers), ',')))
print("\tFlags:")
for fl, id in pairs(rule.flags or E) do
print(string.format("\t\t%s: %s", fl, id))
end
end
end
local function std_mean(tbl)
local function mean()
local sum = 0
local count = 0
for _, v in ipairs(tbl) do
sum = sum + v
count = count + 1
end
return (sum / count)
end
local m
local vm
local sum = 0
local count = 0
local result
m = mean(tbl)
for _, v in ipairs(tbl) do
vm = v - m
sum = sum + (vm * vm)
count = count + 1
end
result = math.sqrt(sum / (count - 1))
return result, m
end
local function maxmin(tbl)
local max = -math.huge
local min = math.huge
for _, v in ipairs(tbl) do
max = math.max(max, v)
min = math.min(min, v)
end
return max, min
end
local function print_results(results)
local servers = {}
local err_servers = {}
for _, res in ipairs(results) do
if res.success then
if servers[res.server] then
table.insert(servers[res.server], res.latency)
else
servers[res.server] = { res.latency }
end
else
if err_servers[res.server] then
err_servers[res.server] = err_servers[res.server] + 1
else
err_servers[res.server] = 1
end
-- For the case if no successful replies are detected
if not servers[res.server] then
servers[res.server] = {}
end
end
end
for s, l in pairs(servers) do
local total = #l + (err_servers[s] or 0)
print(highlight('Summary for %s: %d packets transmitted, %d packets received, %.1f%% packet loss',
s, total, #l, (total - #l) * 100.0 / total))
local mean, std = std_mean(l)
local max, min = maxmin(l)
print(string.format('round-trip min/avg/max/std-dev = %.2f/%.2f/%.2f/%.2f ms',
min, mean,
max, std))
end
end
local function handler(args)
local opts = parser:parse(args)
load_config(opts)
if opts.list then
print_storages(rspamd_plugins.fuzzy_check.list_storages(rspamd_config))
os.exit(0)
end
-- Perform ping using a fake task from async stuff provided by rspamadm
local rspamd_task = require "rspamd_task"
-- TODO: this task is not cleared at the end, do something about it some day
local task = rspamd_task.create(rspamd_config, rspamadm_ev_base)
task:set_session(rspamadm_session)
task:set_resolver(rspamadm_dns_resolver)
local replied = 0
local results = {}
local ping_fuzzy
local function gen_ping_fuzzy_cb(num)
return function(success, server, latency_or_err)
if not success then
if not opts.silent then
print(highlight_err('error from %s: %s', server, latency_or_err))
end
results[num] = {
success = false,
error = latency_or_err,
server = tostring(server),
}
else
if not opts.silent then
local adjusted_latency = math.floor(latency_or_err * 1000) * 1.0 / 1000;
print(highlight('reply from %s: %s ms', server, adjusted_latency))
end
results[num] = {
success = true,
latency = latency_or_err,
server = tostring(server),
}
end
if replied == opts.number - 1 then
print_results(results)
else
replied = replied + 1
if not opts.flood then
ping_fuzzy(replied + 1)
end
end
end
end
ping_fuzzy = function(num)
local ret, err = rspamd_plugins.fuzzy_check.ping_storage(task, gen_ping_fuzzy_cb(num),
opts.rule, opts.timeout, opts.server)
if not ret then
print(highlight_err('error from %s: %s', opts.server, err))
opts.number = opts.number - 1 -- To avoid issues with waiting for other replies
end
end
if opts.flood then
for i = 1, opts.number do
ping_fuzzy(i)
end
else
ping_fuzzy(1)
end
end
return {
name = 'fuzzy_ping',
aliases = { 'fuzzyping' },
handler = handler,
description = parser._description
}
|