Skip to content

Commit 8dcea4e

Browse files
Work on sysdig captures
1 parent 99fa508 commit 8dcea4e

File tree

8 files changed

+226
-27
lines changed

8 files changed

+226
-27
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ script:
1515
- examples/get_data_simple.py XXX
1616
- examples/list_alerts.py XXX
1717
- examples/list_alert_notifications.py XXX
18-
- examples/resolve_alert_notifications.py XXX
18+
- examples/resolve_alert_notifications.py XXX 1
1919
- examples/list_dashboards.py XXX
2020
- examples/list_hosts.py XXX
2121
- examples/list_metrics.py XXX
@@ -25,6 +25,8 @@ script:
2525
- examples/print_data_retention_info.py XXX
2626
- examples/print_explore_grouping.py XXX
2727
- examples/print_user_info.py XXX
28+
- examples/list_sysdig_captures.py XXX
29+
- examples/create_sysdig_capture.py XXX ip-10-0-2-202.ec2.internal apicapture 10
2830
- echo "Testing pip version"
2931
- rm -rf sdcclient
3032
- pip install sdcclient
@@ -37,7 +39,7 @@ script:
3739
- examples/get_data_simple.py XXX
3840
- examples/list_alerts.py XXX
3941
- examples/list_alert_notifications.py XXX
40-
- examples/resolve_alert_notifications.py XXX
42+
- examples/resolve_alert_notifications.py XXX 1
4143
- examples/list_dashboards.py XXX
4244
- examples/list_hosts.py XXX
4345
- examples/list_metrics.py XXX
@@ -47,3 +49,5 @@ script:
4749
- examples/print_data_retention_info.py XXX
4850
- examples/print_explore_grouping.py XXX
4951
- examples/print_user_info.py XXX
52+
- examples/list_sysdig_captures.py XXX
53+
- examples/create_sysdig_capture.py XXX ip-10-0-2-202.ec2.internal apicapture 10

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ A dictionary containing the list of available sampling intervals.
164164

165165
#### `get_data_retention_info(self)`
166166
**Description**
167-
Return the list of data retention intervals, with beginning and end UTC time for each of them. Sysdig Cloud performs rollups of the data it stores. This means that data is stored at different time granularities depending on how far in time it is. This call can be used to know what precision you can expect before you make a call to get_data().
167+
Return the list of data retention intervals, with beginning and end UTC time for each of them. Sysdig Cloud performs rollups of the data it stores. This means that data is stored at different time granularities depending on how far in time it is. This call can be used to know what precision you can expect before you make a call to `get_data()`.
168168
**Success Return Value**
169169
A dictionary containing the list of available sampling intervals.
170170
**Example**
@@ -175,8 +175,8 @@ A dictionary containing the list of available sampling intervals.
175175
Returns the list of Sysdig Cloud events.
176176
**Arguments**
177177
- **name**: filter events by name.
178-
- **from_ts**: filter events created after `from_ts`.
179-
- **to_ts**: filter events created before `to_ts`.
178+
- **from_ts**: filter events by start time. Timestamp format is in UTC (seconds).
179+
- **to_ts**: filter events by end time. Timestamp format is in UTC (seconds).
180180
- **tags**: filter events by tags. Can be, for example `tag1 = 'value1'`.
181181

182182
**Success Return Value**
@@ -208,6 +208,32 @@ An integer number.
208208
**Example**
209209
[examples/print_user_info.py](examples/print_user_info.py).
210210

211+
#### `get_notifications(self, from_ts, to_ts, state=None, resolved=None)`
212+
**Description**
213+
Returns the list of Sysdig Cloud alert notifications.
214+
**Arguments**
215+
- **from_ts**: filter events by start time. Timestamp format is in UTC (seconds).
216+
- **to_ts**: filter events by start time. Timestamp format is in UTC (seconds).
217+
- **state**: filter events by alert state. Supported values are `OK` and `ACTIVE`.
218+
- **resolved**: filter events by resolution status. Supported values are `True` and `False.
219+
220+
**Success Return Value**
221+
A dictionary containing the list of notifications.
222+
**Example**
223+
[examples/list_alert_notifications.py](examples/list_alert_notifications.py).
224+
225+
#### `update_notification_resolution(self, notification, resolved)`
226+
**Description**
227+
Updates the resolution status of an alert notification.
228+
**Arguments**
229+
- **notification**: notification object as returned by `get_notifications()`.
230+
- **resolved**: new resolution status. Supported values are `True` and `False.
231+
232+
**Success Return Value**
233+
The updated notification.
234+
**Example**
235+
[examples/resolve_alert_notifications.py](examples/resolve_alert_notifications.py).
236+
211237
#### `get_user_info(self)`
212238
**Description**
213239
Get details about the current user.

