Skip to content
Snippets Groups Projects
Commit 0f0c3846 authored by jurgenhaas's avatar jurgenhaas
Browse files

ansible-inventories/arocom#17 Initiate uptime plugin

parents
No related branches found
No related tags found
No related merge requests found
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
.Python
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml
# Translations
*.mo
# Mr Developer
.mr.developer.cfg
.project
.pydevproject
# Rope
.ropeproject
# Django stuff:
*.log
*.pot
# Sphinx documentation
docs/_build/
LICENSE 0 → 100644
The MIT License (MIT)
Copyright (c) 2014 Jürgen Haas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
Ansible plugin for Uptime
=========================
See https://gitlab.paragon-es.de/tools/uptime
import json
import requests
import ConfigParser
from ansible.errors import AnsibleError as ae
from ansible.plugins.action import ActionBase
from requests.auth import HTTPBasicAuth
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
class ActionModule(ActionBase):
'''Manage hosts and services monitored at uptime'''
#BYPASS_HOST_LOOP = True
TRANSFERS_FILES = False
def __init__(self, task, connection, play_context, loader, templar,
shared_loader_obj):
super(ActionModule, self).__init__(task, connection, play_context,
loader, templar, shared_loader_obj)
self.args = {}
self.vars = {}
configPath = '/etc/uptime.cfg'
config = ConfigParser.ConfigParser()
config.read(configPath)
section = 'Credentials'
if config.has_option(section, 'url') and config.has_option(section, 'username') and config.has_option(section, 'password'):
self.url = config.get(section, 'url')
self.username = config.get(section, 'username')
self.password = config.get(section, 'password')
else:
raise ae('Can not find credentials')
self.checks = self._request('checks')
display.vv('Initialized')
def run(self, tmp=None, task_vars=None):
if task_vars is None:
task_vars = dict()
self.vars = task_vars
result = super(ActionModule, self).run(tmp, task_vars)
if self._play_context.check_mode:
result['skipped'] = True
result['msg'] = 'check mode not supported for this module'
return result
print('RUN')
result['msg'] = 'Completed successfully!'
return result
def _request(self, path, data = None, method = 'GET'):
encoder = json.JSONEncoder()
postData = {}
if data:
if method == 'GET':
method = 'POST'
for key in data:
item = data.get(key)
if type(item) is list or type(item) is dict:
if len(item) > 0 or key == 'recipients':
item = encoder.encode(item)
if type(item) is int or type(item) is unicode or type(item) is bool:
item = str(item)
if item and type(item) is str and len(item) > 0:
postData.__setitem__(key, item)
auth = HTTPBasicAuth(self.username, self.password)
request_result = {}
try:
if method == 'GET':
request_result = requests.get(self.url + '/api/' + path, auth = auth)
elif method == 'POST':
request_result = requests.post(self.url + '/api/' + path, auth = auth, data = postData)
elif method == 'PUT':
request_result = requests.put(self.url + '/api/' + path, auth = auth, data = postData)
elif method == 'DELETE':
request_result = requests.delete(self.url + '/api/' + path, auth = auth)
except ae, e:
raise ae('No result from Uptime API')
display.vvvvvv(request_result)
if request_result.status_code != 200:
msg = 'Error %s' % request_result.status_code
raise ae('%s' % msg)
decoder = json.JSONDecoder()
if request_result.content == '':
content = []
else:
content = decoder.decode(request_result.content)
return content
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment