Projet

Général

Profil

Netconsole » Historique » Version 14

Laurent GUERBY, 12/08/2017 09:29

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 13 Laurent GUERBY
87 13 Laurent GUERBY
DIR="/root/netconsole/"
88 13 Laurent GUERBY
PORT=6665
89 13 Laurent GUERBY
90 13 Laurent GUERBY
sock4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
91 13 Laurent GUERBY
sock4.bind(('0.0.0.0',PORT))
92 13 Laurent GUERBY
93 13 Laurent GUERBY
sock6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
94 13 Laurent GUERBY
sock6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
95 13 Laurent GUERBY
sock6.bind(('::',PORT))
96 13 Laurent GUERBY
97 13 Laurent GUERBY
sel=selectors.DefaultSelector()
98 13 Laurent GUERBY
sel.register(sock4,selectors.EVENT_READ)
99 13 Laurent GUERBY
sel.register(sock6,selectors.EVENT_READ)
100 13 Laurent GUERBY
101 13 Laurent GUERBY
while True:
102 13 Laurent GUERBY
    events = sel.select()
103 13 Laurent GUERBY
    for key,mask in events:
104 13 Laurent GUERBY
        data, address = key.fileobj.recvfrom(4096)
105 13 Laurent GUERBY
        if len(data)>0 and data[-1]!=10: data+=bytearray([10])
106 13 Laurent GUERBY
        with open(DIR + "-".join([str(PORT)]+[str(s) for s in address]), "a") as f:
107 13 Laurent GUERBY
            try:
108 13 Laurent GUERBY
                f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
109 13 Laurent GUERBY
            except Exception as e:
110 13 Laurent GUERBY
                print("Unexpected error:" + str(e))
111 13 Laurent GUERBY
</pre>
112 13 Laurent GUERBY
113 13 Laurent GUERBY
h3. nagios dual
114 13 Laurent GUERBY
115 13 Laurent GUERBY
<pre>
116 13 Laurent GUERBY
# if not managed by supervisord see root@nucnagios:/etc/supervisor/conf.d/*
117 13 Laurent GUERBY
root@nucnagios:~/netconsole# nohup python3 pyudplogger.py >& log.txt < /dev/null &
118 13 Laurent GUERBY
root@nucnagios:~/netconsole# nohup python3 pyudplogger6.py >& log6.txt < /dev/null &
119 13 Laurent GUERBY
</pre>
120 13 Laurent GUERBY
121 13 Laurent GUERBY
Source code :
122 13 Laurent GUERBY
123 13 Laurent GUERBY
<pre>
124 13 Laurent GUERBY
root@nucnagios:~/netconsole# cat pyudplogger.py 
125 13 Laurent GUERBY
#!/usr/bin/python3
126 13 Laurent GUERBY
127 13 Laurent GUERBY
import socket
128 13 Laurent GUERBY
import time
129 13 Laurent GUERBY
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
130 13 Laurent GUERBY
sock.bind(('',6666))
131 13 Laurent GUERBY
while True:
132 13 Laurent GUERBY
    data, address = sock.recvfrom(4096)
133 13 Laurent GUERBY
    if len(data)>0 and data[-1]!=10: data+=bytearray([10])
134 13 Laurent GUERBY
    ip, port = address
135 13 Laurent GUERBY
    with open("/root/netconsole/" + ip + '-' + str(port), "a") as f:
136 13 Laurent GUERBY
        try:
137 13 Laurent GUERBY
            f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
138 13 Laurent GUERBY
        except Exception as e:
139 13 Laurent GUERBY
            print("Unexpected error:" + str(e))
140 13 Laurent GUERBY
root@nucnagios:~/netconsole# cat pyudplogger6.py 
141 13 Laurent GUERBY
#!/usr/bin/python3
142 13 Laurent GUERBY
143 13 Laurent GUERBY
import socket
144 13 Laurent GUERBY
import time
145 13 Laurent GUERBY
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
146 13 Laurent GUERBY
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
147 13 Laurent GUERBY
sock.bind(('::',6666))
148 13 Laurent GUERBY
while True:
149 13 Laurent GUERBY
    data, address = sock.recvfrom(4096)
150 13 Laurent GUERBY
    if len(data)>0 and data[-1]!=10: data+=bytearray([10]) # broken by python2
151 13 Laurent GUERBY
    ip, port, flowinfo, scopeid = address
152 13 Laurent GUERBY
    with open("/root/netconsole/" + ip + '-' + str(port) + '-' + str(flowinfo) + '-' + str(scopeid),"a") as f:
153 13 Laurent GUERBY
        try:
