Skip to content

Commit 09f5a31

Browse files
committed
merge master
2 parents de10af4 + 775426a commit 09f5a31

File tree

6 files changed

+140
-99
lines changed

6 files changed

+140
-99
lines changed

.travis.yml

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,34 @@ script:
2828
- examples/print_explore_grouping.py XXX
2929
- examples/print_user_info.py XXX
3030
- examples/list_sysdig_captures.py XXX
31-
- examples/create_sysdig_capture.py XXX ip-10-0-2-202.ec2.internal apicapture 10
31+
- examples/create_sysdig_capture.py XXX ip-10-0-1-110.ec2.internal apicapture 10
32+
- examples/notification_channels.py XXX
3233
- echo "Testing pip version"
3334
- rm -rf sdcclient
3435
- pip install sdcclient
35-
- examples/create_alert.py XXX
36-
- examples/delete_alert.py XXX
37-
- examples/dashboard.py XXX
38-
- examples/create_dashboard.py XXX
39-
- examples/delete_dashboard.py XXX
40-
- examples/get_data_advanced.py XXX ip-10-0-2-180.ec2.internal
41-
- examples/get_data_datasource.py XXX
42-
- examples/get_data_simple.py XXX
43-
- examples/list_alerts.py XXX
44-
- examples/list_alert_notifications.py XXX
45-
- examples/resolve_alert_notifications.py XXX 1
46-
- examples/list_dashboards.py XXX
47-
- examples/list_hosts.py XXX
48-
- examples/list_metrics.py XXX
49-
- examples/post_event.py XXX "test event name" -d "test event description"
50-
- examples/post_event_simple.py XXX "test event name" "test event description"
51-
- examples/list_events.py XXX
52-
- examples/delete_event.py XXX
53-
- examples/print_data_retention_info.py XXX
54-
- examples/print_explore_grouping.py XXX
55-
- examples/print_user_info.py XXX
56-
- examples/list_sysdig_captures.py XXX
57-
- examples/create_sysdig_capture.py XXX ip-10-0-2-202.ec2.internal apicapture 10
36+
# - examples/create_alert.py XXX
37+
# - examples/delete_alert.py XXX
38+
# - examples/dashboard.py XXX
39+
# - examples/create_dashboard.py XXX
40+
# - examples/delete_dashboard.py XXX
41+
# - examples/get_data_advanced.py XXX ip-10-0-2-180.ec2.internal
42+
# - examples/get_data_datasource.py XXX
43+
# - examples/get_data_simple.py XXX
44+
# - examples/list_alerts.py XXX
45+
# - examples/list_alert_notifications.py XXX
46+
# - examples/resolve_alert_notifications.py XXX 1
47+
# - examples/list_dashboards.py XXX
48+
# - examples/list_hosts.py XXX
49+
# - examples/list_metrics.py XXX
50+
# - examples/post_event.py XXX "test event name" -d "test event description"
51+
# - examples/post_event_simple.py XXX "test event name" "test event description"
52+
# - examples/list_events.py XXX
53+
# - examples/delete_event.py XXX
54+
# - examples/print_data_retention_info.py XXX
55+
# - examples/print_explore_grouping.py XXX
56+
# - examples/print_user_info.py XXX
57+
# - examples/list_sysdig_captures.py XXX
58+
# - examples/create_sysdig_capture.py XXX ip-10-0-2-202.ec2.internal apicapture 10
5859
notifications:
5960
slack:
6061
secure: GJ0H2wAaW67t3+x+cCjOVLw/b+YAZjH9rebAfluCFJklJS9u+V6/qqIyiNDAkMPIcgilFyhiyOQCZYXapgthQ1ieFbZZo//mrRtuGo2wuxCAwcOx2mXAZpZ4I5b3+Q/znVqSuqkwFRid1d138z4TW7sYSIburouDX3QUNoUOy+g7VJxCFQCcqlN8LYxGJHQYdOZa9zIGCtKrOtZ/B8C1TLgXmDMwAAVNO2WzL4GiBTLCGuMsQWMTLw2Qmv1ayZPztmeDWo1C9oa7HIH8Bg2YVjssR87el28X+EqEO533mgYjPmW2/hii30WVFOUE5hMdvKeQLBvy5N3/OCch1np0RQBd8eYEtaPv38rc5L2wAnUq9G5Zzr252z7vnwSLi6lap9jWU8tOerSTEPU+jG05PnuCnufVDXVNPyiPsH6BDP4qxHmLjooNpxfe63Df7NNyUi2I3QoroLj/UzI7zZVQjJEqsTrr5BbsH4z6NTGY91+ZqobBn62+hV3ESAam0ivQgC7s2AKko0qkKyIUGjj7ozU8ebo1UpagNvKC/J9szMqtdXJgKtG8BeonyLMeN6MEEyEvcMJbB4dCcfet+1Sb9AZWnGvYVdajhVLb1HE6OrbzZyhC3KqCe06J9O5BCY9ncy+l16i7MyIfcKTibHQlxPU+Id/VijD97JSRXxnd2i4=