examples/create_sysdig_capture.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
#
3+
# Creates a sysdig capture, waits for termination and prints the download URL.
4+
#
5+
6+
import os
7+
import sys
8+
import time
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 (5, 6):
16+
print 'usage: %s <sysdig-token> hostname capture_name duration [filter]' % 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+
hostname = sys.argv[2]
22+
capture_name = sys.argv[3]
23+
duration = sys.argv[4]
24+
capture_filter = ''
25+
26+
if len(sys.argv) == 6:
27+
capture_filter = sys.argv[5]
28+
29+
#
30+
# Instantiate the SDC client
31+
#
32+
sdclient = SdcClient(sdc_token)
33+
34+
res = sdclient.create_sysdig_capture(hostname, capture_name, int(duration), capture_filter)
35+
36+
#
37+
# Show the list of metrics
38+
#
39+
if res[0]:
40+
capture = res[1]
41+
else:
42+
print res[1]
43+
sys.exit(1)
44+
45+
while True:
46+
res = sdclient.poll_sysdig_capture(capture)
47+
if res[0]:
48+
capture = res[1]
49+
else:
50+
print res[1]
51+
sys.exit(1)
52+
53+
print 'Capture is in state ' + capture['status']
54+
if capture['status'] in ('requested', 'capturing', 'uploading'):
55+
pass
56+
elif capture['status'] in ('error', 'uploadingError'):
57+
sys.exit(1)
58+
elif capture['status'] in ('done', 'uploaded'):
59+
print 'Download at: ' + sdclient.url + capture['downloadURL']
60+
sys.exit(0)
61+
62+
time.sleep(1)

examples/list_alert_notifications.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@
3131

3232
print res[1]
3333
if not res[0]:
34-
sys.exit(1)
34+
sys.exit(1)
3535

3636
#
37-
# Get the notifications in the active state
37+
# Get the notifications in the last day and active state
3838
#
3939
res = sdclient.get_notifications(from_ts=int(time.time()-86400), to_ts=int(time.time()), state='ACTIVE')
4040

4141
print res[1]
4242
if not res[0]:
43-
sys.exit(1)
43+
sys.exit(1)
4444

4545
#
46-
# Get the notifications in the active state
46+
# Get the notifications in the last day and active state
4747
#
4848
res = sdclient.get_notifications(from_ts=int(time.time()-86400), to_ts=int(time.time()), state='OK')
4949

5050
print res[1]
5151
if not res[0]:
52-
sys.exit(1)
52+
sys.exit(1)
5353

5454
#
55-
# Get the resolved notifications
55+
# Get the notifications in the last day and resolved state
5656
#
5757
res = sdclient.get_notifications(from_ts=int(time.time()-86400), to_ts=int(time.time()), resolved=True)
5858

5959
print res[1]
6060
if not res[0]:
61-
sys.exit(1)
61+
sys.exit(1)

examples/list_events.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
99
from sdcclient import SdcClient
1010

11+
def print_events(data):
12+
for event in data['events']:
13+
print 'time: %d, name: %s, description: %s, severity: %d' % (event['timestamp'], event['name'], event['description'], event['severity'])
14+
1115
#
1216
# Parse arguments
1317
#
@@ -28,33 +32,41 @@
2832
#
2933
res = sdclient.get_events()
3034

31-
print res[1]
32-
if not res[0]:
33-
sys.exit(1)
35+
if res[0]:
36+
print_events(res[1])
37+
else:
38+
print res[1]
39+
sys.exit(1)
3440

3541
#
3642
# Get the events that match a period in time
3743
#
3844
res = sdclient.get_events(from_ts=1460365211, to_ts=1460465211)
3945

40-
print res[1]
41-
if not res[0]:
42-
sys.exit(1)
46+
if res[0]:
47+
print_events(res[1])
48+
else:
49+
print res[1]
50+
sys.exit(1)
4351

4452
#
4553
# Get the events that match a name
4654
#
4755
res = sdclient.get_events(name='test event')
4856

49-
print res[1]
50-
if not res[0]:
51-
sys.exit(1)
57+
if res[0]:
58+
print_events(res[1])
59+
else:
60+
print res[1]
61+
sys.exit(1)
5262

5363
#
5464
# Get the events that match a tag/value pair
5565
#
5666
res = sdclient.get_events(tags="tag1 = 'value1'")
5767

