diff options
Diffstat (limited to 'monitoring')
-rw-r--r-- | monitoring/monit.py | 4 | ||||
-rw-r--r-- | monitoring/nagios.py | 18 | ||||
-rw-r--r-- | monitoring/pagerduty.py | 1 | ||||
-rw-r--r-- | monitoring/uptimerobot.py | 168 | ||||
-rw-r--r-- | monitoring/zabbix_group.py | 212 |
5 files changed, 397 insertions, 6 deletions
diff --git a/monitoring/monit.py b/monitoring/monit.py index 558f1e69..8772d22b 100644 --- a/monitoring/monit.py +++ b/monitoring/monit.py @@ -75,8 +75,8 @@ def main(): # Sample output lines: # Process 'name' Running # Process 'name' Running - restart pending - parts = line.lower().split() - if len(parts) > 2 and parts[0] == 'process' and parts[1] == "'%s'" % name: + parts = line.split() + if len(parts) > 2 and parts[0].lower() == 'process' and parts[1] == "'%s'" % name: return ' '.join(parts[2:]) else: return '' diff --git a/monitoring/nagios.py b/monitoring/nagios.py index f0904a44..bb2509e7 100644 --- a/monitoring/nagios.py +++ b/monitoring/nagios.py @@ -395,7 +395,7 @@ class Nagios(object): return notif_str - def schedule_svc_downtime(self, host, services=[], minutes=30): + def schedule_svc_downtime(self, host, services=None, minutes=30): """ This command is used to schedule downtime for a particular service. @@ -409,6 +409,10 @@ class Nagios(object): """ cmd = "SCHEDULE_SVC_DOWNTIME" + + if services is None: + services = [] + for service in services: dt_cmd_str = self._fmt_dt_str(cmd, host, minutes, svc=service) self._write_command(dt_cmd_str) @@ -549,7 +553,7 @@ class Nagios(object): notif_str = self._fmt_notif_str(cmd, host) self._write_command(notif_str) - def disable_svc_notifications(self, host, services=[]): + def disable_svc_notifications(self, host, services=None): """ This command is used to prevent notifications from being sent out for the specified service. @@ -561,6 +565,10 @@ class Nagios(object): """ cmd = "DISABLE_SVC_NOTIFICATIONS" + + if services is None: + services = [] + for service in services: notif_str = self._fmt_notif_str(cmd, host, svc=service) self._write_command(notif_str) @@ -659,7 +667,7 @@ class Nagios(object): else: return "Fail: could not write to the command file" - def enable_svc_notifications(self, host, services=[]): + def enable_svc_notifications(self, host, services=None): """ Enables notifications for a particular service. @@ -669,6 +677,10 @@ class Nagios(object): """ cmd = "ENABLE_SVC_NOTIFICATIONS" + + if services is None: + services = [] + nagios_return = True return_str_list = [] for service in services: diff --git a/monitoring/pagerduty.py b/monitoring/pagerduty.py index 5ca33717..aa690341 100644 --- a/monitoring/pagerduty.py +++ b/monitoring/pagerduty.py @@ -131,7 +131,6 @@ EXAMPLES=''' desc=deployment ''' -import json import datetime import base64 diff --git a/monitoring/uptimerobot.py b/monitoring/uptimerobot.py new file mode 100644 index 00000000..889d144c --- /dev/null +++ b/monitoring/uptimerobot.py @@ -0,0 +1,168 @@ +#!/usr/bin/python + +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 +version_added: "1.9" +requirements: + - Valid Uptime Robot API Key +options: + state: + description: + - Define whether or not the monitor should be running or paused. + required: true + default: null + choices: [ "started", "paused" ] + aliases: [] + monitorid: + description: + - ID of the monitor to check. + required: true + default: null + choices: [] + aliases: [] + apikey: + description: + - Uptime Robot API key. + required: true + default: null + choices: [] + aliases: [] +notes: + - Support for adding and removing monitors and alert contacts has not yet been implemented. +''' + +EXAMPLES = ''' +# Pause the monitor with an ID of 12345. +- uptimerobot: monitorid=12345 + apikey=12345-1234512345 + state=paused + +# Start the monitor with an ID of 12345. +- uptimerobot: monitorid=12345 + apikey=12345-1234512345 + state=started + +''' + +import json +import urllib +import urllib2 +import time + +API_BASE = "http://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(params): + + data = urllib.urlencode(params) + + full_uri = API_BASE + API_ACTIONS['status'] + data + + req = urllib2.urlopen(full_uri) + + result = req.read() + + jsonresult = json.loads(result) + + req.close() + + return jsonresult + + +def startMonitor(params): + + params['monitorStatus'] = 1 + + data = urllib.urlencode(params) + + full_uri = API_BASE + API_ACTIONS['editMonitor'] + data + + req = urllib2.urlopen(full_uri) + + result = req.read() + + jsonresult = json.loads(result) + + req.close() + + return jsonresult['stat'] + + +def pauseMonitor(params): + + params['monitorStatus'] = 0 + + data = urllib.urlencode(params) + + full_uri = API_BASE + API_ACTIONS['editMonitor'] + data + + req = urllib2.urlopen(full_uri) + + result = 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), + 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(params) + + if check_result['stat'] != "ok": + module.fail_json( + msg="failed", + result=check_result['message'] + ) + + if module.params['state'] == 'started': + monitor_result = startMonitor(params) + else: + monitor_result = pauseMonitor(params) + + + + module.exit_json( + msg="success", + result=monitor_result + ) + + +from ansible.module_utils.basic import * +main() diff --git a/monitoring/zabbix_group.py b/monitoring/zabbix_group.py new file mode 100644 index 00000000..489a8617 --- /dev/null +++ b/monitoring/zabbix_group.py @@ -0,0 +1,212 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2014, René Moser <mail@renemoser.net> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + + +DOCUMENTATION = ''' +--- +module: zabbix_group +short_description: Add or remove a host group to Zabbix. +description: + - This module uses the Zabbix API to add and remove host groups. +version_added: '1.8' +requirements: [ 'zabbix-api' ] +options: + state: + description: + - Whether the host group should be added or removed. + required: false + default: present + choices: [ 'present', 'absent' ] + host_group: + description: + - Name of the host group to be added or removed. + required: true + default: null + aliases: [ ] + server_url: + description: + - Url of Zabbix server, with protocol (http or https) e.g. + https://monitoring.example.com/zabbix. C(url) is an alias + for C(server_url). If not set environment variable + C(ZABBIX_SERVER_URL) is used. + required: true + default: null + aliases: [ 'url' ] + login_user: + description: + - Zabbix user name. If not set environment variable + C(ZABBIX_LOGIN_USER) is used. + required: true + default: null + login_password: + description: + - Zabbix user password. If not set environment variable + C(ZABBIX_LOGIN_PASSWORD) is used. + required: true +notes: + - The module has been tested with Zabbix Server 2.2. +author: René Moser +''' + +EXAMPLES = ''' +--- +# Add a new host group to Zabbix +- zabbix_group: host_group='Linux servers' + server_url=https://monitoring.example.com/zabbix + login_user=ansible + login_password=secure + +# Add a new host group, login data is provided by environment variables: +# ZABBIX_LOGIN_USER, ZABBIX_LOGIN_PASSWORD, ZABBIX_SERVER_URL: +- zabbix_group: host_group=Webservers + +# Remove a host group from Zabbix +- zabbix_group: host_group='Linux servers' + state=absent + server_url=https://monitoring.example.com/zabbix + login_user=ansible + login_password=secure +''' + +try: + from zabbix_api import ZabbixAPI + HAS_ZABBIX_API = True +except ImportError: + HAS_ZABBIX_API = False + + +def create_group(zbx, host_group): + try: + result = zbx.hostgroup.create( + { + 'name': host_group + } + ) + except BaseException as e: + return 1, None, str(e) + return 0, result['groupids'], None + + +def get_group(zbx, host_group): + try: + result = zbx.hostgroup.get( + { + 'filter': + { + 'name': host_group, + } + } + ) + except BaseException as e: + return 1, None, str(e) + + return 0, result[0]['groupid'], None + + +def delete_group(zbx, group_id): + try: + zbx.hostgroup.delete([ group_id ]) + except BaseException as e: + return 1, None, str(e) + return 0, None, None + + +def check_group(zbx, host_group): + try: + result = zbx.hostgroup.exists( + { + 'name': host_group + } + ) + except BaseException as e: + return 1, None, str(e) + return 0, result, None + + +def main(): + module = AnsibleModule( + argument_spec=dict( + state=dict(default='present', choices=['present', 'absent']), + host_group=dict(required=True, default=None), + server_url=dict(default=None, aliases=['url']), + login_user=dict(default=None), + login_password=dict(default=None), + ), + supports_check_mode=True, + ) + + if not HAS_ZABBIX_API: + module.fail_json(msg='Missing requried zabbix-api module (check docs or install with: pip install zabbix-api)') + + try: + login_user = module.params['login_user'] or os.environ['ZABBIX_LOGIN_USER'] + login_password = module.params['login_password'] or os.environ['ZABBIX_LOGIN_PASSWORD'] + server_url = module.params['server_url'] or os.environ['ZABBIX_SERVER_URL'] + except KeyError, e: + module.fail_json(msg='Missing login data: %s is not set.' % e.message) + + host_group = module.params['host_group'] + state = module.params['state'] + + try: + zbx = ZabbixAPI(server_url) + zbx.login(login_user, login_password) + except BaseException as e: + module.fail_json(msg='Failed to connect to Zabbix server: %s' % e) + + changed = False + msg = '' + + if state == 'present': + (rc, exists, error) = check_group(zbx, host_group) + if rc != 0: + module.fail_json(msg='Failed to check host group %s existance: %s' % (host_group, error)) + if not exists: + if module.check_mode: + changed = True + else: + (rc, group, error) = create_group(zbx, host_group) + if rc == 0: + changed = True + else: + module.fail_json(msg='Failed to get host group: %s' % error) + + if state == 'absent': + (rc, exists, error) = check_group(zbx, host_group) + if rc != 0: + module.fail_json(msg='Failed to check host group %s existance: %s' % (host_group, error)) + if exists: + if module.check_mode: + changed = True + else: + (rc, group_id, error) = get_group(zbx, host_group) + if rc != 0: + module.fail_json(msg='Failed to get host group: %s' % error) + + (rc, _, error) = delete_group(zbx, group_id) + if rc == 0: + changed = True + else: + module.fail_json(msg='Failed to remove host group: %s' % error) + + module.exit_json(changed=changed) + +from ansible.module_utils.basic import * +main() |