Projet

Général

Profil

Netconsole » Historique » Version 15

Laurent GUERBY, 14/08/2017 16:43

1 1 Laurent GUERBY
{{>toc}}
2 1 Laurent GUERBY
3 1 Laurent GUERBY
h1. Netconsole
4 1 Laurent GUERBY
5 13 Laurent GUERBY
h2. Setup
6 1 Laurent GUERBY
7 14 Laurent GUERBY
h3. modprobe client
8 14 Laurent GUERBY
9 14 Laurent GUERBY
<pre>
10 14 Laurent GUERBY
# if needed to remove old netconsole setup
11 14 Laurent GUERBY
echo 0 > /sys/kernel/config/netconsole/nagios/enabled 
12 14 Laurent GUERBY
rmdir /sys/kernel/config/netconsole/nagios/
13 14 Laurent GUERBY
rmmod netconsole
14 14 Laurent GUERBY
15 14 Laurent GUERBY
#h8
16 14 Laurent GUERBY
modprobe netconsole netconsole=+@192.168.128.53/eth0,6666@192.168.128.248/b8:ae:ed:70:8b:80
17 14 Laurent GUERBY
#gcc67
18 14 Laurent GUERBY
modprobe netconsole netconsole=+@91.224.148.12/enp10s0,6666@89.234.156.171/b8:ae:ed:70:8b:80
19 14 Laurent GUERBY
#gcc68
20 14 Laurent GUERBY
modprobe netconsole netconsole=+@91.224.148.13/enp10s0,6666@89.234.156.171/b8:ae:ed:70:8b:80
21 14 Laurent GUERBY
#gcc68 ipv6 link local
22 14 Laurent GUERBY
modprobe netconsole netconsole=+@fe80::7285:c2ff:fe3a:167e/enp10s0,6666@fe80::baae:edff:fe70:8b80/b8:ae:ed:70:8b:80
23 14 Laurent GUERBY
</pre>
24 14 Laurent GUERBY
25 14 Laurent GUERBY
h3. TODO
26 14 Laurent GUERBY
27 14 Laurent GUERBY
TODO early boot le meme format dans grub https://www.kernel.org/doc/Documentation/networking/netconsole.txt
28 14 Laurent GUERBY
29 14 Laurent GUERBY
30 13 Laurent GUERBY
h3. nagios asyncio
31 13 Laurent GUERBY
32 1 Laurent GUERBY
<pre>
33 13 Laurent GUERBY
root@nucnagios:~/netconsole# cat asyncio-udp-logger.py 
34 13 Laurent GUERBY
#!/usr/bin/python3
35 13 Laurent GUERBY
36 13 Laurent GUERBY
import asyncio
37 13 Laurent GUERBY
import datetime
38 13 Laurent GUERBY
import signal
39 13 Laurent GUERBY
import sys
40 13 Laurent GUERBY
41 13 Laurent GUERBY
class NetConsole:
42 13 Laurent GUERBY
    def log(self, *args, **kwargs):
43 13 Laurent GUERBY
        print(sys._getframe().f_code.co_name, args, kwargs)
44 13 Laurent GUERBY
45 13 Laurent GUERBY
    connection_made = log
46 13 Laurent GUERBY
    error_received = log
47 13 Laurent GUERBY
    connection_lost = log
48 13 Laurent GUERBY
49 13 Laurent GUERBY
    def datagram_received(self, data, addr):
50 13 Laurent GUERBY
        logfile = "/root/netconsole/" + "-".join([str(s) for s in addr])
51 13 Laurent GUERBY
        with open(logfile, "a") as f:
52 13 Laurent GUERBY
            f.write(datetime.datetime.utcnow().isoformat()+' '+data.decode())
53 13 Laurent GUERBY
54 13 Laurent GUERBY
    @classmethod
55 13 Laurent GUERBY
    def listen(cls, loop, **kwargs):
56 13 Laurent GUERBY
        return loop.run_until_complete(asyncio.Task(
57 13 Laurent GUERBY
            loop.create_datagram_endpoint(cls, **kwargs)
58 13 Laurent GUERBY
        )) 
