@@ -29,6 +29,7 @@ import (
29
29
"github.com/cea-hpc/sshproxy/pkg/utils"
30
30
31
31
"github.com/olekukonko/tablewriter"
32
+ "github.com/olekukonko/tablewriter/tw"
32
33
)
33
34
34
35
var (
@@ -76,12 +77,18 @@ func displayJSON(objs interface{}) {
76
77
}
77
78
}
78
79
79
- func displayTable (headers []string , rows [][]string ) {
80
+ func displayTable (headers []string , rows [][]string , footers [] string ) {
80
81
table := tablewriter .NewTable (os .Stdout ,
81
82
tablewriter .WithConfig (tablewriter.Config {
82
83
MaxWidth : 200 ,
84
+ Row : tw.CellConfig {
85
+ Formatting : tw.CellFormatting {
86
+ Alignment : tw .AlignRight ,
87
+ },
88
+ },
83
89
}))
84
90
table .Header (headers )
91
+ table .Footer (footers )
85
92
table .Bulk (rows )
86
93
table .Render ()
87
94
}
@@ -98,28 +105,42 @@ type aggConnection struct {
98
105
99
106
type aggregatedConnections []* aggConnection
100
107
101
- func (ac aggregatedConnections ) toRows (passthrough bool ) [][]string {
108
+ func (ac aggregatedConnections ) toRows (passthrough bool ) ( [][]string , map [ string ] string ) {
102
109
rows := make ([][]string , len (ac ))
110
+ totalConnections := 0
111
+ totalIn := 0
112
+ totalOut := 0
103
113
104
114
for i , c := range ac {
105
115
rows [i ] = []string {
106
116
c .User ,
107
117
c .Service ,
108
118
c .Dest ,
109
- strconv . Itoa ( c .N ),
119
+ fmt . Sprintf ( "%d" , c .N ),
110
120
c .Last .Format ("2006-01-02 15:04:05" ),
111
121
byteToHuman (c .BwIn , passthrough ),
112
122
byteToHuman (c .BwOut , passthrough ),
113
123
}
124
+ totalConnections += c .N
125
+ totalIn += c .BwIn
126
+ totalOut += c .BwOut
127
+ }
128
+ footer := map [string ]string {
129
+ "connections" : fmt .Sprintf ("%d" , totalConnections ),
130
+ "in" : byteToHuman (totalIn , passthrough ),
131
+ "out" : byteToHuman (totalOut , passthrough ),
114
132
}
115
133
116
- return rows
134
+ return rows , footer
117
135
}
118
136
119
137
type flatConnections []* utils.FlatConnection
120
138
121
- func (fc flatConnections ) getAllConnections (passthrough bool ) [][]string {
139
+ func (fc flatConnections ) getAllConnections (passthrough bool ) ( [][]string , map [ string ] string ) {
122
140
rows := make ([][]string , len (fc ))
141
+ totalConnections := 0
142
+ totalIn := 0
143
+ totalOut := 0
123
144
124
145
for i , c := range fc {
125
146
rows [i ] = []string {
@@ -131,9 +152,17 @@ func (fc flatConnections) getAllConnections(passthrough bool) [][]string {
131
152
byteToHuman (c .BwIn , passthrough ),
132
153
byteToHuman (c .BwOut , passthrough ),
133
154
}
155
+ totalConnections += 1
156
+ totalIn += c .BwIn
157
+ totalOut += c .BwOut
158
+ }
159
+ footer := map [string ]string {
160
+ "connections" : fmt .Sprintf ("%d" , totalConnections ),
161
+ "in" : byteToHuman (totalIn , passthrough ),
162
+ "out" : byteToHuman (totalOut , passthrough ),
134
163
}
135
164
136
- return rows
165
+ return rows , footer
137
166
}
138
167
139
168
func (fc flatConnections ) getAggregatedConnections () aggregatedConnections {
@@ -203,9 +232,9 @@ func (fc flatConnections) displayCSV(allFlag bool) {
203
232
var rows [][]string
204
233
205
234
if allFlag {
206
- rows = fc .getAllConnections (true )
235
+ rows , _ = fc .getAllConnections (true )
207
236
} else {
208
- rows = fc .getAggregatedConnections ().toRows (true )
237
+ rows , _ = fc .getAggregatedConnections ().toRows (true )
209
238
}
210
239
211
240
displayCSV (rows )
@@ -225,21 +254,25 @@ func (fc flatConnections) displayJSON(allFlag bool) {
225
254
226
255
func (fc flatConnections ) displayTable (allFlag bool ) {
227
256
var rows [][]string
257
+ var footer map [string ]string
228
258
229
259
if allFlag {
230
- rows = fc .getAllConnections (false )
260
+ rows , footer = fc .getAllConnections (false )
231
261
} else {
232
- rows = fc .getAggregatedConnections ().toRows (false )
262
+ rows , footer = fc .getAggregatedConnections ().toRows (false )
233
263
}
234
264
235
265
var headers []string
266
+ var footers []string
236
267
if allFlag {
237
268
headers = []string {"User" , "Service" , "From" , "Destination" , "Start time" , "Bw in" , "Bw out" }
269
+ footers = []string {"" , "" , "" , "Totals" , footer ["connections" ], footer ["in" ], footer ["out" ]}
238
270
} else {
239
271
headers = []string {"User" , "Service" , "Destination" , "# of conns" , "Last connection" , "Bw in" , "Bw out" }
272
+ footers = []string {"" , "" , "Totals" , footer ["connections" ], "" , footer ["in" ], footer ["out" ]}
240
273
}
241
274
242
- displayTable (headers , rows )
275
+ displayTable (headers , rows , footers )
243
276
}
244
277
245
278
func showConnections (configFile string , csvFlag bool , jsonFlag bool , allFlag bool ) {
@@ -271,8 +304,12 @@ type flatUserLight struct {
271
304
272
305
type flatUsers []* utils.FlatUser
273
306
274
- func (fu flatUsers ) getAllUsers (allFlag bool , passthrough bool ) [][]string {
307
+ func (fu flatUsers ) getAllUsers (allFlag bool , passthrough bool ) ( [][]string , map [ string ] string ) {
275
308
rows := make ([][]string , len (fu ))
309
+ totalConnections := 0
310
+ totalIn := 0
311
+ totalOut := 0
312
+
276
313
for i , v := range fu {
277
314
if allFlag {
278
315
rows [i ] = []string {
@@ -294,6 +331,9 @@ func (fu flatUsers) getAllUsers(allFlag bool, passthrough bool) [][]string {
294
331
byteToHuman (v .BwOut , passthrough ),
295
332
}
296
333
}
334
+ totalConnections += v .N
335
+ totalIn += v .BwIn
336
+ totalOut += v .BwOut
297
337
}
298
338
299
339
sort .Slice (rows , func (i , j int ) bool {
@@ -303,8 +343,13 @@ func (fu flatUsers) getAllUsers(allFlag bool, passthrough bool) [][]string {
303
343
return rows [i ][0 ] < rows [j ][0 ]
304
344
}
305
345
})
346
+ footer := map [string ]string {
347
+ "connections" : fmt .Sprintf ("%d" , totalConnections ),
348
+ "in" : byteToHuman (totalIn , passthrough ),
349
+ "out" : byteToHuman (totalOut , passthrough ),
350
+ }
306
351
307
- return rows
352
+ return rows , footer
308
353
}
309
354
310
355
func (fu flatUsers ) displayJSON (allFlag bool ) {
@@ -326,22 +371,25 @@ func (fu flatUsers) displayJSON(allFlag bool) {
326
371
}
327
372
328
373
func (fu flatUsers ) displayCSV (allFlag bool ) {
329
- rows := fu .getAllUsers (allFlag , true )
374
+ rows , _ := fu .getAllUsers (allFlag , true )
330
375
331
376
displayCSV (rows )
332
377
}
333
378
334
379
func (fu flatUsers ) displayTable (allFlag bool ) {
335
- rows := fu .getAllUsers (allFlag , false )
380
+ rows , footer := fu .getAllUsers (allFlag , false )
336
381
337
382
var headers []string
383
+ var footers []string
338
384
if allFlag {
339
385
headers = []string {"User" , "Service" , "Groups" , "# of conns" , "Bw in" , "Bw out" , "Persist to" , "Persist TTL" }
386
+ footers = []string {"" , "" , "Totals" , footer ["connections" ], footer ["in" ], footer ["out" ], "" , "" }
340
387
} else {
341
388
headers = []string {"User" , "Groups" , "# of conns" , "Bw in" , "Bw out" }
389
+ footers = []string {"" , "Totals" , footer ["connections" ], footer ["in" ], footer ["out" ]}
342
390
}
343
391
344
- displayTable (headers , rows )
392
+ displayTable (headers , rows , footers )
345
393
}
346
394
347
395
func showUsers (configFile string , csvFlag bool , jsonFlag bool , allFlag bool ) {
@@ -431,13 +479,14 @@ func (fg flatGroups) displayTable(allFlag bool) {
431
479
rows := fg .getAllGroups (allFlag , false )
432
480
433
481
var headers []string
482
+ var footers []string
434
483
if allFlag {
435
484
headers = []string {"Group" , "Service" , "Users" , "# of conns" , "Bw in" , "Bw out" }
436
485
} else {
437
486
headers = []string {"Group" , "Users" , "# of conns" , "Bw in" , "Bw out" }
438
487
}
439
488
440
- displayTable (headers , rows )
489
+ displayTable (headers , rows , footers )
441
490
}
442
491
443
492
func showGroups (configFile string , csvFlag bool , jsonFlag bool , allFlag bool ) {
@@ -474,6 +523,10 @@ func showHosts(configFile string, csvFlag bool, jsonFlag bool) {
474
523
}
475
524
476
525
rows := make ([][]string , len (hosts ))
526
+ totalConns := 0
527
+ totalIn := 0
528
+ totalOut := 0
529
+ totalPersist := 0
477
530
478
531
for i , h := range hosts {
479
532
rows [i ] = []string {
@@ -485,12 +538,34 @@ func showHosts(configFile string, csvFlag bool, jsonFlag bool) {
485
538
byteToHuman (h .BwOut , csvFlag ),
486
539
fmt .Sprintf ("%d" , h .HistoryN ),
487
540
}
541
+ totalConns += h .N
542
+ totalIn += h .BwIn
543
+ totalOut += h .BwOut
544
+ totalPersist += h .HistoryN
488
545
}
489
546
490
547
if csvFlag {
491
548
displayCSV (rows )
492
549
} else {
493
- displayTable ([]string {"Host" , "State" , "Last check" , "# of conns" , "Bw in" , "Bw out" , "# persist" }, rows )
550
+ headers := []string {
551
+ "Host" ,
552
+ "State" ,
553
+ "Last check" ,
554
+ "# of conns" ,
555
+ "Bw in" ,
556
+ "Bw out" ,
557
+ "# persist" ,
558
+ }
559
+ footers := []string {
560
+ "" ,
561
+ "" ,
562
+ "Totals" ,
563
+ fmt .Sprintf ("%d" , totalConns ),
564
+ byteToHuman (totalIn , false ),
565
+ byteToHuman (totalOut , false ),
566
+ fmt .Sprintf ("%d" , totalPersist ),
567
+ }
568
+ displayTable (headers , rows , footers )
494
569
}
495
570
}
496
571
0 commit comments