-
Notifications
You must be signed in to change notification settings - Fork 44
/
print_conn_table.py
executable file
·132 lines (113 loc) · 2.7 KB
/
print_conn_table.py
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
#!/usr/bin/env python
#
# The request prints N entries from the conn table for the filter specified
# mimicking the top connections table in the Sysdig Monitor UI
#
import sys
from sdcclient import SdcClient
#
# Parse arguments
#
if len(sys.argv) not in [2, 3]:
print(('usage: %s <sysdig-token> <hostname>' % sys.argv[0]))
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)
sdc_token = sys.argv[1]
if len(sys.argv) == 3:
hostname = sys.argv[2]
else:
hostname = None
#
# Instantiate the SDC client
#
sdclient = SdcClient(sdc_token)
#
# Prepare the metrics list.
#
metrics = [
{"id": "net.local.endpoint"},
{"id": "net.local.service"},
{"id": "net.remote.endpoint"},
{"id": "net.remote.service"},
{"id": "net.connection.count.total",
"aggregations": {
"time": "timeAvg",
"group": "sum"
}
},
{"id": "net.bytes.in",
"aggregations": {
"time": "timeAvg",
"group": "avg"
},
},
{"id": "net.bytes.out",
"aggregations": {
"time": "timeAvg",
"group": "avg"
}
},
{"id": "net.bytes.total",
"aggregations": {
"time": "timeAvg",
"group": "avg"
}
},
{"id": "net.request.count.in",
"aggregations": {
"time": "timeAvg",
"group": "avg"
}
},
{"id": "net.request.count.out",
"aggregations": {
"time": "timeAvg",
"group": "avg"
}
},
{"id": "net.request.count",
"aggregations": {
"time": "timeAvg",
"group": "avg"
}
}
]
#
# Prepare the filter
#
if hostname is not None:
flt = "host.hostName = '%s'" % hostname
else:
flt = ""
#
# Time window:
# - for "last X seconds": start is equal to -X, end is equal to 0
#
start = -7200
end = 0
#
# Fire the query.
#
page_size = 500
fetch_limit = 10000
cur = 0
row_format = "{:20.20}\t{:20.20}\t{:20.20}\t{:20.20}\t{:10}\t{:10}\t{:10}\t{:10}\t{:10}\t{:10}\t{:10}"
print((row_format.format("Source", "Source Process", "Destination", "Destination Process", "Count",
"Bytes In", "Bytes Out", "Bytes", "Req In", "Req Out", "Req")))
while cur < fetch_limit:
paging = {'from': cur, 'to': cur + page_size}
ok, res = sdclient.get_data(metrics,
start,
end,
0,
flt,
'host',
paging)
if not ok:
sys.exit(res)
data = res['data']
if len(data) == 0:
break
cur += len(data)
for line in data:
print((row_format.format(*line['d'])))