启动 netconsole 将内核消息发送到远程 syslog 服务器


启动 netconsole 将内核消息发送到远程 syslog 服务器

netconsole 可以将内核的 printk 消息通过 udp 发送到远程主机上,而且还可以将消息发送到远程主机的syslogd里. 

内核配置

一般情况下系统内核都已编译 netconsole 模块, 直接使用即可. 
自定义内核只需将以下内核选项打开即可(编译为模块).

CONFIG_NETCONSOLE=m
CONFIG_NETCONSOLE_DYNAMIC=y

netconsole 模块加载语法

insmod netconsole.ko netconsole=[src-port]@[src-ip]/[<dev>],[tgt-port]@<tgt-ip>/[tgt-macaddr]

src-port 源端口
src-ip   源ip
dev      指定发送数据网卡设备
tgt-port 目的端口
tgt-ip   目的ip
tgt-macaddr 目的MAC地址,一般为当前设备网关; 统一个网关内可直接指定目的ip的MAC

下面为一个自动化脚本, 自动获取当前默认路由网卡ip及网关ip&MAC地址加载netconsole模块脚本,只需指定syslogd服务器目的地址即可.
REMOTE_IP=1.1.1.1
DEVNAME=`ip r |grep default |cut -d' ' -f5`
REMOTE_PORT=514
LOCAL_IP=`ifconfig $DEVNAME |grep "inet addr"|cut -f2 -d':'|cut -d' ' -f1`
LOCAL_PORT=${LOCAL_IP##*.}
VIA=`ip r |grep default |awk '{print $3}'`    
VIAHW=`ping -c 1 $VIA  > /dev/null 2>&1; /sbin/arp -an |grep $VIA | awk '{print $4}'`
insmod netconsole.ko netconsole=$LOCAL_PORT@$LOCAL_IP/$DEVNAME,$REMOTE_PORT@$REMOTE_IP/$VIAHW



注:RHEL6上使用的 syslogd 服务器为 rsyslog, 开启远程接收日志需将 /etc/rsyslog.conf 中以下选项开启UDP 514端口:
$ModLoad imudp
$UDPServerRun 514
重新启动服务/etc/init.d/rsyslog restart.

RHEL5 则只需修改 /etc/sysconfig/syslog 添加 -r 参数
SYSLOGD_OPTIONS="-r -m 0" 
重新启动服务 /etc/init.d/syslog restart

使用lsof -i:514 或者 netstat -anp|grep 514 命令来查看 syslog 服务是不是正常侦听端口, 如有防火墙则需开放UDP的514端口.

在没有syslogd在运行的主机上可以 netcat/socat 接收来自远程主机的消息:
'nc -u -l -p <port>' / 'nc -u -l <port>' or
'netcat -u -l -p <port>' / 'netcat -u -l <port>'
'socat udp-recv:<port> -'

参考:
Documentation/networking/netconsole.txt

你可能感兴趣的:(启动 netconsole 将内核消息发送到远程 syslog 服务器)