Maintenance / pause alerts

If you're performing maintenance on a host and need to pause the alerts for a device / service temporarily, you can use the prepared script below. This will disable all alerts, and move the device / service out of the group it may be in otherwise group alerts will be disabled, affecting other servers / web checks not going down for maintenance.

Example:
./toggleSubjectAlerts.py -s 520dee51cbda5de279000013 -t device -a b97da80a41c4f61bff05975ee51eb1aa -e False

Then to re-enable ensure you pass a group name if applicable:
./toggleSubjectAlerts.py -s 520dee51cbda5de279000013 -t device -a b97da80a41c4f61bff05975ee51eb1aa -e True -g Apache

You could, for example, have this running as a cron job if you run weekly backups between 4-6AM and wish to suppress alerts during those times.

#!/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('-s','--subjectId', help='Subject ID ',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
subject_id = args.subjectId
subject_type = args.subjectType
enabled = args.enabled
if args.group != None:
    group = args.group
else:
    group = '_Maintenance'
print 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))