Skip to content

Commit eb07cff

Browse files
KranzSysdigDavide Schiera
authored and
Davide Schiera
committed
Created a script to enable/disable a list of alerts (#72)
1 parent 09e513e commit eb07cff

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

examples/flip_alerts_enabled.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 getopt
9+
import os
10+
import sys
11+
import json
12+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
13+
from sdcclient import SdcClient
14+
15+
#
16+
# Parse arguments
17+
#
18+
def usage():
19+
print ('usage: %s [-a|--alert <name>] <sysdig-token>' % sys.argv[0])
20+
print ('-a|--alert: Comma seperated list of alerts')
21+
print ('You can find your token at https://app.sysdigcloud.com/#/settings/user')
22+
sys.exit(1)
23+
24+
try:
25+
opts, args = getopt.getopt(sys.argv[1:],"a:",["alert="])
26+
except getopt.GetoptError:
27+
usage()
28+
29+
alert_list = "95% CPU"
30+
for opt, arg in opts:
31+
if opt in ("-a", "--alert"):
32+
alert_list = arg
33+
34+
if len(args) != 1:
35+
usage()
36+
37+
sdc_token = args[0]
38+
39+
#
40+
# Instantiate the SDC client
41+
#
42+
sdclient = SdcClient(sdc_token)
43+
44+
res = sdclient.get_alerts()
45+
if not res[0]:
46+
print (res[1])
47+
sys.exit(1)
48+
49+
alert_found = False
50+
for alert in res[1]['alerts']:
51+
if alert['name'] in alert_list:
52+
alert_found = True
53+
print ("Updating \'" + alert['name'] + "\'. Enabled status before change:")
54+
print (alert['enabled'])
55+
if alert['enabled'] == True:
56+
alert['enabled'] = False
57+
else:
58+
alert['enabled'] = True
59+
res_update = sdclient.update_alert(alert)
60+
61+
if not res_update[0]:
62+
print (res_update[1])
63+
sys.exit(1)
64+
65+
# Validate and print the results
66+
print ('Alert status after modification:')
67+
print (alert['enabled'])
68+
print (' ')
69+
70+
if not alert_found:
71+
print ('Alert to be updated not found')
72+
sys.exit(1)

0 commit comments

Comments
 (0)