netconsole 重定向kernel日志到远程服务器

netconsole 重定向kernel日志到远程服务器

linux netconsole 内核模块可以通过配置转发规则讲内核日志通过 udp 发送到远程服务器。通用的Linux发行版内核都已编译 netconsole 模块, 直接使用即可。

如自定义内核启用netconsole只需将以下内核选项打开即可(编译为模块).

CONFIG_NETCONSOLE=m
CONFIG_NETCONSOLE_DYNAMIC=y


此文为之前的netconsole博文的更新版本.

配置

netconsole 模块加载语法

modprobe netconsole 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

日志服务器在同一内网, 直接通过ping获取服务器目的MAC:

$ ping -c 1 192.168.1.103 > /dev/null
$ arp -n 192.168.1.103
 Address                  HWtype  HWaddress           Flags Mask            Iface
 192.168.1.103            ether   08:00:46:d4:1d:82   C                     eth0

日志服务器不在同一内网, 获取网关目的MAC:

$ netstat -rn | grep ^0.0.0.0
 0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
$ ping -c 1 192.168.1.1 > /dev/null
$ arp -n 192.168.1.1
 Address                  HWtype  HWaddress           Flags Mask            Iface
 192.168.1.1              ether   00:0f:66:5b:2a:3c   C                     eth0

加载netconsole:

echo options netconsole netconsole=6666@192.168.1.102/eth0,6666@192.168.1.103/08:00:46:d4:1d:82 > /etc/modprobe.d/netconsole.conf
modprobe netconsole
or
modprobe netconsole netconsole=6666@192.168.1.102/eth0,6666@192.168.1.103/08:00:46:d4:1d:82

以下为自动化配置脚本, 用于非同网段使用。
自动获取当前默认路由网卡ip及网关ip&MAC地址加载netconsole模块脚本,只需指定syslogd服务器目的地址REMOTE_IP即可.

REMOTE_IP=1.1.1.1
DEVNAME=`ip r |grep default |cut -d' ' -f5`
REMOTE_PORT=6666
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}'`
modprobe netconsole netconsole=$LOCAL_PORT@$LOCAL_IP/$DEVNAME,$REMOTE_PORT@$REMOTE_IP/$VIAHW

netconsole 动态配置

netconsole 使用 configfs 接口实现动态配置, CentOS/RHEL/Fedora/Ubuntu 启动默认都已自动 mount configfs:

mount |grep configfs

如未mount, 执行以下命令:

mount -t configfs configfs /sys/kernel/config

动态配置命令:

modprobe netconsole
cd /sys/kernel/config/netconsole
# 新建一条规则
mkdir 103
cd 103
ls -l
total 0
-rw-r--r-- 1 root root 4096 Mar 11 01:17 dev_name
-rw-r--r-- 1 root root 4096 Mar 11 01:17 enabled
-rw-r--r-- 1 root root 4096 Mar 11 01:17 extended
-rw-r--r-- 1 root root 4096 Mar 11 01:17 local_ip
-r--r--r-- 1 root root 4096 Mar 11 01:17 local_mac
-rw-r--r-- 1 root root 4096 Mar 11 01:17 local_port
-rw-r--r-- 1 root root 4096 Mar 11 01:17 remote_ip
-rw-r--r-- 1 root root 4096 Mar 11 01:17 remote_mac
-rw-r--r-- 1 root root 4096 Mar 11 01:17 remote_port
# 配置与命令行参数一致
echo eth0 > dev_name
echo 6666 > local_port
echo 192.168.1.102 > local_ip
echo 192.168.1.103 > remote_ip
echo 6666 > remote_port
echo 08:00:46:d4:1d:82 > remote_mac
# 启用规则
echo 1 > enabled
# 禁用规则
echo 0 > enabled

删除规则

cd 
rmdir /sys/kernel/config/netconsole/103

取消netconsole只需卸载模块即可:

rmmod netconsole

rsyslog

/etc/rsyslog.conf 添加UDP 6666端口监听:

module(load="imudp")
input(type="imudp" port="6666")

重启rsyslog 服务:

systemctl  restart rsyslog

:
防火墙需开放6666端口.

firewall-cmd --zone=$(firewall-cmd --get-default-zone) --add-port=6666/udp --permanent

netcat

在没有syslogd在运行的主机上可以使用 netcat/socat 接收来自远程主机的消息:

nc -u -l -p <port> / nc -u -l <port>
netcat -u -l -p <port> / netcat -u -l <port>
socat udp-recv:<port> -

示例:

nc -l -u 192.168.1.103 6666 | tee ~/netconsole.log

dmesg

-n/--console-level 控制kernel message console 输出级别, 设置输出level为debug(-n 8):

dmesg -n 8

参考:
Documentation/networking/netconsole.txt

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值