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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! ERROR组件JSON格式化器
//!
//! 为ERROR组件事件提供专业的JSON格式化输出
use crate::events::error::{ErrorEvent, ErrorSeverity, ErrorType};
use serde_json::{json, Value};
/// ERROR组件JSON格式化器
pub struct ErrorJsonFormatter;
impl ErrorJsonFormatter {
/// 格式化ERROR事件为JSON
pub fn format_error_event(event: &ErrorEvent) -> Value {
match event {
ErrorEvent::DeliveryDeferred {
queue_id,
to,
relay,
delay,
delays,
dsn,
status,
reason,
error_type,
} => {
let severity = error_type.severity();
json!({
"event_type": "delivery_deferred",
"description": "邮件投递延迟错误",
"queue_id": queue_id,
"to": to,
"relay": relay,
"delay": delay,
"delays": delays,
"dsn": dsn,
"status": status,
"reason": reason,
"error_type": error_type,
"error_category": Self::format_error_type(error_type),
"severity": Self::format_severity(&severity),
"fields": {
"queue_id": {
"value": queue_id,
"type": "string",
"description": "队列标识符"
},
"to": {
"value": to,
"type": "string",
"description": "收件人地址"
},
"relay": {
"value": relay,
"type": "string",
"description": "中继服务器"
},
"delay": {
"value": delay,
"type": "number",
"description": "总延迟时间(秒)"
},
"delays": {
"value": delays,
"type": "string",
"description": "各阶段延迟时间"
},
"dsn": {
"value": dsn,
"type": "string",
"description": "投递状态通知代码"
},
"reason": {
"value": reason,
"type": "string",
"description": "错误原因"
}
}
})
}
ErrorEvent::DeliveryFailed {
queue_id,
to,
relay,
delay,
delays,
dsn,
status,
reason,
error_type,
} => {
let severity = error_type.severity();
json!({
"event_type": "delivery_failed",
"description": "邮件投递永久失败",
"queue_id": queue_id,
"to": to,
"relay": relay,
"delay": delay,
"delays": delays,
"dsn": dsn,
"status": status,
"reason": reason,
"error_type": error_type,
"error_category": Self::format_error_type(error_type),
"severity": Self::format_severity(&severity),
"fields": {
"queue_id": {
"value": queue_id,
"type": "string",
"description": "队列标识符"
},
"to": {
"value": to,
"type": "string",
"description": "收件人地址"
},
"relay": {
"value": relay,
"type": "string",
"description": "中继服务器"
},
"delay": {
"value": delay,
"type": "number",
"description": "总延迟时间(秒)"
},
"dsn": {
"value": dsn,
"type": "string",
"description": "投递状态通知代码"
},
"reason": {
"value": reason,
"type": "string",
"description": "失败原因"
}
}
})
}
ErrorEvent::SystemError {
queue_id,
error_type,
message,
} => {
let severity = error_type.severity();
json!({
"event_type": "system_error",
"description": "系统配置或运行错误",
"queue_id": queue_id,
"message": message,
"error_type": error_type,
"error_category": Self::format_error_type(error_type),
"severity": Self::format_severity(&severity),
"fields": {
"message": {
"value": message,
"type": "string",
"description": "错误消息内容"
},
"queue_id": {
"value": queue_id,
"type": "optional_string",
"description": "队列标识符(如果有)"
}
}
})
}
ErrorEvent::ConnectionError {
queue_id,
to,
relay,
delay,
delays,
dsn,
reason,
error_type,
} => {
let severity = error_type.severity();
json!({
"event_type": "connection_error",
"description": "网络连接错误",
"queue_id": queue_id,
"to": to,
"relay": relay,
"delay": delay,
"delays": delays,
"dsn": dsn,
"reason": reason,
"error_type": error_type,
"error_category": Self::format_error_type(error_type),
"severity": Self::format_severity(&severity),
"fields": {
"queue_id": {
"value": queue_id,
"type": "string",
"description": "队列标识符"
},
"reason": {
"value": reason,
"type": "string",
"description": "连接错误原因"
},
"to": {
"value": to,
"type": "string",
"description": "目标收件人(如果有)"
},
"relay": {
"value": relay,
"type": "string",
"description": "目标中继服务器(如果有)"
}
}
})
}
ErrorEvent::Other {
queue_id,
error_type,
message,
} => {
json!({
"event_type": "error_other",
"description": "其他未分类错误",
"queue_id": queue_id,
"error_type": error_type,
"message": message,
"severity": {
"level": 2,
"name": "unknown",
"description": "未知严重性"
},
"fields": {
"message": {
"value": message,
"type": "string",
"description": "原始错误消息"
},
"queue_id": {
"value": queue_id,
"type": "optional_string",
"description": "队列标识符(如果有)"
}
}
})
}
}
}
/// 格式化错误类型信息
fn format_error_type(error_type: &ErrorType) -> Value {
json!({
"type": error_type,
"name": error_type,
"description": error_type.description(),
"severity": error_type.severity()
})
}
/// 格式化错误严重性信息
fn format_severity(severity: &ErrorSeverity) -> Value {
json!({
"level": severity.level(),
"name": severity,
"description": severity.description()
})
}
/// 为ERROR事件添加统计信息
pub fn add_statistics(mut json: Value, event: &ErrorEvent) -> Value {
if let Some(obj) = json.as_object_mut() {
// 添加错误分析统计
obj.insert(
"statistics".to_string(),
json!({
"retry_recommended": Self::is_retry_recommended(event),
"requires_admin_attention": Self::requires_admin_attention(event),
"affects_mail_flow": Self::affects_mail_flow(event),
"error_frequency": Self::get_error_frequency_hint(event)
}),
);
// 添加故障排除提示
obj.insert(
"troubleshooting".to_string(),
json!({
"suggested_actions": Self::get_suggested_actions(event),
"related_components": Self::get_related_components(event),
"documentation_links": Self::get_documentation_links(event)
}),
);
}
json
}
/// 判断是否建议重试
fn is_retry_recommended(event: &ErrorEvent) -> bool {
match event {
ErrorEvent::DeliveryDeferred { error_type, .. } => {
matches!(
error_type.severity(),
ErrorSeverity::Temporary | ErrorSeverity::Network
)
}
ErrorEvent::ConnectionError { error_type, .. } => {
matches!(
error_type.severity(),
ErrorSeverity::Temporary | ErrorSeverity::Network
)
}
_ => false,
}
}
/// 判断是否需要管理员关注
fn requires_admin_attention(event: &ErrorEvent) -> bool {
match event {
ErrorEvent::SystemError { .. } => true,
ErrorEvent::DeliveryFailed { error_type, .. } => {
matches!(
error_type.severity(),
ErrorSeverity::SystemIssue | ErrorSeverity::ProtocolIssue
)
}
_ => false,
}
}
/// 判断是否影响邮件流
fn affects_mail_flow(event: &ErrorEvent) -> bool {
match event {
ErrorEvent::SystemError { .. } => true,
ErrorEvent::DeliveryFailed { .. } => true,
ErrorEvent::DeliveryDeferred { .. } => true,
ErrorEvent::ConnectionError { .. } => true,
ErrorEvent::Other { .. } => false,
}
}
/// 获取错误频率提示
fn get_error_frequency_hint(event: &ErrorEvent) -> &'static str {
match event {
ErrorEvent::DeliveryDeferred { error_type, .. } => match error_type {
ErrorType::DnsResolution => "高频错误",
ErrorType::ConnectionRefused => "中频错误",
_ => "低频错误",
},
_ => "变动频率",
}
}
/// 获取建议的故障排除操作
fn get_suggested_actions(event: &ErrorEvent) -> Vec<&'static str> {
match event {
ErrorEvent::DeliveryDeferred { error_type, .. }
| ErrorEvent::DeliveryFailed { error_type, .. }
| ErrorEvent::SystemError { error_type, .. }
| ErrorEvent::ConnectionError { error_type, .. } => match error_type {
ErrorType::DnsResolution => vec!["检查DNS配置", "验证域名解析", "检查网络连接"],
ErrorType::ConnectionRefused => {
vec!["检查目标服务器状态", "验证端口是否开放", "检查防火墙规则"]
}
ErrorType::ConnectionLost => {
vec!["检查网络稳定性", "验证服务器负载", "检查超时配置"]
}
ErrorType::SystemConfig => vec!["检查Postfix配置", "验证文件权限", "重新加载配置"],
_ => vec!["查看错误详情", "联系系统管理员"],
},
ErrorEvent::Other { .. } => vec!["分析原始日志", "联系技术支持"],
}
}
/// 获取相关组件
fn get_related_components(event: &ErrorEvent) -> Vec<&'static str> {
match event {
ErrorEvent::DeliveryDeferred { .. } | ErrorEvent::DeliveryFailed { .. } => {
vec!["smtp", "qmgr", "cleanup"]
}
ErrorEvent::ConnectionError { .. } => {
vec!["smtp", "network"]
}
ErrorEvent::SystemError { .. } => {
vec!["master", "postfix-config"]
}
ErrorEvent::Other { .. } => vec!["all"],
}
}
/// 获取文档链接
fn get_documentation_links(_event: &ErrorEvent) -> Vec<&'static str> {
vec![
"http://www.postfix.org/DEBUG_README.html",
"http://www.postfix.org/TROUBLE_SHOOTING_README.html",
]
}
}