温馨提示×

CentOS Syslog与ELK Stack集成方法

小樊
42
2025-12-13 13:46:13
栏目: 智能运维

CentOS Syslog 与 ELK Stack 集成方法

一 架构与方案选择

  • 推荐两种落地方式:
    • 方式A:Syslog 直连 Logstash(UDP/TCP 514),在 Logstash 中解析后写入 Elasticsearch,再由 Kibana 可视化。
    • 方式B:Syslog 先到 rsyslog(文件或内存队列),再由 Filebeat 读取并发送到 Logstash(默认 5044),后续同上。
  • 版本建议:在 CentOS 7/8/Stream/Rocky/AlmaLinux 上使用 Elastic Stack 8.x 可获得更好的安全与兼容性;8.x 默认启用 TLS/认证,安装时会生成 elastic 超级用户密码与 Kibana 注册令牌,便于快速接入。

二 方案一 Syslog 直连 Logstash

  • 服务端 Logstash 配置
    • 创建配置 /etc/logstash/conf.d/syslog.conf(示例为 UDP,按需启用 TCP):
      input {
        udp {
          port => 514
          type => "syslog"
          codec => json { charset => "UTF-8" }
        }
        # tcp {
        #   port => 514
        #   type => "syslog"
        #   codec => json { charset => "UTF-8" }
        # }
      }
      
      filter {
        if [type] == "syslog" {
          grok {
            match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:hostname} %{DATA:program}(?:\[%{POSINT:pid}\])?: %{GREEDYDATA:syslog_message}" }
            overwrite => [ "message" ]
          }
          syslog_pri { }
          date {
            match => [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
            target => "@timestamp"
          }
          mutate {
            remove_field => [ "syslog_timestamp", "pid", "program" ]
          }
        }
      }
      
      output {
        elasticsearch {
          hosts => ["http://localhost:9200"]
          index => "syslog-%{+YYYY.MM.dd}"
          user => "elastic"
          password => "YourElasticPassword"
        }
        stdout { codec => rubydebug }   # 调试时可开启
      }
      
    • 启动服务:systemctl enable --now logstash
  • 客户端 rsyslog 配置
    • 启用远程发送(编辑 /etc/rsyslog.conf 或 /etc/rsyslog.d/50-default.conf):
      # 全部日志发往 Logstash(UDP)
      *.* @logstash-server-ip:514
      # 如需 TCP
      # *.* @@logstash-server-ip:514
      
    • 重启:systemctl restart rsyslog
  • 防火墙放行
    • firewall-cmd --permanent --add-port=514/udp
    • firewall-cmd --permanent --add-port=514/tcp
    • firewall-cmd --reload
  • 说明
    • 若使用 Logstash syslog input 插件(input { syslog { port => 514 } })也可直接接收,但生产更常用 UDP/TCP 原始输入配合 grok 解析,可控性更强。

三 方案二 通过 Filebeat 转发 Syslog

  • 服务端 Logstash 配置(接收 Beats)
    • 创建 /etc/logstash/conf.d/beats-input.conf:
      input {
        beats {
          port => 5044
        }
      }
      filter {
        if [fileset][module] == "system" {
          grok {
            match => { "message" => "%{SYSLOGTIMESTAMP:timestamp} %{SYSLOGHOST:hostname} %{DATA:program}(?:\[%{POSINT:pid}\])?: %{GREEDYDATA:syslog_message}" }
            overwrite => [ "message" ]
          }
          syslog_pri { }
          date {
            match => [ "timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
            target => "@timestamp"
          }
        }
      }
      output {
        elasticsearch {
          hosts => ["http://localhost:9200"]
          index => "syslog-beats-%{+YYYY.MM.dd}"
          user => "elastic"
          password => "YourElasticPassword"
        }
        stdout { codec => rubydebug }
      }
      
    • 启动:systemctl enable --now logstash
  • 客户端 Filebeat 配置
    • 启用 system 模块并指向 Logstash(/etc/filebeat/filebeat.yml):
      filebeat.inputs:
      - type: filestream
        id: syslog
        paths:
          - /var/log/messages
          - /var/log/secure
      
      filebeat.config.modules:
        path: ${path.config}/modules.d/*.yml
        reload.enabled: true
      
      output.logstash:
        hosts: ["logstash-server-ip:5044"]
      
    • 启用模块并启动:
      • filebeat modules enable system
      • systemctl enable --now filebeat
  • 说明
    • 该方式更利于横向扩展与缓冲(Filebeat 本地队列),在 ELK 8.x 环境中与 TLS/Kibana 注册流程配合更顺畅。

四 服务端组件安装与 Kibana 配置

  • 安装(以 8.x 为例)
    • 导入 GPG 并配置 8.x 仓库后:
      • dnf/yum install -y elasticsearch logstash kibana
      • systemctl enable --now elasticsearch logstash kibana
    • 安全与接入
      • 安装时会生成 elastic 密码;如需 Kibana 自动接入,使用:/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
      • 若测试环境需临时关闭 HTTP 加密:在 /etc/elasticsearch/elasticsearch.yml 中设置 xpack.security.http.ssl.enabled: false(生产不建议)
  • Kibana 索引模式
    • 访问 https://:5601,进入 Stack Management → Index Patterns,创建索引模式(如 syslog-* 或 syslog-beats-*),时间字段选择 @timestamp

五 验证与常见问题

  • 验证
    • 客户端执行:logger “Test syslog to ELK”
    • Kibana Discover 中查看是否出现对应时间点的日志;Logstash 控制台(若启用 stdout)应有结构化输出。
  • 常见问题
    • 端口未放行:确保 514/udp、514/tcp(直连 Logstash)或 5044/tcp(Beats)已在防火墙放行。
    • 解析失败:检查 grok 模式与日志格式是否匹配;可先用 stdout 调试。
    • 权限/证书:若启用 TLS/认证,确保 Logstash 输出到 Elasticsearch 使用正确账号密码或证书;Kibana 需通过 enrollment token 或有效凭据接入。
    • 版本兼容:部署前核对 Elastic Stack 与操作系统 的兼容矩阵,避免版本不匹配导致启动失败。

0