Skip to content

Commit 9dbff91

Browse files
authored
Merge pull request #29 from draios/update-alert
Add update_alert()
2 parents 5fc7aea + 98b8c42 commit 9dbff91

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ python:
77
script:
88
- echo "Testing source version"
99
- examples/create_alert.py XXX
10+
- examples/update_alert.py XXX
1011
- examples/delete_alert.py XXX
1112
- examples/dashboard.py XXX
1213
- examples/create_dashboard.py XXX

examples/update_alert.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script shows how to use the update_alert() call to modify the
4+
# details of an existing alert.
5+
#
6+
#
7+
8+
import os
9+
import sys
10+
import json
11+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
12+
from sdcclient import SdcClient
13+
14+
#
15+
# Parse arguments
16+
#
17+
if len(sys.argv) != 2:
18+
print 'usage: %s <sysdig-token>' % sys.argv[0]
19+
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
20+
sys.exit(1)
21+
22+
sdc_token = sys.argv[1]
23+
24+
#
25+
# Instantiate the SDC client
26+
#
27+
sdclient = SdcClient(sdc_token)
28+
29+
res = sdclient.get_alerts()
30+
if not res[0]:
31+
print res[1]
32+
sys.exit(1)
33+
34+
alert_found = False
35+
for alert in res[1]['alerts']:
36+
if alert['name'] == "tomcat cpu > 80% on any host":
37+
alert_found = True
38+
print 'Updating alert. Configuration before changing timespan, description, and notification channels:'
39+
print json.dumps(alert, sort_keys=True, indent=4)
40+
if 'notificationChannelIds' in alert:
41+
alert['notificationChannelIds'] = alert['notificationChannelIds'][0:-1]
42+
update_txt = ' (changed by update_alert)'
43+
if alert['description'][-len(update_txt):] != update_txt:
44+
alert['description'] = alert['description'] + update_txt
45+
alert['timespan'] = alert['timespan'] * 2 # Note: Expressed in seconds * 1000000
46+
res_update = sdclient.update_alert(alert)
47+
48+
if not res_update[0]:
49+
print res_update[1]
50+
sys.exit(1)
51+
52+
# Validate and print the results
53+
print '\nAlert after modification:'
54+
print json.dumps(res_update[1], sort_keys=True, indent=4)
55+
56+
if not alert_found:
57+
print 'Alert to be updated not found'
58+
sys.exit(1)

sdcclient/_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,28 @@ def create_alert(self, name, description, severity, for_atleast_s, condition, se
304304
return [False, self.lasterr]
305305
return [True, res.json()]
306306

307+
def update_alert(self, alert):
308+
'''**Description**
309+
Update a modified threshold-based alert.
310+
311+
**Arguments**
312+
- **alert**: one modified alert object of the same format as those in the list returned by :func:`~SdcClient.get_alerts`.
313+
314+
**Success Return Value**
315+
The updated alert.
316+
317+
**Example**
318+
`examples/update_alert.py <https://github.com/draios/python-sdc-client/blob/master/examples/update_alert.py>`_
319+
'''
320+
if 'id' not in alert:
321+
return [False, "Invalid alert format"]
322+
323+
res = requests.put(self.url + '/api/alerts/' + str(alert['id']), headers=self.hdrs, data=json.dumps({ "alert": alert}), verify=self.ssl_verify)
324+
if not self.__checkResponse(res):
325+
return [False, self.lasterr]
326+
327+
return [True, res.json()]
328+
307329
def delete_alert(self, alert):
308330
'''**Description**
309331
Deletes an alert.

0 commit comments

Comments
 (0)