Created
April 8, 2019 15:16
-
-
Save diasjorge/a126cb0856bbf099de76a351873102e8 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
# | |
# Script for disabling all alerts on a device | |
# chmod to make executable, then ./toggleSubjectAlerts.py -s [SubjectID] -t [SubjectType] -a [APIToken] -e [True/False] -g [groupName] | |
import requests | |
import json | |
import argparse | |
parser = argparse.ArgumentParser(description='This is a script for enabling / disabling all alerts on a device') | |
parser.add_argument('-k','--agentKey', help='Agent Key',required=True) | |
parser.add_argument('-t','--subjectType',help='Subject type, device or service', required=True) | |
parser.add_argument('-a','--token',help='Your API Token', required=True) | |
parser.add_argument('-e','--enabled',help='Alert enabled, true or false.', required=True) | |
parser.add_argument('-g','--group',help='Group name, used when returning the device to its group.') | |
args = parser.parse_args() | |
token = args.token | |
agent_key=args.agentKey | |
subject_type = args.subjectType | |
enabled = args.enabled | |
if args.group != None: | |
group = args.group | |
else: | |
group = '_Maintenance' | |
print 'Setting device to group: "{}"'.format(group) | |
try: | |
device = requests.get( | |
'https://api.serverdensity.io/inventory/devices/{0}/byagentkey'.format(agent_key), | |
params={ | |
'token': token | |
} | |
) | |
except Exception: | |
print ('There was an error looking up the subject id, please check your token and agent key and try again.') | |
subject_id = device.json()["_id"] | |
print 'Current device group: "{}"'.format(device.json()["group"]) | |
try: | |
alert_list = requests.get( | |
'https://api.serverdensity.io/alerts/configs/{0}'.format(subject_id), | |
params={ | |
'token': token, | |
'subjectType': subject_type | |
} | |
) | |
except Exception: | |
print ('There was an error looking up your alerts, please check your token and subjectId and try again.') | |
for alert in alert_list.json(): | |
try: | |
print ('Updating alert id: ' + alert['_id']) | |
alert_update = requests.put( | |
'https://api.serverdensity.io/alerts/configs/{0}'.format(alert['_id']), | |
params={'token': token}, | |
data={"enabled": enabled} | |
) | |
print ('Response:' + str(alert_update.status_code)) | |
except Exception: | |
print ('Something went wrong while updating your alerts') | |
print ('Response:' + str(alert_update.status_code)) | |
try: | |
if subject_type == 'device': | |
move_group = requests.put( | |
'https://api.serverdensity.io/inventory/devices/{0}'.format(subject_id), | |
params={ | |
'token': token | |
}, | |
data={ | |
"group": group | |
} | |
) | |
device = move_group.json() | |
print device | |
if subject_type == 'service': | |
move_group = requests.put( | |
'https://api.serverdensity.io/inventory/services/{0}'.format(subject_id), | |
params={ | |
'token': token | |
}, | |
data={ | |
"group": group | |
} | |
) | |
service = move_group.json() | |
print service | |
except Exception: | |
print ('Something went wrong with moving groups') | |
print ('Response:' + str(move_group.status_code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment