首页主机资讯CentOS系统中如何自定义消息模板

CentOS系统中如何自定义消息模板

时间2025-11-18 14:35:04发布访客分类主机资讯浏览1242
导读:在 CentOS 中自定义消息模板的常用做法 一 使用 rsyslog 定义日志消息模板 适用场景:统一日志输出格式(文件、远程、控制台等),便于检索与对接日志平台。 操作步骤 备份并编辑配置文件 配置文件路径:/etc/rsysl...

在 CentOS 中自定义消息模板的常用做法


一 使用 rsyslog 定义日志消息模板

  • 适用场景:统一日志输出格式(文件、远程、控制台等),便于检索与对接日志平台。
  • 操作步骤
    1. 备份并编辑配置文件
      • 配置文件路径:/etc/rsyslog.conf/etc/rsyslog.d/50-default.conf
      • 建议先备份:sudo cp /etc/rsyslog.conf /etc/rsyslog.conf.bak
    2. 定义模板(示例:带毫秒时间戳与结构化字段)
      $template MyCustomFormat,"%timegenerated:::date-rfc3339% %syslogseverity-text%.%syslogfacility-text% %syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
      
      常用属性:%timegenerated%%syslogtag%%msg%%hostname%%programname%%syslogseverity-text%%syslogfacility-text%
    3. 应用模板到规则
      • 写入自定义文件并使用模板:
        *.* action(type="omfile" file="/var/log/custom.log" template="MyCustomFormat")
        
      • 或替换默认文件模板(传统格式):
        $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
        
      • 若需仅用模板输出并停止后续处理(谨慎使用):
        *.* ?MyCustomFormat
        &
             stop
        
    4. 使配置生效
      sudo systemctl restart rsyslog
      sudo systemctl enable rsyslog
      
    5. 验证
      logger -t test "this is a custom template message"
      tail -f /var/log/custom.log
      
    说明:模板语法与动作在不同版本 rsyslog 中略有差异,以上示例基于常见用法;如需兼容旧版,可优先使用传统的模板定义与 omfile 写法。

二 自定义登录前与登录后的消息模板

  • 适用场景:终端登录/SSH 登录时展示公司规范、公告、合规提示等。
  • 预登录提示(登录前,覆盖 /etc/issue)
    sudo tee /etc/issue <
        <
        'EOF'
    \S
    Kernel \r on an \m
    Hostname: \n
    Time: \t
    EOF
    
  • 登录后提示(登录后,静态 MOTD 或脚本动态生成)
    • 静态文件:
      sudo tee /etc/motd <
          <
          'EOF'
      Welcome to CentOS Linux
      Please read the compliance notice before use.
      EOF
      
    • 动态脚本(/etc/profile.d/welcome.sh):
      sudo tee /etc/profile.d/welcome.sh >
          /dev/null <
          <
          'EOF'
      #!/bin/bash
      echo -e "\e[1;
          36m$(hostname)\e[0m logged in at $(date '+%F %T')"
      echo -e "Uptime: $(uptime -p)"
      EOF
      sudo chmod +x /etc/profile.d/welcome.sh
      
  • 常用转义序列:\d(日期)、\t(时间)、\n(主机名)、\r(内核版本)、\s(系统名)、\m(架构)、\o(域名)、\l(终端名)。
  • 验证:退出并重新登录终端,或执行 ssh localhost 查看效果。

三 自定义系统广播与桌面通知模板

  • 面向所有已登录终端的广播(wall)
    echo -e "$(date '+%F %T') [NOTICE] Maintenance at 22:00–23:00. Save your work." | wall
    
  • 图形会话桌面通知(notify-send,需 GUI 会话与 D-Bus)
    # 安装(最小化系统按需安装)
    sudo yum install -y libnotify
    
    # 发送示例
    notify-send "系统告警" "磁盘使用率超过 80%:/dev/sda1"
    
  • 结合日志触发即时通知(journalctl + notify-send)
    #!/usr/bin/env bash
    journalctl -f -p err --since "1 min ago" | while IFS= read -r line;
         do
      notify-send "Journal ERROR" "$line"
    done
    
  • 邮件通知模板(esmtp + mailx)
    # 安装
    sudo yum install -y esmtp mailx
    
    # 配置 /etc/mail.rc(示例,按实际 SMTP 调整)
    cat >
        >
        /etc/mail.rc <
        <
        'EOF'
    set smtp=smtp://smtp.example.com:587
    set smtp-auth=login
    set smtp-auth-user=yourname
    set smtp-auth-password=yourpass
    set ssl-verify=ignore
    set nss-config-dir=/etc/pki/nssdb
    EOF
    
    # 发送(模板化主题与正文)
    SUBJECT="[$(hostname)] $(date '+%F %T') 磁盘告警"
    BODY="主机: $(hostname)\n时间: $(date '+%F %T')\n详情: /dev/sda1 使用率 >
     80%"
    echo -e "$BODY" | mail -s "$SUBJECT" admin@example.com
    
  • 提示:桌面通知仅在图形会话有效;无 GUI 时可改用邮件、企业微信/钉钉机器人、Pushover 等通道。

四 实用模板示例与排错要点

  • 常用模板示例
    • JSON 日志模板(便于 ELK/EFK 解析)
      $template JSONFormat,"{
      \"@timestamp\":\"%timegenerated:::date-rfc3339%\",\"host\":\"%hostname%\",\"severity\":\"%syslogseverity-text%\",\"facility\":\"%syslogfacility-text%\",\"tag\":\"%syslogtag%\",\"msg\":\"%msg:::json%\"}
          \n"
      *.* action(type="omfile" file="/var/log/app.json" template="JSONFormat")
      
    • Key-Value 模板(便于 grep/awk)
      $template KVFormat,"time=%timegenerated:::date-rfc3339% host=%hostname% sev=%syslogseverity-text% fac=%syslogfacility-text% tag=%syslogtag% msg=%msg%\n"
      local0.* action(type="omfile" file="/var/log/local0_kv.log" template="KVFormat")
      
  • 排错要点
    • 检查语法与加载顺序:配置片段建议放入 /etc/rsyslog.d/*.conf,避免与主线规则冲突;变更后用 sudo rsyslogd -N1 -f /etc/rsyslog.conf 做语法检查。
    • 权限与目录:确保日志目录(如 /var/log/)对 syslog 可写;SELinux 异常可用 ausearch -m avc -ts recentsetsebool -P rsyslogd_can_write_var_log 1 处理。
    • 服务状态:确认 systemctl status rsyslog 为 active;远程发送需开放 UDP/TCP 514 并配置相应规则。
    • 登录提示:确保 /etc/issue/etc/motd 可被登录进程读取;脚本方式需可执行并位于 /etc/profile.d/

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: CentOS系统中如何自定义消息模板
本文地址: https://pptw.com/jishu/750132.html
Java日志在CentOS如何分割 CentOS消息推送失败的原因及解决方法

游客 回复需填写必要信息