postfix-log-parser 0.2.0

高性能模块化Postfix日志解析器,经3.2GB生产数据验证,SMTPD事件100%准确率
Documentation
//! Postfix Script JSON formatter

use crate::events::postfix_script::{
    PostfixScriptEvent, PostfixScriptFatalError, PostfixScriptOperation, PostfixScriptWarningType,
};
use serde_json::{json, Value};

/// Format a postfix-script event to JSON
pub fn format_postfix_script_event(event: &PostfixScriptEvent) -> Value {
    match event {
        PostfixScriptEvent::SystemOperation { operation, .. } => match operation {
            PostfixScriptOperation::Starting => {
                json!({
                    "event_type": "system_starting",
                    "description": "Postfix mail system is starting"
                })
            }
            PostfixScriptOperation::Running { pid } => {
                json!({
                    "event_type": "system_running",
                    "master_pid": pid,
                    "description": "Postfix mail system is running"
                })
            }
            PostfixScriptOperation::Refreshing => {
                json!({
                    "event_type": "system_refreshing",
                    "description": "Postfix mail system is refreshing"
                })
            }
        },
        PostfixScriptEvent::FatalError { error, .. } => match error {
            PostfixScriptFatalError::CannotExecutePostconf => {
                json!({
                    "event_type": "fatal_error",
                    "error_type": "cannot_execute_postconf",
                    "description": "Cannot execute postconf command",
                    "severity": "fatal"
                })
            }
            PostfixScriptFatalError::CannotExecuteCommand { command } => {
                json!({
                    "event_type": "fatal_error",
                    "error_type": "cannot_execute_command",
                    "command": command,
                    "description": format!("Cannot execute command: {}", command),
                    "severity": "fatal"
                })
            }
            PostfixScriptFatalError::MissingPath { path } => {
                json!({
                    "event_type": "fatal_error",
                    "error_type": "missing_path",
                    "path": path,
                    "description": format!("Missing directory or file: {}", path),
                    "severity": "fatal"
                })
            }
            PostfixScriptFatalError::Other { message } => {
                json!({
                    "event_type": "fatal_error",
                    "error_type": "other",
                    "message": message,
                    "description": "Other fatal error",
                    "severity": "fatal"
                })
            }
        },
        PostfixScriptEvent::Warning { warning, .. } => match warning {
            PostfixScriptWarningType::NotOwnedBy {
                path,
                expected_owner,
            } => {
                json!({
                    "event_type": "warning",
                    "warning_type": "not_owned_by",
                    "path": path,
                    "expected_owner": expected_owner,
                    "description": format!("File/directory not owned by {}: {}", expected_owner, path),
                    "severity": "warning"
                })
            }
            PostfixScriptWarningType::GroupWritable { path } => {
                json!({
                    "event_type": "warning",
                    "warning_type": "group_writable",
                    "path": path,
                    "description": format!("File/directory is group or other writable: {}", path),
                    "severity": "warning"
                })
            }
            PostfixScriptWarningType::SymlinkLeaves { path } => {
                json!({
                    "event_type": "warning",
                    "warning_type": "symlink_leaves",
                    "path": path,
                    "description": format!("Symlink leaves directory: {}", path),
                    "severity": "warning"
                })
            }
            PostfixScriptWarningType::Other { message } => {
                json!({
                    "event_type": "warning",
                    "warning_type": "other",
                    "message": message,
                    "description": "Other warning",
                    "severity": "warning"
                })
            }
        },
    }
}