12
12
sys .path .insert (0 , os .path .join (os .path .dirname (os .path .realpath (sys .argv [0 ])), '..' ))
13
13
from sdcclient import SdcClient
14
14
15
+
15
16
#
16
17
# Parse arguments
17
18
#
18
19
def 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' )
22
23
sys .exit (1 )
23
24
25
+
24
26
try :
25
- opts , args = getopt .getopt (sys .argv [1 :],"a:" ,["alert=" ])
27
+ opts , args = getopt .getopt (sys .argv [1 :], "a:" , ["alert=" ])
26
28
except getopt .GetoptError :
27
29
usage ()
28
30
@@ -44,35 +46,36 @@ def usage():
44
46
#
45
47
# Find notification channels (you need IDs to create an alert).
46
48
#
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
+ ]
51
53
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 )
55
57
sys .exit (- 1 )
56
58
57
- notification_channel_ids = res [ 1 ]
59
+ notification_channel_ids = res
58
60
59
61
#
60
62
# Create the alert.
61
63
#
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.
72
75
73
76
#
74
77
# Validate a print the results.
75
78
#
76
- print res [ 1 ]
77
- if not res [ 0 ] :
79
+ print ( res )
80
+ if not ok :
78
81
sys .exit (1 )
0 commit comments