154 13 Laurent GUERBY
            f.write(time.strftime("%Y%m%dT%H%M%S")+':'+data.decode())
155 13 Laurent GUERBY
        except Exception as e:
156 13 Laurent GUERBY
            print("Unexpected error:" + str(e))
157 13 Laurent GUERBY
158 13 Laurent GUERBY
159 13 Laurent GUERBY
</pre>
160 13 Laurent GUERBY
161 13 Laurent GUERBY
162 13 Laurent GUERBY
163 13 Laurent GUERBY
h2. Old setup
164 13 Laurent GUERBY
165 13 Laurent GUERBY
h3. nagios netcat
166 13 Laurent GUERBY
167 13 Laurent GUERBY
<pre>
168 1 Laurent GUERBY
root@nucnagios:~/netconsole# more setup-console.sh 
169 1 Laurent GUERBY
#!/bin/bash
170 1 Laurent GUERBY
171 1 Laurent GUERBY
here=$(dirname $(readlink -f $0))
172 1 Laurent GUERBY
cleanup(){
173 2 Laurent GUERBY
	kill $(jobs -p)
174 1 Laurent GUERBY
	exit 0
175 1 Laurent GUERBY
}
176 1 Laurent GUERBY
177 1 Laurent GUERBY
trap cleanup EXIT
178 2 Laurent GUERBY
179 2 Laurent GUERBY
nc -u -l -p 6600 > $here/stri.dmesg 2>&1 &
180 1 Laurent GUERBY
nc -u -l -p 6601 > $here/g1.dmesg 2>&1 &
181 1 Laurent GUERBY
nc -u -l -p 6602 > $here/g2.dmesg 2>&1 &
182 1 Laurent GUERBY
nc -u -l -p 6603 > $here/g3.dmesg 2>&1 &
183 1 Laurent GUERBY
nc -u -l -p 6604 > $here/g4.dmesg 2>&1 &
184 1 Laurent GUERBY
nc -u -l -p 6605 > $here/g5.dmesg 2>&1 &
185 1 Laurent GUERBY
nc -u -l -p 6606 > $here/g6.dmesg 2>&1 &
186 1 Laurent GUERBY
nc -u -l -p 6607 > $here/n7.dmesg 2>&1 &
187 1 Laurent GUERBY
nc -u -l -p 6608 > $here/g8.dmesg 2>&1 &
188 1 Laurent GUERBY
nc -u -l -p 6609 > $here/g9.dmesg 2>&1 &
189 1 Laurent GUERBY
nc -u -l -p 6610 > $here/g10.dmesg 2>&1 &
190 1 Laurent GUERBY
nc -u -l -p 6611 > $here/g11.dmesg 2>&1 &
191 1 Laurent GUERBY
nc -u -l -p 6612 > $here/g12.dmesg 2>&1 &
192 1 Laurent GUERBY
nc -u -l -p 6613 > $here/g13.dmesg 2>&1 &
193 1 Laurent GUERBY
nc -u -l -p 6614 > $here/g14.dmesg 2>&1 &
194 1 Laurent GUERBY
nc -u -l -p 6615 > $here/g15.dmesg 2>&1 &
195 1 Laurent GUERBY
nc -u -l -p 6616 > $here/g16.dmesg 2>&1 &
196 1 Laurent GUERBY
197 1 Laurent GUERBY
echo "netconsole listener running"
198 1 Laurent GUERBY
wait
199 1 Laurent GUERBY
200 1 Laurent GUERBY
root@nucnagios:~/netconsole# nohup ./setup-console.sh >& /dev/null < /dev/null &
201 1 Laurent GUERBY
</pre>
202 1 Laurent GUERBY
203 13 Laurent GUERBY
h3. old g1
204 1 Laurent GUERBY
205 1 Laurent GUERBY
<pre>
206 1 Laurent GUERBY
root@g1:~# more /etc/rc.local
207 1 Laurent GUERBY
208 1 Laurent GUERBY
declare -A netconsole_cfg
209 1 Laurent GUERBY
210 1 Laurent GUERBY
netconsole_cfg["g1"]="192.168.128.201:6601"
211 1 Laurent GUERBY
212 2 Laurent GUERBY
213 2 Laurent GUERBY
# Setup some helper vars
214 2 Laurent GUERBY
h=$(hostname -s)
215 2 Laurent GUERBY
netconsole_path=/sys/kernel/config/netconsole/nagios
216 2 Laurent GUERBY
netconsole_ip=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $1}')
217 2 Laurent GUERBY
netconsole_port=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $2}')
218 4 Laurent GUERBY
netconsole_int=$(echo ${netconsole_cfg[$h]} | awk -F: '{print $3}')
219 2 Laurent GUERBY
netconsole_int=${netconsole_int:-eth2}
220 2 Laurent GUERBY
221 2 Laurent GUERBY
# NETCONSOLE interface
222 3 Laurent GUERBY
[ "${netconsole_cfg[$h]}" ] && ip a a $netconsole_ip/24 dev $netconsole_int
223 2 Laurent GUERBY
224 3 Laurent GUERBY
# Setup netconsole
225 5 Laurent GUERBY
modprobe configfs
226 3 Laurent GUERBY
modprobe netconsole
227 2 Laurent GUERBY
mount | grep -q configfs || mount none -t configfs /sys/kernel/config
228 2 Laurent GUERBY
mkdir -p $netconsole_path
229 2 Laurent GUERBY
if [ "${netconsole_cfg[$h]}" ]; then
230 1 Laurent GUERBY
	echo 0 > $netconsole_path/enabled
