Projet

Général

Profil

Netconsole » Historique » Version 13

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