Skip to content

Commit e917a6c

Browse files
committed
Supporting new API for notification channels.
1 parent 40d057b commit e917a6c

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

examples/create_alert.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
#
2727
sdclient = SdcClient(sdc_token)
2828

29+
#
30+
# Find notification channels (you need IDs to create an alert).
31+
#
32+
notify_channels = [ {'type': 'SLACK', 'channel': 'sysdig-demo2-alerts'},
33+
{'type': 'EMAIL', 'emailRecipients': [u'[email protected]']},
34+
{'type': 'SNS', 'snsTopicARNs': [u'arn:aws:sns:us-east-1:273107874544:alarms-stg']}
35+
]
36+
res = sdclient.get_notification_ids(notify_channels)
37+
if not res[0]:
38+
print "Could not get IDs and hence not creating the alert"
39+
sys.exit(-1)
40+
41+
notification_channel_ids = res[1]
42+
2943
#
3044
# Create the alert.
3145
#
@@ -37,7 +51,7 @@
3751
['host.mac', 'proc.name'], # Segmentation. We want to check this metric for every process on every machine.
3852
'ANY', # in case there is more than one tomcat process, this alert will fire when a single one of them crosses the 80% threshold.
3953
'proc.name = "tomcat"', # Filter. We want to receive a notification only if the name of the process meeting the condition is 'tomcat'.
40-
['EMAIL'], # Notification target. We want an email to be sent. Alerts email recipients can be defined here: https://app.sysdigcloud.com/#/settings/notifications
54+
notification_channel_ids,
4155
False) # This alert will be disabled when it's created.
4256

4357
#

sdcclient/_client.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,39 @@ def update_notification_resolution(self, notification, resolved):
102102
return [False, self.lasterr]
103103
return [True, res.json()]
104104

105+
def get_notification_ids(self, channels):
106+
res = requests.get(self.url + '/api/notificationChannels', headers=self.hdrs)
107+
108+
if not self.__checkResponse(res):
109+
return [False, self.lasterr]
110+
111+
# Should try and improve this M * N lookup
112+
ids = []
113+
for ch in res.json()["notificationChannels"]:
114+
for c in channels:
115+
if c['type'] == ch['type']:
116+
print c, ch
117+
if c['type'] == 'SNS':
118+
opt = ch['options']
119+
if set(opt['snsTopicARNs']) == set(c['snsTopicARNs']):
120+
ids.append(ch['id'])
121+
elif c['type'] == 'EMAIL':
122+
opt = ch['options']
123+
if set(c['emailRecipients']) == set(opt['emailRecipients']):
124+
ids.append(ch['id'])
125+
elif c['type'] == 'PAGER_DUTY':
126+
opt = ch['options']
127+
if opt['account'] == c['account'] and opt['serviceName'] == c['serviceName']:
128+
ids.append(ch['id'])
129+
elif c['type'] == 'SLACK':
130+
opt = ch['options']
131+
if opt['channel'] == c['channel']:
132+
ids.append(ch['id'])
133+
134+
return [True, ids]
135+
105136
def create_alert(self, name, description, severity, for_atleast_s, condition, segmentby=[],
106-
segment_condition='ANY', user_filter='', notify='', enabled=True, annotations={}):
137+
segment_condition='ANY', user_filter='', notify=None, enabled=True, annotations={}):
107138
#
108139
# Get the list of alerts from the server
109140
#
@@ -143,8 +174,8 @@ def create_alert(self, name, description, severity, for_atleast_s, condition, se
143174
if annotations != None and annotations != {}:
144175
alert_json['alert']['annotations'] = annotations
145176

146-
if notify != None and notify != []:
147-
alert_json['alert']['notify'] = notify
177+
if notify != None:
178+
alert_json['alert']['notificationChannelIds'] = notify
148179

149180
#
150181
# Create the new alert

0 commit comments

Comments
 (0)