-
Notifications
You must be signed in to change notification settings - Fork 44
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!