59 13 Laurent GUERBY
60 13 Laurent GUERBY
61 13 Laurent GUERBY
if __name__ == '__main__':
62 13 Laurent GUERBY
63 13 Laurent GUERBY
    loop = asyncio.get_event_loop()
64 13 Laurent GUERBY
    loop.add_signal_handler(signal.SIGINT, loop.stop)
65 13 Laurent GUERBY
66 13 Laurent GUERBY
    transportv4, _ = NetConsole.listen(loop, local_addr=("::", 6666))
67 13 Laurent GUERBY
    transportv6, _ = NetConsole.listen(loop, local_addr=("0.0.0.0", 6666))
68 13 Laurent GUERBY
69 13 Laurent GUERBY
    try:
70 13 Laurent GUERBY
        loop.run_forever()
71 13 Laurent GUERBY
    finally:
72 13 Laurent GUERBY
        transportv4.close()
73 13 Laurent GUERBY
        transportv6.close()
74 13 Laurent GUERBY
        loop.stop()
75 13 Laurent GUERBY
</pre>
76 13 Laurent GUERBY
77 13 Laurent GUERBY
h3. nagios selectors
78 13 Laurent GUERBY
79 13 Laurent GUERBY
<pre>
80 13 Laurent GUERBY
root@nucnagios:~/netconsole# cat selectors-udp-logger.py 
81 13 Laurent GUERBY
#!/usr/bin/python3
82 13 Laurent GUERBY
83 13 Laurent GUERBY
import selectors
84 13 Laurent GUERBY
import socket
85 13 Laurent GUERBY
import time
86 15 Laurent GUERBY
import struct
87 15 Laurent GUERBY
import sys
88 13 Laurent GUERBY
89 13 Laurent GUERBY
DIR="/root/netconsole/"
90 13 Laurent GUERBY
PORT=6665
91 13 Laurent GUERBY
92 15 Laurent GUERBY
IP_PKTINFO=8 # socket.IP_PKTINFO missing ?
93 15 Laurent GUERBY
94 13 Laurent GUERBY
sock4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
95 15 Laurent GUERBY
sock4.setsockopt(socket.IPPROTO_IP, IP_PKTINFO, 1) 
96 13 Laurent GUERBY
sock4.bind(('0.0.0.0',PORT))
97 13 Laurent GUERBY
98 13 Laurent GUERBY
sock6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
99 13 Laurent GUERBY
sock6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
100 15 Laurent GUERBY
sock6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVPKTINFO, 1)
101 13 Laurent GUERBY
sock6.bind(('::',PORT))
102 13 Laurent GUERBY
103 13 Laurent GUERBY
sel=selectors.DefaultSelector()
104 13 Laurent GUERBY
sel.register(sock4,selectors.EVENT_READ)
105 13 Laurent GUERBY
sel.register(sock6,selectors.EVENT_READ)
106 13 Laurent GUERBY
107 13 Laurent GUERBY
while True:
108 13 Laurent GUERBY
    events = sel.select()
109 1 Laurent GUERBY
    for key,mask in events:
110 15 Laurent GUERBY
        data, ancdata, msg_flags, address = key.fileobj.recvmsg(16384,16384)
111 15 Laurent GUERBY
        cmsg_l=[]
112 15 Laurent GUERBY
        for cmsg_level, cmsg_type, cmsg_data in ancdata:
113 15 Laurent GUERBY
            if cmsg_level == socket.IPPROTO_IPV6 and cmsg_type==socket.IPV6_PKTINFO:
114 15 Laurent GUERBY
                cmsg_l=[socket.inet_ntop(socket.AF_INET6,cmsg_data[:16]),str(int.from_bytes(cmsg_data[16:20],sys.byteorder))]
115 15 Laurent GUERBY
            elif cmsg_level == socket.IPPROTO_IP and cmsg_type==IP_PKTINFO:
116 15 Laurent GUERBY
                cmsg_l=[socket.inet_ntop(socket.AF_INET,cmsg_data[4:8]),str(int.from_bytes(cmsg_data[0:4],sys.byteorder))]
117 13 Laurent GUERBY
        if len(data)>0 and data[-1]!=10: data+=bytearray([10])
118 15 Laurent GUERBY
        with open(DIR + "-".join(cmsg_l+[str(PORT)]+[str(s) for s in address]), "a") as f:
119 13 Laurent GUERBY
            try:
120 13 Laurent GUERBY
                f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
121 13 Laurent GUERBY
            except Exception as e:
122 13 Laurent GUERBY
                print("Unexpected error:" + str(e))
123 13 Laurent GUERBY
</pre>
124 13 Laurent GUERBY
125 13 Laurent GUERBY
h3. nagios dual
126 13 Laurent GUERBY
127 13 Laurent GUERBY
<pre>
128 13 Laurent GUERBY
# if not managed by supervisord see root@nucnagios:/etc/supervisor/conf.d/*
129 13 Laurent GUERBY
root@nucnagios:~/netconsole# nohup python3 pyudplogger.py >& log.txt < /dev/null &
130 13 Laurent GUERBY
root@nucnagios:~/netconsole# nohup python3 pyudplogger6.py >& log6.txt < /dev/null &
131 13 Laurent GUERBY
</pre>
132 13 Laurent GUERBY
133 13 Laurent GUERBY
Source code :
134 13 Laurent GUERBY
135 13 Laurent GUERBY
<pre>
136 13 Laurent GUERBY
root@nucnagios:~/netconsole# cat pyudplogger.py 
137 13 Laurent GUERBY
#!/usr/bin/python3
138 13 Laurent GUERBY
139 13 Laurent GUERBY
import socket
140 13 Laurent GUERBY
import time
141 13 Laurent GUERBY
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
142 13 Laurent GUERBY
sock.bind(('',6666))
143 13 Laurent GUERBY
while True:
144 13 Laurent GUERBY
    data, address = sock.recvfrom(4096)
145 13 Laurent GUERBY
    if len(data)>0 and data[-1]!=10: data+=bytearray([10])
146 13 Laurent GUERBY
    ip, port = address
147 13 Laurent GUERBY
    with open("/root/netconsole/" + ip + '-' + str(port), "a") as f:
148 13 Laurent GUERBY
        try:
149 13 Laurent GUERBY
            f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
150 13 Laurent GUERBY
        except Exception as e:
151 13 Laurent GUERBY
            print("Unexpected error:" + str(e))
152 13 Laurent GUERBY
root@nucnagios:~/netconsole# cat pyudplogger6.py 
153 13 Laurent GUERBY
#!/usr/bin/python3
154 13 Laurent GUERBY
155 13 Laurent GUERBY
import socket
156 13 Laurent GUERBY
import time
157 13 Laurent GUERBY
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
158 13 Laurent GUERBY
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
159 13 Laurent GUERBY
sock.bind(('::',6666))
160 13 Laurent GUERBY
while True:
161 13 Laurent GUERBY
    data, address = sock.recvfrom(4096)
162 13 Laurent GUERBY
    if len(data)>0 and data[-1]!=10: data+=bytearray([10]) # broken by python2
163 13 Laurent GUERBY
    ip, port, flowinfo, scopeid = address
164 13 Laurent GUERBY
    with open("/root/netconsole/" + ip + '-' + str(port) + '-' + str(flowinfo) + '-' + str(scopeid),"a") as f:
165 13 Laurent GUERBY
        try:
166 13 Laurent GUERBY
            f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
167 13 Laurent GUERBY
        except Exception as e:
168 13 Laurent GUERBY
            print("Unexpected error:" + str(e))
