Skip to content

Instantly share code, notes, and snippets.

@scanterog
Last active October 3, 2017 20:07
Show Gist options
  • Save scanterog/22afc9f26fe78c86766635d1983e5b75 to your computer and use it in GitHub Desktop.
Save scanterog/22afc9f26fe78c86766635d1983e5b75 to your computer and use it in GitHub Desktop.
Check app status in servers and add/remove accordingly
import sys
import argparse
import redis
import json
import requests
def parse_arguments():
parser = argparse.ArgumentParser(description='')
parser.add_argument(
"-c", "--config", dest="config", default='config.json',
help="Configuration file"
)
return parser.parse_args()
def get_config(config_file):
try:
with open(config_file) as f:
config = json.loads(f.read())
except (IOError, json.decoder.JSONDecodeError) as e:
print("[%s]: %s" % (e.__class__.__name__, e))
sys.exit(1)
return config
def redis_connect(config):
host = config['redis']['host']
port = config['redis']['port']
db = config['redis']['db']
pwd = config['redis']['password']
return redis.StrictRedis(host=host, port=port, db=db, password=pwd)
def exclude_host(_redis, host):
print("Removing host %s from cluster" % host)
# TODO: Remove DNS entry in case of DNS RR.
# Add in failed set into redis
_redis.sadd('failed', host)
return True
def include_host(_redis, host):
print("Including host %s to cluster again" % host)
# TODO: Add DNS entry back in case of DNS RR.
# Remove from failed set into redis
_redis.srem('failed', host)
return True
def do_checks(_redis, config):
hosts = config['hosts']
for host in hosts:
# check API
headers = {'Host': config['app']['domain_api']}
url = 'http://' + host + '/api'
try:
response = requests.get(url, headers=headers)
except requests.exceptions.ConnectionError as e:
print(e)
sys.exit(1)
if response.status_code == 200:
_redis.lpush(host, 0)
else:
_redis.lpush(host, 1)
# trim to last N checks
number_checks = int(config['number_checks'])
_redis.ltrim(host, 0, number_checks - 1)
# get last N checks
checks = _redis.lrange(host, 0, -1)
checks = [ int(e) for e in checks ]
if response.status_code != 200:
if _redis.sismember('failed', host):
continue
if sum(checks) == number_checks:
exclude_host(_redis, host)
else:
if _redis.sismember('failed', host):
if sum(checks) == 0:
include_host(_redis, host)
def main():
args = parse_arguments()
config = get_config(args.config)
_redis = redis_connect(config)
do_checks(_redis, config)
if __name__ == '__main__':
main()
{
"redis": {
"host": "127.0.0.1",
"port": 6379,
"db": 0,
"password": ""
},
"number_checks": 5,
"hosts": ["fe.domain.tld", "fe2.domain.tld"],
"app": {
"domain_api": "app-api.domain.tld"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment