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