doumeng3345 2017-03-09 15:52
浏览 140
已采纳

构造系统日志消息时的输出混乱

I was writing a program which could convert a standard syslog message that is already logged in the file, for eg:

Mar  9 15:51:36 ldaptestserver slapd[392]: slap_global_control: unrecognized control: 1.3.6.1.4.1.42.2.27.8.5.1

to a syslog message including the PRI, header etc.

I was able to do this from a syslog message stored in my kafka test cluster, which visually looked like

2017-03-09T15:22:00.642769+00:00 ldaptestserver slapd[392]: slap_global_control: unrecognized control: 1.3.6.1.4.1.42.2.27.8.5.1

by prepending the priority and initiating a tcp connection to the syslog port, and writing the message as a []byte slice to the socket. The final message before sending it to syslog, after subscribing from kafka looked like

<13>2017-03-09T15:22:00.642769+00:00 ldaptestserver slapd[392]: slap_global_control: unrecognized control: 1.3.6.1.4.1.42.2.27.8.5.1

where only <13> is prepended to the entire log.

Now, instead of kafka, I try to open and read the /var/log/syslog, and constructed the same message that looked like the above,

<13>2017-03-08T12:29:02.231335+00:00 ldaptestserver slapd[392]: slap_global_control: unrecognized control: 1.3.6.1.4.1.42.2.27.8.5.1 // Original one from kafka that worked
<13>2017-00-01T16:18:04.000000+00:00 ldaptestserver slapd[392]: slap_global_control: unrecognized control: 1.3.6.1.4.1.42.2.27.8.5.1 // The message that is hand crafted.

But when I write the []byte message to the syslog connection socket, it produces a scrambled output.

scrambled output

(Notice that the IP is the localhost IP, instead of the hostname in the message. In the case where the similar kafka message was used, it logged perfectly with the original hostname.)

Can someone help me understand where is it messed up, and what should I do to get it without being scrambled?

I was using golang to program this.

-- Scott.

  • 写回答

1条回答 默认 最新

  • doujiong9915 2017-03-17 10:08
    关注

    Instead of printing the working log extracted from kafka as string(), try to dump the []byte slice itself as it is. This will give you a bunch of numbers, which you can translate to its corresponding ascii characters, and you can see if there are any separators like carriage return or line feed that is separating the end of each section in the header and end of line of the message. This wouldn't be visible if you are casting it to string() when printing. Chances are that the syslog server's parser is expecting a line terminator to distinguish between header sections or end of a message, which it is failing to find, and keeps on appending subsequent messages until the parser's buffer runs out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