Skip to content

Clean up examples #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions examples/add_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdSecureClient


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


#
# Parse arguments
#
Expand All @@ -30,15 +32,13 @@ def usage():
#
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')

res = sdclient.add_policy(policy_json)
ok, res = sdclient.add_policy(policy_json)

#
# Return the result
#
if res[0]:
print json.dumps(res[1], indent=2)
if ok:
print(json.dumps(res, indent=2))
else:
print res[1]
print(res)
sys.exit(1)


51 changes: 27 additions & 24 deletions examples/create_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdcClient


#
# Parse arguments
#
def usage():
print 'usage: %s [-a|--alert <name>] <sysdig-token>' % sys.argv[0]
print '-a|--alert: Set name of alert to create'
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s [-a|--alert <name>] <sysdig-token>' % sys.argv[0])
print('-a|--alert: Set name of alert to create')
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)


try:
opts, args = getopt.getopt(sys.argv[1:],"a:",["alert="])
opts, args = getopt.getopt(sys.argv[1:], "a:", ["alert="])
except getopt.GetoptError:
usage()

Expand All @@ -44,35 +46,36 @@ def usage():
#
# Find notification channels (you need IDs to create an alert).
#
notify_channels = [ {'type': 'SLACK', 'channel': '#python-sdc-test-alert'},
{'type': 'EMAIL', 'emailRecipients': ['[email protected]', '[email protected]']},
{'type': 'SNS', 'snsTopicARNs': ['arn:aws:sns:us-east-1:273107874544:alarms-stg']}
]
notify_channels = [{'type': 'SLACK', 'channel': '#python-sdc-test-alert'},
{'type': 'EMAIL', 'emailRecipients': ['[email protected]', '[email protected]']},
{'type': 'SNS', 'snsTopicARNs': ['arn:aws:sns:us-east-1:273107874544:alarms-stg']}
]

res = sdclient.get_notification_ids(notify_channels)
if not res[0]:
print "Could not get IDs and hence not creating the alert: " + res[1]
ok, res = sdclient.get_notification_ids(notify_channels)
if not ok:
print("Could not get IDs and hence not creating the alert: " + res)
sys.exit(-1)

notification_channel_ids = res[1]
notification_channel_ids = res

#
# Create the alert.
#
res = sdclient.create_alert(alert_name, # Alert name.
'this alert was automatically created using the python Sysdig Cloud library', # Alert description.
6, # Syslog-encoded severity. 6 means 'info'.
60, # The alert will fire if the condition is met for at least 60 seconds.
'avg(cpu.used.percent) > 80', # The condition.
['host.mac', 'proc.name'], # Segmentation. We want to check this metric for every process on every machine.
'ANY', # in case there is more than one tomcat process, this alert will fire when a single one of them crosses the 80% threshold.
'proc.name = "tomcat"', # Filter. We want to receive a notification only if the name of the process meeting the condition is 'tomcat'.
notification_channel_ids,
False) # This alert will be disabled when it's created.
ok, res = sdclient.create_alert(
alert_name, # Alert name.
'this alert was automatically created using the python Sysdig Cloud library', # Alert description.
6, # Syslog-encoded severity. 6 means 'info'.
60, # The alert will fire if the condition is met for at least 60 seconds.
'avg(cpu.used.percent) > 80', # The condition.
['host.mac', 'proc.name'], # Segmentation. We want to check this metric for every process on every machine.
'ANY', # in case there is more than one tomcat process, this alert will fire when a single one of them crosses the 80% threshold.
'proc.name = "tomcat"', # Filter. We want to receive a notification only if the name of the process meeting the condition is 'tomcat'.
notification_channel_ids,
False) # This alert will be disabled when it's created.

#
# Validate a print the results.
#
print res[1]
if not res[0]:
print(res)
if not ok:
sys.exit(1)
30 changes: 16 additions & 14 deletions examples/create_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdcClient


#
# Parse arguments
#
def usage():
print 'usage: %s [-d|--dashboard <name>] <sysdig-token>' % sys.argv[0]
print '-d|--dashboard: Set name of dashboard to create'
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s [-d|--dashboard <name>] <sysdig-token>' % sys.argv[0])
print('-d|--dashboard: Set name of dashboard to create')
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)


try:
opts, args = getopt.getopt(sys.argv[1:],"d:",["dashboard="])
opts, args = getopt.getopt(sys.argv[1:], "d:", ["dashboard="])
except getopt.GetoptError:
usage()

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

print 'Creating dashboard from view'
res = sdclient.create_dashboard_from_view(dashboardName, viewName, dashboardFilter)
print('Creating dashboard from view')
ok, res = sdclient.create_dashboard_from_view(dashboardName, viewName, dashboardFilter)
#
# Check the result
#
if res[0]:
print 'Dashboard created successfully'
if ok:
print('Dashboard created successfully')
else:
print res[1]
print(res)
sys.exit(1)

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

print 'Creating dashboard from dashboard'
res = sdclient.create_dashboard_from_dashboard(dashboardCopy, dashboardName, dashboardFilter)
print('Creating dashboard from dashboard')
ok, res = sdclient.create_dashboard_from_dashboard(dashboardCopy, dashboardName, dashboardFilter)

#
# Check the result
#
if res[0]:
print 'Dashboard copied successfully'
if ok:
print('Dashboard copied successfully')
else:
print res[1]
print(res)
sys.exit(1)
16 changes: 8 additions & 8 deletions examples/create_default_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdSecureClient


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


#
# Parse arguments
#
Expand All @@ -30,15 +32,13 @@ def usage():
#
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')

res = sdclient.create_default_policies()
ok, res = sdclient.create_default_policies()

#
# Return the result
#
if res[0]:
print json.dumps(res[1], indent=2)
if ok:
print(json.dumps(res, indent=2))
else:
print res[1]
print(res)
sys.exit(1)


24 changes: 12 additions & 12 deletions examples/create_sysdig_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# Parse arguments
#
if len(sys.argv) not in (5, 6):
print 'usage: %s <sysdig-token> hostname capture_name duration [filter]' % sys.argv[0]
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
print('usage: %s <sysdig-token> hostname capture_name duration [filter]' % sys.argv[0])
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)

sdc_token = sys.argv[1]
Expand All @@ -31,32 +31,32 @@
#
sdclient = SdcClient(sdc_token)

res = sdclient.create_sysdig_capture(hostname, capture_name, int(duration), capture_filter)
ok, res = sdclient.create_sysdig_capture(hostname, capture_name, int(duration), capture_filter)

#
# Show the list of metrics
#
if res[0]:
capture = res[1]['dump']
if ok:
capture = res['dump']
else:
print res[1]
print(res)
sys.exit(1)

while True:
res = sdclient.poll_sysdig_capture(capture)
if res[0]:
capture = res[1]['dump']
ok, res = sdclient.poll_sysdig_capture(capture)
if ok:
capture = res['dump']
else:
print res[1]
print(res)
sys.exit(1)

print 'Capture is in state ' + capture['status']
print('Capture is in state ' + capture['status'])
if capture['status'] in ('requested', 'capturing', 'uploading'):
pass
elif capture['status'] in ('error', 'uploadingError'):
sys.exit(1)
elif capture['status'] in ('done', 'uploaded'):
print 'Download at: ' + sdclient.url + capture['downloadURL']
print('Download at: ' + sdclient.url + capture['downloadURL'])
sys.exit(0)

time.sleep(1)
Loading