Skip to content

Commit c4c691f

Browse files
omerazrDavide Schiera
authored andcommitted
Clean up examples (#78)
* Let's use the pattern 'ok, res = sdclient.operation()' * Add python3 support * Clean up some pep8 errors
1 parent d17198d commit c4c691f

40 files changed

+674
-643
lines changed

examples/add_policy.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1010
from sdcclient import SdSecureClient
1111

12+
1213
def usage():
13-
print 'usage: %s <sysdig-token>' % sys.argv[0]
14-
print 'Reads policy json from standard input'
15-
print 'You can find your token at https://secure.sysdig.com/#/settings/user'
14+
print('usage: %s <sysdig-token>' % sys.argv[0])
15+
print('Reads policy json from standard input')
16+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
1617
sys.exit(1)
1718

19+
1820
#
1921
# Parse arguments
2022
#
@@ -30,15 +32,13 @@ def usage():
3032
#
3133
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')
3234

33-
res = sdclient.add_policy(policy_json)
35+
ok, res = sdclient.add_policy(policy_json)
3436

3537
#
3638
# Return the result
3739
#
38-
if res[0]:
39-
print json.dumps(res[1], indent=2)
40+
if ok:
41+
print(json.dumps(res, indent=2))
4042
else:
41-
print res[1]
43+
print(res)
4244
sys.exit(1)
43-
44-

examples/create_alert.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1313
from sdcclient import SdcClient
1414

15+
1516
#
1617
# Parse arguments
1718
#
1819
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')
2223
sys.exit(1)
2324

25+
2426
try:
25-
opts, args = getopt.getopt(sys.argv[1:],"a:",["alert="])
27+
opts, args = getopt.getopt(sys.argv[1:], "a:", ["alert="])
2628
except 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)

examples/create_dashboard.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1313
from sdcclient import SdcClient
1414

15+
1516
#
1617
# Parse arguments
1718
#
1819
def usage():
19-
print 'usage: %s [-d|--dashboard <name>] <sysdig-token>' % sys.argv[0]
20-
print '-d|--dashboard: Set name of dashboard to create'
21-
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
20+
print('usage: %s [-d|--dashboard <name>] <sysdig-token>' % sys.argv[0])
21+
print('-d|--dashboard: Set name of dashboard to create')
22+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
2223
sys.exit(1)
2324

25+
2426
try:
25-
opts, args = getopt.getopt(sys.argv[1:],"d:",["dashboard="])
27+
opts, args = getopt.getopt(sys.argv[1:], "d:", ["dashboard="])
2628
except getopt.GetoptError:
2729
usage()
2830

@@ -55,15 +57,15 @@ def usage():
5557
# agent tags by using "agent.tag.*" metadata
5658
dashboardFilter = "kubernetes.namespace.name = prod and proc.name = cassandra"
5759

58-
print 'Creating dashboard from view'
59-
res = sdclient.create_dashboard_from_view(dashboardName, viewName, dashboardFilter)
60+
print('Creating dashboard from view')
61+
ok, res = sdclient.create_dashboard_from_view(dashboardName, viewName, dashboardFilter)
6062
#
6163
# Check the result
6264
#
63-
if res[0]:
64-
print 'Dashboard created successfully'
65+
if ok:
66+
print('Dashboard created successfully')
6567
else:
66-
print res[1]
68+
print(res)
6769
sys.exit(1)
6870

6971
#
@@ -76,14 +78,14 @@ def usage():
7678
# Filter to apply to the new dashboard. Same as above.
7779
dashboardFilter = "kubernetes.namespace.name = dev and proc.name = cassandra"
7880

79-
print 'Creating dashboard from dashboard'
80-
res = sdclient.create_dashboard_from_dashboard(dashboardCopy, dashboardName, dashboardFilter)
81+
print('Creating dashboard from dashboard')
82+
ok, res = sdclient.create_dashboard_from_dashboard(dashboardCopy, dashboardName, dashboardFilter)
8183

8284
#
8385
# Check the result
8486
#
85-
if res[0]:
86-
print 'Dashboard copied successfully'
87+
if ok:
88+
print('Dashboard copied successfully')
8789
else:
88-
print res[1]
90+
print(res)
8991
sys.exit(1)

examples/create_default_policies.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1313
from sdcclient import SdSecureClient
1414

15+
1516
def usage():
16-
print 'usage: %s <sysdig-token>' % sys.argv[0]
17-
print 'You can find your token at https://secure.sysdig.com/#/settings/user'
17+
print('usage: %s <sysdig-token>' % sys.argv[0])
18+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
1819
sys.exit(1)
1920

21+
2022
#
2123
# Parse arguments
2224
#
@@ -30,15 +32,13 @@ def usage():
3032
#
3133
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')
3234

33-
res = sdclient.create_default_policies()
35+
ok, res = sdclient.create_default_policies()
3436

3537
#
3638
# Return the result
3739
#
38-
if res[0]:
39-
print json.dumps(res[1], indent=2)
40+
if ok:
41+
print(json.dumps(res, indent=2))
4042
else:
41-
print res[1]
43+
print(res)
4244
sys.exit(1)
43-
44-

examples/create_sysdig_capture.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# Parse arguments
1414
#
1515
if len(sys.argv) not in (5, 6):
16-
print 'usage: %s <sysdig-token> hostname capture_name duration [filter]' % sys.argv[0]
17-
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
16+
print('usage: %s <sysdig-token> hostname capture_name duration [filter]' % sys.argv[0])
17+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
1818
sys.exit(1)
1919

2020
sdc_token = sys.argv[1]
@@ -31,32 +31,32 @@
3131
#
3232
sdclient = SdcClient(sdc_token)
3333

34-
res = sdclient.create_sysdig_capture(hostname, capture_name, int(duration), capture_filter)
34+
ok, res = sdclient.create_sysdig_capture(hostname, capture_name, int(duration), capture_filter)
3535

3636
#
3737
# Show the list of metrics
3838
#
39-
if res[0]:
40-
capture = res[1]['dump']
39+
if ok:
40+
capture = res['dump']
4141
else:
42-
print res[1]
42+
print(res)
4343
sys.exit(1)
4444

4545
while True:
46-
res = sdclient.poll_sysdig_capture(capture)
47-
if res[0]:
48-
capture = res[1]['dump']
46+
ok, res = sdclient.poll_sysdig_capture(capture)
47+
if ok:
48+
capture = res['dump']
4949
else:
50-
print res[1]
50+
print(res)
5151
sys.exit(1)
5252

53-
print 'Capture is in state ' + capture['status']
53+
print('Capture is in state ' + capture['status'])
5454
if capture['status'] in ('requested', 'capturing', 'uploading'):
5555
pass
5656
elif capture['status'] in ('error', 'uploadingError'):
5757
sys.exit(1)
5858
elif capture['status'] in ('done', 'uploaded'):
59-
print 'Download at: ' + sdclient.url + capture['downloadURL']
59+
print('Download at: ' + sdclient.url + capture['downloadURL'])
6060
sys.exit(0)
6161

6262
time.sleep(1)

0 commit comments

Comments
 (0)