Esempio n. 1
0
    def __init__(self):

        self.file_name = 'configs/apollo.ini'
        self.env_type = env_file_conf('ENV_TYPE').upper() if env_file_conf(
            'ENV_TYPE') else "DEV"
        self.client = apo_client(self.env_type, self.file_name)

        self.client.start()
Esempio n. 2
0
    def query_env(self):
        """
                查询所有产品线
                :return: list
        """

        env_type = env_file_conf('ENV_TYPE').upper() if env_file_conf('ENV_TYPE') else "DEV"

        return env_type
def static_storage(static_interval):
    """
            查询服务可用性
            :param product: 产品名称
            :param service: 服务名称
            :param env_type: 环境类型
            :return: float
    """
    env_type = env_file_conf('ENV_TYPE').upper() if env_file_conf(
        'ENV_TYPE') else "DEV"
    external = env_file_conf('EXTERNAL')

    apollo_query = ApolloQuery()
    host_conf_name = 'mysql_host'
    if external.upper() != "FALSE":
        host_conf_name = 'mysql_external_host'

    db_host = apollo_query.apo_config(host_conf_name)
    db_port = apollo_query.apo_config('mysql_port')
    database = apollo_query.apo_config('mysql_db')
    table = apollo_query.apo_config('mysql_table')
    db_user = apollo_query.apo_config('mysql_user')
    db_passwd = apollo_query.apo_config('mysql_passwd')

    service_dict = {}
    service_health = ServiceHealth()
    all_product = service_health.query_product()

    conn = mysql_conn(db_host, db_port, database, db_user, db_passwd)

    for product in all_product:
        service_dict[product] = service_health.query_service(product=product)

    for product, service_list in service_dict.items():
        for service in service_list:
            service_ava = service_health.service_availability(
                product, service, static_interval)

            print(
                'Debug service {} of product {} availability of environment{}: {} '
                .format(product, env_type, service, env_type, service_ava))

            sql = service_health.sql_genarate(table, product, service,
                                              float('%.4f' % service_ava))
            mysql_excute(conn, sql)

    conn.close()
    def __init__(self, name='service_metrics'):
        log_level = env_file_conf('LOG_LEVEL').lower()
        self.__logger = logging.getLogger(name)
        log_level = level_relations[log_level]
        self.__logger.setLevel(log_level)

        std_handler = logging.StreamHandler()
        formatter = logging.Formatter(
            '%(asctime)s %(process)d %(levelname)s %(thread)d - %(funcName)s %(filename)s:%(lineno)d -[日志信息]: %('
            'message)s ')
        std_handler.setFormatter(formatter)

        self.__logger.addHandler(std_handler)
def eureka_client():

    external = env_file_conf('EXTERNAL', conf_type='bool')
    eureka_conf = 'eureka_ip' if not external else 'eureka_external_ip'
    global eureka_ip
    try:
        eureka_ip = apollo_envs_conf(apollo_client, eureka_conf)
    except Exception as e:
        logger.error(
            "Getting eureka addr configures from apollo or temp file failed! {}"
            .format(e.__repr__()))
        exit(1)
    global ek_client
    if not ek_client:
        ek_client = get_client(eureka_ip)
    if not ek_client: return None

    return ek_client
def redis_conf(apollo_client, ):
    """
            从apollo获取缓存配置
            :return: dict
    """
    external = env_file_conf('EXTERNAL', conf_type=bool, default=False)

    redis_host_conf = 'redis_host' if not external else "redis_external_host"
    redis_host = apollo_envs_conf(apollo_client, redis_host_conf)
    redis_port = apollo_envs_conf(apollo_client, "redis_port")
    redis_db = apollo_envs_conf(apollo_client, "redis_db")
    redis_passwd = apollo_envs_conf(apollo_client, "redis_passwd")

    logger.debug("Debug redis info {} {} {}".format(redis_host, redis_port,
                                                    redis_db))

    return {
        "host": redis_host,
        "port": redis_port,
        "db": redis_db,
        "passwd": redis_passwd
    }