examples/create_alert.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
# Find notification channels (you need IDs to create an alert).
3131
#
3232
notify_channels = [ {'type': 'SLACK', 'channel': 'sysdig-demo2-alerts'},
33-
{'type': 'EMAIL', 'emailRecipients': [u'kini@sysdig.com']},
34-
{'type': 'SNS', 'snsTopicARNs': [u'arn:aws:sns:us-east-1:273107874544:alarms-stg']}
33+
{'type': 'EMAIL', 'emailRecipients': ['gianluca@sysdig.com']},
34+
{'type': 'SNS', 'snsTopicARNs': ['arn:aws:sns:us-east-1:273107874544:alarms-stg']}
3535
]
36+
3637
res = sdclient.get_notification_ids(notify_channels)
3738
if not res[0]:
38-
print "Could not get IDs and hence not creating the alert"
39+
print "Could not get IDs and hence not creating the alert: " + res[1]
3940
sys.exit(-1)
4041

4142
notification_channel_ids = res[1]

examples/delete_event.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,3 @@
4444
if not res[0]:
4545
print res[1]
4646
sys.exit(1)
47-
else:
48-
sys.exit(0)

examples/list_hosts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
# come as a single sample.
3838
#
3939
res = sdclient.get_data(metrics, # metrics list
40-
-600, # cover the last 300 seconds...
40+
-600, # cover the last 600 seconds...
4141
0, # ... ending now...
42-
600) # ... with just one 300s sample
42+
600) # ... with just one 600s sample
4343

4444
#
4545
# Show the results!

examples/notification_channels.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script shows how to manipulate the notification channel list for alerts
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+
# Create an email notification channel
28+
#
29+
res = sdclient.create_email_notification_channel('Api Channel', ['[email protected]', '[email protected]', '[email protected]'])
30+
if not res[0]:
31+
print res[1]
32+
sys.exit(1)
33+
34+
#
35+
# The notification channel will contain the id, that can be used when creating alerts
36+
#
37+
channel = res[1]['notificationChannel']
38+
print channel
39+
40+
#
41+
# Notification channels can also be programmatically deleted
42+
#
43+
res = sdclient.delete_notification_channel(channel)
44+
if not res[0]:
45+
print res[1]
46+
sys.exit(1)

sdcclient/_client.py

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import copy
55

66
class SdcClient:
7-
userinfo = None
8-
n_connected_agents = None
97
lasterr = None
108

119
def __init__(self, token="", sdc_url='https://app.sysdigcloud.com'):
@@ -44,12 +42,10 @@ def __checkResponse(self, res):
4442

4543

4644
def get_user_info(self):
47-
if self.userinfo is None:
48-
res = requests.get(self.url + '/api/user/me', headers=self.hdrs)
49-
if not self.__checkResponse(res):
50-
return [False, self.lasterr]
51-
self.userinfo = res.json()
52-
return [True, self.userinfo]
45+
res = requests.get(self.url + '/api/user/me', headers=self.hdrs)
46+
if not self.__checkResponse(res):
47+
return [False, self.lasterr]
48+
return [True, res.json()]
5349

5450
def get_connected_agents(self):
5551
res = requests.get(self.url + '/api/agents/connected', headers=self.hdrs)
@@ -93,7 +89,7 @@ def get_notifications(self, from_ts, to_ts, state=None, resolved=None):
9389

9490
def update_notification_resolution(self, notification, resolved):
9591
if 'id' not in notification:
96-
return [False, "Invalid notification format"]
92+
return [False, 'Invalid notification format']
9793

9894
notification['resolved'] = resolved
9995
data = {'notification': notification}
@@ -103,35 +99,42 @@ def update_notification_resolution(self, notification, resolved):
10399
return [False, self.lasterr]
104100
return [True, res.json()]
105101

