diff --git a/library/monitoring/pagerduty b/library/monitoring/pagerduty new file mode 100644 index 0000000000..10309036f4 --- /dev/null +++ b/library/monitoring/pagerduty @@ -0,0 +1,157 @@ +#!/usr/bin/python + +DOCUMENTATION = ''' + +module: pagerduty +short_description: Create PagerDuty maintenance windows +description: + - This module will let you create PagerDuty maintenance windows +version_added: 0.1 +author: Justin Johns +requirements: + - PagerDuty API access +options: + state: + description: + - Create a maintenance window or get a list of ongoing windows. + required: true + default: null + choices: [ "running", "started", "ongoing" ] + aliases: [] + version_added: 0.1 + name: + description: + - PagerDuty unique subdomain. + required: true + default: null + choices: [] + aliases: [] + version_added: 0.1 + user: + description: + - PagerDuty user ID. + required: true + default: null + choices: [] + aliases: [] + version_added: 0.1 + passwd: + description: + - PagerDuty user password. + required: true + default: null + choices: [] + aliases: [] + version_added: 0.1 + service: + description: + - PagerDuty service ID. + required: false + default: null + choices: [] + aliases: [] + version_added: 0.1 + hours: + description: + - Length of maintenance window in hours. + required: false + default: 1 + choices: [] + aliases: [] + version_added: 0.1 + desc: + description: + - Short description of maintenance window. + required: false + default: Created by Ansible + choices: [] + aliases: [] + version_added: 0.1 +examples: + - code: pagerduty name=companyabc user=example@example.com passwd=password123 state=ongoing" + description: List ongoing maintenance windows. + - code: pagerduty name=companyabc user=example@example.com passwd=password123 state=running service=FOO123" + description: Create a 1 hour maintenance window for service FOO123. + - code: pagerduty name=companyabc user=example@example.com passwd=password123 state=running service=FOO123 hours=4 desc=deployment" + description: Create a 4 hour maintenance window for service FOO123 with the description "deployment". +notes: + - This module does not yet have support to end maintenance windows. +''' + +import json +import datetime +import urllib2 +import base64 + + +def ongoing(name, user, passwd): + + url = "https://" + name + ".pagerduty.com/api/v1/maintenance_windows/ongoing" + auth = base64.encodestring('%s:%s' % (user, passwd)).replace('\n', '') + + req = urllib2.Request(url) + req.add_header("Authorization", "Basic %s" % auth) + res = urllib2.urlopen(req) + out = res.read() + + return False, out + + +def create(name, user, passwd, service, hours, desc): + + now = datetime.datetime.utcnow() + later = now + datetime.timedelta(hours=int(hours)) + start = now.strftime("%Y-%m-%dT%H:%M:%SZ") + end = later.strftime("%Y-%m-%dT%H:%M:%SZ") + + url = "https://" + name + ".pagerduty.com/api/v1/maintenance_windows" + auth = base64.encodestring('%s:%s' % (user, passwd)).replace('\n', '') + data = json.dumps({'maintenance_window': {'start_time': start, 'end_time': end, 'description': desc, 'service_ids': [service]}}) + + req = urllib2.Request(url, data) + req.add_header("Authorization", "Basic %s" % auth) + req.add_header('Content-Type', 'application/json') + res = urllib2.urlopen(req) + out = res.read() + + return False, out + + +def main(): + + module = AnsibleModule( + argument_spec=dict( + state=dict(required=True, choices=['running', 'started', 'ongoing']), + name=dict(required=True), + user=dict(required=True), + passwd=dict(required=True), + service=dict(required=False), + hours=dict(default='1', required=False), + desc=dict(default='Created by Ansible', required=False) + ) + ) + + state = module.params['state'] + name = module.params['name'] + user = module.params['user'] + passwd = module.params['passwd'] + service = module.params['service'] + hours = module.params['hours'] + desc = module.params['desc'] + + if state == "running" or state == "started": + if not service: + module.fail_json(msg="service not specified") + (rc, out) = create(name, user, passwd, service, hours, desc) + + if state == "ongoing": + (rc, out) = ongoing(name, user, passwd) + + if rc != 0: + module.fail_json(msg="failed", result=out) + + module.exit_json(msg="success", result=out) + +# include magic from lib/ansible/module_common.py +#<> +main() diff --git a/library/monitoring/pingdom b/library/monitoring/pingdom new file mode 100644 index 0000000000..2917e504e6 --- /dev/null +++ b/library/monitoring/pingdom @@ -0,0 +1,123 @@ +#!/usr/bin/python + +DOCUMENTATION = ''' + +module: pingdom +short_description: Pause/unpause Pingdom alerts +description: + - This module will let you pause/unpause Pingdom alerts +version_added: 0.1 +author: Justin Johns +requirements: + - pingdom Python library. Install using either of the following: + - pip install pingdom + - easy_install pingdom +options: + state: + description: + - Define whether or not the check should be running or paused. + required: true + default: null + choices: [ "running", "paused" ] + aliases: [] + version_added: 0.1 + checkid: + description: + - Pingdom ID of the check. + required: true + default: null + choices: [] + aliases: [] + version_added: 0.1 + uid: + description: + - Pingdom user ID. + required: true + default: null + choices: [] + aliases: [] + version_added: 0.1 + passwd: + description: + - Pingdom user password. + required: true + default: null + choices: [] + aliases: [] + version_added: 0.1 + key: + description: + - Pingdom API key. + required: true + default: null + choices: [] + aliases: [] + version_added: 0.1 +examples: + - code: pingdom uid=example@example.com passwd=password123 key=apipassword123 checkid=12345 state=paused + description: Pause the check with the ID of 12345. + - code: pingdom uid=example@example.com passwd=password123 key=apipassword123 checkid=12345 state=running + description: Unpause the check with the ID of 12345. +notes: + - This module does not yet have support to add/remove checks. +''' + +import pingdom + + +def pause(checkid, uid, passwd, key): + + c = pingdom.PingdomConnection(uid, passwd, key) + c.modify_check(checkid, paused=True) + check = c.get_check(checkid) + name = check.name + result = check.status + #if result != "paused": # api output buggy - accept raw exception for now + # return (True, name, result) + return (False, name, result) + + +def unpause(checkid, uid, passwd, key): + + c = pingdom.PingdomConnection(uid, passwd, key) + c.modify_check(checkid, paused=False) + check = c.get_check(checkid) + name = check.name + result = check.status + #if result != "up": # api output buggy - accept raw exception for now + # return (True, name, result) + return (False, name, result) + + +def main(): + + module = AnsibleModule( + argument_spec=dict( + state=dict(required=True, choices=['running', 'paused', 'started', 'stopped']), + checkid=dict(required=True), + uid=dict(required=True), + passwd=dict(required=True), + key=dict(required=True) + ) + ) + + checkid = module.params['checkid'] + state = module.params['state'] + uid = module.params['uid'] + passwd = module.params['passwd'] + key = module.params['key'] + + if (state == "paused" or state == "stopped"): + (rc, name, result) = pause(checkid, uid, passwd, key) + + if (state == "running" or state == "started"): + (rc, name, result) = unpause(checkid, uid, passwd, key) + + if rc != 0: + module.fail_json(checkid=checkid, name=name, status=result) + + module.exit_json(checkid=checkid, name=name, status=result) + +# include magic from lib/ansible/module_common.py +#<> +main()