Esempio n. 7
0
def prometh_hosts():
    """
            从apollo查询prome地址
            :return: list
    """
    external = env_file_conf('EXTERNAL', conf_type='bool')
    if not external:
        conf_name = 'prome_host'
    else:
        conf_name = 'prome_external_host'
    if external:
        print('Conneting to apollo from external net!')
    apollo_query = ApolloQuery()
    prome_hosts = None
    try:
        prome_hosts = apollo_query.apo_config(conf_name).split(',')
    except Exception as e:
        print('Getting prometheus addr from apollo failed!{}'.format(
            e.__str__()))
        exit(1)

    print('Debug prometheus hosts: {}'.format(prome_hosts))

    return prome_hosts
def cache_services(apollo_client, applications_info):
    """
            服务注册到缓存
            :param app_infos: service_info返回的所有服务状态信息
            :return:
    """
    env_type = env_file_conf('ENV_TYPE', default='DEV')
    service_lists = []
    for app_name, app_info_list in applications_info.items():
        for app_info in app_info_list:
            logger.info(app_info)
            service_lists.append({"product": app_info["product"], "service": app_info["service"], "env_type": env_type})
    cached_services = cache_get(apollo_client, 'services')
    delete_services = unregister_services_conf()
    if not cached_services:
        services = service_lists
        unavailable_services = set()
    else:
        logger.debug("{} {} {}".format(cached_services, service_lists, delete_services))
        cached_services_str_list = list(map(lambda service_dict: '_'.join(service_dict.values()), cached_services))
        service_str_lists = list(map(lambda service_dict: '_'.join(service_dict.values()), service_lists))
        delete_str_services = list(map(lambda service_dict: '_'.join(service_dict.values()), delete_services))
        all_services = set(cached_services_str_list).union(set(service_str_lists)).difference(set(delete_str_services))
        # 异常服务集合
        unavailable_services = \
            set(cached_services_str_list).difference(set(service_str_lists)).difference(set(delete_str_services))
        services = list(map(lambda service_str: {
                "product": service_str.split('_')[0],
                "service": service_str.split('_')[1],
                "env_type": service_str.split('_')[2]
            }, all_services))

    cache_set(apollo_client, 'services', services)
    logger.info("Services unavailable are: {}".format(unavailable_services))
    logger.info("Service cache updated!")
    return unavailable_services
Esempio n. 9
0
        print("Service {} of product {} in env {} avalibility:{}".format(service, product, env_type, service_ava))

        return service_ava

    def sql_genarate(self, table, product, service, ava):
        env_type = self.query_env()
        yesterday = datetime.date.today() + datetime.timedelta(-1)
        sql = 'INSERT INTO %s values(%s, %s, %s, %s, %s)' % \
              (table, product, env_type, service, yesterday, ava)

        return sql


if __name__ == '__main__':

    env_type = env_file_conf('ENV_TYPE').upper() if env_file_conf('ENV_TYPE') else "DEV"
    external = env_file_conf('EXTERNAL')

    apollo_query = ApolloQuery()
    host_conf_name = 'mysql_host'
    if external.upper() != "FALSE":
        host_conf_name = 'mysql_external_host'

    db_host = apollo_query.apo_config(host_conf_name)
    db_port = apollo_query.apo_config('mysql_port')
    database = apollo_query.apo_config('mysql_db')
    table = apollo_query.apo_config('mysql_table')
    db_user = apollo_query.apo_config('mysql_user')
    db_passwd = apollo_query.apo_config('mysql_passwd')

    service_dict = {}
Esempio n. 10
0
#!env/bin/python
# -*- coding:utf-8 -*-

import threading

from utils.get_configure import env_file_conf

eureka_client = None
env_type = env_file_conf('ENV_TYPE', default='DEV')


class MetricThread(threading.Thread):
    def __init__(self, func, args):
        super(MetricThread, self).__init__()
        self.func = func
        self.args = args

    def run(self):
        """
            线程执行
            :return:
        """
        self.result = self.func(*self.args)

    def get_result(self):
        """
                返回线程结果
                :return: list
        """
        try:
            return self.result