def create_batch_graph(): """创建图形""" urls_info = ret_urls() zbix = Moniter() zbix.hostid = "10452" for it in urls_info: try: items = zbix.item_get(key_=it.url) graph_items = [{'itemid': i.get('itemid')} for i in items] colors = [{ 'color': '199C0D' }, { 'color': 'F63100' }, { 'color': '2774A4' }, { 'color': 'F7941D' }] for i in range(len(colors)): graph_items[i].update(colors[i]) res = zbix.graph_create(name=it.name, gitems=graph_items) print(res) except Exception as e: print(e)
def create_batch_item(): """批量创建 监控项目 名称 和键值""" zbix = Moniter() all_state = [("health_total_{}", "health_total[{}]"), ("health_code_{}", "health_code[{}]"), ("health_connect_{}", "health_connect[{}]"), ("health_start_{}", "health_start[{}]")] from bin.read_xlsx import ret_urls urls = ret_urls() for i in urls: for name in all_state: try: itemname = name[0].format(i.url[0]) key_name = name[1].format(i.url[0]) if "code" in name[0]: zbix.item_create(item_name=itemname, key_name=key_name, value_type=3) else: zbix.item_create(item_name=itemname, key_name=key_name) except Exception as e: print(e)
def crate_mhost_grpaph(): urls_info = ret_urls() zbix = Moniter() zbix.hostid = "10452" for it in urls_info: try: items_domain = zbix.item_get(name=it.url) items_real = (zbix.item_get(name=i) for i in it.real_host) print(list(items_real)) graph_items = [{ 'itemid': i.get('itemid') } for i in items_domain] print(graph_items) colors = [{ 'color': '199C0D' }, { 'color': 'F63100' }, { 'color': '2774A4' }, { 'color': 'F7941D' }] for i in range(len(colors)): graph_items[i].update(colors[i]) res = zbix.graph_create(name=it.name, gitems=graph_items) # print(res) except Exception as e: print(e) pass
def create_ms_graph(): """域名和主机的都在一个图形中""" urls_info = ret_urls() zbix = Moniter() zbix.hostid = "10452" s = zbix.graph_get() url_info = namedtuple( 'urls_info', ['name', 'url', 'return_msg', 'code', 'real_host']) it = url_info(name='bike-bike', url=['https://me.baojia.com/'], return_msg=['没有相关操作权限'], code=[200], real_host=[ '10.1.11.140:8080', '10.1.11.220:8080', '10.1.11.221:8080' ]) for it in urls_info: # 获取所有 域名下 后端的真实主机 try: items_domainip_color = [ { 'itemid': i.get('itemid'), 'yaxisside': 1 if "health_code_http" in i.get('name') else 0 } for i in zbix.item_get(name=it.url[0]) if re.findall("{}$".format(it.url[0]), i.get("name")) ] # 后端真实主机监控 items_real = (zbix.item_get(name=_) for _ in it.real_host) [ items_domainip_color.append({ 'itemid': item.get('itemid'), 'yaxisside': 1 if "health_code" in item.get('name') else 0 }) for i in items_real for item in i if item.get('name').startswith('real') ] all_items_color = [{ "color": _ } for _ in zbix.choose_color[:len(items_domainip_color)]] for i in range(len(items_domainip_color)): items_domainip_color[i].update(all_items_color[i]) print(items_domainip_color) res = zbix.graph_create(name=f"{it.name}_{it.url[0]}", gitems=items_domainip_color) # print(res) except Exception as e: print(e) pass
def crate_batch_mhost(): """创建 监控项 多主机 toal code""" zbix = Moniter() hostid = zbix.get_host_id(name="1-11-57") zbix.hostid = hostid graph = zbix.graph_get() urls_info = ret_urls() # pprint(urls_info) def code_toal_string(real_backend=None): """生成监控项所用的,key , code total""" return [ ("real_health_code_{}", "real_health_code[{},{}]"), ("real_health_total_{}", "real_health_total[{},{}]") ] if real_backend else [("health_code_{}", "health_code[{}]"), ("health_total_{}", "health_total[{}]")] # else [("health_total_{}", "health_total[{}]"), ("health_code_{}", "health_code[{}]"), # ("health_connect_{}", "health_connect[{}]"), ("health_start_{}", "health_start[{}]")] def item_create_(itemname, key_name): if "code" in itemname: zbix.item_create(item_name=itemname, key_name=key_name, value_type=3) else: zbix.item_create(item_name=itemname, key_name=key_name) # items = zbix.item_get(key_=it.url) for it in urls_info: # if it.url[0] == 'https://me.baojia.com/': for name in code_toal_string(): try: itemname_key = map( lambda x: x.format(it.url[0]) if "[" not in x else x.format(quote_plus(it.url[0])), name) item_create_(*itemname_key) except Exception as e: print(e) if len(it.real_host) >= 1: for i in it.real_host: for name in code_toal_string(real_backend=True): try: itemname = name[0].format(i) key_name = name[1].format(quote_plus(it.url[0]), quote_plus(i)) item_create_(itemname, key_name) except Exception as e: print(e)
def run(): all_urls = ret_urls() tasks = [] for i in all_urls: tasks.append(tornado_curl.fetch_async(i)) event_loop = asyncio.get_event_loop() event_loop.run_until_complete(asyncio.gather(*tasks)) event_loop.close() with open( os.path.join(BASE_DIR, 'url_time_info.json'), mode='w', ) as f: json.dump(DICT_INFO, f, sort_keys=True, indent=4, ensure_ascii=False)
def crete_mhost_graph_(): """创建图形, 域名和 后端真实主机分别创建图形""" urls_info = ret_urls() zbix = Moniter() zbix.hostid = "10452" s = zbix.graph_get() url_info = namedtuple( 'urls_info', ['name', 'url', 'return_msg', 'code', 'real_host']) it = url_info(name='bike-bike', url=['https://me.baojia.com/'], return_msg=['没有相关操作权限'], code=[200], real_host=[ '10.1.11.140:8080', '10.1.11.220:8080', '10.1.11.221:8080' ]) def url_conversion(url, item_name): regular = re.compile( "(?:https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]" ) if regular.search(item_name): re_item_url = regular.findall(item_name) if unquote_plus(re_item_url[0]) == unquote_plus(url): return True else: return item_name for it in urls_info: # 获取所有 域名下 后端的真实主机 try: # 域名 url # items_domainip_color = [ # {'itemid': i.get('itemid'), 'yaxisside': 1 if "health_code_http" in i.get('name') else 0} for i in # zbix.item_get(name=it.url[0])] print(it.url[0]) items_domainip_color = [ { 'itemid': i.get('itemid'), 'yaxisside': 0 if "health_code_http" in i.get('name') else 1, "drawtype": 5 if "health_code_http" in i.get('name') else 1 } for i in zbix.item_get(name=it.url[0]) if url_conversion(it.url[0], i.get('name')) ] all_items_color = [{ "color": _ } for _ in zbix.choose_color[:len(items_domainip_color)]] for i in range(len(items_domainip_color)): items_domainip_color[i].update(all_items_color[i]) pprint(items_domainip_color) zbix.graph_create(name=f"{it.name}_{it.url[0]}", gitems=items_domainip_color) # 后端真实主机监控 items_real = (zbix.item_get(name=_) for _ in it.real_host) # realend_items = [ # {'itemid': item.get('itemid'), 'yaxisside': 1 if "health_code" in item.get('name') else 0} # for i in items_real for item in i] yaxisside_code = lambda x: 0 if "health_code" in x else 1 drawtype_code = lambda x: 5 if "health_code" in x else 1 realend_items = [{ 'itemid': item.get('itemid'), 'yaxisside': yaxisside_code(item.get('name')), 'drawtype': drawtype_code(item.get('name')) } for i in items_real for item in i if item.get('name').startswith('real')] real_end_color = [{ "color": _ } for _ in zbix.choose_color[:len(realend_items)]] for i in range(len(realend_items)): realend_items[i].update(real_end_color[i]) zbix.graph_create(name=f"realend_{it.name}_{it.url[0]}", gitems=realend_items) except Exception as e: print(e) pass
def domain_search(): # 域名提取 urls_info = ret_urls() for i in urls_info: res = urlparse(i.url[0]) print(res)