Project

General

Profile

Netconsole

Setup

modprobe client

# if needed to remove old netconsole setup
echo 0 > /sys/kernel/config/netconsole/nagios/enabled 
rmdir /sys/kernel/config/netconsole/nagios/
rmmod netconsole

#h8
modprobe netconsole netconsole=+@192.168.128.53/eth0,6666@192.168.128.248/b8:ae:ed:70:8b:80
#gcc67
modprobe netconsole netconsole=+@91.224.148.12/enp10s0,6666@89.234.156.171/b8:ae:ed:70:8b:80
#gcc68
modprobe netconsole netconsole=+@91.224.148.13/enp10s0,6666@89.234.156.171/b8:ae:ed:70:8b:80
#gcc68 ipv6 link local
modprobe netconsole netconsole=+@fe80::7285:c2ff:fe3a:167e/enp10s0,6666@fe80::baae:edff:fe70:8b80/b8:ae:ed:70:8b:80

TODO

TODO early boot le meme format dans grub https://www.kernel.org/doc/Documentation/networking/netconsole.txt

nagios asyncio

root@nucnagios:~/netconsole# cat asyncio-udp-logger.py 
#!/usr/bin/python3

import asyncio
import datetime
import signal
import sys

class NetConsole:
    def log(self, *args, **kwargs):
        print(sys._getframe().f_code.co_name, args, kwargs)

    connection_made = log
    error_received = log
    connection_lost = log

    def datagram_received(self, data, addr):
        logfile = "/root/netconsole/" + "-".join([str(s) for s in addr])
        with open(logfile, "a") as f:
            f.write(datetime.datetime.utcnow().isoformat()+' '+data.decode())

    @classmethod
    def listen(cls, loop, **kwargs):
        return loop.run_until_complete(asyncio.Task(
            loop.create_datagram_endpoint(cls, **kwargs)
        )) 

if __name__ == '__main__':

    loop = asyncio.get_event_loop()
    loop.add_signal_handler(signal.SIGINT, loop.stop)

    transportv4, _ = NetConsole.listen(loop, local_addr=("::", 6666))
    transportv6, _ = NetConsole.listen(loop, local_addr=("0.0.0.0", 6666))

    try:
        loop.run_forever()
    finally:
        transportv4.close()
        transportv6.close()
        loop.stop()

nagios selectors

root@nucnagios:~/netconsole# cat selectors-udp-logger.py 
#!/usr/bin/python3

import selectors
import socket
import time
import sys

DIR="/root/netconsole/" 
PORT=6665

IP_PKTINFO=8 # socket.IP_PKTINFO missing ? http://bugs.python.org/issue31203

sock4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock4.setsockopt(socket.IPPROTO_IP, IP_PKTINFO, 1) 
sock4.bind(('0.0.0.0',PORT))

sock6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
sock6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVPKTINFO, 1)
sock6.bind(('::',PORT))

sel=selectors.DefaultSelector()
sel.register(sock4,selectors.EVENT_READ)
sel.register(sock6,selectors.EVENT_READ)

while True:
    events = sel.select()
    for key,mask in events:
        data, ancdata, msg_flags, address = key.fileobj.recvmsg(16384,16384)
        cmsg_l=[]
        for cmsg_level, cmsg_type, cmsg_data in ancdata:
            if cmsg_level == socket.IPPROTO_IPV6 and cmsg_type==socket.IPV6_PKTINFO:
                cmsg_l=[socket.inet_ntop(socket.AF_INET6,cmsg_data[:16]),str(int.from_bytes(cmsg_data[16:20],sys.byteorder))]
            elif cmsg_level == socket.IPPROTO_IP and cmsg_type==IP_PKTINFO:
                cmsg_l=[socket.inet_ntop(socket.AF_INET,cmsg_data[4:8]),str(int.from_bytes(cmsg_data[0:4],sys.byteorder))]
        if len(data)>0 and data[-1]!=10: data+=bytearray([10])
        with open(DIR + "-".join(cmsg_l+[str(PORT)]+[str(s) for s in address]), "a") as f:
            try:
                f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
            except Exception as e:
                print("Unexpected error:" + str(e))

nagios dual

# if not managed by supervisord see root@nucnagios:/etc/supervisor/conf.d/*
root@nucnagios:~/netconsole# nohup python3 pyudplogger.py >& log.txt < /dev/null &
root@nucnagios:~/netconsole# nohup python3 pyudplogger6.py >& log6.txt < /dev/null &

Source code :

root@nucnagios:~/netconsole# cat pyudplogger.py 
#!/usr/bin/python3

import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('',6666))
while True:
    data, address = sock.recvfrom(4096)
    if len(data)>0 and data[-1]!=10: data+=bytearray([10])
    ip, port = address
    with open("/root/netconsole/" + ip + '-' + str(port), "a") as f:
        try:
            f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
        except Exception as e:
            print("Unexpected error:" + str(e))
