2014-11-02 20:06:17 +00:00
|
|
|
import boto.rds2
|
|
|
|
import logging
|
2017-05-24 22:26:22 +00:00
|
|
|
|
|
|
|
from auth.permissions import SuperUserPermission
|
|
|
|
from flask import session
|
2015-01-20 21:53:05 +00:00
|
|
|
from health.services import check_all_services
|
2014-11-02 20:06:17 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-07-11 10:48:55 +00:00
|
|
|
|
2016-07-05 18:14:22 +00:00
|
|
|
def get_healthchecker(app, config_provider, instance_keys):
|
2015-01-20 21:53:05 +00:00
|
|
|
""" Returns a HealthCheck instance for the given app. """
|
2016-07-05 18:14:22 +00:00
|
|
|
return HealthCheck.get_checker(app, config_provider, instance_keys)
|
2015-01-20 21:53:05 +00:00
|
|
|
|
|
|
|
|
2014-11-02 20:06:17 +00:00
|
|
|
class HealthCheck(object):
|
2016-07-05 18:14:22 +00:00
|
|
|
def __init__(self, app, config_provider, instance_keys, instance_skips=None):
|
2015-01-20 21:53:05 +00:00
|
|
|
self.app = app
|
2015-07-27 15:17:44 +00:00
|
|
|
self.config_provider = config_provider
|
2016-07-05 18:14:22 +00:00
|
|
|
self.instance_keys = instance_keys
|
2015-10-22 17:24:56 +00:00
|
|
|
self.instance_skips = instance_skips or []
|
2015-01-20 21:53:05 +00:00
|
|
|
|
|
|
|
def check_instance(self):
|
|
|
|
"""
|
|
|
|
Conducts a check on this specific instance, returning a dict representing the HealthCheck
|
|
|
|
output and a number indicating the health check response code.
|
|
|
|
"""
|
2015-10-22 19:57:34 +00:00
|
|
|
service_statuses = check_all_services(self.app, self.instance_skips)
|
2015-01-20 21:53:05 +00:00
|
|
|
return self.get_instance_health(service_statuses)
|
|
|
|
|
|
|
|
def check_endtoend(self):
|
|
|
|
"""
|
|
|
|
Conducts a check on all services, returning a dict representing the HealthCheck
|
|
|
|
output and a number indicating the health check response code.
|
|
|
|
"""
|
2015-10-22 19:57:34 +00:00
|
|
|
service_statuses = check_all_services(self.app, [])
|
2015-01-20 21:53:05 +00:00
|
|
|
return self.calculate_overall_health(service_statuses)
|
2014-11-02 20:06:17 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
def get_instance_health(self, service_statuses):
|
2014-11-02 20:06:17 +00:00
|
|
|
"""
|
2015-01-20 21:53:05 +00:00
|
|
|
For the given service statuses, returns a dict representing the HealthCheck
|
|
|
|
output and a number indicating the health check response code. By default,
|
|
|
|
this simply ensures that all services are reporting as healthy.
|
2014-11-02 20:06:17 +00:00
|
|
|
"""
|
2015-01-20 21:53:05 +00:00
|
|
|
return self.calculate_overall_health(service_statuses)
|
|
|
|
|
|
|
|
def calculate_overall_health(self, service_statuses, skip=None, notes=None):
|
|
|
|
""" Returns true if and only if all the given service statuses report as healthy. """
|
|
|
|
is_healthy = True
|
|
|
|
notes = notes or []
|
|
|
|
|
2017-05-11 04:05:14 +00:00
|
|
|
service_statuses_bools = {}
|
|
|
|
service_status_expanded = {}
|
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
for service_name in service_statuses:
|
2017-11-10 20:46:09 +00:00
|
|
|
status, message = service_statuses[service_name]
|
2017-05-11 04:05:14 +00:00
|
|
|
|
|
|
|
service_statuses_bools[service_name] = status
|
|
|
|
service_status_expanded[service_name] = {
|
|
|
|
'status': status,
|
|
|
|
}
|
|
|
|
|
|
|
|
if not status:
|
2017-11-10 20:46:09 +00:00
|
|
|
service_status_expanded[service_name]['failure'] = message
|
|
|
|
elif message:
|
|
|
|
service_status_expanded[service_name]['message'] = message
|
2017-05-11 04:05:14 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
if skip and service_name in skip:
|
|
|
|
notes.append('%s skipped in compute health' % service_name)
|
|
|
|
continue
|
|
|
|
|
2017-05-11 04:05:14 +00:00
|
|
|
is_healthy = is_healthy and status
|
2015-01-20 21:53:05 +00:00
|
|
|
|
|
|
|
data = {
|
2017-05-11 04:05:14 +00:00
|
|
|
'services': service_statuses_bools,
|
2015-01-20 21:53:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 22:26:22 +00:00
|
|
|
add_debug_information = SuperUserPermission().can() or session.get('health_debug', False)
|
|
|
|
if add_debug_information:
|
|
|
|
data.update({
|
|
|
|
'services_expanded': service_status_expanded,
|
|
|
|
'notes': notes,
|
|
|
|
'is_testing': self.app.config['TESTING'],
|
|
|
|
'config_provider': self.config_provider.provider_id,
|
|
|
|
'local_service_key_id': self.instance_keys.local_key_id,
|
|
|
|
})
|
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
return (data, 200 if is_healthy else 503)
|
|
|
|
|
2014-11-02 20:06:17 +00:00
|
|
|
@classmethod
|
2016-07-05 18:14:22 +00:00
|
|
|
def get_checker(cls, app, config_provider, instance_keys):
|
2015-01-20 21:53:05 +00:00
|
|
|
name = app.config['HEALTH_CHECKER'][0]
|
|
|
|
parameters = app.config['HEALTH_CHECKER'][1] or {}
|
|
|
|
|
2014-11-02 20:06:17 +00:00
|
|
|
for subc in cls.__subclasses__():
|
2016-03-25 19:25:42 +00:00
|
|
|
if name in subc.check_names():
|
2016-07-05 18:14:22 +00:00
|
|
|
return subc(app, config_provider, instance_keys, **parameters)
|
2014-11-02 20:06:17 +00:00
|
|
|
|
|
|
|
raise Exception('Unknown health check with name %s' % name)
|
|
|
|
|
|
|
|
|
|
|
|
class LocalHealthCheck(HealthCheck):
|
2016-08-01 17:02:15 +00:00
|
|
|
def __init__(self, app, config_provider, instance_keys):
|
2017-07-11 10:48:55 +00:00
|
|
|
super(LocalHealthCheck, self).__init__(app, config_provider, instance_keys, [
|
|
|
|
'redis', 'storage'])
|
2016-08-01 17:02:15 +00:00
|
|
|
|
2014-11-02 20:06:17 +00:00
|
|
|
@classmethod
|
2016-03-25 19:25:42 +00:00
|
|
|
def check_names(cls):
|
|
|
|
return ['LocalHealthCheck']
|
2014-11-02 20:06:17 +00:00
|
|
|
|
|
|
|
|
2016-03-25 19:25:42 +00:00
|
|
|
class RDSAwareHealthCheck(HealthCheck):
|
2016-07-06 20:17:02 +00:00
|
|
|
def __init__(self, app, config_provider, instance_keys, access_key, secret_key,
|
|
|
|
db_instance='quay', region='us-east-1'):
|
2017-07-11 10:48:55 +00:00
|
|
|
super(RDSAwareHealthCheck, self).__init__(app, config_provider, instance_keys, [
|
|
|
|
'redis', 'storage'])
|
2016-08-01 17:02:15 +00:00
|
|
|
|
2014-11-02 20:06:17 +00:00
|
|
|
self.access_key = access_key
|
|
|
|
self.secret_key = secret_key
|
2015-05-20 21:40:43 +00:00
|
|
|
self.db_instance = db_instance
|
2016-03-25 19:25:42 +00:00
|
|
|
self.region = region
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-11-02 20:06:17 +00:00
|
|
|
@classmethod
|
2016-03-25 19:25:42 +00:00
|
|
|
def check_names(cls):
|
|
|
|
return ['RDSAwareHealthCheck', 'ProductionHealthCheck']
|
2014-11-02 20:06:17 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
def get_instance_health(self, service_statuses):
|
|
|
|
# Note: We skip the redis check because if redis is down, we don't want ELB taking the
|
|
|
|
# machines out of service. Redis is not considered a high avaliability-required service.
|
2015-10-22 17:24:56 +00:00
|
|
|
skip = []
|
2015-01-20 21:53:05 +00:00
|
|
|
notes = []
|
2014-11-02 20:06:17 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
# If the database is marked as unhealthy, check the status of RDS directly. If RDS is
|
|
|
|
# reporting as available, then the problem is with this instance. Otherwise, the problem is
|
|
|
|
# with RDS, and so we skip the DB status so we can keep this machine as 'healthy'.
|
|
|
|
db_healthy = service_statuses['database']
|
2014-11-02 20:06:17 +00:00
|
|
|
if not db_healthy:
|
2015-01-20 21:53:05 +00:00
|
|
|
rds_status = self._get_rds_status()
|
|
|
|
notes.append('DB reports unhealthy; RDS status: %s' % rds_status)
|
|
|
|
|
|
|
|
# If the RDS is in any state but available, then we skip the DB check since it will
|
|
|
|
# fail and bring down the instance.
|
|
|
|
if rds_status != 'available':
|
|
|
|
skip.append('database')
|
|
|
|
|
|
|
|
return self.calculate_overall_health(service_statuses, skip=skip, notes=notes)
|
|
|
|
|
|
|
|
def _get_rds_status(self):
|
|
|
|
""" Returns the status of the RDS instance as reported by AWS. """
|
|
|
|
try:
|
2016-03-25 19:25:42 +00:00
|
|
|
region = boto.rds2.connect_to_region(self.region, aws_access_key_id=self.access_key,
|
|
|
|
aws_secret_access_key=self.secret_key)
|
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
response = region.describe_db_instances()['DescribeDBInstancesResponse']
|
|
|
|
result = response['DescribeDBInstancesResult']
|
2017-07-11 10:48:55 +00:00
|
|
|
instances = [
|
|
|
|
i for i in result['DBInstances'] if i['DBInstanceIdentifier'] == self.db_instance]
|
2015-05-20 21:40:43 +00:00
|
|
|
if not instances:
|
|
|
|
return 'error'
|
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
status = instances[0]['DBInstanceStatus']
|
|
|
|
return status
|
|
|
|
except:
|
|
|
|
logger.exception("Exception while checking RDS status")
|
|
|
|
return 'error'
|