如何在系统日志消息中欺骗主机名?
我在 perl 中记录这样的消息 -
syslog ("LOG_INFO", "this is info");
syslog ("LOG_WARNING", "this is warning");
当我看到这些消息时,我得到这个 -
Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is info
Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is warning
syslog 消息中的单词“ubuntu”恰好是本地主机的主机名。
有没有办法可以在本地登录并指定主机名?
我的应用程序处理来自其他主机的数据并记录有关它们的信息。如果我可以在记录消息时指定主机名,那就太好了,这样我就可以轻松使用第三方工具,因为它们可以轻松地根据主机名过滤掉日志。
顺便说一句,如果我可以添加其他问题 - 为什么日志不显示消息级别?我不应该期望在我正在记录的信息系统日志消息中看到“信息”吗?
I am logging messages like this in perl -
syslog ("LOG_INFO", "this is info");
syslog ("LOG_WARNING", "this is warning");
when I see these messages, I get this-
Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is info
Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is warning
The word "ubuntu" in syslog message happens to be host name of the local host.
Is there a way I can log locally and but specify a hostname?
My app processes data from other hosts and logs information about them. It will be great if I can specify the host name when I log messages, this way I can use third party tools easily as they can easily filter out logs based upon hostname.
btw, if I can add additional question- why are the logs not showing level of message? shouldn't I expect to see "info" in info syslog message I am logging ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完成此任务的最简单方法可能是将
syslog
设置为通过网络接收消息。对于rsyslog
,这通常位于/etc/rsyslog.conf
中:我在这里使用UDP,因为它很容易在任何
syslog
上执行code> 守护进程并演示它很容易:简而言之:
表示设施和优先级,如 RFC 第 4.1.1 节。时间戳在 4.1.2 中得到了高度指定:简短的三个字母的英文月份缩写,没有前导0
——而是一个前导空格:Aug__8
而不是Aug_8
(使用下划线是因为代码块中的空格会折叠)。主机名不能包含任何域部分。 IP 地址很好,IPv4 和 IPv6 都可以。您还可以使用 Unix 域套接字 (
unix(7)
),例如/dev/log
。这比 UDP 更可靠。Probably the easiest way to accomplish this task is to set the
syslog
to receive messages over the network. Forrsyslog
, this is often in/etc/rsyslog.conf
:I'm using UDP here because it is easy to do on just about any
syslog
daemon and demonstrating it is easy:In short: the
<nnn>
represents the facility and priority, as described in section 4.1.1 of the RFC. The timestamp is highly specified in 4.1.2: in short, three-letter English month abbreviations, no leading0
-- instead a leading space:Aug__8
rather thanAug_8
(underscores used because spaces collapse in code blocks). The hostname can't have any domain portions. IP addresses are fine, both IPv4 and IPv6.You could also use Unix domain sockets (
unix(7)
) such as/dev/log
. That would be more reliable than UDP.不幸的是,我认为如果您在本地登录,则无法指定主机名。
另外,您不应该在日志前面看到“信息”。日志级别会影响消息的去向(您可以在 /etc/syslog.conf 中对此进行自定义)。默认情况下,LOG_INFO 和 LOG_WARNING 转到 /var/log/messages.log,LOG_EMERG 和 LOG_ERR 转到 /var/log/errors.log。该级别不会出现在输出中。
Unfortunately, I don't believe it is possible to specify a hostname if you are logging locally.
Also, you are not supposed to be seeing 'info' in front of your logs. The log level affects where the messages go (you can customize this in /etc/syslog.conf). By default, LOG_INFO and LOG_WARNING go to /var/log/messages.log and LOG_EMERG and LOG_ERR go to /var/log/errors.log. The level does not appear in the output.