root@nucnagios:~/netconsole# cat pyudplogger6.py 
#!/usr/bin/python3

import socket
import time
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
sock.bind(('::',6666))
while True:
    data, address = sock.recvfrom(4096)
    if len(data)>0 and data[-1]!=10: data+=bytearray([10]) # broken by python2
    ip, port, flowinfo, scopeid = address
    with open("/root/netconsole/" + ip + '-' + str(port) + '-' + str(flowinfo) + '-' + str(scopeid),"a") as f:
        try:
            f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
        except Exception as e:
            print("Unexpected error:" + str(e))

Old setup

nagios netcat

root@nucnagios:~/netconsole# more setup-console.sh 
#!/bin/bash

here=$(dirname $(readlink -f $0))
cleanup(){
    kill $(jobs -p)
    exit 0
}

trap cleanup EXIT

nc -u -l -p 6600 > $here/stri.dmesg 2>&1 &
nc -u -l -p 6601 > $here/g1.dmesg 2>&1 &
nc -u -l -p 6602 > $here/g2.dmesg 2>&1 &
nc -u -l -p 6603 > $here/g3.dmesg 2>&1 &
nc -u -l -p 6604 > $here/g4.dmesg 2>&1 &
nc -u -l -p 6605 > $here/g5.dmesg 2>&1 &
nc -u -l -p 6606 > $here/g6.dmesg 2>&1 &
nc -u -l -p 6607 > $here/n7.dmesg 2>&1 &
nc -u -l -p 6608 > $here/g8.dmesg 2>&1 &
nc -u -l -p 6609 > $here/g9.dmesg 2>&1 &
nc -u -l -p 6610 > $here/g10.dmesg 2>&1 &
nc -u -l -p 6611 > $here/g11.dmesg 2>&1 &
nc -u -l -p 6612 > $here/g12.dmesg 2>&1 &
nc -u -l -p 6613 > $here/g13.dmesg 2>&1 &
nc -u -l -p 6614 > $here/g14.dmesg 2>&1 &
nc -u -l -p 6615 > $here/g15.dmesg 2>&1 &
nc -u -l -p 6616 > $here/g16.dmesg 2>&1 &

echo "netconsole listener running" 
wait

root@nucnagios:~/netconsole# nohup ./setup-console.sh >& /dev/null < /dev/null &

old g1

root@g1:~# more /etc/rc.local

declare -A netconsole_cfg

netconsole_cfg["g1"]="192.168.128.201:6601" 

# Setup some helper vars
h=$(hostname -s)
netconsole_path=/sys/kernel/config/netconsole/nagios
netconsole_ip=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $1}')
netconsole_port=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $2}')
netconsole_int=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $3}')
netconsole_int=${netconsole_int:-eth2}

# NETCONSOLE interface
[ "${netconsole_cfg[$h]}" ] && ip a a $netconsole_ip/24 dev $netconsole_int

# Setup netconsole
modprobe configfs
modprobe netconsole
mount | grep -q configfs || mount none -t configfs /sys/kernel/config
mkdir -p $netconsole_path
if [ "${netconsole_cfg[$h]}" ]; then
    echo 0 > $netconsole_path/enabled
    echo $netconsole_int > $netconsole_path/dev_name
    echo $netconsole_ip > $netconsole_path/local_ip
    echo $netconsole_port > $netconsole_path/local_port
    echo "192.168.128.248" > $netconsole_path/remote_ip
    echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
    echo 1 > $netconsole_path/enabled
    sleep 1
    echo m > /proc/sysrq-trigger  # test-it
fi

old gcc67

modprobe configfs
modprobe netconsole
#mount none -t configfs /sys/kernel/config
netconsole_path=/sys/kernel/config/netconsole/nagios
mkdir -p $netconsole_path
echo 0 > $netconsole_path/enabled
echo enp10s0 > $netconsole_path/dev_name
echo 91.224.148.12 > $netconsole_path/local_ip
echo 6615  > $netconsole_path/local_port
echo 6615  > $netconsole_path/remote_port
echo "89.234.156.171" > $netconsole_path/remote_ip
echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
echo 1 > $netconsole_path/enabled
sleep 1
echo m > /proc/sysrq-trigger  # test-it

old gcc68

modprobe configfs
modprobe netconsole
mount none -t configfs /sys/kernel/config
netconsole_path=/sys/kernel/config/netconsole/nagios
mkdir -p $netconsole_path
echo 0 > $netconsole_path/enabled
echo enp10s0 > $netconsole_path/dev_name
echo 91.224.148.13 > $netconsole_path/local_ip
echo 6616  > $netconsole_path/local_port
echo 6616  > $netconsole_path/remote_port
echo "89.234.156.171" > $netconsole_path/remote_ip
echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
echo 1 > $netconsole_path/enabled
sleep 1
echo m > /proc/sysrq-trigger  # test-it