-
Notifications
You must be signed in to change notification settings - Fork 4
/
monitor.go
484 lines (450 loc) · 10.8 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package monitor
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"syscall"
"time"
"unsafe"
"github.com/platinasystems/goes/cmd/ip/internal/options"
"github.com/platinasystems/goes/lang"
"github.com/platinasystems/goes/internal/nl"
"github.com/platinasystems/goes/internal/nl/rtnl"
)
type Command struct{}
func (Command) String() string { return "monitor" }
func (Command) Usage() string {
return `
ip monitor file FILE [label]
ip monitor [ all | OBJECT... ] save file
ip monitor [ all | OBJECT... ] [label] [all-nsid] [-t | -ts]
OBJECT := link | address | route | mroute | prefix | neigh | netconf | rule |
nsid`
}
func (Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "print netlink messages",
}
}
func (Command) Man() lang.Alt {
return lang.Alt{
lang.EnUS: `
OPTIONS
file FILE
read netlink messages from FILE instead of socket
save FILE
instead of print, output raw, time stamped messages to FILE
label identify type of message (e.g. LINK, ADDR, NEIGH, ROUTE)
all-nsid
listen in all identified network namespaces
-t, -timestamp
Prints timestamp before the event message on the separated line
in format:
Timestamp: <Day> <Mon> <DD> <hh:mm:ss><.ns> <z> <YYYY>
<EVENT>
-ts, -tshort
Prints short timestamp before the event message on the same
line in format:
[<YYYY>-<MM>-<DD>T<hh:mm:ss>.<ns><+|-tz>] <EVENT>
SEE ALSO
ip man monitor || ip monitor -man
man ip || ip -man`,
}
}
func (Command) Main(args ...string) error {
var err error
var handle func([]byte)
var save save
var show show
show.opt, args = options.New(args)
args = show.opt.Flags.More(args,
"all",
"link",
"address",
"route",
"mroute",
"prefix",
"neigh",
"netconf",
"rule",
"nsid",
"all-nsid",
"label",
)
args = show.opt.Parms.More(args,
"file",
"save",
"dev",
)
if len(args) > 0 {
return fmt.Errorf("%v: unexpected", args)
}
if fn := show.opt.Parms.ByName["save"]; len(fn) > 0 {
save.File, err = os.Create(fn)
if err != nil {
return err
}
defer save.Close()
save.tsbuf = make([]byte, nl.SizeofHdr+sizeofTstamp)
handle = save.Handle
} else {
show.nsid = -1
handle = show.Handle
}
if fn := show.opt.Parms.ByName["file"]; len(fn) > 0 {
b, err := ioutil.ReadFile(fn)
if err != nil {
return err
}
for err == nil && len(b) > nl.SizeofHdr {
var msg []byte
msg, b, err = nl.Pop(b)
handle(msg)
}
return err
}
err = func() error {
sock, err := nl.NewSock()
if err != nil {
return err
}
defer sock.Close()
return rtnl.MakeIfMaps(nl.NewSockReceiver(sock))
}()
if err != nil {
return err
}
sock, err := nl.NewSock(nl.NETLINK_ROUTE, 16, groups(show.opt),
show.opt.Flags.ByName["all-nsid"])
if err != nil {
return err
}
defer sock.Close()
sigch := make(chan os.Signal)
signal.Notify(sigch, os.Interrupt, os.Signal(syscall.SIGTERM))
selectLoop:
for err == nil {
select {
case <-sigch:
break selectLoop
case b, opened := <-sock.RxCh:
if !opened {
break selectLoop
}
for err == nil && len(b) > nl.SizeofHdr {
var msg []byte
msg, b, err = nl.Pop(b)
handle(msg)
}
}
}
return err
}
func (Command) Complete(args ...string) (list []string) {
var larg, llarg string
n := len(args)
if n > 0 {
larg = args[n-1]
}
if n > 1 {
llarg = args[n-2]
}
cpv := options.CompleteParmValue
cpv["file"] = options.CompleteFile
cpv["save"] = options.NoComplete
if method, found := cpv[llarg]; found {
list = method(larg)
} else {
for _, name := range append(options.CompleteOptNames,
"file",
"save",
"label",
"all-nsid",
"all",
"link",
"address",
"route",
"mroute",
"prefix",
"neigh",
"netconf",
"rule",
"nsid",
) {
if len(larg) == 0 || strings.HasPrefix(name, larg) {
list = append(list, name)
}
}
}
return
}
func groups(opt *options.Options) uint32 {
var groups uint32
if opt.Flags.ByName["link"] {
groups |= rtnl.RTNLGRP_LINK.Bit()
}
if opt.Flags.ByName["neigh"] {
groups |= rtnl.RTNLGRP_NEIGH.Bit()
groups |= rtnl.RTNLGRP_ND_USEROPT.Bit()
}
if opt.Flags.ByName["nsid"] {
groups |= rtnl.RTNLGRP_NSID.Bit()
}
switch opt.Parms.ByName["-f"] {
case "inet":
if opt.Flags.ByName["address"] {
groups |= rtnl.RTNLGRP_IPV4_IFADDR.Bit()
}
if opt.Flags.ByName["route"] {
groups |= rtnl.RTNLGRP_IPV4_ROUTE.Bit()
}
if opt.Flags.ByName["mroute"] {
groups |= rtnl.RTNLGRP_IPV4_MROUTE.Bit()
}
if opt.Flags.ByName["netconf"] {
groups |= rtnl.RTNLGRP_IPV4_NETCONF.Bit()
}
if opt.Flags.ByName["rule"] {
groups |= rtnl.RTNLGRP_IPV4_RULE.Bit()
}
case "inet6":
if opt.Flags.ByName["address"] {
groups |= rtnl.RTNLGRP_IPV6_IFADDR.Bit()
}
if opt.Flags.ByName["route"] {
groups |= rtnl.RTNLGRP_IPV6_ROUTE.Bit()
}
if opt.Flags.ByName["mroute"] {
groups |= rtnl.RTNLGRP_IPV6_MROUTE.Bit()
}
if opt.Flags.ByName["netconf"] {
groups |= rtnl.RTNLGRP_IPV6_NETCONF.Bit()
}
if opt.Flags.ByName["prefix"] {
groups |= rtnl.RTNLGRP_IPV6_PREFIX.Bit()
}
if opt.Flags.ByName["rule"] {
groups |= rtnl.RTNLGRP_IPV6_RULE.Bit()
}
default:
if opt.Flags.ByName["address"] {
groups |= rtnl.RTNLGRP_IPV4_IFADDR.Bit()
groups |= rtnl.RTNLGRP_IPV6_IFADDR.Bit()
}
if opt.Flags.ByName["route"] {
groups |= rtnl.RTNLGRP_IPV4_ROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV6_ROUTE.Bit()
}
if opt.Flags.ByName["mroute"] {
groups |= rtnl.RTNLGRP_IPV4_MROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV6_MROUTE.Bit()
}
if opt.Flags.ByName["netconf"] {
groups |= rtnl.RTNLGRP_IPV4_NETCONF.Bit()
groups |= rtnl.RTNLGRP_IPV6_NETCONF.Bit()
}
if opt.Flags.ByName["prefix"] {
groups |= rtnl.RTNLGRP_IPV6_PREFIX.Bit()
}
if opt.Flags.ByName["rule"] {
groups |= rtnl.RTNLGRP_IPV4_RULE.Bit()
groups |= rtnl.RTNLGRP_IPV6_RULE.Bit()
}
}
if groups != 0 && !opt.Flags.ByName["all"] {
return groups
}
groups |= rtnl.RTNLGRP_LINK.Bit()
groups |= rtnl.RTNLGRP_NEIGH.Bit()
groups |= rtnl.RTNLGRP_ND_USEROPT.Bit()
groups |= rtnl.RTNLGRP_NSID.Bit()
switch opt.Parms.ByName["-f"] {
case "inet":
groups |= rtnl.RTNLGRP_IPV4_IFADDR.Bit()
groups |= rtnl.RTNLGRP_IPV4_ROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV4_MROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV4_NETCONF.Bit()
groups |= rtnl.RTNLGRP_IPV4_RULE.Bit()
case "inet6":
groups |= rtnl.RTNLGRP_IPV6_IFADDR.Bit()
groups |= rtnl.RTNLGRP_IPV6_ROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV6_MROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV6_NETCONF.Bit()
groups |= rtnl.RTNLGRP_IPV6_PREFIX.Bit()
groups |= rtnl.RTNLGRP_IPV6_RULE.Bit()
default:
groups |= rtnl.RTNLGRP_IPV4_IFADDR.Bit()
groups |= rtnl.RTNLGRP_IPV4_ROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV4_MROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV4_NETCONF.Bit()
groups |= rtnl.RTNLGRP_IPV4_RULE.Bit()
groups |= rtnl.RTNLGRP_IPV6_IFADDR.Bit()
groups |= rtnl.RTNLGRP_IPV6_ROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV6_MROUTE.Bit()
groups |= rtnl.RTNLGRP_IPV6_NETCONF.Bit()
groups |= rtnl.RTNLGRP_IPV6_PREFIX.Bit()
groups |= rtnl.RTNLGRP_IPV6_RULE.Bit()
}
return groups
}
type save struct {
*os.File
tsbuf []byte
}
func (save *save) Handle(b []byte) {
if len(b) < nl.SizeofHdr {
return
}
now := time.Now()
*(nl.HdrPtr(save.tsbuf)) = nl.Hdr{
Len: uint32(len(save.tsbuf)),
Type: nl.NLMSG_TSTAMP,
}
*(*tstamp)(unsafe.Pointer(&save.tsbuf[nl.SizeofHdr])) = tstamp{
secs: uint32(now.Unix()),
usecs: uint32(now.UnixNano() / 1000),
}
save.Write(save.tsbuf)
save.Write(b)
}
type show struct {
opt *options.Options
nsid int
}
func (show *show) Handle(b []byte) {
const tfmt = "Mon Jan 01 15:04:05.999999999-07:00 2006"
var deleted bool
if len(b) < nl.SizeofHdr {
return
}
h := nl.HdrPtr(b)
heading := func(label string) {
if show.opt.Flags.ByName["-t"] {
show.opt.Print(time.Now().Format(tfmt), "\n")
} else if show.opt.Flags.ByName["-ts"] {
show.opt.Print("[",
time.Now().Format(time.RFC3339Nano),
"] ")
}
if show.opt.Flags.ByName["all-nsid"] {
if show.nsid == -1 {
show.opt.Print("[nsid current] ")
} else {
show.opt.Print("[nsid ", show.nsid, "] ")
}
}
if show.opt.Flags.ByName["label"] && len(label) > 0 {
show.opt.Print("[", label, "] ")
}
if deleted {
show.opt.Print("Deleted ")
}
}
switch h.Type {
case nl.NLMSG_NSID:
show.nsid = *(*int)(unsafe.Pointer(&b[nl.SizeofHdr]))
case rtnl.RTM_DELROUTE:
deleted = true
fallthrough
case rtnl.RTM_NEWROUTE:
heading("ROUTE")
show.opt.ShowRoute(b)
case rtnl.RTM_DELLINK:
deleted = true
heading("LINK")
show.opt.ShowIfInfo(b)
msg := rtnl.IfInfoMsgPtr(b)
delete(rtnl.If.IndexByName, rtnl.If.NameByIndex[msg.Index])
delete(rtnl.If.NameByIndex, msg.Index)
case rtnl.RTM_NEWLINK:
var ifla rtnl.Ifla
heading("LINK")
ifla.Write(b)
msg := rtnl.IfInfoMsgPtr(b)
rtnl.If.NameByIndex[msg.Index] =
nl.Kstring(ifla[rtnl.IFLA_IFNAME])
show.opt.ShowIfInfo(b)
case rtnl.RTM_DELADDR:
deleted = true
fallthrough
case rtnl.RTM_NEWADDR:
const withCacheInfo = false
heading("ADDR")
show.opt.ShowIfAddr(b, withCacheInfo)
case rtnl.RTM_DELADDRLABEL:
deleted = true
fallthrough
case rtnl.RTM_NEWADDRLABEL:
heading("ADDRLABEL")
show.opt.ShowIfAddrLbl(b)
case rtnl.RTM_DELNEIGH:
deleted = true
fallthrough
case rtnl.RTM_NEWNEIGH, rtnl.RTM_GETNEIGH:
heading("NEIGH")
show.opt.ShowNeigh(b)
case rtnl.RTM_NEWPREFIX:
heading("PREFIX")
show.opt.ShowPrefix(b)
case rtnl.RTM_DELRULE:
deleted = true
fallthrough
case rtnl.RTM_NEWRULE:
heading("RULE")
show.opt.ShowRule(b)
case rtnl.RTM_NEWNETCONF:
heading("NETCONF")
show.opt.ShowNetconf(b)
case nl.NLMSG_TSTAMP:
ts := *(*tstamp)(unsafe.Pointer(&b[nl.SizeofHdr]))
show.opt.Print("Timestamp: ", time.Unix(int64(ts.secs),
int64(ts.usecs*1000)))
case rtnl.RTM_DELNSID:
deleted = true
fallthrough
case rtnl.RTM_NEWNSID:
heading("NSID")
var netnsa rtnl.Netnsa
var sep string
netnsa.Write(b)
if val := netnsa[rtnl.NETNSA_NSID]; len(val) > 0 {
show.opt.Print("nsid=", nl.Int32(val))
sep = ", "
}
if val := netnsa[rtnl.NETNSA_PID]; len(val) > 0 {
show.opt.Print(sep, "pid=", nl.Uint32(val))
sep = ", "
}
if val := netnsa[rtnl.NETNSA_FD]; len(val) > 0 {
show.opt.Print(sep, "fd=", nl.Uint32(val))
}
case nl.NLMSG_NOOP:
heading("NOOP")
show.opt.Print("pid=", h.Pid, ", seq=", h.Seq)
case nl.NLMSG_DONE:
heading("DONE")
show.opt.Print("pid=", h.Pid, ", seq=", h.Seq)
case nl.NLMSG_ERROR:
p := nl.NlmsgerrPtr(b)
if p == nil || p.Errno == 0 {
heading("ACK")
show.opt.Print("pid=", h.Pid, " seq=", h.Seq)
} else {
heading("ERROR")
show.opt.Println(syscall.Errno(p.Errno))
show.opt.Print("type=", p.Req.Type)
show.opt.Print("; pid=", p.Req.Pid)
show.opt.Print("; seq=", p.Req.Seq)
}
}
fmt.Println()
}
const sizeofTstamp = 4 + 4
type tstamp struct {
secs, usecs uint32
}