【问题标题】:Inspect UDP syslog packets in Ruby在 Ruby 中检查 UDP syslog 数据包
【发布时间】:2013-08-02 22:39:55
【问题描述】:

我正在尝试构建一个基于 Ruby (1.9.1) 的系统日志服务器,并且从一开始就遇到了一个非常基本的问题。

这是我的(非常基本的)代码:

#!/usr/bin/env ruby

require 'socket'
require 'io/wait'
require 'syslog'

class Server
    def initialize
        @listener = UDPSocket.new
        @listener.bind("192.168.253.5", "514")
        getdata
    end

    def getdata
        while true
            @text, @sender = @listener.recvfrom(9000)
            p @listener
            p @text
            p @sender
        end
    end
end

x = Server.new

一切正常,只是它不显示消息的功能或严重性:

#<UDPSocket:fd 5>
"<189>49: *Mar  1 00:24:37.862: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/8, changed state to down"
["AF_INET", 56970, "192.168.253.10", "192.168.253.10"]

Tcpdump 可以很好地显示此信息(“local7”设施,“notice”严重性):

15:18:01.987542 IP 192.168.253.10.56970 > 192.168.253.5.514: SYSLOG local7.notice, length: 115

如何检查发送给我的 UDP 数据包,以便收集系统日志消息的功能和严重性?

【问题讨论】:

    标签: ruby sockets udp syslog


    【解决方案1】:

    每当您要实现定义良好的网络协议时,请始终查看 RFC:

    https://www.rfc-editor.org/rfc/rfc5424

    The Priority value is calculated by first multiplying the Facility
       number by 8 and then adding the numerical value of the Severity. 
    

    所以“local7”根据 RFC 是 23。 23 * 8 = 184

    “通知”的严重性为 5:184 + 5 = 189。

    在您的消息开头有 189 - 这是 RFC 引用的“优先级”编号。

    因此,您需要将来自 RFC 的数值和文本描述之间的映射编码到您的程序中并自行计算。

    要获得 severify 和设施:

    Severity = Priority % 8
    Facility = Priority / 8
    

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 2014-06-23
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多