postfix_log_parser/events/
cleanup.rs

1use serde::{Deserialize, Serialize};
2
3/// Cleanup组件事件
4/// 基于896,788个真实生产数据分析,cleanup组件占4.5%的日志
5/// cleanup主要负责邮件内容处理、重写和清理
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub enum CleanupEvent {
8    /// Message-ID处理 - 最常见的cleanup事件
9    /// 示例: "4C79F1C801AD: message-id=<1833e987d3ecefa9.6d2e4a5d94f9a46d.99a741ed1cc424f9@m01>"
10    MessageId {
11        queue_id: String,
12        message_id: String,
13    },
14
15    /// 队列文件操作警告
16    /// 示例: "warning: mail_queue_enter: create file incoming/635139.92: Permission denied"
17    QueueFileWarning {
18        operation: String,    // 如 "mail_queue_enter"
19        file_path: String,    // 如 "incoming/635139.92"
20        error_reason: String, // 如 "Permission denied"
21    },
22
23    /// 邮件内容重写
24    /// cleanup可能会重写邮件头、地址等
25    MessageRewrite {
26        queue_id: String,
27        rewrite_type: String, // 重写类型
28        original: String,     // 原始内容
29        rewritten: String,    // 重写后内容
30    },
31
32    /// 邮件大小信息
33    /// cleanup处理邮件时会记录大小信息
34    MessageSize {
35        queue_id: String,
36        size: u64, // 邮件大小(字节)
37    },
38
39    /// 邮件头处理
40    /// cleanup处理各种邮件头
41    HeaderProcessing {
42        queue_id: String,
43        header_name: String,  // 头字段名
44        header_value: String, // 头字段值
45        action: String,       // 动作 (add, remove, replace)
46    },
47
48    /// 邮件过滤器处理
49    /// 与milter等过滤器交互的事件
50    FilterAction {
51        queue_id: String,
52        filter_name: String,     // 过滤器名称
53        action: String,          // 过滤器动作
54        details: Option<String>, // 详细信息
55    },
56
57    /// 地址重写
58    /// cleanup可能重写发件人或收件人地址
59    AddressRewrite {
60        queue_id: String,
61        address_type: String, // "from" 或 "to"
62        original_address: String,
63        rewritten_address: String,
64    },
65
66    /// 邮件拒绝
67    /// cleanup阶段的邮件拒绝
68    MessageReject {
69        queue_id: String,
70        reason: String, // 拒绝原因
71        action: String, // 拒绝动作
72    },
73
74    /// 资源限制警告
75    /// 磁盘空间、内存等资源限制
76    ResourceLimit {
77        resource_type: String, // 资源类型
78        limit_details: String, // 限制详情
79        current_value: Option<u64>,
80        limit_value: Option<u64>,
81    },
82
83    /// Milter交互
84    /// 与邮件过滤器的交互事件
85    MilterInteraction {
86        queue_id: String,
87        milter_name: String,      // milter名称
88        command: String,          // milter命令
89        response: Option<String>, // milter响应
90    },
91
92    /// 配置警告
93    /// cleanup相关的配置问题
94    ConfigurationWarning {
95        warning_type: String, // 警告类型
96        message: String,      // 警告消息
97    },
98
99    /// 统计信息
100    /// cleanup处理统计
101    Statistics {
102        processed: Option<u32>, // 处理的邮件数
103        rejected: Option<u32>,  // 拒绝的邮件数
104        errors: Option<u32>,    // 错误数
105    },
106
107    /// 其他cleanup事件
108    /// 用于处理暂时无法分类的cleanup事件
109    Other {
110        event_type: String,
111        message: String,
112        queue_id: Option<String>,
113    },
114}
115
116impl CleanupEvent {
117    pub fn event_type(&self) -> &'static str {
118        match self {
119            CleanupEvent::MessageId { .. } => "message_id",
120            CleanupEvent::QueueFileWarning { .. } => "queue_file_warning",
121            CleanupEvent::MessageRewrite { .. } => "message_rewrite",
122            CleanupEvent::MessageSize { .. } => "message_size",
123            CleanupEvent::HeaderProcessing { .. } => "header_processing",
124            CleanupEvent::FilterAction { .. } => "filter_action",
125            CleanupEvent::AddressRewrite { .. } => "address_rewrite",
126            CleanupEvent::MessageReject { .. } => "message_reject",
127            CleanupEvent::ResourceLimit { .. } => "resource_limit",
128            CleanupEvent::MilterInteraction { .. } => "milter_interaction",
129            CleanupEvent::ConfigurationWarning { .. } => "configuration_warning",
130            CleanupEvent::Statistics { .. } => "statistics",
131            CleanupEvent::Other { .. } => "other",
132        }
133    }
134
135    /// 获取队列ID(如果存在)
136    pub fn queue_id(&self) -> Option<&str> {
137        match self {
138            CleanupEvent::MessageId { queue_id, .. } => Some(queue_id),
139            CleanupEvent::MessageRewrite { queue_id, .. } => Some(queue_id),
140            CleanupEvent::MessageSize { queue_id, .. } => Some(queue_id),
141            CleanupEvent::HeaderProcessing { queue_id, .. } => Some(queue_id),
142            CleanupEvent::FilterAction { queue_id, .. } => Some(queue_id),
143            CleanupEvent::AddressRewrite { queue_id, .. } => Some(queue_id),
144            CleanupEvent::MessageReject { queue_id, .. } => Some(queue_id),
145            CleanupEvent::MilterInteraction { queue_id, .. } => Some(queue_id),
146            CleanupEvent::Other { queue_id, .. } => queue_id.as_deref(),
147            _ => None,
148        }
149    }
150
151    /// 检查是否为错误级别事件
152    pub fn is_error_event(&self) -> bool {
153        matches!(
154            self,
155            CleanupEvent::QueueFileWarning { .. }
156                | CleanupEvent::MessageReject { .. }
157                | CleanupEvent::ResourceLimit { .. }
158        )
159    }
160
161    /// 检查是否为警告级别事件
162    pub fn is_warning_event(&self) -> bool {
163        matches!(
164            self,
165            CleanupEvent::QueueFileWarning { .. }
166                | CleanupEvent::ConfigurationWarning { .. }
167                | CleanupEvent::ResourceLimit { .. }
168        )
169    }
170
171    /// 检查是否为正常处理事件
172    pub fn is_normal_event(&self) -> bool {
173        matches!(
174            self,
175            CleanupEvent::MessageId { .. }
176                | CleanupEvent::MessageSize { .. }
177                | CleanupEvent::HeaderProcessing { .. }
178                | CleanupEvent::FilterAction { .. }
179        )
180    }
181}