def get_excep_result():
    username = '******'
    password = '******'
    mongos_host = '10.68.112.11'
    mongos_port = 22001
    db_name = 'aihc'
    coll_name = 'stat_excep'
    period = 3600
    end_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    start_time = (
        datetime.datetime.now() +
        datetime.timedelta(minutes=-period)).strftime('%Y-%m-%d %H:%M:%S')
    host_mongo_query_exceptions = {}
    client = mongo_writer.auth(username, password, mongos_host, mongos_port)
    result_list = exception_stat_query.query_excep(client, db_name, coll_name,
                                                   start_time, end_time,
                                                   period)
    logger.info(f'result_list is {result_list}')
    if len(result_list) != 0:
        for doc in result_list:
            id_doc = doc.get("_id")
            result_doc = id_doc
            result_doc.update({'count': doc.get('count')})
            new_id = id_doc.get('Ip') + '—' + id_doc.get('FunctionName')
            result_doc.update({'_id': new_id})
            logger.info(f'result_doc is {result_doc}')

            if host_mongo_query_exceptions is not None and host_mongo_query_exceptions.__contains__(
                    new_id):
                excep_doc = host_mongo_query_exceptions.get(new_id)
                context_old = excep_doc.get('context')
                context_new = result_doc.get('ExceptionType') + "," + str(
                    result_doc.get('count'))
                excep_doc.update({'context': context_old + ',' + context_new})
                print(excep_doc)
                excep_doc.update({
                    'sumCount':
                    (result_doc.get('count') + excep_doc.get('sumCount'))
                })
            else:
                result_doc.update({
                    'context':
                    result_doc.get('ExceptionType') + "," +
                    str(result_doc.get('count'))
                })
                result_doc.update({'sumCount': result_doc.get('count')})
                host_mongo_query_exceptions.update({new_id: result_doc})
    excep_result_value = host_mongo_query_exceptions.values()
    excep_result = sorted(excep_result_value,
                          key=lambda x: x['Ip'],
                          reverse=False)
    logger.info(f'excep_result is {excep_result}')
    return excep_result
Ejemplo n.º 2
0
logging.basicConfig(level=logging.DEBUG,
                    # filename='/Users/mtr/PycharmProjects/mongoQuery/resource/log/recv_stat_load.log',
                    filemode='a',
                    format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s')


# rollback stat 查询,统计各台主机rollback文件数量
# 统计InputTime为当天时间的rollback记录
if __name__ == '__main__':
    username = '******'
    password = '******'
    mongos_host = '10.19.85.33'
    mongos_port = 34000
    db_name = 'test'
    coll_name = 'stat_rollback'
    client = mongo_writer.auth(username, password, mongos_host, mongos_port)
    start_time = datetime.datetime.strptime('2021-03-08 00:00:00', '%Y-%m-%d %H:%M:%S')
    period = 1
    end_time = (start_time + datetime.timedelta(days=period)).strftime("%Y-%m-%d %H:%M:%S")
    aggregate_sql = ('[{"$match":{"InputTime":{"$gte":"starttime","$lte":"endtime"}}},'
                     '{"$group":{"_id":null,'
                     '"rollbackfilecount":{"$sum":1}}}]').replace("starttime", str(start_time)).\
        replace("endtime", str(end_time))
    logging.info(f'aggregate_sql is {aggregate_sql}')
    aggregate_sql_list = json.loads(aggregate_sql)
    result_list = mongo_writer.conn_aggregate(client, db_name, coll_name, aggregate_sql_list)
    logging.info(f'result_list is {result_list}')
    if len(result_list) == 0:
        rollbackfilecount = 0
    else:
        rollbackfilecount = result_list[0].get('rollbackfilecount')
Ejemplo n.º 3
0
def mongo_client_auth(username, password, mongos_host, mongos_port):
    client = mongo_writer.auth(username, password, mongos_host, mongos_port)
    return client