Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    def graphitem_get_(self):
        zbix = Moniter()
        zbix.hostid = "10452"

        graph = zbix.graph_get()
        s = (i for i in graph if 'bike-bike' in i.get('name'))
        for i in s:
            print(i)

        mutil_cr = Create_Batch()
        mutil_cr.crete_mhost_graph_()
        pprint(zbix.graphitem_get("3196"))
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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