postfix_log_parser/formatters/json/
cleanup.rs

1use crate::events::cleanup::CleanupEvent;
2use serde_json::{json, Value};
3
4/// Cleanup事件JSON格式化器
5/// 基于896,788个真实生产数据分析
6pub fn format_cleanup_event(event: &CleanupEvent) -> Value {
7    match event {
8        CleanupEvent::MessageId {
9            queue_id,
10            message_id,
11        } => {
12            json!({
13                "event_type": "message_id",
14                "queue_id": queue_id,
15                "message_id": message_id,
16                "description": "邮件Message-ID处理",
17                "fields": {
18                    "queue_id": {
19                        "value": queue_id,
20                        "description": "队列标识符",
21                        "type": "string"
22                    },
23                    "message_id": {
24                        "value": message_id,
25                        "description": "邮件Message-ID",
26                        "type": "string"
27                    }
28                }
29            })
30        }
31
32        CleanupEvent::QueueFileWarning {
33            operation,
34            file_path,
35            error_reason,
36        } => {
37            json!({
38                "event_type": "queue_file_warning",
39                "operation": operation,
40                "file_path": file_path,
41                "error_reason": error_reason,
42                "severity": "warning",
43                "description": "队列文件操作警告",
44                "fields": {
45                    "operation": {
46                        "value": operation,
47                        "description": "操作类型",
48                        "type": "string"
49                    },
50                    "file_path": {
51                        "value": file_path,
52                        "description": "文件路径",
53                        "type": "string"
54                    },
55                    "error_reason": {
56                        "value": error_reason,
57                        "description": "错误原因",
58                        "type": "string"
59                    }
60                }
61            })
62        }
63
64        CleanupEvent::MessageSize { queue_id, size } => {
65            json!({
66                "event_type": "message_size",
67                "queue_id": queue_id,
68                "size": size,
69                "size_mb": *size as f64 / 1024.0 / 1024.0,
70                "description": "邮件大小信息",
71                "fields": {
72                    "queue_id": {
73                        "value": queue_id,
74                        "description": "队列标识符",
75                        "type": "string"
76                    },
77                    "size": {
78                        "value": size,
79                        "description": "邮件大小(字节)",
80                        "type": "number"
81                    },
82                    "size_mb": {
83                        "value": *size as f64 / 1024.0 / 1024.0,
84                        "description": "邮件大小(MB)",
85                        "type": "number"
86                    }
87                }
88            })
89        }
90
91        CleanupEvent::HeaderProcessing {
92            queue_id,
93            header_name,
94            header_value,
95            action,
96        } => {
97            json!({
98                "event_type": "header_processing",
99                "queue_id": queue_id,
100                "header_name": header_name,
101                "header_value": header_value,
102                "action": action,
103                "description": "邮件头处理",
104                "fields": {
105                    "queue_id": {
106                        "value": queue_id,
107                        "description": "队列标识符",
108                        "type": "string"
109                    },
110                    "header_name": {
111                        "value": header_name,
112                        "description": "邮件头字段名",
113                        "type": "string"
114                    },
115                    "header_value": {
116                        "value": header_value,
117                        "description": "邮件头字段值",
118                        "type": "string"
119                    },
120                    "action": {
121                        "value": action,
122                        "description": "处理动作",
123                        "type": "string"
124                    }
125                }
126            })
127        }
128
129        CleanupEvent::FilterAction {
130            queue_id,
131            filter_name,
132            action,
133            details,
134        } => {
135            json!({
136                "event_type": "filter_action",
137                "queue_id": queue_id,
138                "filter_name": filter_name,
139                "action": action,
140                "details": details,
141                "description": "邮件过滤器处理",
142                "fields": {
143                    "queue_id": {
144                        "value": queue_id,
145                        "description": "队列标识符",
146                        "type": "string"
147                    },
148                    "filter_name": {
149                        "value": filter_name,
150                        "description": "过滤器名称",
151                        "type": "string"
152                    },
153                    "action": {
154                        "value": action,
155                        "description": "过滤器动作",
156                        "type": "string"
157                    },
158                    "details": {
159                        "value": details,
160                        "description": "详细信息",
161                        "type": "optional_string"
162                    }
163                }
164            })
165        }
166
167        CleanupEvent::AddressRewrite {
168            queue_id,
169            address_type,
170            original_address,
171            rewritten_address,
172        } => {
173            json!({
174                "event_type": "address_rewrite",
175                "queue_id": queue_id,
176                "address_type": address_type,
177                "original_address": original_address,
178                "rewritten_address": rewritten_address,
179                "description": "地址重写",
180                "fields": {
181                    "queue_id": {
182                        "value": queue_id,
183                        "description": "队列标识符",
184                        "type": "string"
185                    },
186                    "address_type": {
187                        "value": address_type,
188                        "description": "地址类型(from/to)",
189                        "type": "string"
190                    },
191                    "original_address": {
192                        "value": original_address,
193                        "description": "原始地址",
194                        "type": "string"
195                    },
196                    "rewritten_address": {
197                        "value": rewritten_address,
198                        "description": "重写后地址",
199                        "type": "string"
200                    }
201                }
202            })
203        }
204
205        CleanupEvent::MessageRewrite {
206            queue_id,
207            rewrite_type,
208            original,
209            rewritten,
210        } => {
211            json!({
212                "event_type": "message_rewrite",
213                "queue_id": queue_id,
214                "rewrite_type": rewrite_type,
215                "original": original,
216                "rewritten": rewritten,
217                "description": "邮件内容重写",
218                "fields": {
219                    "queue_id": {
220                        "value": queue_id,
221                        "description": "队列标识符",
222                        "type": "string"
223                    },
224                    "rewrite_type": {
225                        "value": rewrite_type,
226                        "description": "重写类型",
227                        "type": "string"
228                    },
229                    "original": {
230                        "value": original,
231                        "description": "原始内容",
232                        "type": "string"
233                    },
234                    "rewritten": {
235                        "value": rewritten,
236                        "description": "重写后内容",
237                        "type": "string"
238                    }
239                }
240            })
241        }
242
243        CleanupEvent::MessageReject {
244            queue_id,
245            reason,
246            action,
247        } => {
248            json!({
249                "event_type": "message_reject",
250                "queue_id": queue_id,
251                "reason": reason,
252                "action": action,
253                "severity": "error",
254                "description": "邮件拒绝",
255                "fields": {
256                    "queue_id": {
257                        "value": queue_id,
258                        "description": "队列标识符",
259                        "type": "string"
260                    },
261                    "reason": {
262                        "value": reason,
263                        "description": "拒绝原因",
264                        "type": "string"
265                    },
266                    "action": {
267                        "value": action,
268                        "description": "拒绝动作",
269                        "type": "string"
270                    }
271                }
272            })
273        }
274
275        CleanupEvent::ResourceLimit {
276            resource_type,
277            limit_details,
278            current_value,
279            limit_value,
280        } => {
281            json!({
282                "event_type": "resource_limit",
283                "resource_type": resource_type,
284                "limit_details": limit_details,
285                "current_value": current_value,
286                "limit_value": limit_value,
287                "severity": "warning",
288                "description": "资源限制警告",
289                "fields": {
290                    "resource_type": {
291                        "value": resource_type,
292                        "description": "资源类型",
293                        "type": "string"
294                    },
295                    "limit_details": {
296                        "value": limit_details,
297                        "description": "限制详情",
298                        "type": "string"
299                    },
300                    "current_value": {
301                        "value": current_value,
302                        "description": "当前值",
303                        "type": "optional_number"
304                    },
305                    "limit_value": {
306                        "value": limit_value,
307                        "description": "限制值",
308                        "type": "optional_number"
309                    }
310                }
311            })
312        }
313
314        CleanupEvent::MilterInteraction {
315            queue_id,
316            milter_name,
317            command,
318            response,
319        } => {
320            json!({
321                "event_type": "milter_interaction",
322                "queue_id": queue_id,
323                "milter_name": milter_name,
324                "command": command,
325                "response": response,
326                "description": "Milter交互",
327                "fields": {
328                    "queue_id": {
329                        "value": queue_id,
330                        "description": "队列标识符",
331                        "type": "string"
332                    },
333                    "milter_name": {
334                        "value": milter_name,
335                        "description": "Milter名称",
336                        "type": "string"
337                    },
338                    "command": {
339                        "value": command,
340                        "description": "Milter命令",
341                        "type": "string"
342                    },
343                    "response": {
344                        "value": response,
345                        "description": "Milter响应",
346                        "type": "optional_string"
347                    }
348                }
349            })
350        }
351
352        CleanupEvent::ConfigurationWarning {
353            warning_type,
354            message,
355        } => {
356            json!({
357                "event_type": "configuration_warning",
358                "warning_type": warning_type,
359                "message": message,
360                "severity": "warning",
361                "description": "配置警告",
362                "fields": {
363                    "warning_type": {
364                        "value": warning_type,
365                        "description": "警告类型",
366                        "type": "string"
367                    },
368                    "message": {
369                        "value": message,
370                        "description": "警告消息",
371                        "type": "string"
372                    }
373                }
374            })
375        }
376
377        CleanupEvent::Statistics {
378            processed,
379            rejected,
380            errors,
381        } => {
382            json!({
383                "event_type": "statistics",
384                "processed": processed,
385                "rejected": rejected,
386                "errors": errors,
387                "description": "Cleanup处理统计",
388                "fields": {
389                    "processed": {
390                        "value": processed,
391                        "description": "处理的邮件数",
392                        "type": "optional_number"
393                    },
394                    "rejected": {
395                        "value": rejected,
396                        "description": "拒绝的邮件数",
397                        "type": "optional_number"
398                    },
399                    "errors": {
400                        "value": errors,
401                        "description": "错误数",
402                        "type": "optional_number"
403                    }
404                }
405            })
406        }
407
408        CleanupEvent::Other {
409            event_type,
410            message,
411            queue_id,
412        } => {
413            json!({
414                "event_type": "other",
415                "sub_type": event_type,
416                "message": message,
417                "queue_id": queue_id,
418                "description": "其他Cleanup事件",
419                "fields": {
420                    "sub_type": {
421                        "value": event_type,
422                        "description": "事件子类型",
423                        "type": "string"
424                    },
425                    "message": {
426                        "value": message,
427                        "description": "消息内容",
428                        "type": "string"
429                    },
430                    "queue_id": {
431                        "value": queue_id,
432                        "description": "队列标识符",
433                        "type": "optional_string"
434                    }
435                }
436            })
437        }
438    }
439}