""" 获取各指标(卡顿、点播、直播、媒体丢包、 二层丢帧、断流、内存、RTP丢包) 不同媒体类型(mdiType=点播、高清直播、 2k高清直播、4k高清直播、EPG页面)的质差用户数与总用户数的比值 时间段:2018-5-24 6:10 通过索引m-stb统计用户总数, 通过iptv-monitor-m-gather-apk-yyyyMMdd 获取各指标质差用户数 南充 111000 广安 113000 """ from elasticsearch import Elasticsearch import os import re es = Elasticsearch(['localhost'], http_auth=('iptvmoni', 'V^8rt32e'), port=9200, timeout=50000) def get_iptv_monitor_m_mlr(time_, localnet_id, field, value, start, end): """ 获取不同指标类型的媒体丢包质差用户数 :param time_: 索引时间 :param localnet_id: 本地网id :param field: 媒体丢包字段 :param value: 媒体丢包衡量质差标准 :param start: 开始时间 :param end: 结束时间 :return: """ index_ = "iptv-monitor-m-gather-apk-{0}-{1}".format(time_, localnet_id) try: rs = es.search(index=index_, body={ "size": 0, "query": { "bool": { "must": [ {"term": { "obd2_localnet_id": { "value": localnet_id } }}, { "range": { "uploadTime": { "gte": start, "lte": end, "format": "yyyy-MM-dd HH:mm", "time_zone": "+08:00" } } }, { "nested": { "path": "mdiNested", "query": { "range": { "mdiNested.mdiMLR": { "gt": 0 } } } } } ] } }, "aggs": { "1": { "nested": { "path": "mdiNested" }, "aggs": { "2": { "terms": { "field": "mdiNested.mdiType", "size": 5 }, "aggs": { "3": { "reverse_nested": {}, "aggs": { "4": { "cardinality": { "field": "iptvAccount" } } } } } } } } } }) return rs except: return -1 def get_m_stb_usr_account(localnet_id): """ 获取本地网对应的总用户数 :param localnet_id: :return: """ index_ = "m-stb" try: rs = es.search(index=index_, body={ "size": 0, "query": { "term": { "2fg_localnet_id.keyword": { "value": localnet_id } } }, "aggs": { "1": { "cardinality": { "field": "iptv_ua.keyword" } } } }) return rs['aggregations']['1']['value'] except: return -1 def get_iptv_monitor_m(time_, localnet_id, field, value, start, end): """ 获取各个指标对应的质差用户数 :param time_: 索引时间 :param localnet_id: 本地网id :param field: 指标字段 :param value: 衡量指标质差的值 :param start: 开始时间 :param end: 结束时间 :return: """ if field.upper() == "mdiMLR".upper(): try: rs = get_iptv_monitor_m_mlr(time_, localnet_id, field, value, start, end) return rs except: return -1 index_ = "iptv-monitor-m-gather-apk-{0}-{1}".format(time_, localnet_id) try: rs = es.search(index=index_, body={ "size": 0, "query": { "bool": { "must": [ {"term": { "obd2_localnet_id": { "value": localnet_id } }}, { "range": { "uploadTime": { "gte": start, "lte": end, "format": "yyyy-MM-dd HH:mm", "time_zone": "+08:00" } } }, { "range": { field: { "gt": value } } } ] } }, "aggs": { "1": { "nested": { "path": "mdiNested" }, "aggs": { "2": { "terms": { "field": "mdiNested.mdiType", "size": 5 }, "aggs": { "3": { "reverse_nested": {}, "aggs": { "4": { "cardinality": { "field": "iptvAccount" } } } } } } } } } }) return rs except: return -1 def kind_dict(): """ 八个指标及对应的指标值 :return: """ dict_ = { "memRate": 80, "frameLR": 0, "responseDelay": 1500, "channelSwitchDelay": 1000, "streamBreak": 0, "kaNum": 0, "rtpLossRate": 0, "mdiMLR": 0 } return dict_ if __name__ == '__main__': time_ = "20180524" # 南充 111000 广安 113000 localnet_ids = ["111000", "113000"] # 南充总用户数 广安总用户数 total_usr = [南充总用户数, 广安总用户数] start = "2018-05-24 18:00" end = "2018-05-24 22:00" i = 0 for localnet_id in localnet_ids: localnet_id = localnet_id.strip() path = "E:\\公司\\案例数据采集\\olt告警案例分析\\{0}\\{1}".format(time_, localnet_id) if os.path.exists(path): pass else: os.makedirs(path) for key, value_dict in kind_dict().items(): field = key value = value_dict write_fact_path = "E:\\公司\\案例数据采集\\olt告警案例分析\\{0}\\{1}\\{2}__alarm.txt".format(time_, localnet_id, field) # total_usr = get_m_stb_usr_account(localnet_id) total_usr = total_usr[i] rs = get_iptv_monitor_m(time_, localnet_id, field, value, start, end) with open(write_fact_path, "w") as f: dict_key = {} for hit in rs['aggregations']['1']['2']['buckets']: key = hit['key'] value = hit['3']['4']['value'] dict_key.setdefault(key, value) for i in range(6): value_ = dict_key.get(str(i)) if value_ is None: value_ = 0 value_2 = round(value_ / total_usr, 6) f.write(str(value_2) + ",") f.write("\n") f.close() i += 1