169 13 Laurent GUERBY
170 13 Laurent GUERBY
171 13 Laurent GUERBY
</pre>
172 13 Laurent GUERBY
173 13 Laurent GUERBY
174 13 Laurent GUERBY
175 13 Laurent GUERBY
h2. Old setup
176 13 Laurent GUERBY
177 13 Laurent GUERBY
h3. nagios netcat
178 13 Laurent GUERBY
179 13 Laurent GUERBY
<pre>
180 1 Laurent GUERBY
root@nucnagios:~/netconsole# more setup-console.sh 
181 1 Laurent GUERBY
#!/bin/bash
182 1 Laurent GUERBY
183 1 Laurent GUERBY
here=$(dirname $(readlink -f $0))
184 1 Laurent GUERBY
cleanup(){
185 2 Laurent GUERBY
	kill $(jobs -p)
186 1 Laurent GUERBY
	exit 0
187 1 Laurent GUERBY
}
188 1 Laurent GUERBY
189 1 Laurent GUERBY
trap cleanup EXIT
190 2 Laurent GUERBY
191 2 Laurent GUERBY
nc -u -l -p 6600 > $here/stri.dmesg 2>&1 &
192 1 Laurent GUERBY
nc -u -l -p 6601 > $here/g1.dmesg 2>&1 &
193 1 Laurent GUERBY
nc -u -l -p 6602 > $here/g2.dmesg 2>&1 &
194 1 Laurent GUERBY
nc -u -l -p 6603 > $here/g3.dmesg 2>&1 &
195 1 Laurent GUERBY
nc -u -l -p 6604 > $here/g4.dmesg 2>&1 &
196 1 Laurent GUERBY
nc -u -l -p 6605 > $here/g5.dmesg 2>&1 &
197 1 Laurent GUERBY
nc -u -l -p 6606 > $here/g6.dmesg 2>&1 &
198 1 Laurent GUERBY
nc -u -l -p 6607 > $here/n7.dmesg 2>&1 &
199 1 Laurent GUERBY
nc -u -l -p 6608 > $here/g8.dmesg 2>&1 &
200 1 Laurent GUERBY
nc -u -l -p 6609 > $here/g9.dmesg 2>&1 &
201 1 Laurent GUERBY
nc -u -l -p 6610 > $here/g10.dmesg 2>&1 &
202 1 Laurent GUERBY
nc -u -l -p 6611 > $here/g11.dmesg 2>&1 &
203 1 Laurent GUERBY
nc -u -l -p 6612 > $here/g12.dmesg 2>&1 &
204 1 Laurent GUERBY
nc -u -l -p 6613 > $here/g13.dmesg 2>&1 &
205 1 Laurent GUERBY
nc -u -l -p 6614 > $here/g14.dmesg 2>&1 &
206 1 Laurent GUERBY
nc -u -l -p 6615 > $here/g15.dmesg 2>&1 &
207 1 Laurent GUERBY
nc -u -l -p 6616 > $here/g16.dmesg 2>&1 &
208 1 Laurent GUERBY
209 1 Laurent GUERBY
echo "netconsole listener running"
210 1 Laurent GUERBY
wait
211 1 Laurent GUERBY
212 1 Laurent GUERBY
root@nucnagios:~/netconsole# nohup ./setup-console.sh >& /dev/null < /dev/null &
213 1 Laurent GUERBY
</pre>
214 1 Laurent GUERBY
215 13 Laurent GUERBY
h3. old g1
216 1 Laurent GUERBY
217 1 Laurent GUERBY
<pre>
218 1 Laurent GUERBY
root@g1:~# more /etc/rc.local
219 1 Laurent GUERBY
220 1 Laurent GUERBY
declare -A netconsole_cfg
221 1 Laurent GUERBY
222 1 Laurent GUERBY
netconsole_cfg["g1"]="192.168.128.201:6601"
223 1 Laurent GUERBY
224 2 Laurent GUERBY
225 2 Laurent GUERBY
# Setup some helper vars
226 2 Laurent GUERBY
h=$(hostname -s)
227 2 Laurent GUERBY
netconsole_path=/sys/kernel/config/netconsole/nagios
228 2 Laurent GUERBY
netconsole_ip=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $1}')
229 2 Laurent GUERBY
netconsole_port=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $2}')
230 4 Laurent GUERBY
netconsole_int=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $3}')
231 2 Laurent GUERBY
netconsole_int=${netconsole_int:-eth2}
232 2 Laurent GUERBY
233 2 Laurent GUERBY
# NETCONSOLE interface
234 3 Laurent GUERBY
[ "${netconsole_cfg[$h]}" ] && ip a a $netconsole_ip/24 dev $netconsole_int
235 2 Laurent GUERBY
236 3 Laurent GUERBY
# Setup netconsole
237 5 Laurent GUERBY
modprobe configfs
238 3 Laurent GUERBY
modprobe netconsole
239 2 Laurent GUERBY
mount | grep -q configfs || mount none -t configfs /sys/kernel/config
240 2 Laurent GUERBY
mkdir -p $netconsole_path
241 2 Laurent GUERBY
if [ "${netconsole_cfg[$h]}" ]; then
242 1 Laurent GUERBY
	echo 0 > $netconsole_path/enabled
