forked from projectcalico/calicoctl
/
status.go
423 lines (369 loc) · 11.8 KB
/
status.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
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package node
import (
"bufio"
"errors"
"fmt"
"net"
"os"
"regexp"
"strings"
"time"
"reflect"
log "github.com/Sirupsen/logrus"
"github.com/docopt/docopt-go"
gops "github.com/mitchellh/go-ps"
"github.com/olekukonko/tablewriter"
gobgp "github.com/osrg/gobgp/client"
"github.com/osrg/gobgp/packet/bgp"
)
// Status prints status of the node and returns error (if any)
func Status(args []string) {
doc := `Usage:
calicoctl node status
Options:
-h --help Show this screen.
Description:
Check the status of the Calico node instance. This incudes the status and
uptime of the node instance, and BGP peering states.
`
parsedArgs, err := docopt.Parse(doc, args, true, "", false, false)
if err != nil {
fmt.Printf("Invalid option: 'calicoctl %s'. Use flag '--help' to read about a specific subcommand.\n", strings.Join(args, " "))
os.Exit(1)
}
if len(parsedArgs) == 0 {
return
}
// Must run this command as root to be able to connect to BIRD sockets
enforceRoot()
// Go through running processes and check if `calico-felix` processes is not running
processes, err := gops.Processes()
if err != nil {
fmt.Println(err)
}
if !psContains("calico-felix", processes) {
// Return and print message if calico-node is not running
fmt.Printf("Calico process is not running.\n")
os.Exit(1)
}
fmt.Printf("Calico process is running.\n")
if psContains("bird", processes) || psContains("bird6", processes) {
// Check if birdv4 process is running, print the BGP peer table if it is, else print a warning
if psContains("bird", processes) {
printBIRDPeers("4")
} else {
fmt.Printf("\nINFO: BIRDv4 process: 'bird' is not running.\n")
}
// Check if birdv6 process is running, print the BGP peer table if it is, else print a warning
if psContains("bird6", processes) {
printBIRDPeers("6")
} else {
fmt.Printf("\nINFO: BIRDv6 process: 'bird6' is not running.\n")
}
} else if psContains("calico-bgp-daemon", processes) {
printGoBGPPeers("4")
printGoBGPPeers("6")
} else {
fmt.Printf("\nNone of the BGP backend processes (BIRD or GoBGP) are running.\n")
}
// Have to manually enter an empty line because the table print
// library prints the last line, so can't insert a '\n' there
fmt.Println()
}
func psContains(proc string, procList []gops.Process) bool {
if len(proc) > 15 {
// go-ps returns executable name which is truncated to 15 characters.
// Some work is proceeding to fix this. https://github.com/mitchellh/go-ps/pull/14
// Until this get fixed, truncate proc to size 15.
proc = proc[:15]
}
for _, p := range procList {
if p.Executable() == proc {
return true
}
}
return false
}
// Check for Word_<IP> where every octate is seperated by "_", regardless of IP protocols
// Example match: "Mesh_192_168_56_101" or "Mesh_fd80_24e2_f998_72d7__2"
var bgpPeerRegex = regexp.MustCompile(`^(Global|Node|Mesh)_(.+)$`)
// Mapping the BIRD/GoBGP type extracted from the peer name to the display type.
var bgpTypeMap = map[string]string{
"Global": "global",
"Mesh": "node-to-node mesh",
"Node": "node specific",
}
// Timeout for querying BIRD
var birdTimeOut = 2 * time.Second
// Expected BIRD protocol table columns
var birdExpectedHeadings = []string{"name", "proto", "table", "state", "since", "info"}
// bgpPeer is a structure containing details about a BGP peer.
type bgpPeer struct {
PeerIP string
PeerType string
State string
Since string
BGPState string
Info string
}
// Unmarshal a peer from a line in the BIRD protocol output. Returns true if
// successful, false otherwise.
func (b *bgpPeer) unmarshalBIRD(line, ipSep string) bool {
// Split into fields. We expect at least 6 columns:
// name, proto, table, state, since and info.
// The info column contains the BGP state plus possibly some additional
// info (which will be columns > 6).
//
// Peer names will be of the format described by bgpPeerRegex.
log.Debugf("Parsing line: %s", line)
columns := strings.Fields(line)
if len(columns) < 6 {
log.Debugf("Not a valid line: fewer than 6 columns")
return false
}
if columns[1] != "BGP" {
log.Debugf("Not a valid line: protocol is not BGP")
return false
}
// Check the name of the peer is of the correct format. This regex
// returns two components:
// - A type (Global|Node|Mesh) which we can map to a display type
// - An IP address (with _ separating the octets)
sm := bgpPeerRegex.FindStringSubmatch(columns[0])
if len(sm) != 3 {
log.Debugf("Not a valid line: peer name '%s' is not correct format", columns[0])
return false
}
var ok bool
b.PeerIP = strings.Replace(sm[2], "_", ipSep, -1)
if b.PeerType, ok = bgpTypeMap[sm[1]]; !ok {
log.Debugf("Not a valid line: peer type '%s' is not recognized", sm[1])
return false
}
// Store remaining columns (piecing back together the info string)
b.State = columns[3]
b.Since = columns[4]
b.BGPState = columns[5]
if len(columns) > 6 {
b.Info = strings.Join(columns[6:], " ")
}
return true
}
// printBIRDPeers queries BIRD and displays the local peers in table format.
func printBIRDPeers(ipv string) {
log.Debugf("Print BIRD peers for IPv%s", ipv)
birdSuffix := ""
if ipv == "6" {
birdSuffix = "6"
}
fmt.Printf("\nIPv%s BGP status\n", ipv)
// Try connecting to the bird socket in `/var/run/calico/` first to get the data
c, err := net.Dial("unix", fmt.Sprintf("/var/run/calico/bird%s.ctl", birdSuffix))
if err != nil {
// If that fails, try connecting to bird socket in `/var/run/bird` (which is the
// default socket location for bird install) for non-containerized installs
log.Debugln("Failed to connect to BIRD socket in /var/run/calic, trying /var/run/bird")
c, err = net.Dial("unix", fmt.Sprintf("/var/run/bird/bird%s.ctl", birdSuffix))
if err != nil {
fmt.Printf("Error querying BIRD: unable to connect to BIRDv%s socket: %v", ipv, err)
return
}
}
defer c.Close()
// To query the current state of the BGP peers, we connect to the BIRD
// socket and send a "show protocols" message. BIRD responds with
// peer data in a table format.
//
// Send the request.
_, err = c.Write([]byte("show protocols\n"))
if err != nil {
fmt.Printf("Error executing command: unable to write to BIRD socket: %s\n", err)
os.Exit(1)
}
// Scan the output and collect parsed BGP peers
log.Debugln("Reading output from BIRD")
peers, err := scanBIRDPeers(ipv, c)
if err != nil {
fmt.Printf("Error executing command: %v", err)
os.Exit(1)
}
// If no peers were returned then just print a message.
if len(peers) == 0 {
fmt.Printf("No IPv%s peers found.\n", ipv)
return
}
// Finally, print the peers.
printPeers(peers)
}
// scanBIRDPeers scans through BIRD output to return a slice of bgpPeer
// structs.
//
// We split this out from the main printBIRDPeers() function to allow us to
// test this processing in isolation.
func scanBIRDPeers(ipv string, conn net.Conn) ([]bgpPeer, error) {
// Determine the separator to use for an IP address, based on the
// IP version.
ipSep := "."
if ipv == "6" {
ipSep = ":"
}
// The following is sample output from BIRD
//
// 0001 BIRD 1.5.0 ready.
// 2002-name proto table state since info
// 1002-kernel1 Kernel master up 2016-11-21
// device1 Device master up 2016-11-21
// direct1 Direct master up 2016-11-21
// Mesh_172_17_8_102 BGP master up 2016-11-21 Established
// 0000
scanner := bufio.NewScanner(conn)
peers := []bgpPeer{}
// Set a time-out for reading from the socket connection.
conn.SetReadDeadline(time.Now().Add(birdTimeOut))
for scanner.Scan() {
// Process the next line that has been read by the scanner.
str := scanner.Text()
log.Debugf("Read: %s\n", str)
if strings.HasPrefix(str, "0000") {
// "0000" means end of data
break
} else if strings.HasPrefix(str, "0001") {
// "0001" code means BIRD is ready.
} else if strings.HasPrefix(str, "2002") {
// "2002" code means start of headings
f := strings.Fields(str[5:])
if !reflect.DeepEqual(f, birdExpectedHeadings) {
return nil, errors.New("unknown BIRD table output format")
}
} else if strings.HasPrefix(str, "1002") {
// "1002" code means first row of data.
peer := bgpPeer{}
if peer.unmarshalBIRD(str[5:], ipSep) {
peers = append(peers, peer)
}
} else if strings.HasPrefix(str, " ") {
// Row starting with a " " is another row of data.
peer := bgpPeer{}
if peer.unmarshalBIRD(str[1:], ipSep) {
peers = append(peers, peer)
}
} else {
// Format of row is unexpected.
return nil, errors.New("unexpected output line from BIRD")
}
// Before reading the next line, adjust the time-out for
// reading from the socket connection.
conn.SetReadDeadline(time.Now().Add(birdTimeOut))
}
return peers, scanner.Err()
}
// printGoBGPPeers queries GoBGP and displays the local peers in table format.
func printGoBGPPeers(ipv string) {
client, err := gobgp.NewGoBGPClient("")
if err != nil {
fmt.Printf("Error creating gobgp client: %s\n", err)
os.Exit(1)
}
defer client.Close()
afi := bgp.AFI_IP
if ipv == "6" {
afi = bgp.AFI_IP6
}
fmt.Printf("\nIPv%s BGP status\n", ipv)
neighbors, err := client.ListNeighborByTransport(afi)
if err != nil {
fmt.Printf("Error retrieving neighbor info: %s\n", err)
os.Exit(1)
}
formatTimedelta := func(d int64) string {
u := uint64(d)
neg := d < 0
if neg {
u = -u
}
secs := u % 60
u /= 60
mins := u % 60
u /= 60
hours := u % 24
days := u / 24
if days == 0 {
return fmt.Sprintf("%02d:%02d:%02d", hours, mins, secs)
} else {
return fmt.Sprintf("%dd ", days) + fmt.Sprintf("%02d:%02d:%02d", hours, mins, secs)
}
}
now := time.Now()
peers := make([]bgpPeer, 0, len(neighbors))
for _, n := range neighbors {
ipString := n.Config.NeighborAddress
description := n.Config.Description
adminState := string(n.State.AdminState)
sessionState := strings.Title(string(n.State.SessionState))
timeStr := "never"
if n.Timers.State.Uptime != 0 {
t := int(n.Timers.State.Downtime)
if sessionState == "Established" {
t = int(n.Timers.State.Uptime)
}
timeStr = formatTimedelta(int64(now.Sub(time.Unix(int64(t), 0)).Seconds()))
}
sm := bgpPeerRegex.FindStringSubmatch(description)
if len(sm) != 3 {
log.Debugf("Not a valid line: peer name '%s' is not recognized", description)
continue
}
var ok bool
var typ string
if typ, ok = bgpTypeMap[sm[1]]; !ok {
log.Debugf("Not a valid line: peer type '%s' is not recognized", sm[1])
continue
}
peers = append(peers, bgpPeer{
PeerIP: ipString,
PeerType: typ,
State: adminState,
Since: timeStr,
BGPState: sessionState,
})
}
// If no peers were returned then just print a message.
if len(peers) == 0 {
fmt.Printf("No IPv%s peers found.\n", ipv)
return
}
// Finally, print the peers.
printPeers(peers)
}
// printPeers prints out the slice of peers in table format.
func printPeers(peers []bgpPeer) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Peer address", "Peer type", "State", "Since", "Info"})
for _, peer := range peers {
info := peer.BGPState
if peer.Info != "" {
info += " " + peer.Info
}
row := []string{
peer.PeerIP,
peer.PeerType,
peer.State,
peer.Since,
info,
}
table.Append(row)
}
table.Render()
}