Skip to content

Commit ee61f33

Browse files
Merge pull request #40 from draios/conn-table
Add a netstat-like example
2 parents 17afab3 + eea562a commit ee61f33

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

examples/print_conn_table.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python
2+
#
3+
# The request prints N entries from the conn table for the filter specified
4+
# mimicking the top connections table in the Sysdig Monitor UI
5+
#
6+
7+
import os
8+
import sys
9+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10+
from sdcclient import SdcClient
11+
12+
#
13+
# Parse arguments
14+
#
15+
if len(sys.argv) not in [2, 3]:
16+
print 'usage: %s <sysdig-token> <hostname>' % sys.argv[0]
17+
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
18+
sys.exit(1)
19+
20+
sdc_token = sys.argv[1]
21+
22+
if len(sys.argv) == 3:
23+
hostname = sys.argv[2]
24+
else:
25+
hostname = None
26+
27+
#
28+
# Instantiate the SDC client
29+
#
30+
sdclient = SdcClient(sdc_token)
31+
32+
#
33+
# Prepare the metrics list.
34+
#
35+
metrics = [
36+
{"id": "net.local.endpoint"},
37+
{"id": "net.local.service"},
38+
{"id": "net.remote.endpoint"},
39+
{"id": "net.remote.service"},
40+
{"id": "net.connection.count.total",
41+
"aggregations": {
42+
"time": "timeAvg",
43+
"group": "sum"
44+
}
45+
},
46+
{"id": "net.bytes.in",
47+
"aggregations": {
48+
"time": "timeAvg",
49+
"group": "avg"
50+
},
51+
},
52+
{"id": "net.bytes.out",
53+
"aggregations": {
54+
"time": "timeAvg",
55+
"group": "avg"
56+
}
57+
},
58+
{"id": "net.bytes.total",
59+
"aggregations": {
60+
"time": "timeAvg",
61+
"group": "avg"
62+
}
63+
},
64+
{"id": "net.request.count.in",
65+
"aggregations": {
66+
"time": "timeAvg",
67+
"group": "avg"
68+
}
69+
},
70+
{"id": "net.request.count.out",
71+
"aggregations": {
72+
"time": "timeAvg",
73+
"group": "avg"
74+
}
75+
},
76+
{"id": "net.request.count",
77+
"aggregations": {
78+
"time": "timeAvg",
79+
"group": "avg"
80+
}
81+
}
82+
]
83+
84+
#
85+
# Prepare the filter
86+
#
87+
88+
if hostname is not None:
89+
flt = "host.hostName = '%s'" % hostname
90+
else:
91+
flt = ""
92+
93+
#
94+
# Time window:
95+
# - for "last X seconds": start is equal to -X, end is equal to 0
96+
#
97+
start = -600
98+
end = 0
99+
100+
#
101+
# Fire the query.
102+
#
103+
page_size = 500
104+
fetch_limit = 10000
105+
106+
cur = 0
107+
108+
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}"
109+
110+
print row_format.format("Source", "Source Process", "Destination", "Destination Process", "Count",
111+
"Bytes In", "Bytes Out", "Bytes", "Req In", "Req Out", "Req")
112+
113+
while cur < fetch_limit:
114+
paging = {'from': cur, 'to': cur + page_size }
115+
res = sdclient.get_data(metrics,
116+
-600,
117+
0,
118+
600,
119+
flt,
120+
'host',
121+
paging)
122+
123+
if not res[0]:
124+
sys.exit(res[1])
125+
126+
data = res[1]['data']
127+
128+
if len(data) == 0:
129+
break
130+
131+
cur += len(data)
132+
for line in data:
133+
print row_format.format(*line['d'])

0 commit comments

Comments
 (0)