@@ -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,23 @@ 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
+ },
89
+ Footer : tw.CellConfig {
90
+ Formatting : tw.CellFormatting {
91
+ MergeMode : tw .MergeHorizontal ,
92
+ },
93
+ },
83
94
}))
84
95
table .Header (headers )
96
+ table .Footer (footers )
85
97
table .Bulk (rows )
86
98
table .Render ()
87
99
}
@@ -98,28 +110,42 @@ type aggConnection struct {
98
110
99
111
type aggregatedConnections []* aggConnection
100
112
101
- func (ac aggregatedConnections ) toRows (passthrough bool ) [][]string {
113
+ func (ac aggregatedConnections ) toRows (passthrough bool ) ( [][]string , map [ string ] string ) {
102
114
rows := make ([][]string , len (ac ))
115
+ totalConnections := 0
116
+ totalIn := 0
117
+ totalOut := 0
103
118
104
119
for i , c := range ac {
105
120
rows [i ] = []string {
106
121
c .User ,
107
122
c .Service ,
108
123
c .Dest ,
109
- strconv . Itoa ( c .N ),
124
+ fmt . Sprintf ( "%d" , c .N ),
110
125
c .Last .Format ("2006-01-02 15:04:05" ),
111
126
byteToHuman (c .BwIn , passthrough ),
112
127
byteToHuman (c .BwOut , passthrough ),
113
128
}
129
+ totalConnections += c .N
130
+ totalIn += c .BwIn
131
+ totalOut += c .BwOut
132
+ }
133
+ footer := map [string ]string {
134
+ "connections" : fmt .Sprintf ("%d" , totalConnections ),
135
+ "in" : byteToHuman (totalIn , passthrough ),
136
+ "out" : byteToHuman (totalOut , passthrough ),
114
137
}
115
138
116
- return rows
139
+ return rows , footer
117
140
}
118
141
119
142
type flatConnections []* utils.FlatConnection
120
143
121
- func (fc flatConnections ) getAllConnections (passthrough bool ) [][]string {
144
+ func (fc flatConnections ) getAllConnections (passthrough bool ) ( [][]string , map [ string ] string ) {
122
145
rows := make ([][]string , len (fc ))
146
+ totalConnections := 0
147
+ totalIn := 0
148
+ totalOut := 0
123
149
124
150
for i , c := range fc {
125
151
rows [i ] = []string {
@@ -131,9 +157,17 @@ func (fc flatConnections) getAllConnections(passthrough bool) [][]string {
131
157
byteToHuman (c .BwIn , passthrough ),
132
158
byteToHuman (c .BwOut , passthrough ),
133
159
}
160
+ totalConnections += 1
161
+ totalIn += c .BwIn
162
+ totalOut += c .BwOut
163
+ }
164
+ footer := map [string ]string {
165
+ "connections" : fmt .Sprintf ("%d" , totalConnections ),
166
+ "in" : byteToHuman (totalIn , passthrough ),
167
+ "out" : byteToHuman (totalOut , passthrough ),
134
168
}
135
169
136
- return rows
170
+ return rows , footer
137
171
}
138
172
139
173
func (fc flatConnections ) getAggregatedConnections () aggregatedConnections {
@@ -203,9 +237,9 @@ func (fc flatConnections) displayCSV(allFlag bool) {
203
237
var rows [][]string
204
238
205
239
if allFlag {
206
- rows = fc .getAllConnections (true )
240
+ rows , _ = fc .getAllConnections (true )
207
241
} else {
208
- rows = fc .getAggregatedConnections ().toRows (true )
242
+ rows , _ = fc .getAggregatedConnections ().toRows (true )
209
243
}
210
244
211
245
displayCSV (rows )
@@ -225,21 +259,25 @@ func (fc flatConnections) displayJSON(allFlag bool) {
225
259
226
260
func (fc flatConnections ) displayTable (allFlag bool ) {
227
261
var rows [][]string
262
+ var footer map [string ]string
228
263
229
264
if allFlag {
230
- rows = fc .getAllConnections (false )
265
+ rows , footer = fc .getAllConnections (false )
231
266
} else {
232
- rows = fc .getAggregatedConnections ().toRows (false )
267
+ rows , footer = fc .getAggregatedConnections ().toRows (false )
233
268
}
234
269
235
270
var headers []string
271
+ var footers []string
236
272
if allFlag {
237
273
headers = []string {"User" , "Service" , "From" , "Destination" , "Start time" , "Bw in" , "Bw out" }
274
+ footers = []string {"Totals" , "Totals" , "Totals" , "Totals" , footer ["connections" ], footer ["in" ], footer ["out" ]}
238
275
} else {
239
276
headers = []string {"User" , "Service" , "Destination" , "# of conns" , "Last connection" , "Bw in" , "Bw out" }
277
+ footers = []string {"Totals" , "Totals" , "Totals" , footer ["connections" ], "" , footer ["in" ], footer ["out" ]}
240
278
}
241
279
242
- displayTable (headers , rows )
280
+ displayTable (headers , rows , footers )
243
281
}
244
282
245
283
func showConnections (configFile string , csvFlag bool , jsonFlag bool , allFlag bool ) {
@@ -271,8 +309,12 @@ type flatUserLight struct {
271
309
272
310
type flatUsers []* utils.FlatUser
273
311
274
- func (fu flatUsers ) getAllUsers (allFlag bool , passthrough bool ) [][]string {
312
+ func (fu flatUsers ) getAllUsers (allFlag bool , passthrough bool ) ( [][]string , map [ string ] string ) {
275
313
rows := make ([][]string , len (fu ))
314
+ totalConnections := 0
315
+ totalIn := 0
316
+ totalOut := 0
317
+
276
318
for i , v := range fu {
277
319
if allFlag {
278
320
rows [i ] = []string {
@@ -294,6 +336,9 @@ func (fu flatUsers) getAllUsers(allFlag bool, passthrough bool) [][]string {
294
336
byteToHuman (v .BwOut , passthrough ),
295
337
}
296
338
}
339
+ totalConnections += v .N
340
+ totalIn += v .BwIn
341
+ totalOut += v .BwOut
297
342
}
298
343
299
344
sort .Slice (rows , func (i , j int ) bool {
@@ -303,8 +348,13 @@ func (fu flatUsers) getAllUsers(allFlag bool, passthrough bool) [][]string {
303
348
return rows [i ][0 ] < rows [j ][0 ]
304
349
}
305
350
})
351
+ footer := map [string ]string {
352
+ "connections" : fmt .Sprintf ("%d" , totalConnections ),
353
+ "in" : byteToHuman (totalIn , passthrough ),
354
+ "out" : byteToHuman (totalOut , passthrough ),
355
+ }
306
356
307
- return rows
357
+ return rows , footer
308
358
}
309
359
310
360
func (fu flatUsers ) displayJSON (allFlag bool ) {
@@ -326,22 +376,25 @@ func (fu flatUsers) displayJSON(allFlag bool) {
326
376
}
327
377
328
378
func (fu flatUsers ) displayCSV (allFlag bool ) {
329
- rows := fu .getAllUsers (allFlag , true )
379
+ rows , _ := fu .getAllUsers (allFlag , true )
330
380
331
381
displayCSV (rows )
332
382
}
333
383
334
384
func (fu flatUsers ) displayTable (allFlag bool ) {
335
- rows := fu .getAllUsers (allFlag , false )
385
+ rows , footer := fu .getAllUsers (allFlag , false )
336
386
337
387
var headers []string
388
+ var footers []string
338
389
if allFlag {
339
390
headers = []string {"User" , "Service" , "Groups" , "# of conns" , "Bw in" , "Bw out" , "Persist to" , "Persist TTL" }
391
+ footers = []string {"Totals" , "Totals" , "Totals" , footer ["connections" ], footer ["in" ], footer ["out" ], "" , "" }
340
392
} else {
341
393
headers = []string {"User" , "Groups" , "# of conns" , "Bw in" , "Bw out" }
394
+ footers = []string {"Totals" , "Totals" , footer ["connections" ], footer ["in" ], footer ["out" ]}
342
395
}
343
396
344
- displayTable (headers , rows )
397
+ displayTable (headers , rows , footers )
345
398
}
346
399
347
400
func showUsers (configFile string , csvFlag bool , jsonFlag bool , allFlag bool ) {
@@ -431,13 +484,14 @@ func (fg flatGroups) displayTable(allFlag bool) {
431
484
rows := fg .getAllGroups (allFlag , false )
432
485
433
486
var headers []string
487
+ var footers []string
434
488
if allFlag {
435
489
headers = []string {"Group" , "Service" , "Users" , "# of conns" , "Bw in" , "Bw out" }
436
490
} else {
437
491
headers = []string {"Group" , "Users" , "# of conns" , "Bw in" , "Bw out" }
438
492
}
439
493
440
- displayTable (headers , rows )
494
+ displayTable (headers , rows , footers )
441
495
}
442
496
443
497
func showGroups (configFile string , csvFlag bool , jsonFlag bool , allFlag bool ) {
@@ -474,6 +528,10 @@ func showHosts(configFile string, csvFlag bool, jsonFlag bool) {
474
528
}
475
529
476
530
rows := make ([][]string , len (hosts ))
531
+ totalConns := 0
532
+ totalIn := 0
533
+ totalOut := 0
534
+ totalPersist := 0
477
535
478
536
for i , h := range hosts {
479
537
rows [i ] = []string {
@@ -485,12 +543,16 @@ func showHosts(configFile string, csvFlag bool, jsonFlag bool) {
485
543
byteToHuman (h .BwOut , csvFlag ),
486
544
fmt .Sprintf ("%d" , h .HistoryN ),
487
545
}
546
+ totalConns += h .N
547
+ totalIn += h .BwIn
548
+ totalOut += h .BwOut
549
+ totalPersist += h .HistoryN
488
550
}
489
551
490
552
if csvFlag {
491
553
displayCSV (rows )
492
554
} else {
493
- displayTable ([]string {"Host" , "State" , "Last check" , "# of conns" , "Bw in" , "Bw out" , "# persist" }, rows )
555
+ displayTable ([]string {"Host" , "State" , "Last check" , "# of conns" , "Bw in" , "Bw out" , "# persist" }, rows , [] string { "Totals" , "Totals" , "Totals" , fmt . Sprintf ( "%d" , totalConns ), byteToHuman ( totalIn , false ), byteToHuman ( totalOut , false ), fmt . Sprintf ( "%d" , totalPersist )} )
494
556
}
495
557
}
496
558
0 commit comments