1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/monitoring/uptimerobot.py
Alexei Znamensky ce5fc7764a
Tidy up validate-modules ignores for monitoring modules (#1350)
* fixed validation-modules for plugins/modules/monitoring/bigpanda.py

* fixed validation-modules for plugins/modules/monitoring/circonus_annotation.py

* fixed validation-modules for plugins/modules/monitoring/honeybadger_deployment.py

* fixed validation-modules for plugins/modules/monitoring/icinga2_feature.py

* fixed validation-modules for plugins/modules/monitoring/icinga2_host.py

* fixed validation-modules for plugins/modules/monitoring/librato_annotation.py

* fixed validation-modules for plugins/modules/monitoring/logentries.py

* fixed validation-modules for plugins/modules/monitoring/logstash_plugin.py

* fixed validation-modules for plugins/modules/monitoring/newrelic_deployment.py

* fixed validation-modules for plugins/modules/monitoring/pagerduty_alert.py

* fixed validation-modules for plugins/modules/monitoring/pagerduty.py

* fixed validation-modules for plugins/modules/monitoring/pingdom.py

* fixed validation-modules for plugins/modules/monitoring/rollbar_deployment.py

* fixed validation-modules for plugins/modules/monitoring/spectrum_device.py

* fixed validation-modules for plugins/modules/monitoring/stackdriver.py

* fixed validation-modules for plugins/modules/monitoring/statusio_maintenance.py

* fixed validation-modules for plugins/modules/monitoring/uptimerobot.py

* fixed validation-modules for plugins/modules/monitoring/datadog/datadog_event.py

* fixed validation-modules for plugins/modules/monitoring/datadog/datadog_monitor.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_check.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_client.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_handler.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_silence.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_subscription.py

* fixed trailing space

* Enabling validation-modules for monitoring modules

* Added line for 2.9

* Update plugins/modules/monitoring/icinga2_host.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/monitoring/datadog/datadog_event.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Suggestion from PR

* oops, missed the 2.11 ignore file

* Update plugins/modules/monitoring/icinga2_host.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
2020-11-23 22:07:03 +01:00

149 lines
3.6 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
module: uptimerobot
short_description: Pause and start Uptime Robot monitoring
description:
- This module will let you start and pause Uptime Robot Monitoring
author: "Nate Kingsley (@nate-kingsley)"
requirements:
- Valid Uptime Robot API Key
options:
state:
type: str
description:
- Define whether or not the monitor should be running or paused.
required: true
choices: [ "started", "paused" ]
monitorid:
type: str
description:
- ID of the monitor to check.
required: true
apikey:
type: str
description:
- Uptime Robot API key.
required: true
notes:
- Support for adding and removing monitors and alert contacts has not yet been implemented.
'''
EXAMPLES = '''
- name: Pause the monitor with an ID of 12345
community.general.uptimerobot:
monitorid: 12345
apikey: 12345-1234512345
state: paused
- name: Start the monitor with an ID of 12345
community.general.uptimerobot:
monitorid: 12345
apikey: 12345-1234512345
state: started
'''
import json
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils.urls import fetch_url
from ansible.module_utils._text import to_text
API_BASE = "https://api.uptimerobot.com/"
API_ACTIONS = dict(
status='getMonitors?',
editMonitor='editMonitor?'
)
API_FORMAT = 'json'
API_NOJSONCALLBACK = 1
CHANGED_STATE = False
SUPPORTS_CHECK_MODE = False
def checkID(module, params):
data = urlencode(params)
full_uri = API_BASE + API_ACTIONS['status'] + data
req, info = fetch_url(module, full_uri)
result = to_text(req.read())
jsonresult = json.loads(result)
req.close()
return jsonresult
def startMonitor(module, params):
params['monitorStatus'] = 1
data = urlencode(params)
full_uri = API_BASE + API_ACTIONS['editMonitor'] + data
req, info = fetch_url(module, full_uri)
result = to_text(req.read())
jsonresult = json.loads(result)
req.close()
return jsonresult['stat']
def pauseMonitor(module, params):
params['monitorStatus'] = 0
data = urlencode(params)
full_uri = API_BASE + API_ACTIONS['editMonitor'] + data
req, info = fetch_url(module, full_uri)
result = to_text(req.read())
jsonresult = json.loads(result)
req.close()
return jsonresult['stat']
def main():
module = AnsibleModule(
argument_spec=dict(
state=dict(required=True, choices=['started', 'paused']),
apikey=dict(required=True, no_log=True),
monitorid=dict(required=True)
),
supports_check_mode=SUPPORTS_CHECK_MODE
)
params = dict(
apiKey=module.params['apikey'],
monitors=module.params['monitorid'],
monitorID=module.params['monitorid'],
format=API_FORMAT,
noJsonCallback=API_NOJSONCALLBACK
)
check_result = checkID(module, params)
if check_result['stat'] != "ok":
module.fail_json(
msg="failed",
result=check_result['message']
)
if module.params['state'] == 'started':
monitor_result = startMonitor(module, params)
else:
monitor_result = pauseMonitor(module, params)
module.exit_json(
msg="success",
result=monitor_result
)
if __name__ == '__main__':
main()