使用 Azure Monitor 从虚拟机客户端收集 Syslog 事件

Syslog 是普遍适用于 Linux 的事件日志记录协议。 可以使用内置于 Linux 设备的 Syslog 守护程序来收集你指定的类型的本地事件。 应用程序会发送存储在本地计算机或传递到 Syslog 收集器的消息。 使用具有 Linux Syslog 数据源的数据收集规则(DCR)从虚拟机收集 Syslog 事件。

小窍门

若要从不允许本地安装 Azure Monitor 代理的设备收集数据,请按照使用 Azure Monitor 代理将 Syslog 数据转发到 Microsoft Sentinel 的 Log Analytics 工作区中描述的方式配置基于 Linux 的专用日志转发器。

有关创建 DCR 的详细信息,请参阅 使用 Azure Monitor 从 VM 客户端收集数据。 本文提供有关 Linux Syslog 数据源类型的更多详细信息。

注释

若要直接使用 DCR 定义或使用 ARM 模板等其他方法进行部署,请参阅 Azure Monitor 中的数据收集规则(DCR)示例

配置 Syslog 数据源

根据使用 Azure Monitor 从虚拟机客户端收集数据的过程创建 DCR。 在 DCR 的“收集和传递”选项卡上,从“数据源类型”下拉列表中选择 Linux Syslog

为每个设施选择最小日志级别,或者选择NONE来不收集该设施的任何事件。 你可以一次性配置多个设施,方法是选中其复选框,然后在 “设置所选设施的最低日志级别”中选择日志级别

用于选择数据源类型和最低日志级别的页面的屏幕截图。

针对设施收集具有所选严重性级别及更高级别的所有日志。 支持的严重性级别及其相对严重性如下所示:

  1. 调试
  2. 信息
  3. 通知
  4. 警告
  5. 错误
  6. 危急
  7. 警报
  8. 紧急情况

添加目标

Syslog 数据只能发送到存储在 Syslog 表中的 Log Analytics 工作区。 添加 Azure Monitor 日志 类型的目标并选择 Log Analytics 工作区。 虽然可以添加多个工作区,但请注意,这会向每个工作区发送重复的数据,这将导致额外的成本。

显示数据收集规则中 Azure Monitor 日志目标的配置的屏幕截图。

验证数据收集

若要验证是否正在收集数据,请检查 Syslog 表中的记录。 在虚拟机或 Azure 门户中的 Log Analytics 工作区中,选择 “日志 ”,然后单击“ ”按钮。 在“虚拟机”类别下,单击 Syslog 旁边的“运行”。

显示从 Syslog 表返回的记录的屏幕截图。

在 Linux 代理上配置 Syslog

在 Linux 计算机上安装 Azure Monitor 代理时,它会安装默认的 Syslog 配置文件,该文件定义设施以及在 DCR 中启用 Syslog 时收集的消息的严重级别。 此配置文件视客户端已安装的 Syslog 守护程序而异。

注释

Azure Monitor Linux 代理版本 1.15.2 或更高版本支持 Syslog RFC 格式,包括 Cisco Meraki、Cisco ASA、Cisco FTD、Sophos XG、Juniper Networks、Corelight Zeek、CipherTrust、NXLog、McAfee 和通用事件格式 (CEF)。

Rsyslog

在许多 Linux 发行版中,rsyslogd 守护程序负责使用、存储和路由使用 Linux Syslog API 发送的日志消息。 Azure Monitor 代理使用 rsyslog 中的 TCP 转发输出模块 (omfwd) 转发日志消息。

Azure Monitor 代理安装包括位于 /etc/opt/microsoft/azuremonitoragent/syslog/rsyslogconf/ 中的默认配置文件。 在 Syslog 添加到 DCR 时,此配置将安装在 etc/rsyslog.d 系统目录下,rsyslog 将自动重启,从而使更改生效。

注释

在基于 rsyslog 的系统上,Azure Monitor Linux 代理将转发规则添加到 rsyslog 配置中定义的默认规则集。 如果使用了多个规则集,则绑定到非默认规则集的输入不会转发到 Azure Monitor 代理。 有关 rsyslog 中的多个规则集的详细信息,请参阅官方文档

下面是默认配置,它用于收集从本地代理发送的 Syslog 消息,涵盖所有设备的所有日志级别。

$ cat /etc/rsyslog.d/10-azuremonitoragent-omfwd.conf
# Azure Monitor Agent configuration: forward logs to azuremonitoragent

