From b684480493cc7e98ba50a2f979a82887958923c0 Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Tue, 19 Jun 2018 14:08:37 -0500 Subject: [PATCH 1/8] Added ligo test --- Dockerfile | 2 +- scripts/check-tale | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100755 scripts/check-tale diff --git a/Dockerfile b/Dockerfile index 34fae25..f0f4297 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM debian:jessie RUN apt-get update -y && apt-get install bash curl python3 python3-pip xinetd -y && \ - pip3 install docker + pip3 install argparse docker COPY check_mk/etc /etc/ COPY check_mk/usr /usr/ diff --git a/scripts/check-tale b/scripts/check-tale new file mode 100755 index 0000000..339c136 --- /dev/null +++ b/scripts/check-tale @@ -0,0 +1,64 @@ +import argparse +import requests + + +# Check_MK script to create and delete an instance of the specified tale +def main(): + parser = argparse.ArgumentParser() + + parser.add_argument("-s", "--server", required=True, help="Server URL") + parser.add_argument("-k", "--key", required=True, help="API key") + parser.add_argument("-t", "--tale", required=True, help="Tale ID") + + args = parser.parse_args() + + apiUrl = "%s/api/v1" % args.server + + token = get_token(apiUrl, args.key) + if token is not None: + create_instance(apiUrl, token, args.tale) + + +# Get a token for the API key +def get_token(apiUrl, key): + try: + tokenUrl = "%s/api_key/token?key=%s" % (apiUrl, key) + r = requests.post(tokenUrl, headers={'Accept': 'application/json', 'Content-Type': 'application/json'}) + if r.status_code == requests.codes.ok: + body = r.json() + return body['authToken']['token'] + else: + print("2 Tale status=%s CRITICAL - error getting token" % r.status_code) + + except Exception as e: + print("2 Tale status=failed CRITICAL - unexpected exception getting token") + + return None + + +# Create and delete an instance of the specified tale +def create_instance(apiUrl, token, tale): + + try: + postInstanceUrl = "%s/instance?taleId=%s" % (apiUrl, tale) + r = requests.post(postInstanceUrl, headers={'Girder-Token': token, 'Content-Type': 'application/json', + 'Accept': 'application/json'}) + if r.status_code == requests.codes.ok: + body = r.json() + instanceId = body['_id'] + + deleteInstanceUrl = "%s/instance/%s" % (apiUrl, instanceId) + r = requests.delete(deleteInstanceUrl, headers={'Girder-Token': token, 'Accept': 'application/json'}) + if r.status_code == requests.codes.ok: + print("0 Tale status=%s OK - tale successfully created/deleted") + else: + print("2 Tale status=%s CRITICAL - error deleting tale" % r.status_code) + else: + print("2 Tale status=%s CRITICAL - error creating tale" % r.status_code) + + except Exception as e: + print("2 Tale status=failed CRITICAL - unexpected exception creating tale") + +if __name__ == "__main__": + main() + From b0da99310e6baf8bd9ceaaab331201e0e23a58d8 Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Tue, 19 Jun 2018 14:45:56 -0500 Subject: [PATCH 2/8] Change check-tale to run every 10 minutes --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index 47ff717..53a77f4 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,5 +6,6 @@ mkdir -p /usr/lib/check_mk_agent/local cp /scripts/check-services /usr/lib/check_mk_agent/local cp /scripts/check-nodes /usr/lib/check_mk_agent/local cp /scripts/check-celery /usr/lib/check_mk_agent/local +cp /scripts/check-tale /usr/lib/check_mk_agent/local/600/ /usr/sbin/xinetd -f /etc/xinetd.conf -dontfork -stayalive From eb7d3c7123f896173074eb96137ec15571ea3b61 Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Tue, 19 Jun 2018 14:46:12 -0500 Subject: [PATCH 3/8] Only run on Swarm master --- scripts/check-tale | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/check-tale b/scripts/check-tale index 339c136..551e9e4 100755 --- a/scripts/check-tale +++ b/scripts/check-tale @@ -1,19 +1,32 @@ import argparse +import docker import requests +import os +import sys # Check_MK script to create and delete an instance of the specified tale def main(): parser = argparse.ArgumentParser() - parser.add_argument("-s", "--server", required=True, help="Server URL") - parser.add_argument("-k", "--key", required=True, help="API key") - parser.add_argument("-t", "--tale", required=True, help="Tale ID") - + # Allow arguments via command line and environment + parser.add_argument("-s", "--server", required=False, help="Server URL", default=os.environ.get('GIRDER_URL', None)) + parser.add_argument("-k", "--key", required=False, help="API key", default=os.environ.get('GIRDER_API_KEY', None)) + parser.add_argument("-t", "--tale", required=False, help="Tale ID", default=os.environ.get('TALE_ID', None)) + parser.add_argument("-d", "--dev", required=False, help="Development mode", default=False) args = parser.parse_args() apiUrl = "%s/api/v1" % args.server + # Unless in dev mode, only run this check on the Swarm master + if args.dev: + print("server=%s key=%s tale=%s" % (args.server, args.key, args.tale)) + else: + client = docker.from_env() + attrs = client.swarm.attrs + if not attrs.get("ID"): + sys.exit(0) + token = get_token(apiUrl, args.key) if token is not None: create_instance(apiUrl, token, args.tale) From befea118894dc6f9d9f6920dfc950c5b2b681d7f Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Wed, 20 Jun 2018 09:36:30 -0500 Subject: [PATCH 4/8] Changed tale check caching to 1 hour --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 53a77f4..e9b0cdc 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,6 +6,6 @@ mkdir -p /usr/lib/check_mk_agent/local cp /scripts/check-services /usr/lib/check_mk_agent/local cp /scripts/check-nodes /usr/lib/check_mk_agent/local cp /scripts/check-celery /usr/lib/check_mk_agent/local -cp /scripts/check-tale /usr/lib/check_mk_agent/local/600/ +cp /scripts/check-tale /usr/lib/check_mk_agent/local/3600/ /usr/sbin/xinetd -f /etc/xinetd.conf -dontfork -stayalive From 1af11ce51412a1663550d18233aa2c12dd328590 Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Wed, 20 Jun 2018 09:41:39 -0500 Subject: [PATCH 5/8] Create directory --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index e9b0cdc..7d18c11 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,7 +2,7 @@ set -e -mkdir -p /usr/lib/check_mk_agent/local +mkdir -p /usr/lib/check_mk_agent/local/3600 cp /scripts/check-services /usr/lib/check_mk_agent/local cp /scripts/check-nodes /usr/lib/check_mk_agent/local cp /scripts/check-celery /usr/lib/check_mk_agent/local From 07d64784f360c203570cecadf69245b7048e612a Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Wed, 20 Jun 2018 10:34:37 -0500 Subject: [PATCH 6/8] Added interpreter --- scripts/check-tale | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/check-tale b/scripts/check-tale index 551e9e4..f2f71a3 100755 --- a/scripts/check-tale +++ b/scripts/check-tale @@ -1,3 +1,4 @@ +#!/usr/bin/python3 import argparse import docker import requests @@ -63,7 +64,7 @@ def create_instance(apiUrl, token, tale): deleteInstanceUrl = "%s/instance/%s" % (apiUrl, instanceId) r = requests.delete(deleteInstanceUrl, headers={'Girder-Token': token, 'Accept': 'application/json'}) if r.status_code == requests.codes.ok: - print("0 Tale status=%s OK - tale successfully created/deleted") + print("0 Tale status=%s OK - tale successfully created/deleted" % r.status_code ) else: print("2 Tale status=%s CRITICAL - error deleting tale" % r.status_code) else: From 9aba80ae7a8a7d8db997cd0ddeec1f4b266aa206 Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Mon, 25 Jun 2018 10:33:46 -0500 Subject: [PATCH 7/8] Added logging to capture requests errors --- scripts/check-tale | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/check-tale b/scripts/check-tale index f2f71a3..add15d0 100755 --- a/scripts/check-tale +++ b/scripts/check-tale @@ -4,10 +4,13 @@ import docker import requests import os import sys +import logging # Check_MK script to create and delete an instance of the specified tale def main(): + + logging.basicConfig(filename='check-tale.log',level=logging.DEBUG) parser = argparse.ArgumentParser() # Allow arguments via command line and environment @@ -43,9 +46,11 @@ def get_token(apiUrl, key): return body['authToken']['token'] else: print("2 Tale status=%s CRITICAL - error getting token" % r.status_code) + logging.error("Error getting token " % r.content) except Exception as e: print("2 Tale status=failed CRITICAL - unexpected exception getting token") + logging.exception("Exception getting token") return None @@ -67,11 +72,14 @@ def create_instance(apiUrl, token, tale): print("0 Tale status=%s OK - tale successfully created/deleted" % r.status_code ) else: print("2 Tale status=%s CRITICAL - error deleting tale" % r.status_code) + logging.error("Error deleting tale " % r.content) else: print("2 Tale status=%s CRITICAL - error creating tale" % r.status_code) + logging.error("Error creating tale " % r.content) except Exception as e: print("2 Tale status=failed CRITICAL - unexpected exception creating tale") + logging.exception("Exception creating tale") if __name__ == "__main__": main() From d4ba6ad56b43d183c111ae82a30c659da66a3efc Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Sat, 30 Jun 2018 13:54:25 -0500 Subject: [PATCH 8/8] Changed tale test to run every 10 minutes --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 7d18c11..2bf09c1 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,10 +2,10 @@ set -e -mkdir -p /usr/lib/check_mk_agent/local/3600 +mkdir -p /usr/lib/check_mk_agent/local/600 cp /scripts/check-services /usr/lib/check_mk_agent/local cp /scripts/check-nodes /usr/lib/check_mk_agent/local cp /scripts/check-celery /usr/lib/check_mk_agent/local -cp /scripts/check-tale /usr/lib/check_mk_agent/local/3600/ +cp /scripts/check-tale /usr/lib/check_mk_agent/local/600 /usr/sbin/xinetd -f /etc/xinetd.conf -dontfork -stayalive