-
Notifications
You must be signed in to change notification settings - Fork 1
/
beye
executable file
·301 lines (241 loc) · 6.35 KB
/
beye
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
#!/usr/bin/python -u
import argparse
import json
import logging
import os
import pprint
import re
import sys
import time
import socket
import requests
from datetime import datetime
from dateutil import parser
from tabulate import tabulate
###########
defcols = {
'status': ['version','router_id','server_time','last_reconfig','last_reboot','message'],
'protocols': ['protocol,state,bgp_state,routes,description'],
'routes': ['network,interface,gateway,metric,bgp.local_pref,bgp.med,bgp.as_path'],
'route': ['network,interface,gateway,metric,bgp'],
}
commands = 'Commands: status / symbols / symbols tables / symbols protocols / protocols bgp / routes protocol [protocol] / routes table [table] / route [prefix] [table]'
ap = argparse.ArgumentParser(description='beye: birdseye CLI',epilog=commands)
ap.add_argument("cmd", nargs='?', default="status", help="command")
ap.add_argument("args", nargs='*', help="argument(s)")
ap.add_argument("-s", "--server", default="rc1-cix-ipv4.inex.ie", help="Server URL [rc1-cix-ipv4.inex.ie]")
ap.add_argument("-v", "--verbosity", action="count", help="output verbosity (multiple times to increase)")
ap.add_argument("-c", "--cols", action="append", help="columns to display")
ap.add_argument("-p", "--plain", action="count", help="plain output for piping, -pp removes blanks within columns")
ap.add_argument("-a", "--all", action="count", help="output everything as tree structure")
ap.add_argument("-r", "--raw", action="count", help="output full API response as tree structure")
cmdl = ap.parse_args()
os.environ['TZ'] = "UTC 0";
pp = pprint.PrettyPrinter(indent=4)
pid = os.getpid()
logging.basicConfig(level=logging.WARNING)
re_date = re.compile("[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+")
re_version = re.compile("^1\.")
############
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
def datestr(str):
dt = parser.parse(str)
if (dt.hour==0 & dt.minute==0 & dt.second==0):
return(dt.strftime("%Y-%m-%d"))
else:
return(dt.strftime("%Y-%m-%d %H:%M:%S"))
def beyeget(server, cmd):
resp = requests.get('http://'+server+'/api/'+cmd)
if resp.status_code != 200:
logging.error("Server Error: %d, %s", resp.status_code, resp.reason)
sys.exit(1)
if cmdl.verbosity >2:
print "Got string: <", resp.text, ">"
r = resp.json()
if re_version.match(r['api']['version']) == None:
logging.warning("API major version changed from 1.x.x to %s", r['api']['version'])
return(r)
def pformat(v):
if cmdl.plain>1:
return(str(do_pformat(v)).replace(' ','_'))
else:
return(do_pformat(v))
def do_pformat(v):
if type(v) is list:
if type(v[0]) is list:
if (len(v)==1):
return(str(v[0]).replace(' ',''))
else:
return(str(v).replace(' ',''))
return(','.join(v))
else:
try:
if re_date.match(v) != None:
return(datestr(v))
else:
return(v)
except Exception:
return(v)
def getrow(dict, cols):
r =[]
res =[]
for c in cols:
d = dict
c = c.split('.')
if len(c)>1:
for s in range(len(c)-1):
d = dict[c[s]]
v = d.get(c[-1], '-')
if 'values' in dir(v):
for vv in v.values():
r.append(pformat(vv))
else:
r.append(pformat(v))
return(r)
def gethead(dict, cols):
r =[]
for c in cols:
v = dict.get(c, '-')
if 'keys' in dir(v):
for vv in v.keys():
r.append(vv)
else:
r.append(c)
return(r)
def colsexpand(cols):
r=[]
for c in cols:
for cc in c.split(','):
r.append(cc)
return(r)
def statprint(r):
tab=[]
rv = r['status']
if cmdl.all > 0:
pp.pprint(rv)
else:
headers = gethead(rv,cols)
tab.append(getrow(rv, cols))
if len(tab) > 0:
if cmdl.plain>0:
print tabulate(tab, tablefmt="plain")
else:
print tabulate(tab, headers=headers)
def protprint(r):
if cmdl.all > 0:
pp.pprint(r['protocols'])
else:
tab=[]
headers=[]
for rk in r['protocols']:
rv = r['protocols'][rk]
if len(headers) == 0:
headers = gethead(rv,cols)
tab.append(getrow(rv, cols))
if len(tab) > 0:
if cmdl.plain>0:
print tabulate(tab, tablefmt="plain")
else:
print tabulate(tab, headers=headers)
def routesprint(r):
if cmdl.all > 0:
pp.pprint(r)
else:
tab=[]
headers=[]
for rv in r:
if len(headers) == 0:
headers = gethead(rv,cols)
tab.append(getrow(rv, cols))
if len(tab) > 0:
if cmdl.plain>0:
print tabulate(tab, tablefmt="plain")
else:
print tabulate(tab, headers=headers)
def symsprint(r):
if cmdl.all>0 | len(cmdl.args)==0:
pp.pprint(r['symbols'])
else:
for rv in r['symbols']:
print rv
###############
if cmdl.verbosity >1:
print "Running '%s' at '%s':" % (cmdl.cmd, cmdl.server,)
print "args: ", cmdl.args
print "cols: ", cmdl.cols
if cmdl.cols == None:
try:
cols = colsexpand(defcols[cmdl.cmd])
except Exception:
cmdl.all = 1
else:
cols = colsexpand(cmdl.cols)
for case in switch(cmdl.cmd):
if case('status'):
r = beyeget(cmdl.server, 'status')
if cmdl.raw >0:
pp.pprint(r)
else:
statprint(r)
break
if case('protocols'):
r = beyeget(cmdl.server, 'protocols/'+cmdl.args[0])
if cmdl.raw >0:
pp.pprint(r)
else:
protprint(r)
break
if case('routes'):
r = beyeget(cmdl.server, 'routes/'+cmdl.args[0]+'/'+cmdl.args[1])
if cmdl.raw >0:
pp.pprint(r)
else:
routesprint(r['routes'])
break
if case('route'):
if len(cmdl.args) == 1:
r = beyeget(cmdl.server, 'route/'+cmdl.args[0])
elif len(cmdl.args) == 2:
r = beyeget(cmdl.server, 'route/'+cmdl.args[0]+'/table/'+cmdl.args[1])
else:
logging.error("number of arguments incorrect: %s", cmdl.args)
sys.exit(1)
if cmdl.raw >0:
pp.pprint(r)
else:
routesprint(r['routes'])
break
if case('symbols'):
if len(cmdl.args) ==0:
r = beyeget(cmdl.server, 'symbols')
elif len(cmdl.args) == 1:
r = beyeget(cmdl.server, 'symbols'+'/'+cmdl.args[0])
else:
logging.error("argument error: %s", cmdl.args)
sys.exit(1)
if cmdl.raw >0:
pp.pprint(r)
else:
symsprint(r)
break
if case():
logging.error("Unknown command: %s", cmdl.cmd)
sys.exit(1)
if cmdl.verbosity >1:
print "%s done." % (cmdl.cmd,)