From 740149ceb83e332ac59612a0d9789cbb422e64e1 Mon Sep 17 00:00:00 2001 From: hanshuaikang <1758504262@qq.com> Date: Fri, 15 Sep 2023 16:39:26 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E7=9B=91=E6=8E=A7(=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E5=B1=8F=E8=94=BD=E8=8C=83=E5=9B=B4)=E6=94=AF=E6=8C=81ipv6?= =?UTF-8?q?=E5=92=8Cipv4=E4=B8=BB=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collections/sites/open/monitor/base.py | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/pipeline_plugins/components/collections/sites/open/monitor/base.py b/pipeline_plugins/components/collections/sites/open/monitor/base.py index a37fd45581..aa06985350 100644 --- a/pipeline_plugins/components/collections/sites/open/monitor/base.py +++ b/pipeline_plugins/components/collections/sites/open/monitor/base.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import ipaddress from functools import partial from django.conf import settings @@ -37,21 +38,46 @@ def build_request_body(self, begin_time, bk_biz_id, shied_type, dimension_config def get_ip_dimension_config(self, scope_value, bk_biz_id, username): ip_list = scope_value.split(",") if settings.ENABLE_IPV6: - hosts = cmdb.get_business_host_ipv6( + # 开启了IPV6 要同时查ipv6和ipv4 + ipv4_list = [] + ipv6_list = [] + + for ip in ip_list: + p_address = ipaddress.ip_address(ip) + if p_address.version == 6: + ipv6_list.append(ip) + else: + ipv4_list.append(ip) + + ip_v6_hosts = cmdb.get_business_host_ipv6( username=username, bk_biz_id=bk_biz_id, supplier_account=Business.objects.supplier_account_for_business(bk_biz_id), host_fields=["bk_host_id", "bk_cloud_id", "bk_host_innerip", "bk_host_innerip_v6"], - ip_list=ip_list, + ip_list=ipv6_list, ) # 监控接口不支持 host_id, 进支持 ip - host_without_innerip = [host for host in hosts if host["bk_host_innerip"] == ""] + host_without_innerip = [host for host in ip_v6_hosts if host["bk_host_innerip"] == ""] if host_without_innerip: raise Exception( _("主机[{}]innerip字段为空,蓝鲸监控接口仅支持通过该字段进行ip传参").format( ",".join([str(host["bk_host_id"]) for host in host_without_innerip]) ) ) + + ip_v4_hosts = cmdb.get_business_host( + username=username, + bk_biz_id=bk_biz_id, + supplier_account=Business.objects.supplier_account_for_business(bk_biz_id), + host_fields=["bk_host_id", "bk_cloud_id", "bk_host_innerip"], + ip_list=ipv4_list, + ) + + if not ip_v4_hosts: + raise Exception(_("当前业务下未查询到ip信息, 请检查ip地址是否填写正确:{}").format(ipv4_list)) + + hosts = ip_v4_hosts + ip_v6_hosts + else: hosts = cmdb.get_business_host( username=username,