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 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 items_del(): """获取主机上所有监控项,然后删除""" zbix = Moniter() zbix.hostid = "10452" item = zbix.item_get() iems = [i.get('itemid') for i in item] zbix.item_del(iems)
def item_upde_(): """ 更新主机上所有监控项的类型,为主动模式 type 7 https://www.zabbix.com/documentation/4.0/manual/api/reference/item/object#item :return: """ zbix = Moniter() zbix.hostid = "10452" all_itemid = (i.get('itemid') for i in zbix.item_get()) for i in all_itemid: try: zbix.item_update(i, type=7) # zbix.item_update(i, delay="60s") except Exception as e: print(e)
def _create_test(): """未完成, 搜索""" zbix = Moniter() hostid = zbix.get_host_name("Meboth-IDC-Switch-1") all_graphics_received = zbix.item_get(hostid, key_="Bits received") import re sind = re.compile( # r"Interface GigabitEthernet1/0/.*\(GigabitEthernet1/0/[1-9]{1}.* Interface\): Bits received") # r"Interface GigabitEthernet.*\(D_BJ-MiBuKeJi-FW:.*\): Bits received") r"D_BJ-MiBuKeJi-FW") # r"Interface GigabitEthernet1/0/(10|[1-9])\(D_BJ-MiBuKeJi-FW:1-G1/0/[1-9]{1}.*\): Bits sent") # r"Interface GigabitEthernet1/0/(10|[1-9])\(D_BJ-MiBuKeJi-FW:1-G1/0/.*\): Bits received") # r"Interface GigabitEthernet(10|[1-9]){1}/0/48\(D_BJ-MiBuKeJi-FW:(10|[1-9])-G1/0/15\): Bits received") # "nterface GigabitEthernet1/0/(10|[1-9])\(D_BJ-MiBuKeJi-FW:1-G1/0/.*\): Bits received" for i in all_graphics_received: if sind.findall(i.get('name')): # print(i.get('name')) print(i)
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