template(name="AMA_RSYSLOG_TraditionalForwardFormat" type="string" string="<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg%")
# queue.workerThreads sets the maximum worker threads, it will scale back to 0 if there is no activity
# Forwarding all events through TCP port
*.* action(type="omfwd"
template="AMA_RSYSLOG_TraditionalForwardFormat"
queue.type="LinkedList"
queue.filename="omfwd-azuremonitoragent"
queue.maxFileSize="32m"
queue.maxDiskSpace="1g"
action.resumeRetryCount="-1"
action.resumeInterval="5"
action.reportSuspension="on"
action.reportSuspensionContinuation="on"
queue.size="25000"
queue.workerThreads="100"
queue.dequeueBatchSize="2048"
queue.saveonshutdown="on"
target="127.0.0.1" Port="28330" Protocol="tcp")

当你使用 SELinux 并决定使用 Unix 套接字时,将使用以下配置。

$ cat /etc/rsyslog.d/10-azuremonitoragent.conf
# Azure Monitor Agent configuration: forward logs to azuremonitoragent
$OMUxSockSocket /run/azuremonitoragent/default_syslog.socket
template(name="AMA_RSYSLOG_TraditionalForwardFormat" type="string" string="<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg%") 
$OMUxSockDefaultTemplate AMA_RSYSLOG_TraditionalForwardFormat
# Forwarding all events through Unix Domain Socket
*.* :omuxsock: 
$ cat /etc/rsyslog.d/05-azuremonitoragent-loadomuxsock.conf
# Azure Monitor Agent configuration: load rsyslog forwarding module. 
$ModLoad omuxsock

在某些旧系统上,当使用传统转发格式将 Syslog 事件发送到 Azure Monitor 代理时,可能会出现 rsyslog 日志格式设置问题。 对于这些系统,Azure Monitor 代理会自动放置旧版转发器模板:

template(name="AMA_RSYSLOG_TraditionalForwardFormat" type="string" string="%TIMESTAMP% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg%\n")

Syslog-ng

Azure Monitor 代理安装包括位于 /etc/opt/microsoft/azuremonitoragent/syslog/syslog-ngconf/azuremonitoragent-tcp.conf 中的默认配置文件。 在 Syslog 添加到 DCR 时,此配置将安装在 /etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf 系统目录下,syslog-ng 将自动重启,从而使更改生效。

默认内容如下例所示。 该示例收集从本地代理发送的涉及所有设施和所有严重性级别的 Syslog 消息。

$ cat /etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf 
# Azure MDSD configuration: syslog forwarding config for mdsd agent
options {};

# during install time, we detect if s_src exist, if it does then we
# replace it by appropriate source name like in redhat 's_sys'
# Forwrding using tcp
destination d_azure_mdsd {
	network("127.0.0.1" 
	port(28330)
	log-fifo-size(25000));			
};

log {
	source(s_src); # will be automatically parsed from /etc/syslog-ng/syslog-ng.conf
	destination(d_azure_mdsd);
	flags(flow-control);
};

当你使用 SELinux 并决定使用 Unix 套接字时,将使用以下配置。

$ cat /etc/syslog-ng/conf.d/azuremonitoragent.conf 
# Azure MDSD configuration: syslog forwarding config for mdsd agent options {}; 
# during install time, we detect if s_src exist, if it does then we 
# replace it by appropriate source name like in redhat 's_sys' 
# Forwrding using unix ___domain socket 
destination d_azure_mdsd { 
	unix-dgram("/run/azuremonitoragent/default_syslog.socket" 
	flags(no_multi_line) ); 
};
 
log {
	source(s_src); # will be automatically parsed from /etc/syslog-ng/syslog-ng.conf 
	destination(d_azure_mdsd);
}; 

注释

当 rsyslog 为默认守护程序时,Azure Monitor 支持 rsyslog 或 syslog-ng 发送的消息集合。 Red Hat Enterprise Linux 版本 5 和 Oracle Linux 版本 (sysklog) 上的默认 Syslog 守护程序不支持 Syslog 事件收集。 要从这些发行版的此版本中收集 Syslog 数据,应安装并配置 rsyslog 守护程序以替换 sysklog。

如果编辑 Syslog 配置,必须重启 Syslog 守护程序才能使更改生效。

支持的设施

Syslog 收集器支持以下功能:

Pri 索引 Pri 名称
0 没有
1 Kern
2 user
3 mail
4 daemon
4 auth
5 系统日志 (syslog)
6 lpr
7 news
8 uucp
9 FTP(文件传输协议)
10 ntp
11 审计
12 警报
13 mark
14 local0
15 local1
16 local2
十七 local3
18 local4
19 local5
20 local6
21 local7

后续步骤