Skip to content

Created a script to enable/disable a list of alerts #72

New issue

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

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

Already on GitHub? # to your account

Merged
merged 3 commits into from
Jan 25, 2019
Merged
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
72 changes: 72 additions & 0 deletions examples/flip_alerts_enabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python
#
# This script shows how to use the update_alert() call to modify the
# details of an existing alert.
#
#

import getopt
import os
import sys
import json
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: Comma seperated list of alerts')
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="])
except getopt.GetoptError:
usage()

alert_list = "95% CPU"
for opt, arg in opts:
if opt in ("-a", "--alert"):
alert_list = arg

if len(args) != 1:
usage()

sdc_token = args[0]

#
# Instantiate the SDC client
#
sdclient = SdcClient(sdc_token)

res = sdclient.get_alerts()
if not res[0]:
print (res[1])
sys.exit(1)

alert_found = False
for alert in res[1]['alerts']:
if alert['name'] in alert_list:
alert_found = True
print ("Updating \'" + alert['name'] + "\'. Enabled status before change:")
print (alert['enabled'])
if alert['enabled'] == True:
alert['enabled'] = False
else:
alert['enabled'] = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this is because copy-pasting old code. But this could be improved by a single alert['enabled'] = not alert['enabled']

@davideschiera Should we step by step improve this repo code when adding new code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one-liner seems reasonable (especially in this context, as long as you can "read" the line and it sounds clear enough, then it's all good).

In terms of refactoring, I think it's a good idea. I actually missed this one when I reviewed the PR last week, sorry about that.

Feel free to file PRs to clean up the code base, we can also discuss offline about the major points to solve. Thanks!!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davideschiera If not in a hurry, you can let another pair of 👀 to take a look at the PR 😄 By now, we can live without this one-liner.

Don't have the desired time to attend this repo, but 👍 to offline discussion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Thanks for bringing it up!

res_update = sdclient.update_alert(alert)

if not res_update[0]:
print (res_update[1])
sys.exit(1)

# Validate and print the results
print ('Alert status after modification:')
print (alert['enabled'])
print (' ')

if not alert_found:
print ('Alert to be updated not found')
sys.exit(1)