243 5 Laurent GUERBY
	echo $netconsole_int > $netconsole_path/dev_name
244 6 Laurent GUERBY
	echo $netconsole_ip > $netconsole_path/local_ip
245 6 Laurent GUERBY
	echo $netconsole_port > $netconsole_path/local_port
246 6 Laurent GUERBY
	echo "192.168.128.248" > $netconsole_path/remote_ip
247 6 Laurent GUERBY
	echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
248 6 Laurent GUERBY
	echo 1 > $netconsole_path/enabled
249 6 Laurent GUERBY
	sleep 1
250 7 Laurent GUERBY
	echo m > /proc/sysrq-trigger  # test-it
251 1 Laurent GUERBY
fi
252 6 Laurent GUERBY
</pre>
253 6 Laurent GUERBY
254 13 Laurent GUERBY
h3. old gcc67
255 6 Laurent GUERBY
256 6 Laurent GUERBY
<pre>
257 6 Laurent GUERBY
modprobe configfs
258 6 Laurent GUERBY
modprobe netconsole
259 6 Laurent GUERBY
#mount none -t configfs /sys/kernel/config
260 6 Laurent GUERBY
netconsole_path=/sys/kernel/config/netconsole/nagios
261 6 Laurent GUERBY
mkdir -p $netconsole_path
262 6 Laurent GUERBY
echo 0 > $netconsole_path/enabled
263 6 Laurent GUERBY
echo enp10s0 > $netconsole_path/dev_name
264 6 Laurent GUERBY
echo 91.224.148.12 > $netconsole_path/local_ip
265 8 Laurent GUERBY
echo 6615  > $netconsole_path/local_port
266 8 Laurent GUERBY
echo 6615  > $netconsole_path/remote_port
267 8 Laurent GUERBY
echo "89.234.156.171" > $netconsole_path/remote_ip
268 8 Laurent GUERBY
echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
269 8 Laurent GUERBY
echo 1 > $netconsole_path/enabled
270 1 Laurent GUERBY
sleep 1
271 8 Laurent GUERBY
echo m > /proc/sysrq-trigger  # test-it
272 8 Laurent GUERBY
</pre>
273 8 Laurent GUERBY
274 13 Laurent GUERBY
h3. old gcc68
275 12 Laurent GUERBY
276 8 Laurent GUERBY
<pre>
277 12 Laurent GUERBY
modprobe configfs
278 8 Laurent GUERBY
modprobe netconsole
279 12 Laurent GUERBY
mount none -t configfs /sys/kernel/config
280 9 Laurent GUERBY
netconsole_path=/sys/kernel/config/netconsole/nagios
281 12 Laurent GUERBY
mkdir -p $netconsole_path
282 8 Laurent GUERBY
echo 0 > $netconsole_path/enabled
283 8 Laurent GUERBY
echo enp10s0 > $netconsole_path/dev_name
284 8 Laurent GUERBY
echo 91.224.148.13 > $netconsole_path/local_ip
285 8 Laurent GUERBY
echo 6616  > $netconsole_path/local_port
286 8 Laurent GUERBY
echo 6616  > $netconsole_path/remote_port
287 9 Laurent GUERBY
echo "89.234.156.171" > $netconsole_path/remote_ip
288 8 Laurent GUERBY
echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
289 9 Laurent GUERBY
echo 1 > $netconsole_path/enabled
290 8 Laurent GUERBY
sleep 1
291 11 Laurent GUERBY
echo m > /proc/sysrq-trigger  # test-it
292 11 Laurent GUERBY
</pre>