58-
print res[1]
59-
if not res[0]:
60-
sys.exit(1)
68+
if res[0]:
69+
print_events(res[1])
70+
else:
71+
print res[1]
72+
sys.exit(1)

examples/list_sysdig_captures.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
#
3+
# Print the list of sysdig captures.
4+
#
5+
6+
import os
7+
import sys
8+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
9+
from sdcclient import SdcClient
10+
11+
#
12+
# Parse arguments
13+
#
14+
if len(sys.argv) != 2:
15+
print 'usage: %s <sysdig-token>' % sys.argv[0]
16+
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
17+
sys.exit(1)
18+
19+
sdc_token = sys.argv[1]
20+
21+
#
22+
# Instantiate the SDC client
23+
#
24+
sdclient = SdcClient(sdc_token)
25+
26+
#
27+
# Fire the request.
28+
#
29+
res = sdclient.get_sysdig_captures()
30+
31+
#
32+
# Show the list of metrics
33+
#
34+
if res[0]:
35+
data = res[1]
36+
else:
37+
print res[1]
38+
sys.exit(1)
39+
40+
for capture in data:
41+
print "Folder %s, Name %s, Host: %s, Size: %d, Status: %s" % \
42+
(capture['folder'], capture['name'], capture['agent']['hostName'], capture['size'], capture['status'])

examples/resolve_alert_notifications.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
#
1313
# Parse arguments
1414
#
15-
if len(sys.argv) != 2:
16-
print 'usage: %s <sysdig-token>' % sys.argv[0]
15+
if len(sys.argv) != 3:
16+
print 'usage: %s <sysdig-token> <num-days-to-resolve>' % sys.argv[0]
1717
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
1818
sys.exit(1)
1919

2020
sdc_token = sys.argv[1]
21+
num_days_to_resolve = sys.argv[2]
2122

2223
#
2324
# Instantiate the SDC client
@@ -27,7 +28,8 @@
2728
#
2829
# Get the unresolved notifications in the last day
2930
#
30-
res = sdclient.get_notifications(from_ts=int(time.time()-86400), to_ts=int(time.time()), resolved=False)
31+
res = sdclient.get_notifications(from_ts=int(time.time() - num_days_to_resolve * 86400),
32+
to_ts=int(time.time()), resolved=False)
3133

3234
if not res[0]:
3335
print res[1]

sdcclient/_client.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ def get_user_info(self):
4949
self.userinfo = r.json()
5050
return [True, self.userinfo]
5151

52+
def get_connected_agents(self):
53+
r = requests.get(self.url + '/api/agents/connected', headers=self.hdrs)
54+
if not self.__checkResponse(r):
55+
return [False, self.lasterr]
56+
data = r.json()
57+
return [True, data['agents']]
58+
5259
def get_n_connected_agents(self):
5360
r = requests.get(self.url + '/api/agents/connected', headers=self.hdrs)
5461
if not self.__checkResponse(r):
@@ -589,3 +596,47 @@ def get_metrics(self):
589596
if not self.__checkResponse(r):
590597
return [False, self.lasterr]
591598
return [True, r.json()]
599+
600+
def get_sysdig_captures(self):
601+
r = requests.get(self.url + '/api/sysdig', headers=self.hdrs)
602+
if not self.__checkResponse(r):
603+
return [False, self.lasterr]
604+
return [True, r.json()['dumps']]
605+
606+
def poll_sysdig_capture(self, capture):
607+
if 'id' not in capture:
608+
return [False, 'Invalid capture format']
609+
610+
r = requests.get(self.url + '/api/sysdig/' + str(capture['id']), headers=self.hdrs)
611+
if not self.__checkResponse(r):
612+
return [False, self.lasterr]
613+
return [True, r.json()['dump']]
614+
615+
def create_sysdig_capture(self, hostname, capture_name, duration, capture_filter='', folder='/'):
616+
res = self.get_connected_agents()
617+
if not res[0]:
618+
return res
619+
620+
capture_agent = None
621+
622+
for agent in res[1]:
623+
if hostname == agent['hostName']:
624+
capture_agent = agent
625+
break
626+
627+
if capture_agent is None:
628+
return [False, hostname + ' not found']
629+
630+
data = {
631+
'agent': capture_agent,
632+
'name' : capture_name,
633+
'duration': duration,
634+
'folder': folder,
635+
'filters': capture_filter,
636+
'bucketName': ''
637+
}
638+
639+
r = requests.post(self.url + '/api/sysdig', headers=self.hdrs, data=json.dumps(data))
640+
if not self.__checkResponse(r):
641+
return [False, self.lasterr]
642+
return [True, r.json()['dump']]

0 commit comments

Comments
 (0)