106-
def get_notification_channel_ids(self, channels):
107-
res = requests.get(self.url + '/api/notificationChannels', headers=self.hdrs)
108-
ids = []
109-
if not self.__checkResponse(res):
110-
return [False, self.lasterr]
111-
# Should try and improve this M * N lookup
112-
for ch in res.json()["notificationChannels"]:
102+
def get_notification_ids(self, channels):
103+
res = requests.get(self.url + '/api/notificationChannels', headers=self.hdrs)
104+
105+
if not self.__checkResponse(res):
106+
return [False, self.lasterr]
107+
108+
# Should try and improve this M * N lookup
109+
ids = []
113110
for c in channels:
114-
if c['type'] == ch['type']:
115-
print c, ch
116-
if c['type'] == 'SNS':
117-
opt = ch['options']
118-
if set(opt['snsTopicARNs']) == set(c['snsTopicARNs']):
119-
ids.append(ch['id'])
120-
elif c['type'] == 'EMAIL':
121-
opt = ch['options']
122-
if set(c['emailRecipients']) == set(opt['emailRecipients']):
123-
ids.append(ch['id'])
124-
elif c['type'] == 'PAGER_DUTY':
125-
opt = ch['options']
126-
if opt['account'] == c['account'] and opt['serviceName'] == c['serviceName']:
127-
ids.append(ch['id'])
128-
elif c['type'] == 'SLACK':
129-
opt = ch['options']
130-
if opt['channel'] == c['channel']:
131-
ids.append(ch['id'])
132-
print ids
133-
return [True, ids]
134-
111+
found = False
112+
for ch in res.json()["notificationChannels"]:
113+
if c['type'] == ch['type']:
114+
if c['type'] == 'SNS':
115+
opt = ch['options']
116+
if set(opt['snsTopicARNs']) == set(c['snsTopicARNs']):
117+
found = True
118+
ids.append(ch['id'])
119+
elif c['type'] == 'EMAIL':
120+
opt = ch['options']
121+
if set(c['emailRecipients']) == set(opt['emailRecipients']):
122+
found = True
123+
ids.append(ch['id'])
124+
elif c['type'] == 'PAGER_DUTY':
125+
opt = ch['options']
126+
if opt['account'] == c['account'] and opt['serviceName'] == c['serviceName']:
127+
found = True
128+
ids.append(ch['id'])
129+
elif c['type'] == 'SLACK':
130+
opt = ch['options']
131+
if 'channel' in opt and opt['channel'] == c['channel']:
132+
found = True
133+
ids.append(ch['id'])
134+
if not found:
135+
return [False, "Channel not found: " + str(c)]
136+
137+
return [True, ids]
135138

136139
def create_alert(self, name, description, severity, for_atleast_s, condition, segmentby=[],
137140
segment_condition='ANY', user_filter='', notify=None, enabled=True, annotations={}):
@@ -187,50 +190,39 @@ def create_alert(self, name, description, severity, for_atleast_s, condition, se
187190

188191
def delete_alert(self, alert):
189192
if 'id' not in alert:
190-
return [False, "Invalid alert format"]
193+
return [False, 'Invalid alert format']
191194

192195
res = requests.delete(self.url + '/api/alerts/' + str(alert['id']), headers=self.hdrs)
193196
if not self.__checkResponse(res):
194197
return [False, self.lasterr]
195198

196199
return [True, None]
197200

198-
def get_notification_settings(self):
199-
res = requests.get(self.url + '/api/settings/notifications', headers=self.hdrs)
200-
if not self.__checkResponse(res):
201-
return [False, self.lasterr]
202-
return [True, res.json()]
201+
def create_email_notification_channel(self, channel_name, email_recipients):
202+
channel_json = {
203+
'notificationChannel' : {
204+
'type' : 'EMAIL',
205+
'name' : channel_name,
206+
'enabled' : True,
207+
'options' : {
208+
'emailRecipients' : email_recipients
209+
}
210+
}
211+
}
203212

204-
def set_notification_settings(self, settings):
205-
res = requests.put(self.url + '/api/settings/notifications', headers=self.hdrs,
206-
data=json.dumps(settings))
213+
res = requests.post(self.url + '/api/notificationChannels', headers=self.hdrs, data=json.dumps(channel_json))
207214
if not self.__checkResponse(res):
208215
return [False, self.lasterr]
209216
return [True, res.json()]
210217

211-
def add_email_notification_recipient(self, email):
212-
#
213-
# Retirieve the user's notification settings
214-
#
215-
res = requests.get(self.url + '/api/settings/notifications', headers=self.hdrs)
218+
def delete_notification_channel(self, channel):
219+
if 'id' not in channel:
220+
return [False, "Invalid channel format"]
221+
222+
res = requests.delete(self.url + '/api/notificationChannels/' + str(channel['id']), headers=self.hdrs)
216223
if not self.__checkResponse(res):
217224
return [False, self.lasterr]
218-
j = res.json()
219-
220-
#
221-
# Enable email notifications
222-
#
223-
j['userNotification']['email']['enabled'] = True
224-
225-
#
226-
# Add the given recipient
227-
#
228-
if email not in j['userNotification']['email']['recipients']:
229-
j['userNotification']['email']['recipients'].append(email)
230-
else:
231-
return [False, 'notification target ' + email + ' already present']
232-
233-
return self.set_notification_settings(j)
225+
return [True, None]
234226

235227
def get_explore_grouping_hierarchy(self):
236228
res = requests.get(self.url + '/api/groupConfigurations', headers=self.hdrs)
@@ -803,7 +795,7 @@ def delete_event(self, event):
803795
return [True, None]
804796

805797
def get_data(self, metrics, start_ts, end_ts=0, sampling_s=0,
806-
filter='', datasource_type='host'):
798+
filter='', datasource_type='host', paging=None):
807799
reqbody = {
808800
'metrics': metrics,
809801
'dataSourceType': datasource_type,
@@ -820,6 +812,9 @@ def get_data(self, metrics, start_ts, end_ts=0, sampling_s=0,
820812
if filter != '':
821813
reqbody['filter'] = filter
822814

815+
if paging is not None:
816+
reqbody['paging'] = paging
817+
823818
if sampling_s != 0:
824819
reqbody['sampling'] = sampling_s
825820

0 commit comments

Comments
 (0)