231 5 Laurent GUERBY
	echo $netconsole_int > $netconsole_path/dev_name
232 6 Laurent GUERBY
	echo $netconsole_ip > $netconsole_path/local_ip
233 6 Laurent GUERBY
	echo $netconsole_port > $netconsole_path/local_port
234 6 Laurent GUERBY
	echo "192.168.128.248" > $netconsole_path/remote_ip
235 6 Laurent GUERBY
	echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
236 6 Laurent GUERBY
	echo 1 > $netconsole_path/enabled
237 6 Laurent GUERBY
	sleep 1
238 7 Laurent GUERBY
	echo m > /proc/sysrq-trigger  # test-it
239 1 Laurent GUERBY
fi
240 6 Laurent GUERBY
</pre>
241 6 Laurent GUERBY
242 13 Laurent GUERBY
h3. old gcc67
243 6 Laurent GUERBY
244 6 Laurent GUERBY
<pre>
245 6 Laurent GUERBY
modprobe configfs
246 6 Laurent GUERBY
modprobe netconsole
247 6 Laurent GUERBY
#mount none -t configfs /sys/kernel/config
248 6 Laurent GUERBY
netconsole_path=/sys/kernel/config/netconsole/nagios
249 6 Laurent GUERBY
mkdir -p $netconsole_path
250 6 Laurent GUERBY
echo 0 > $netconsole_path/enabled
251 6 Laurent GUERBY
echo enp10s0 > $netconsole_path/dev_name
252 6 Laurent GUERBY
echo 91.224.148.12 > $netconsole_path/local_ip
253 8 Laurent GUERBY
echo 6615  > $netconsole_path/local_port
254 8 Laurent GUERBY
echo 6615  > $netconsole_path/remote_port
255 8 Laurent GUERBY
echo "89.234.156.171" > $netconsole_path/remote_ip
256 8 Laurent GUERBY
echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
257 8 Laurent GUERBY
echo 1 > $netconsole_path/enabled
258 1 Laurent GUERBY
sleep 1
259 8 Laurent GUERBY
echo m > /proc/sysrq-trigger  # test-it
260 8 Laurent GUERBY
</pre>
261 8 Laurent GUERBY
262 13 Laurent GUERBY
h3. old gcc68
263 12 Laurent GUERBY
264 8 Laurent GUERBY
<pre>
265 12 Laurent GUERBY
modprobe configfs
266 8 Laurent GUERBY
modprobe netconsole
267 12 Laurent GUERBY
mount none -t configfs /sys/kernel/config
268 9 Laurent GUERBY
netconsole_path=/sys/kernel/config/netconsole/nagios
269 12 Laurent GUERBY
mkdir -p $netconsole_path
270 8 Laurent GUERBY
echo 0 > $netconsole_path/enabled
271 8 Laurent GUERBY
echo enp10s0 > $netconsole_path/dev_name
272 8 Laurent GUERBY
echo 91.224.148.13 > $netconsole_path/local_ip
273 8 Laurent GUERBY
echo 6616  > $netconsole_path/local_port
274 8 Laurent GUERBY
echo 6616  > $netconsole_path/remote_port
275 9 Laurent GUERBY
echo "89.234.156.171" > $netconsole_path/remote_ip
276 8 Laurent GUERBY
echo "b8:ae:ed:70:8b:80" > $netconsole_path/remote_mac
277 9 Laurent GUERBY
echo 1 > $netconsole_path/enabled
278 8 Laurent GUERBY
sleep 1
279 11 Laurent GUERBY
echo m > /proc/sysrq-trigger  # test-it
280 11 Laurent GUERBY
</pre>