From e022766f7b91dd1d366439ca6ce2b8d992db475e Mon Sep 17 00:00:00 2001 From: durant <826035498@qq.com> Date: Wed, 25 Sep 2024 19:24:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(backend):=20=E7=8B=AC=E7=AB=8B=E6=89=98?= =?UTF-8?q?=E7=AE=A1=E4=B8=9A=E5=8A=A1=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E7=AD=96=E7=95=A5=E7=9B=AE=E6=A0=87=E8=8C=83?= =?UTF-8?q?=E5=9B=B4=20#7128?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../json_files => db_monitor}/format.py | 0 dbm-ui/backend/db_monitor/models/alarm.py | 119 +++++++++++++++++- dbm-ui/backend/db_monitor/utils.py | 112 ++++++++++++++++- .../local_tasks/db_monitor.py | 105 +--------------- dbm-ui/backend/dbm_init/services.py | 114 ++--------------- dbm-ui/backend/flow/utils/cc_manage.py | 13 +- .../dbm_init/test_auto_create_services.py | 2 +- 7 files changed, 247 insertions(+), 218 deletions(-) rename dbm-ui/backend/{dbm_init/json_files => db_monitor}/format.py (100%) diff --git a/dbm-ui/backend/dbm_init/json_files/format.py b/dbm-ui/backend/db_monitor/format.py similarity index 100% rename from dbm-ui/backend/dbm_init/json_files/format.py rename to dbm-ui/backend/db_monitor/format.py diff --git a/dbm-ui/backend/db_monitor/models/alarm.py b/dbm-ui/backend/db_monitor/models/alarm.py index d7530ee73e..64cb113038 100644 --- a/dbm-ui/backend/db_monitor/models/alarm.py +++ b/dbm-ui/backend/db_monitor/models/alarm.py @@ -12,6 +12,7 @@ import datetime import json import logging +import os from collections import defaultdict from typing import Any, Dict, List @@ -35,14 +36,25 @@ DEFAULT_ALERT_NOTICE, PLAT_PRIORITY, TARGET_LEVEL_TO_PRIORITY, + TPLS_ALARM_DIR, AlertSourceEnum, DutyRuleCategory, PolicyStatus, TargetLevel, + TargetPriority, +) +from backend.db_monitor.exceptions import ( + BkMonitorDeleteAlarmException, + BkMonitorSaveAlarmException, + BuiltInNotAllowDeleteException, ) -from backend.db_monitor.exceptions import BkMonitorDeleteAlarmException, BuiltInNotAllowDeleteException from backend.db_monitor.tasks import update_app_policy -from backend.db_monitor.utils import bkm_delete_alarm_strategy, bkm_save_alarm_strategy, render_promql_sql +from backend.db_monitor.utils import ( + bkm_delete_alarm_strategy, + bkm_save_alarm_strategy, + get_dbm_autofix_action_id, + render_promql_sql, +) from backend.exceptions import ApiError __all__ = ["NoticeGroup", "AlertRule", "RuleTemplate", "DispatchGroup", "MonitorPolicy", "DutyRule"] @@ -996,6 +1008,109 @@ def get_dbha_policies(cls): cls.objects.filter(details__labels__contains=["/DBM_DBHA/"]).values_list("monitor_policy_id", flat=True) ) + @classmethod + def sync_plat_monitor_policy(cls, action_id=None, db_type=None, force=False): + if action_id is None: + action_id = get_dbm_autofix_action_id() + skip_dir = "v1" + now = datetime.datetime.now(timezone.utc) + logger.warning("[sync_plat_monitor_policy] sync bkm alarm policy start: %s", now) + + # 逐个json导入,本地+远程 + updated_policies = 0 + for root, dirs, files in os.walk(TPLS_ALARM_DIR): + if skip_dir in dirs: + dirs.remove(skip_dir) + + for alarm_tpl in files: + + with open(os.path.join(root, alarm_tpl), "r", encoding="utf-8") as f: + logger.info("[sync_plat_monitor_policy] start sync bkm alarm tpl: %s " % alarm_tpl) + try: + template_dict = json.loads(f.read()) + # 监控API不支持传入额外的字段 + template_dict.pop("export_at", "") + policy_name = template_dict["name"] + except json.decoder.JSONDecodeError: + logger.error("[sync_plat_monitor_policy] load template failed: %s", alarm_tpl) + continue + + # 如指定db_type,只同步指定db_type的策略(跳过非指定db_type的策略) + if db_type is not None and template_dict["db_type"] != db_type: + continue + + deleted = template_dict.pop("deleted", False) + + if not template_dict.get("details"): + logger.error(("[sync_plat_monitor_policy] template %s has no details" % alarm_tpl)) + continue + + # patch template + labels = list(set(template_dict["details"]["labels"])) + template_dict["details"]["labels"] = labels + template_dict["details"]["name"] = policy_name + template_dict["details"]["priority"] = TargetPriority.PLATFORM.value + # 平台策略仅开启基于分派通知 + template_dict["details"]["notice"]["options"]["assign_mode"] = ["by_rule"] + for label in labels: + if label.startswith("NEED_AUTOFIX") and action_id is not None: + template_dict["details"]["actions"] = [ + { + "config_id": action_id, + "signal": ["abnormal"], + "user_groups": [], + "options": { + "converge_config": { + "is_enabled": False, + "converge_func": "skip_when_success", + "timedelta": 60, + "count": 1, + } + }, + } + ] + + policy = MonitorPolicy(**template_dict) + + policy_name = policy.name + logger.info("[sync_plat_monitor_policy] start sync bkm alarm policy: %s " % policy_name) + try: + synced_policy = MonitorPolicy.objects.get(bk_biz_id=policy.bk_biz_id, name=policy_name) + + if deleted: + logger.info("[sync_plat_monitor_policy] delete old alarm: %s " % policy_name) + synced_policy.delete() + continue + + if synced_policy.version >= policy.version and not force: + logger.info("[sync_plat_monitor_policy] skip same version alarm: %s " % policy_name) + continue + + for keeped_field in MonitorPolicy.KEEPED_FIELDS: + setattr(policy, keeped_field, getattr(synced_policy, keeped_field)) + + policy.details["id"] = synced_policy.monitor_policy_id + logger.info("[sync_plat_monitor_policy] update bkm alarm policy: %s " % policy_name) + except MonitorPolicy.DoesNotExist: + logger.info("[sync_plat_monitor_policy] create bkm alarm policy: %s " % policy_name) + + try: + # fetch targets/test_rules/notify_rules/notify_groups from parent details + for attr, value in policy.parse_details().items(): + setattr(policy, attr, value) + + policy.save() + updated_policies += 1 + logger.error("[sync_plat_monitor_policy] save bkm alarm policy success: %s", policy_name) + except BkMonitorSaveAlarmException as e: + logger.error("[sync_plat_monitor_policy] save bkm alarm policy failed: %s, %s ", policy_name, e) + + logger.warning( + "[sync_plat_monitor_policy] finish sync bkm alarm policy end: %s, update_cnt: %s", + datetime.datetime.now(timezone.utc) - now, + updated_policies, + ) + @staticmethod def bkm_search_event( bk_biz_ids: list, diff --git a/dbm-ui/backend/db_monitor/utils.py b/dbm-ui/backend/db_monitor/utils.py index dc0efb9651..b243a03a0e 100644 --- a/dbm-ui/backend/db_monitor/utils.py +++ b/dbm-ui/backend/db_monitor/utils.py @@ -7,13 +7,20 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ +import copy +import json import logging +import os import re +from django.utils.translation import gettext as _ + from backend import env -from backend.components import BKMonitorV3Api -from backend.db_monitor.constants import AUTOFIX_ACTION_NAME +from backend.components import BKLogApi, BKMonitorV3Api +from backend.db_monitor.constants import AUTOFIX_ACTION_NAME, AUTOFIX_ACTION_TEMPLATE from backend.db_monitor.exceptions import BkMonitorDeleteAlarmException, BkMonitorSaveAlarmException +from backend.db_monitor.format import JsonConfigFormat +from backend.exceptions import ApiError logger = logging.getLogger("root") @@ -109,3 +116,104 @@ def get_dbm_autofix_action_id() -> int: if action["name"] == AUTOFIX_ACTION_NAME: action_id = action["id"] return action_id + + +def create_bkmonitor_action() -> int: + """ + 创建监控处理套餐 + """ + action_id = get_dbm_autofix_action_id() + action_config = copy.deepcopy(AUTOFIX_ACTION_TEMPLATE) + + if action_id is None: + BKMonitorV3Api.save_action_config(action_config) + else: + action_config["id"] = action_id + BKMonitorV3Api.edit_action_config(action_config) + return action_id + + +def create_bklog_collector(startswith: str = ""): + bklog_json_files_path = "backend/dbm_init/json_files/bklog" + for filename in os.listdir(bklog_json_files_path): + if not filename.endswith(".json"): + continue + if not filename.startswith(startswith): + continue + + # 读取日志采集项json文件,并渲染配置 + with open(os.path.join(bklog_json_files_path, filename), "r", encoding="utf-8") as file: + try: + bklog_params = json.load(file) + except json.decoder.JSONDecodeError as err: + logger.error(f"[create_bklog_collector] Failed to load json: {filename}, {err}") + raise err + log_name = filename.split(".")[0] + # 优先获取指定了 log_name 的 formatter + if hasattr(JsonConfigFormat, f"format_{log_name}"): + bklog_params = JsonConfigFormat.format(bklog_params, f"format_{log_name}") + # 根据不同 db 类型,指定对应的 formatter,主要是区分采集目标 + elif "mysql" in filename: + bklog_params = JsonConfigFormat.format(bklog_params, JsonConfigFormat.format_mysql.__name__) + elif "redis" in filename: + bklog_params = JsonConfigFormat.format(bklog_params, JsonConfigFormat.format_redis.__name__) + elif "mssql" in filename: + bklog_params = JsonConfigFormat.format(bklog_params, JsonConfigFormat.format_mssql.__name__) + else: + logger.warning(_("格式化函数{log_name}不存在(如果无需格式化json可忽略)").format(log_name=log_name)) + + # 针对特殊需求修改请求参数 + if hasattr(JsonConfigFormat, f"custom_modify_{log_name}"): + bklog_params = JsonConfigFormat.custom_modify(bklog_params, f"custom_modify_{log_name}") + # 如果存在对应的环境变量设置了日志自定义的保留天数,则进行更新 + retention = getattr(env, f"BKLOG_{log_name.upper()}_RETENTION", "") or env.BKLOG_DEFAULT_RETENTION + bklog_params["retention"] = retention + # 自定义了 ES 存储集群,则指定 storage_cluster_id + if env.BKLOG_STORAGE_CLUSTER_ID: + bklog_params["storage_cluster_id"] = env.BKLOG_STORAGE_CLUSTER_ID + # 如果集群支持冷热数据,则补充 allocation_min_days,为 retention 的一半即可 + if env.BKLOG_CLUSTER_SUPPORT_HOT_COLD: + bklog_params["allocation_min_days"] = retention // 2 + + # 获取当前采集项的列表 + data = BKLogApi.list_collectors( + {"bk_biz_id": env.DBA_APP_BK_BIZ_ID, "pagesize": 500, "page": 1}, use_admin=True + ) + collectors_name__info_map = {collector["collector_config_name_en"]: collector for collector in data["list"]} + + # 判断采集项是否重复创建 + collector_name = bklog_params["collector_config_name_en"] + data = BKLogApi.pre_check( + { + "bk_biz_id": env.DBA_APP_BK_BIZ_ID, + "collector_config_name_en": collector_name, + }, + use_admin=True, + ) + if not data["allowed"]: + # 采集项已创建,对采集项进行更新 + try: + collector_config_id = collectors_name__info_map[collector_name]["collector_config_id"] + except KeyError: + logger.error(_("采集项{collector_name}被创建后删除,暂无法自动重建,请联系管理员处理。").format(collector_name=collector_name)) + continue + bklog_params.update({"collector_config_id": collector_config_id}) + logger.info(_("采集项{collector_name}已创建, 对采集项进行更新...").format(collector_name=collector_name)) + try: + BKLogApi.fast_update(params=bklog_params, use_admin=True) + except ApiError as err: + logger.error( + _("采集项{collector_name}更新失败,请联系管理员。错误信息:{err}").format(collector_name=collector_name, err=err) + ) + + continue + + # 创建采集项 + try: + data = BKLogApi.fast_create(params=bklog_params, use_admin=True) + logger.info(_("采集项创建成功,相关信息: {data}").format(data=data)) + except ApiError as err: + # 当前采集项创建失败默认不影响下一个采集项的创建 + logger.error(_("采集项创建失败,请联系管理员。错误信息:{err}").format(err=err)) + + return True diff --git a/dbm-ui/backend/db_periodic_task/local_tasks/db_monitor.py b/dbm-ui/backend/db_periodic_task/local_tasks/db_monitor.py index dd0d02f78b..e6ce6395ce 100644 --- a/dbm-ui/backend/db_periodic_task/local_tasks/db_monitor.py +++ b/dbm-ui/backend/db_periodic_task/local_tasks/db_monitor.py @@ -8,24 +8,19 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -import datetime import json import logging -import os from blueapps.core.celery.celery import app from celery.schedules import crontab from django.core.cache import cache -from django.utils import timezone from backend import env from backend.configuration.constants import DEFAULT_DB_ADMINISTRATORS, PLAT_BIZ_ID, SystemSettingsEnum from backend.configuration.models import DBAdministrator, SystemSettings -from backend.db_monitor.constants import DEFAULT_ALERT_NOTICE, MONITOR_EVENTS, TPLS_ALARM_DIR, TargetPriority -from backend.db_monitor.exceptions import BkMonitorSaveAlarmException +from backend.db_monitor.constants import DEFAULT_ALERT_NOTICE, MONITOR_EVENTS from backend.db_monitor.models import CollectInstance, DispatchGroup, MonitorPolicy, NoticeGroup from backend.db_monitor.tasks import update_app_policy -from backend.db_monitor.utils import get_dbm_autofix_action_id from backend.db_periodic_task.local_tasks.register import register_periodic_task from backend.db_periodic_task.utils import TimeUnit, calculate_countdown @@ -73,103 +68,9 @@ def update_dba_notice_group(dba_id: int): @register_periodic_task(run_every=crontab(minute="*/5")) -def sync_plat_monitor_policy(action_id=None): +def sync_plat_monitor_policy(action_id=None, db_type=None, force=False): """同步平台告警策略""" - if action_id is None: - action_id = get_dbm_autofix_action_id() - skip_dir = "v1" - now = datetime.datetime.now(timezone.utc) - logger.warning("[sync_plat_monitor_policy] sync bkm alarm policy start: %s", now) - - # 逐个json导入,本地+远程 - updated_policies = 0 - for root, dirs, files in os.walk(TPLS_ALARM_DIR): - if skip_dir in dirs: - dirs.remove(skip_dir) - - for alarm_tpl in files: - with open(os.path.join(root, alarm_tpl), "r", encoding="utf-8") as f: - logger.info("[sync_plat_monitor_policy] start sync bkm alarm tpl: %s " % alarm_tpl) - try: - template_dict = json.loads(f.read()) - # 监控API不支持传入额外的字段 - template_dict.pop("export_at", "") - policy_name = template_dict["name"] - except json.decoder.JSONDecodeError: - logger.error("[sync_plat_monitor_policy] load template failed: %s", alarm_tpl) - continue - - deleted = template_dict.pop("deleted", False) - - if not template_dict.get("details"): - logger.error(("[sync_plat_monitor_policy] template %s has no details" % alarm_tpl)) - continue - - # patch template - labels = list(set(template_dict["details"]["labels"])) - template_dict["details"]["labels"] = labels - template_dict["details"]["name"] = policy_name - template_dict["details"]["priority"] = TargetPriority.PLATFORM.value - # 平台策略仅开启基于分派通知 - template_dict["details"]["notice"]["options"]["assign_mode"] = ["by_rule"] - for label in labels: - if label.startswith("NEED_AUTOFIX") and action_id is not None: - template_dict["details"]["actions"] = [ - { - "config_id": action_id, - "signal": ["abnormal"], - "user_groups": [], - "options": { - "converge_config": { - "is_enabled": False, - "converge_func": "skip_when_success", - "timedelta": 60, - "count": 1, - } - }, - } - ] - - policy = MonitorPolicy(**template_dict) - - policy_name = policy.name - logger.info("[sync_plat_monitor_policy] start sync bkm alarm policy: %s " % policy_name) - try: - synced_policy = MonitorPolicy.objects.get(bk_biz_id=policy.bk_biz_id, name=policy_name) - - if deleted: - logger.info("[sync_plat_monitor_policy] delete old alarm: %s " % policy_name) - synced_policy.delete() - continue - - if synced_policy.version >= policy.version: - logger.info("[sync_plat_monitor_policy] skip same version alarm: %s " % policy_name) - continue - - for keeped_field in MonitorPolicy.KEEPED_FIELDS: - setattr(policy, keeped_field, getattr(synced_policy, keeped_field)) - - policy.details["id"] = synced_policy.monitor_policy_id - logger.info("[sync_plat_monitor_policy] update bkm alarm policy: %s " % policy_name) - except MonitorPolicy.DoesNotExist: - logger.info("[sync_plat_monitor_policy] create bkm alarm policy: %s " % policy_name) - - try: - # fetch targets/test_rules/notify_rules/notify_groups from parent details - for attr, value in policy.parse_details().items(): - setattr(policy, attr, value) - - policy.save() - updated_policies += 1 - logger.error("[sync_plat_monitor_policy] save bkm alarm policy success: %s", policy_name) - except BkMonitorSaveAlarmException as e: - logger.error("[sync_plat_monitor_policy] save bkm alarm policy failed: %s, %s ", policy_name, e) - - logger.warning( - "[sync_plat_monitor_policy] finish sync bkm alarm policy end: %s, update_cnt: %s", - datetime.datetime.now(timezone.utc) - now, - updated_policies, - ) + MonitorPolicy.sync_plat_monitor_policy(action_id=action_id, db_type=db_type, force=force) @register_periodic_task(run_every=crontab(minute=0, hour="*/1")) diff --git a/dbm-ui/backend/dbm_init/services.py b/dbm-ui/backend/dbm_init/services.py index cdc5ba3c8f..e64a6804a4 100644 --- a/dbm-ui/backend/dbm_init/services.py +++ b/dbm-ui/backend/dbm_init/services.py @@ -8,7 +8,6 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -import copy import datetime import json import logging @@ -24,7 +23,7 @@ from backend import env from backend.bk_dataview.grafana.views import SwitchOrgView -from backend.components import BKLogApi, BKMonitorV3Api, CCApi, ItsmApi +from backend.components import BKMonitorV3Api, CCApi, ItsmApi from backend.components.constants import SSL_KEY from backend.configuration.constants import DBM_REPORT_INITIAL_VALUE, SystemSettingsEnum from backend.configuration.models.system import SystemSettings @@ -32,13 +31,11 @@ from backend.core.storages.file_source import BkJobFileSourceManager from backend.core.storages.storage import get_storage from backend.db_meta.models import AppMonitorTopo -from backend.db_monitor.constants import AUTOFIX_ACTION_TEMPLATE -from backend.db_monitor.utils import get_dbm_autofix_action_id +from backend.db_monitor.utils import create_bklog_collector, create_bkmonitor_action from backend.db_services.cmdb.biz import get_or_create_cmdb_module_with_name, get_or_create_set_with_name from backend.db_services.ipchooser.constants import DB_MANAGE_SET, DEFAULT_CLOUD, DIRTY_MODULE, RESOURCE_MODULE from backend.dbm_init.constants import CC_APP_ABBR_ATTR, CC_HOST_DBM_ATTR -from backend.dbm_init.json_files.format import JsonConfigFormat -from backend.exceptions import ApiError, ApiRequestError, ApiResultError +from backend.exceptions import ApiError from backend.iam_app.dataclass import generate_iam_migration_json from backend.utils.time import datetime2str @@ -110,88 +107,7 @@ def auto_create_bklog_service(startswith: str = "") -> bool: """ 创建/更新 日志平台 采集项 """ - bklog_json_files_path = "backend/dbm_init/json_files/bklog" - for filename in os.listdir(bklog_json_files_path): - if not filename.endswith(".json"): - continue - if not filename.startswith(startswith): - continue - - # 读取日志采集项json文件,并渲染配置 - with open(os.path.join(bklog_json_files_path, filename), "r", encoding="utf-8") as file: - try: - bklog_params = json.load(file) - except json.decoder.JSONDecodeError as err: - logger.error(f"读取json文件失败: {filename}, {err}") - raise err - log_name = filename.split(".")[0] - # 优先获取指定了 log_name 的 formatter - if hasattr(JsonConfigFormat, f"format_{log_name}"): - bklog_params = JsonConfigFormat.format(bklog_params, f"format_{log_name}") - # 根据不同 db 类型,指定对应的 formatter,主要是区分采集目标 - elif "mysql" in filename: - bklog_params = JsonConfigFormat.format(bklog_params, JsonConfigFormat.format_mysql.__name__) - elif "redis" in filename: - bklog_params = JsonConfigFormat.format(bklog_params, JsonConfigFormat.format_redis.__name__) - elif "mssql" in filename: - bklog_params = JsonConfigFormat.format(bklog_params, JsonConfigFormat.format_mssql.__name__) - else: - logger.warning(f"格式化函数{log_name}不存在(如果无需格式化json可忽略)") - - # 针对特殊需求修改请求参数 - if hasattr(JsonConfigFormat, f"custom_modify_{log_name}"): - bklog_params = JsonConfigFormat.custom_modify(bklog_params, f"custom_modify_{log_name}") - # 如果存在对应的环境变量设置了日志自定义的保留天数,则进行更新 - retention = getattr(env, f"BKLOG_{log_name.upper()}_RETENTION", "") or env.BKLOG_DEFAULT_RETENTION - bklog_params["retention"] = retention - # 自定义了 ES 存储集群,则指定 storage_cluster_id - if env.BKLOG_STORAGE_CLUSTER_ID: - bklog_params["storage_cluster_id"] = env.BKLOG_STORAGE_CLUSTER_ID - # 如果集群支持冷热数据,则补充 allocation_min_days,为 retention 的一半即可 - if env.BKLOG_CLUSTER_SUPPORT_HOT_COLD: - bklog_params["allocation_min_days"] = retention // 2 - - # 获取当前采集项的列表 - data = BKLogApi.list_collectors( - {"bk_biz_id": env.DBA_APP_BK_BIZ_ID, "pagesize": 500, "page": 1}, use_admin=True - ) - collectors_name__info_map = { - collector["collector_config_name_en"]: collector for collector in data["list"] - } - - # 判断采集项是否重复创建 - collector_name = bklog_params["collector_config_name_en"] - data = BKLogApi.pre_check( - { - "bk_biz_id": env.DBA_APP_BK_BIZ_ID, - "collector_config_name_en": collector_name, - }, - use_admin=True, - ) - if not data["allowed"]: - # 采集项已创建,对采集项进行更新 - try: - collector_config_id = collectors_name__info_map[collector_name]["collector_config_id"] - except KeyError: - logger.error(f"采集项{collector_name}被创建后删除,暂无法自动重建,请联系管理员处理。") - continue - bklog_params.update({"collector_config_id": collector_config_id}) - logger.info(f"采集项{collector_name}已创建, 对采集项进行更新...") - try: - BKLogApi.fast_update(params=bklog_params, use_admin=True) - except (ApiRequestError, ApiResultError) as e: - logger.error(f"采集项{collector_name}更新失败,请联系管理员。错误信息:{e}") - - continue - - # 创建采集项 - try: - data = BKLogApi.fast_create(params=bklog_params, use_admin=True) - logger.info(f"采集项创建成功,相关信息: {data}") - except (ApiRequestError, ApiResultError) as e: - # 当前采集项创建失败默认不影响下一个采集项的创建 - logger.error(f"采集项创建失败,请联系管理员。错误信息:{e}") - + create_bklog_collector(startswith=startswith) return True @staticmethod @@ -422,36 +338,20 @@ def init_custom_metric_and_event(): key=SystemSettingsEnum.BKM_DBM_REPORT.value, ) - @staticmethod - def auto_create_bkmonitor_action() -> int: - """初始化监控处理套餐""" - action_id = get_dbm_autofix_action_id() - action_config = copy.deepcopy(AUTOFIX_ACTION_TEMPLATE) - - if action_id is None: - BKMonitorV3Api.save_action_config(action_config) - else: - action_config["id"] = action_id - BKMonitorV3Api.edit_action_config(action_config) - - return action_id - @staticmethod def auto_create_bkmonitor_alarm() -> bool: """初始化bkmonitor配置""" - - from backend.db_monitor.models import CollectInstance - from backend.db_periodic_task.local_tasks import sync_plat_monitor_policy + from backend.db_monitor.models import CollectInstance, MonitorPolicy logger.info("auto_create_bkmonitor_service") - action_id = Services.auto_create_bkmonitor_action() + action_id = create_bkmonitor_action() # 加载采集策略 CollectInstance.sync_collect_strategy() # 加载告警策略 - sync_plat_monitor_policy(action_id) + MonitorPolicy.sync_plat_monitor_policy(action_id=action_id) return True diff --git a/dbm-ui/backend/flow/utils/cc_manage.py b/dbm-ui/backend/flow/utils/cc_manage.py index ec080d29a1..9d4ebcf5ad 100644 --- a/dbm-ui/backend/flow/utils/cc_manage.py +++ b/dbm-ui/backend/flow/utils/cc_manage.py @@ -21,7 +21,8 @@ from backend.db_meta.enums import ClusterType, ClusterTypeMachineTypeDefine from backend.db_meta.models import AppMonitorTopo, Cluster, ClusterMonitorTopo, Machine, StorageInstance from backend.db_meta.models.cluster_monitor import INSTANCE_MONITOR_PLUGINS, SET_NAME_TEMPLATE -from backend.db_monitor.models import CollectInstance +from backend.db_monitor.models import CollectInstance, MonitorPolicy +from backend.db_monitor.utils import create_bklog_collector from backend.db_services.cmdb.biz import get_or_create_cmdb_module_with_name, get_or_create_set_with_name from backend.db_services.ipchooser.constants import IDLE_HOST_MODULE from backend.db_services.ipchooser.query.resource import ResourceQueryHelper @@ -107,10 +108,14 @@ def get_or_create_set_module( machine_topo[machine_type] = topo.bk_module_id - # 同步采集项 + # 同步采集项、监控策略 if sync_collector_flag: - CollectInstance.sync_collect_strategy(db_type=db_type, force=True) - Services.auto_create_bklog_service(startswith=db_type) + try: + create_bklog_collector(startswith=db_type) + CollectInstance.sync_collect_strategy(db_type=db_type, force=True) + MonitorPolicy.sync_plat_monitor_policy(db_type=db_type, force=True) + except ApiError as e: + logger.exception("{} sync_collector error: {}".format(db_type, e)) logger.info("get_or_create_set_module machine_topo: {}".format(machine_topo)) return machine_topo diff --git a/dbm-ui/backend/tests/dbm_init/test_auto_create_services.py b/dbm-ui/backend/tests/dbm_init/test_auto_create_services.py index 123ea0626b..1d5fdfbf8f 100644 --- a/dbm-ui/backend/tests/dbm_init/test_auto_create_services.py +++ b/dbm-ui/backend/tests/dbm_init/test_auto_create_services.py @@ -25,7 +25,7 @@ def test_auto_create_itsm_service(self): self.assertEqual(service_id, 94) @patch("backend.dbm_init.services.BKLogApi", BKLogApiMock) - @patch("backend.dbm_init.json_files.format.CCApi", CCApiMock) + @patch("backend.bk_monitor.format.CCApi", CCApiMock) def test_auto_create_bklog_service(self): is_success = Services.auto_create_bklog_service() self.assertEqual(is_success, True)