Skip to content

Commit

Permalink
feat(backend): 监控目标支持自定义key条件 close #1589
Browse files Browse the repository at this point in the history
  • Loading branch information
Myfatguy11 committed Nov 1, 2023
1 parent 2f1f12a commit 110130f
Show file tree
Hide file tree
Showing 93 changed files with 24,403 additions and 8,433 deletions.
22 changes: 16 additions & 6 deletions dbm-ui/backend/db_monitor/management/commands/extract_alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("db_type", choices=DBType.get_values(), type=str, help="db类型")
parser.add_argument("bkmonitor_strategy_list", nargs="+", type=int, help="监控策略ID列表")
parser.add_argument("-c", "--custom-conditions", nargs="*", help="自定义过滤条件的key列表", type=str)
parser.add_argument("-d", "--disabled", dest="is_disabled", action="store_true", help="disable by default")

def to_template(self, db_type: str, instance: dict):
Expand Down Expand Up @@ -99,9 +100,10 @@ def clear_id(self, objs, id_name="id"):

def handle(self, *args, **options):
bkmonitor_strategy_list = options["bkmonitor_strategy_list"]
custom_conditions = options["custom_conditions"] or []
db_type = options["db_type"]
is_disabled = options["is_disabled"]

print(custom_conditions)
res = BKMonitorV3Api.search_alarm_strategy_v3(
{
"page": 1,
Expand Down Expand Up @@ -143,6 +145,7 @@ def handle(self, *args, **options):
strategy_template["labels"] = sorted(set(strategy_template["labels"]))

data_type_label = ""
custom_agg_conditions = []
for item in strategy_template["items"]:
# 清空监控目标
item["target"] = []
Expand Down Expand Up @@ -178,19 +181,26 @@ def handle(self, *args, **options):
if "agg_dimension" in query_config and "appid" not in query_config["agg_dimension"]:
query_config["agg_dimension"].append("appid")

custom_agg_conditions = list(
filter(lambda x: x["key"] in custom_conditions, query_config.get("agg_condition", []))
)

self.clear_id(strategy_template["items"])

with open(os.path.join(TPLS_ALARM_DIR, f"{template_name}_test.json"), "w") as template_file:
with open(os.path.join(TPLS_ALARM_DIR, f"{template_name}.json"), "w") as template_file:
is_enabled = not is_disabled
strategy_template["is_enabled"] = is_enabled
template_dict = OrderedDict(
{
"bk_biz_id": 0,
"version": 0,
"is_enabled": not is_disabled,
"name": template_name,
"details": strategy_template,
"db_type": db_type,
"alert_source": data_type_label,
"details": strategy_template,
"is_enabled": is_enabled,
"monitor_indicator": strategy_template["items"][0]["name"],
"version": 0,
"alert_source": data_type_label,
"custom_conditions": custom_agg_conditions,
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def handle(self, *args, **options):
for alarm_json in alarm_jsons:
with open(alarm_json, "r+") as f:
template_dict = json.loads(f.read())
template_dict["custom_conditions"] = []

old_template_name = template_dict["name"]
template_name = template_dict["name"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.19 on 2023-10-31 11:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("db_monitor", "0015_auto_20231031_1238"),
]

operations = [
migrations.AddField(
model_name="monitorpolicy",
name="custom_conditions",
field=models.JSONField(default=list, verbose_name="自定义过滤列表"),
),
]
12 changes: 9 additions & 3 deletions dbm-ui/backend/db_monitor/models/alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ class MonitorPolicy(AuditedModel):
target_priority = models.PositiveIntegerField(verbose_name=_("监控策略优先级,跟随targets调整"))
target_keyword = models.TextField(verbose_name=_("监控目标检索冗余字段"), default="")

# [{"key": "abc", "method": "eq", "value": ["aa", "bb"], "condition": "and", "dimension_name": "abc"}]
custom_conditions = models.JSONField(verbose_name=_("自定义过滤列表"), default=list)

# Type 目前仅支持 Threshold
# level: 1(致命)、2(预警)、3(提醒)
# item[*].algorithms[*]:
Expand Down Expand Up @@ -707,10 +710,13 @@ def patch_priority_and_agg_conditions(self, details):
for query_config in item["query_configs"]:
if "agg_condition" in query_config:
# remove same type conditions
exclude_keys = list(map(lambda x: x["key"], self.custom_conditions)) + TargetLevel.get_values()
query_config_agg_condition = list(
filter(lambda cond: cond["key"] not in TargetLevel.get_values(), query_config["agg_condition"])
filter(lambda cond: cond["key"] not in exclude_keys, query_config["agg_condition"])
)
query_config_agg_condition.extend(agg_conditions)
query_config_agg_condition.extend(self.custom_conditions)

# overwrite agg_condition
query_config["agg_condition"] = query_config_agg_condition
else:
Expand Down Expand Up @@ -807,8 +813,8 @@ def save(self, force_insert=False, force_update=False, using=None, update_fields
super().save(force_insert, force_update, using, update_fields)

def delete(self, using=None, keep_parents=False):
if self.bk_biz_id == PLAT_BIZ_ID:
raise BuiltInNotAllowDeleteException
# if self.bk_biz_id == PLAT_BIZ_ID:
# raise BuiltInNotAllowDeleteException

if self.monitor_policy_id:
self.bkm_delete_alarm_strategy()
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_monitor/models/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def init_collect_strategy(bk_biz_id=env.DBA_APP_BK_BIZ_ID):
)
collect_params["id"] = collect_instance.collect_id

if template.version != collect_instance.version:
if template.version <= collect_instance.version:
logger.warning("[init_collect_strategy] skip update bkmonitor collector: %s " % template.name)
continue

Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_monitor/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ python manage.py extract_collect influxdb 6
python manage.py extract_alarm mysql 39901 39900 39763 39759 39783 39760 39785 39779 39723 39738 39739 39787 39767 39751 39733 39762 39729 39782 39734 39788 39730 39768 39731 39769 39737 39721 39773 39786 39771 39742 39755 39745 39789 39918 39913 39917 39916 39915 39914 39912 39911
python manage.py extract_alarm redis 39764 39743 39722 39744 39727 39776 39748 39774 39758 39718 39754 39750 39728 39925 39923 39922 39921
python manage.py extract_alarm es 39777 39752 39717 39747 39756 39740 39757 39725
python manage.py extract_alarm kafka 5676 5677 5678 5679 5685
python manage.py extract_alarm kafka 39772 -d -c consumergroup topic
python manage.py extract_alarm pulsar 5683 5682 5681 5680
python manage.py extract_alarm hdfs 5891 5894 5895 5898 5899
python manage.py extract_alarm influxdb 5946 5947 5948 5949 5950 5951 5952
Expand Down
3 changes: 2 additions & 1 deletion dbm-ui/backend/db_monitor/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ class TestRuleConfigSerializer(serializers.Serializer):
child=serializers.ChoiceField(choices=NoticeSignalEnum.get_choices()), allow_empty=False
)
notify_groups = serializers.ListField(child=serializers.IntegerField(), allow_empty=True)
custom_conditions = serializers.ListSerializer(child=serializers.JSONField(), allow_empty=True)

class Meta:
model = MonitorPolicy
fields = ["targets", "test_rules", "notify_rules", "notify_groups"]
fields = ["targets", "test_rules", "notify_rules", "notify_groups", "custom_conditions"]


class MonitorPolicyCloneSerializer(MonitorPolicyUpdateSerializer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(CPU\u4f7f\u7528\u7387)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(I/O\u4f7f\u7528\u7387)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(\u78c1\u76d8\u7a7a\u95f4\u4f7f\u7528\u7387)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(\u7f51\u5361\u5165\u6d41\u91cf)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
5 changes: 3 additions & 2 deletions dbm-ui/backend/db_monitor/tpls/alarm/ES 写拒绝.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
},
"is_enabled": true,
"monitor_indicator": "MAX(ES\u7ebf\u7a0b\u6c60\u4e2d\u88ab\u62d2\u7edd\u4efb\u52a1\u6570)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
5 changes: 3 additions & 2 deletions dbm-ui/backend/db_monitor/tpls/alarm/ES 写排队.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
},
"is_enabled": true,
"monitor_indicator": "MAX(ES\u7ebf\u7a0b\u6c60\u4e2d\u7684\u6392\u961f\u4efb\u52a1\u6570)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
5 changes: 3 additions & 2 deletions dbm-ui/backend/db_monitor/tpls/alarm/ES 搜索拒绝.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
},
"is_enabled": true,
"monitor_indicator": "MAX(ES\u7ebf\u7a0b\u6c60\u4e2d\u88ab\u62d2\u7edd\u7684\u4efb\u52a1\u6570)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
5 changes: 3 additions & 2 deletions dbm-ui/backend/db_monitor/tpls/alarm/ES 集群状态.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
},
"is_enabled": true,
"monitor_indicator": "MAX(ES\u96c6\u7fa4\u5065\u5eb7\u72b6\u6001)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
5 changes: 3 additions & 2 deletions dbm-ui/backend/db_monitor/tpls/alarm/HDFS CorruptBlocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(\u574f\u6570\u636e\u5757)/MIN(NameNode\u72b6\u6001)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
},
"is_enabled": true,
"monitor_indicator": "DataNode\u6700\u540e\u4e00\u6b21\u901a\u4fe1\u65f6\u95f4",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
5 changes: 3 additions & 2 deletions dbm-ui/backend/db_monitor/tpls/alarm/HDFS EditLogRoll.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
},
"is_enabled": true,
"monitor_indicator": "NameNode\u7f16\u8f91\u65e5\u5fd7\u6eda\u52a8\u64cd\u4f5c\u6570",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
5 changes: 3 additions & 2 deletions dbm-ui/backend/db_monitor/tpls/alarm/HDFS MissingBlocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
},
"is_enabled": true,
"monitor_indicator": "\u4e22\u5931\u7684\u6570\u636e\u5757\u6570\u91cf",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
},
"is_enabled": true,
"monitor_indicator": "NameNode\u6545\u969c\u8f6c\u79fb\u6b21\u6570",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
},
"is_enabled": true,
"monitor_indicator": "\u5bb9\u91cf\u4f7f\u7528\u7387",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(CPU\u4f7f\u7528\u7387)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(\u65f6\u95f4\u5e8f\u5217\u6570\u91cf)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(\u5e94\u7528\u7a0b\u5e8f\u5185\u5b58\u4f7f\u7528\u5360\u6bd4)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(\u5199\u5165\u5931\u8d25\u7684\u6570\u636e\u70b9\u6570\u91cf)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
},
"is_enabled": true,
"monitor_indicator": "MAX(http_response_result_code)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(I/O\u4f7f\u7528\u7387)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
},
"is_enabled": true,
"monitor_indicator": "MAX(\u78c1\u76d8\u7a7a\u95f4\u4f7f\u7528\u7387)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
},
"is_enabled": true,
"monitor_indicator": "AVG(CPU\u4f7f\u7528\u7387)",
"version": 10,
"alert_source": "time_series"
"version": 11,
"alert_source": "time_series",
"custom_conditions": []
}
Loading

0 comments on commit 110130f

Please sign in to comment.