1212sys .path .insert (0 , os .path .join (os .path .dirname (os .path .realpath (sys .argv [0 ])), '..' ))
1313from sdcclient import SdcClient
1414
15+
1516#
1617# Parse arguments
1718#
1819def usage ():
19- print 'usage: %s [-a|--alert <name>] <sysdig-token>' % sys .argv [0 ]
20- print '-a|--alert: Set name of alert to create'
21- print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
20+ print ( 'usage: %s [-a|--alert <name>] <sysdig-token>' % sys .argv [0 ])
21+ print ( '-a|--alert: Set name of alert to create' )
22+ print ( 'You can find your token at https://app.sysdigcloud.com/#/settings/user' )
2223 sys .exit (1 )
2324
25+
2426try :
25- opts , args = getopt .getopt (sys .argv [1 :],"a:" ,["alert=" ])
27+ opts , args = getopt .getopt (sys .argv [1 :], "a:" , ["alert=" ])
2628except getopt .GetoptError :
2729 usage ()
2830
@@ -44,35 +46,36 @@ def usage():
4446#
4547# Find notification channels (you need IDs to create an alert).
4648#
47- notify_channels = [ {'type' : 'SLACK' , 'channel' : '#python-sdc-test-alert' },
48- {
'type' :
'EMAIL' ,
'emailRecipients' : [
'[email protected] ' ,
'[email protected] ' ]},
49- {'type' : 'SNS' , 'snsTopicARNs' : ['arn:aws:sns:us-east-1:273107874544:alarms-stg' ]}
50- ]
49+ notify_channels = [{'type' : 'SLACK' , 'channel' : '#python-sdc-test-alert' },
50+ {
'type' :
'EMAIL' ,
'emailRecipients' : [
'[email protected] ' ,
'[email protected] ' ]},
51+ {'type' : 'SNS' , 'snsTopicARNs' : ['arn:aws:sns:us-east-1:273107874544:alarms-stg' ]}
52+ ]
5153
52- res = sdclient .get_notification_ids (notify_channels )
53- if not res [ 0 ] :
54- print "Could not get IDs and hence not creating the alert: " + res [ 1 ]
54+ ok , res = sdclient .get_notification_ids (notify_channels )
55+ if not ok :
56+ print ( "Could not get IDs and hence not creating the alert: " + res )
5557 sys .exit (- 1 )
5658
57- notification_channel_ids = res [ 1 ]
59+ notification_channel_ids = res
5860
5961#
6062# Create the alert.
6163#
62- res = sdclient .create_alert (alert_name , # Alert name.
63- 'this alert was automatically created using the python Sysdig Cloud library' , # Alert description.
64- 6 , # Syslog-encoded severity. 6 means 'info'.
65- 60 , # The alert will fire if the condition is met for at least 60 seconds.
66- 'avg(cpu.used.percent) > 80' , # The condition.
67- ['host.mac' , 'proc.name' ], # Segmentation. We want to check this metric for every process on every machine.
68- 'ANY' , # in case there is more than one tomcat process, this alert will fire when a single one of them crosses the 80% threshold.
69- 'proc.name = "tomcat"' , # Filter. We want to receive a notification only if the name of the process meeting the condition is 'tomcat'.
70- notification_channel_ids ,
71- False ) # This alert will be disabled when it's created.
64+ ok , res = sdclient .create_alert (
65+ alert_name , # Alert name.
66+ 'this alert was automatically created using the python Sysdig Cloud library' , # Alert description.
67+ 6 , # Syslog-encoded severity. 6 means 'info'.
68+ 60 , # The alert will fire if the condition is met for at least 60 seconds.
69+ 'avg(cpu.used.percent) > 80' , # The condition.
70+ ['host.mac' , 'proc.name' ], # Segmentation. We want to check this metric for every process on every machine.
71+ 'ANY' , # in case there is more than one tomcat process, this alert will fire when a single one of them crosses the 80% threshold.
72+ 'proc.name = "tomcat"' , # Filter. We want to receive a notification only if the name of the process meeting the condition is 'tomcat'.
73+ notification_channel_ids ,
74+ False ) # This alert will be disabled when it's created.
7275
7376#
7477# Validate a print the results.
7578#
76- print res [ 1 ]
77- if not res [ 0 ] :
79+ print ( res )
80+ if not ok :
7881 sys .exit (1 )
0 commit comments