diff --git a/build/register.xml b/build/register.xml index aa1afaf9c..18b65f768 100644 --- a/build/register.xml +++ b/build/register.xml @@ -134,6 +134,8 @@ + + @@ -153,7 +155,11 @@ - + + + + + @@ -512,6 +518,7 @@ errorCode errorMessage guid + monitor_key @@ -896,6 +903,12 @@ guid monitorType logPathList + logMonitorTemplate + logMonitorPrefixCode + logMonitorName + deployPath + pathType + logServiceCodeList errorCode diff --git a/monitor-server/api/api.go b/monitor-server/api/api.go index f196e081a..69d1295aa 100644 --- a/monitor-server/api/api.go +++ b/monitor-server/api/api.go @@ -342,6 +342,11 @@ func init() { // 给平台分析迁移数据 &handlerFuncObj{Url: "/trans-export/analyze", Method: http.MethodPost, HandlerFunc: monitor.AnalyzeTransExportData}, + // 平台迁移 根据id读取监控名称数据 + &handlerFuncObj{Url: "/trans-export/log_monitor_template/batch", Method: http.MethodPost, HandlerFunc: service.BatchGetLogMonitorTemplate}, + &handlerFuncObj{Url: "/trans-export/dashboard/batch", Method: http.MethodPost, HandlerFunc: monitor.BatchGetDashboard}, + &handlerFuncObj{Url: "/trans-export/service_group/batch", Method: http.MethodPost, HandlerFunc: alarm.BatchGetServiceGroup}, + &handlerFuncObj{Url: "/trans-export/config/type/batch", Method: http.MethodPost, HandlerFunc: monitor.BatchGetTypeConfigList}, ) } diff --git a/monitor-server/api/v1/agent/dashboard.go b/monitor-server/api/v1/agent/dashboard.go index ddce7fdaa..b5f742c08 100644 --- a/monitor-server/api/v1/agent/dashboard.go +++ b/monitor-server/api/v1/agent/dashboard.go @@ -63,7 +63,7 @@ func ExportPanelAdd(c *gin.Context) { } if tmpMessage != "" { errorMessage = tmpMessage - tmpResult = append(tmpResult, resultOutputObj{CallbackParameter: v.CallbackParameter, ErrorCode: "1", ErrorMessage: tmpMessage}) + tmpResult = append(tmpResult, resultOutputObj{CallbackParameter: v.CallbackParameter, ErrorCode: "1", ErrorMessage: tmpMessage, Guid: v.Guid, MonitorKey: v.Guid}) successFlag = "1" continue } @@ -96,7 +96,7 @@ func ExportPanelAdd(c *gin.Context) { } if tmpMessage != "" { errorMessage = tmpMessage - tmpResult = append(tmpResult, resultOutputObj{Guid: v.Guid, CallbackParameter: v.CallbackParameter, ErrorCode: "1", ErrorMessage: tmpMessage}) + tmpResult = append(tmpResult, resultOutputObj{Guid: v.Guid, CallbackParameter: v.CallbackParameter, ErrorCode: "1", ErrorMessage: tmpMessage, MonitorKey: v.Guid}) successFlag = "1" continue } @@ -104,10 +104,10 @@ func ExportPanelAdd(c *gin.Context) { if err != nil { tmpMessage = fmt.Sprintf(mid.GetMessageMap(c).UpdateTableError, "recursive_panel") errorMessage = tmpMessage - tmpResult = append(tmpResult, resultOutputObj{Guid: v.Guid, CallbackParameter: v.CallbackParameter, ErrorCode: "1", ErrorMessage: tmpMessage, ErrorDetail: err.Error()}) + tmpResult = append(tmpResult, resultOutputObj{Guid: v.Guid, CallbackParameter: v.CallbackParameter, ErrorCode: "1", ErrorMessage: tmpMessage, ErrorDetail: err.Error(), MonitorKey: v.Guid}) successFlag = "1" } else { - tmpResult = append(tmpResult, resultOutputObj{Guid: v.Guid, CallbackParameter: v.CallbackParameter, ErrorCode: "0", ErrorMessage: ""}) + tmpResult = append(tmpResult, resultOutputObj{Guid: v.Guid, CallbackParameter: v.CallbackParameter, ErrorCode: "0", ErrorMessage: "", MonitorKey: v.Guid}) } } result = resultObj{ResultCode: successFlag, ResultMessage: errorMessage, Results: resultOutput{Outputs: tmpResult}} diff --git a/monitor-server/api/v1/alarm/organization.go b/monitor-server/api/v1/alarm/organization.go index a1e0d91ee..c4c31051a 100644 --- a/monitor-server/api/v1/alarm/organization.go +++ b/monitor-server/api/v1/alarm/organization.go @@ -58,7 +58,7 @@ func GetOrgPanelRole(c *gin.Context) { mid.ReturnParamEmptyError(c, "guid") return } - data, err := db.GetOrgRole(guid) + data, err := db.GetOrgRoleNew(guid) if err != nil { mid.ReturnFetchDataError(c, "panel_recursive", "guid", guid) return @@ -214,3 +214,21 @@ func SearchSysPanelData(c *gin.Context) { result := db.SearchPanelByName(search, endpoint) mid.ReturnSuccessData(c, result) } + +func BatchGetServiceGroup(c *gin.Context) { + var param m.IdsParam + if err := c.ShouldBindJSON(¶m); err != nil { + mid.ReturnValidateError(c, err.Error()) + return + } + if len(param.Ids) == 0 { + mid.ReturnSuccess(c) + return + } + result, err := db.BatchGetServiceGroupByIds(param.Ids) + if err != nil { + mid.ReturnHandleError(c, err.Error(), err) + } else { + mid.ReturnSuccessData(c, result) + } +} diff --git a/monitor-server/api/v1/dashboard/dashboard.go b/monitor-server/api/v1/dashboard/dashboard.go index de881f786..2c4621d2d 100644 --- a/monitor-server/api/v1/dashboard/dashboard.go +++ b/monitor-server/api/v1/dashboard/dashboard.go @@ -335,7 +335,7 @@ func getPieData(paramConfig *m.PieChartConfigObj) (result []*m.QueryMonitorData, return } if len(chartSeries) == 0 { - err = fmt.Errorf("Can not find chart serie with guid:%d ", paramConfig.CustomChartGuid) + log.Logger.Warn("Can not find chart series", log.String("guid", paramConfig.CustomChartGuid)) return } seriesObj := chartSeries[0] diff --git a/monitor-server/api/v1/dashboard_new/chart.go b/monitor-server/api/v1/dashboard_new/chart.go index 5494b8296..20917dcde 100644 --- a/monitor-server/api/v1/dashboard_new/chart.go +++ b/monitor-server/api/v1/dashboard_new/chart.go @@ -202,7 +202,7 @@ func GetCustomChartConfig(param *models.ChartQueryParam, result *models.EChartOp return } if len(chartSeries) == 0 { - err = fmt.Errorf("Can not find chart serie with guid:%d ", param.CustomChartGuid) + log.Logger.Warn("Can not find chart series", log.String("guid", param.CustomChartGuid)) return } err = chartCompare(param) @@ -540,6 +540,7 @@ func convertArray2Map(arr []string) map[string]bool { func GetChartQueryData(queryList []*models.QueryMonitorData, param *models.ChartQueryParam, result *models.EChartOption) error { serials := []*models.SerialModel{} var err error + var logType string archiveQueryFlag := false if param.Start < (time.Now().Unix()-models.Config().ArchiveMysql.LocalStorageMaxDay*86400) && db.ArchiveEnable { archiveQueryFlag = true @@ -565,6 +566,13 @@ func GetChartQueryData(queryList []*models.QueryMonitorData, param *models.Chart if param.LineType == 2 { query.ComparisonFlag = "Y" } + if len(query.Metric) > 0 { + // 看指标是否为 业务配置过来的,查询业务配置类型,自定义类型需要特殊处理 tags + if logType, err = db.GetLogTypeByMetric(query.Metric[0]); err != nil { + log.Logger.Error("GetLogType err", log.Error(err)) + } + query.ServiceConfiguration = logType + } tmpSerials := ds.PrometheusData(query) // 如果归档数据可用,尝试从归档数据中补全数据 if db.ArchiveEnable { diff --git a/monitor-server/api/v2/alarm/strategy.go b/monitor-server/api/v2/alarm/strategy.go index 76d5dd545..575766118 100644 --- a/monitor-server/api/v2/alarm/strategy.go +++ b/monitor-server/api/v2/alarm/strategy.go @@ -204,8 +204,9 @@ func ImportAlarmStrategy(c *gin.Context) { } queryType := c.Param("queryType") guid := c.Param("guid") + importRule := c.Query("importRule") var metricNotFound, nameDuplicate []string - err, metricNotFound, nameDuplicate = db.ImportAlarmStrategy(queryType, guid, paramObj, middleware.GetOperateUser(c)) + err, metricNotFound, nameDuplicate = db.ImportAlarmStrategy(queryType, guid, paramObj, middleware.GetOperateUser(c), importRule) if err != nil { if len(metricNotFound) > 0 { err = fmt.Errorf(middleware.GetMessageMap(c).MetricNotFound, strings.Join(metricNotFound, ",")) diff --git a/monitor-server/api/v2/monitor/custom_dashboard.go b/monitor-server/api/v2/monitor/custom_dashboard.go index 54263af4b..3c14b5c56 100644 --- a/monitor-server/api/v2/monitor/custom_dashboard.go +++ b/monitor-server/api/v2/monitor/custom_dashboard.go @@ -732,7 +732,7 @@ func TransImportCustomDashboard(c *gin.Context) { middleware.ReturnParamEmptyError(c, "import dashboard chart is empty") return } - if customDashboard, _, err = db.ImportCustomDashboard(param, middleware.GetOperateUser(c), "insert", param.MgmtRole, param.UseRoles, middleware.GetMessageMap(c)); err != nil { + if customDashboard, _, err = db.ImportCustomDashboard(param, middleware.GetOperateUser(c), "cover", param.MgmtRole, param.UseRoles, middleware.GetMessageMap(c)); err != nil { middleware.ReturnServerHandleError(c, err) return } @@ -742,3 +742,17 @@ func TransImportCustomDashboard(c *gin.Context) { } middleware.ReturnSuccess(c) } + +func BatchGetDashboard(c *gin.Context) { + var param models.IdsParam + if err := c.ShouldBindJSON(¶m); err != nil { + middleware.ReturnValidateError(c, err.Error()) + return + } + result, err := db.BatchGetCustomDashboardByIds(param.Ids) + if err != nil { + middleware.ReturnHandleError(c, err.Error(), err) + } else { + middleware.ReturnSuccessData(c, result) + } +} diff --git a/monitor-server/api/v2/monitor/metric.go b/monitor-server/api/v2/monitor/metric.go index c4bd075c8..a1ad8a0eb 100644 --- a/monitor-server/api/v2/monitor/metric.go +++ b/monitor-server/api/v2/monitor/metric.go @@ -210,11 +210,12 @@ func ImportMetric(c *gin.Context) { func QueryMetricTagValue(c *gin.Context) { var param models.QueryMetricTagParam var orginMetricRow *models.MetricTable + var logType string if err := c.ShouldBindJSON(¶m); err != nil { middleware.ReturnHandleError(c, err.Error(), err) return } - result := []*models.QueryMetricTagResultObj{} + var result []*models.QueryMetricTagResultObj if param.MetricId == "" { middleware.ReturnSuccessData(c, result) return @@ -225,6 +226,14 @@ func QueryMetricTagValue(c *gin.Context) { middleware.ReturnHandleError(c, err.Error(), err) return } + if metricRow == nil { + middleware.ReturnServerHandleError(c, fmt.Errorf("metricId %s is invalid", param.MetricId)) + return + } + if logType, err = db.GetLogTypeByLogMetricGroup(metricRow.LogMetricGroup); err != nil { + middleware.ReturnServerHandleError(c, err) + return + } var tagList []string // 如果是同环比指标需要用原始指标进去查询 if orginMetricRow, err = db.GetOriginMetricByComparisonId(param.MetricId); err != nil { @@ -282,6 +291,10 @@ func QueryMetricTagValue(c *gin.Context) { if seriesMap == nil { continue } + // 如果该指标为自定义类型的业务配置创建,tags内容: tags="test_service_code=addUser,test_retcode=200",需要做特殊解析处理 + if logType == models.LogMonitorCustomType && seriesMap["tags"] != "" { + seriesMap = datasource.ResetPrometheusMetricMap(seriesMap) + } if tmpTagValue, ok := seriesMap[v]; ok { if _, existFlag := tmpValueDistinctMap[tmpTagValue]; !existFlag { tmpValueList = append(tmpValueList, tmpTagValue) diff --git a/monitor-server/api/v2/monitor/type_config.go b/monitor-server/api/v2/monitor/type_config.go index e7dca69d9..9744c1356 100644 --- a/monitor-server/api/v2/monitor/type_config.go +++ b/monitor-server/api/v2/monitor/type_config.go @@ -24,6 +24,25 @@ func QueryTypeConfigList(c *gin.Context) { middleware.ReturnSuccessData(c, list) } +func BatchGetTypeConfigList(c *gin.Context) { + var param models.CommonNameParam + var err error + var list []*models.TypeConfig + if err = c.ShouldBindJSON(¶m); err != nil { + middleware.ReturnServerHandleError(c, err) + return + } + if len(param.Names) == 0 { + middleware.ReturnSuccess(c) + return + } + if list, err = db.GetTypeConfigListByNames(param.Names); err != nil { + middleware.ReturnServerHandleError(c, err) + return + } + middleware.ReturnSuccessData(c, list) +} + func AddTypeConfig(c *gin.Context) { var param models.TypeConfig var typeConfigList []*models.TypeConfig @@ -68,6 +87,7 @@ func BatchAddTypeConfig(c *gin.Context) { } // 如果 role_new表还未初始化,需要先同步数据 if !db.ExistRoles() { + db.SyncCoreRole() db.SyncCoreRoleList() } for _, monitorType := range newMonitorTypeList { diff --git a/monitor-server/api/v2/service/log_metric.go b/monitor-server/api/v2/service/log_metric.go index 8316b80a5..0df4c9d74 100644 --- a/monitor-server/api/v2/service/log_metric.go +++ b/monitor-server/api/v2/service/log_metric.go @@ -424,7 +424,7 @@ func ImportLogMetric(c *gin.Context) { for _, dbMonitor := range paramObj.DBConfig { dbMonitor.ServiceGroup = serviceGroup } - if err = db.ImportLogMetric(¶mObj, middleware.GetOperateUser(c), middleware.GetMessageMap(c)); err != nil { + if err = db.ImportLogMetric(¶mObj, middleware.GetOperateUser(c), middleware.GetOperateUserRoles(c), middleware.GetMessageMap(c)); err != nil { middleware.ReturnHandleError(c, err.Error(), err) } else { middleware.ReturnSuccess(c) @@ -661,7 +661,7 @@ func CheckLogMonitorRegExpMatch(c *gin.Context) { middleware.ReturnValidateError(c, err.Error()) return } - result := []*models.LogParamTemplateObj{} + var result []*models.LogParamTemplateObj for _, v := range param.ParamList { _, v.DemoMatchValue = db.CheckRegExpMatchPCRE(models.CheckRegExpParam{RegString: v.Regular, TestContext: param.DemoLog}) result = append(result, v) @@ -852,6 +852,15 @@ func CreateLogMetricCustomGroup(c *gin.Context) { } } } + if existLogMonitorTemplate, getErr := db.GetLogMonitorTemplateByName(param.Guid, param.Name); getErr != nil { + err = getErr + middleware.ReturnServerHandleError(c, err) + return + } else if existLogMonitorTemplate != nil { + err = fmt.Errorf("log monitor template name:%s duplicate", param.Name) + middleware.ReturnServerHandleError(c, err) + return + } if err := db.ValidateLogMetricGroupName(param.Guid, param.Name, param.LogMetricMonitor); err != nil { err = fmt.Errorf(middleware.GetMessageMap(c).LogGroupNameDuplicateError, param.Name) middleware.ReturnHandleError(c, err.Error(), err) @@ -880,7 +889,8 @@ func CreateLogMetricCustomGroup(c *gin.Context) { func UpdateLogMetricCustomGroup(c *gin.Context) { var param models.LogMetricGroupObj - if err := c.ShouldBindJSON(¶m); err != nil { + var err error + if err = c.ShouldBindJSON(¶m); err != nil { middleware.ReturnValidateError(c, err.Error()) return } @@ -889,8 +899,21 @@ func UpdateLogMetricCustomGroup(c *gin.Context) { middleware.ReturnHandleError(c, err.Error(), err) return } + if existLogMonitorTemplate, getErr := db.GetLogMonitorTemplateByName(param.Guid, param.Name); getErr != nil { + err = getErr + middleware.ReturnServerHandleError(c, err) + return + } else if existLogMonitorTemplate != nil { + err = fmt.Errorf("log monitor template name:%s duplicate", param.Name) + middleware.ReturnServerHandleError(c, err) + return + } if len(param.MetricList) > 0 { for _, metric := range param.MetricList { + // 拼接指标 + if param.MetricPrefixCode != "" { + metric.Metric = fmt.Sprintf("%s_%s", param.MetricPrefixCode, metric.Metric) + } if middleware.IsIllegalLogParamNameOrMetric(metric.LogParamName) || middleware.IsIllegalLogParamNameOrMetric(metric.Metric) { middleware.ReturnValidateError(c, "log_param_name or metric param invalid") return @@ -962,7 +985,7 @@ func LogMonitorTemplateExport(c *gin.Context) { middleware.ReturnHandleError(c, err.Error(), err) return } - resultData := []*models.LogMonitorTemplateDto{} + var resultData []*models.LogMonitorTemplateDto for _, v := range param.GuidList { templateObj, tmpErr := db.GetLogMonitorTemplate(v) if tmpErr != nil { @@ -1044,3 +1067,21 @@ func LogMonitorDataMapRegMatch(c *gin.Context) { } middleware.ReturnSuccessData(c, param) } + +func BatchGetLogMonitorTemplate(c *gin.Context) { + var param models.IdsParam + if err := c.ShouldBindJSON(¶m); err != nil { + middleware.ReturnValidateError(c, err.Error()) + return + } + if len(param.Ids) == 0 { + middleware.ReturnSuccess(c) + return + } + result, err := db.BatchGetLogTemplateByGuid(param.Ids) + if err != nil { + middleware.ReturnHandleError(c, err.Error(), err) + } else { + middleware.ReturnSuccessData(c, result) + } +} diff --git a/monitor-server/api/v2/service/plugin.go b/monitor-server/api/v2/service/plugin.go index 642f39191..5cc8f08d1 100644 --- a/monitor-server/api/v2/service/plugin.go +++ b/monitor-server/api/v2/service/plugin.go @@ -2,6 +2,7 @@ package service import ( "encoding/json" + "github.com/WeBankPartners/open-monitor/monitor-server/middleware" "github.com/WeBankPartners/open-monitor/monitor-server/middleware/log" "github.com/WeBankPartners/open-monitor/monitor-server/models" "github.com/WeBankPartners/open-monitor/monitor-server/services/db" @@ -9,7 +10,7 @@ import ( "net/http" ) -func PluginUpdateServicePath(c *gin.Context) { +func PluginUpdateServicePath(c *gin.Context) { response := models.PluginUpdateServicePathResp{ResultCode: "0", ResultMessage: "success", Results: models.PluginUpdateServicePathOutput{}} var err error defer func() { @@ -28,7 +29,7 @@ func PluginUpdateServicePath(c *gin.Context) { return } for _, input := range param.Inputs { - output, tmpErr := db.PluginUpdateServicePathAction(input) + output, tmpErr := db.PluginUpdateServicePathAction(input, middleware.GetOperateUser(c), []string{}, middleware.GetMessageMap(c)) if tmpErr != nil { output.ErrorCode = "1" output.ErrorMessage = tmpErr.Error() @@ -36,4 +37,4 @@ func PluginUpdateServicePath(c *gin.Context) { } response.Results.Outputs = append(response.Results.Outputs, output) } -} \ No newline at end of file +} diff --git a/monitor-server/conf/default.json b/monitor-server/conf/default.json index 14e9a2d85..8a20dad9f 100644 --- a/monitor-server/conf/default.json +++ b/monitor-server/conf/default.json @@ -16,7 +16,7 @@ "attributes": ["name", "mail", "telephoneNumber"] }, "session": { - "enable": "true", + "enable": "false", "expire": 3600, "server_enable": true, "server_token": "default-token-used-in-server-side", @@ -42,10 +42,10 @@ "store": { "name": "default", "type": "mysql", - "server": "127.0.0.1", - "port": "3306", + "server": "106.52.160.142", + "port": "13307", "user": "root", - "pwd": "wecube", + "pwd": "mF8PnUQ6dK3ppmrKPmGa", "database": "monitor", "maxOpen": 20, "maxIdle": 10, @@ -58,7 +58,7 @@ "id": 1, "type": "prometheus", "env": "dev", - "host": "127.0.0.1:9090", + "host": "106.52.160.142:9090", "token": "" } ], diff --git a/monitor-server/models/alarm.go b/monitor-server/models/alarm.go index 5049445b1..cb2009001 100644 --- a/monitor-server/models/alarm.go +++ b/monitor-server/models/alarm.go @@ -739,6 +739,7 @@ type AutoSimpleAlarmStrategyParam struct { AutoCreateWarn bool LogMetricGroupGuid string MetricPrefixCode string + LogType string } type AlarmOptionsParam struct { @@ -780,3 +781,27 @@ type AlarmFiring struct { UniqueHash string `json:"uniqueHash" xorm:"unique_hash"` // 告警唯一标识(对象+指标+标签+配置) AlarmId int `json:"alarmId" xorm:"alarm_id"` // 告警历史表id } + +type SimpleAlarm struct { + AlarmName string `json:"alarmName" xorm:"alarm_name"` + UpdateAt string `json:"updateAt" xorm:"update_at"` +} + +type SimpleAlarmSort []*SimpleAlarm + +func (s SimpleAlarmSort) Len() int { + return len(s) +} + +func (s SimpleAlarmSort) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +func (s SimpleAlarmSort) Less(i, j int) bool { + if s[i].UpdateAt != "" && s[j].UpdateAt != "" { + updateTime1, _ := time.Parse(DatetimeFormat, s[i].UpdateAt) + updateTime2, _ := time.Parse(DatetimeFormat, s[j].UpdateAt) + return updateTime1.After(updateTime2) + } + return true +} diff --git a/monitor-server/models/dashboard.go b/monitor-server/models/dashboard.go index b9c0834e4..60d50ff04 100644 --- a/monitor-server/models/dashboard.go +++ b/monitor-server/models/dashboard.go @@ -388,6 +388,7 @@ type CustomDashboardTable struct { TimeRange int `json:"time_range"` //时间范围 RefreshWeek int `json:"refresh_week"` // 刷新周期 LogMetricGroup *string `json:"log_metric_group"` + UpdateAtStr string `json:"update_at_str"` } type CustomDashboardObj struct { diff --git a/monitor-server/models/datasource.go b/monitor-server/models/datasource.go index 0e6c8f575..41d4f9778 100644 --- a/monitor-server/models/datasource.go +++ b/monitor-server/models/datasource.go @@ -6,25 +6,26 @@ import ( ) type QueryMonitorData struct { - Start int64 `json:"start"` - End int64 `json:"end"` - Endpoint []string `json:"endpoint"` - Metric []string `json:"metric"` - PromQ string `json:"prom_q"` - Legend string `json:"legend"` - CompareLegend string `json:"compare_legend"` - ChartType string `json:"chart_type"` - PieData EChartPie `json:"pie_data"` - SameEndpoint bool `json:"same_endpoint"` - Step int `json:"step"` - Cluster string `json:"cluster"` - ServiceGroupName string `json:"service_group_name"` - CustomDashboard bool `json:"custom_dashboard"` - PieMetricType string `json:"pie_metric_type"` - PieAggType string `json:"pie_agg_type"` - Tags []string `json:"tags"` - PieDisplayTag string `json:"pie_display_tag"` - ComparisonFlag string `json:"comparison_flag"` + Start int64 `json:"start"` + End int64 `json:"end"` + Endpoint []string `json:"endpoint"` + Metric []string `json:"metric"` + PromQ string `json:"prom_q"` + Legend string `json:"legend"` + CompareLegend string `json:"compare_legend"` + ChartType string `json:"chart_type"` + PieData EChartPie `json:"pie_data"` + SameEndpoint bool `json:"same_endpoint"` + Step int `json:"step"` + Cluster string `json:"cluster"` + ServiceGroupName string `json:"service_group_name"` + CustomDashboard bool `json:"custom_dashboard"` + PieMetricType string `json:"pie_metric_type"` + PieAggType string `json:"pie_agg_type"` + Tags []string `json:"tags"` + PieDisplayTag string `json:"pie_display_tag"` + ComparisonFlag string `json:"comparison_flag"` + ServiceConfiguration string `json:"service_configuration"` // 业务配置, custom 表示自定义 } type PrometheusParam struct { diff --git a/monitor-server/models/endpoint_group.go b/monitor-server/models/endpoint_group.go index 4c51ae895..c018a7a26 100644 --- a/monitor-server/models/endpoint_group.go +++ b/monitor-server/models/endpoint_group.go @@ -135,6 +135,7 @@ type AlarmStrategyMetricObj struct { UpdateUser string `json:"update_user" xorm:"update_user"` LogMetricGroup string `json:"log_metric_group" xorm:"log_metric_group"` AlarmStrategyMetricGuid string `json:"alarm_strategy_metric_guid" xorm:"-"` + LogType string `json:"log_type" xorm:"log_type"` } type GroupStrategyObj struct { @@ -210,6 +211,7 @@ type StrategyConditionObj struct { Condition string `json:"condition"` Last string `json:"last"` Tags []*MetricTag `json:"tags"` + LogType string `json:"logType"` } type MetricTag struct { @@ -265,6 +267,7 @@ type AlarmStrategyMetricWithExpr struct { MetricExpr string `json:"metric_expr" xorm:"metric_expr"` MetricType string `json:"metric_type" xorm:"metric_type"` MonitorEngine int `json:"monitor_engine" xorm:"monitor_engine"` + LogType string `json:"log_type" xorm:"log_type"` } type AlarmStrategyQueryParam struct { diff --git a/monitor-server/models/log_metric.go b/monitor-server/models/log_metric.go index 9389d12b4..afb40795f 100644 --- a/monitor-server/models/log_metric.go +++ b/monitor-server/models/log_metric.go @@ -77,15 +77,16 @@ type LogMetricConfigDto struct { } type LogMetricStringMapTable struct { - Guid string `json:"guid" xorm:"guid"` - LogMetricConfig string `json:"log_metric_config" xorm:"log_metric_config"` - LogMetricGroup string `json:"log_metric_group" xorm:"log_metric_group"` - LogParamName string `json:"log_param_name" xorm:"log_param_name"` - ValueType string `json:"value_type" xorm:"value_type"` - SourceValue string `json:"source_value" xorm:"source_value"` - Regulative int `json:"regulative" xorm:"regulative"` - TargetValue string `json:"target_value" xorm:"target_value"` - UpdateTime string `json:"update_time" xorm:"update_time"` + Guid string `json:"guid" xorm:"guid"` + LogMetricConfig string `json:"log_metric_config" xorm:"log_metric_config"` + LogMetricGroup string `json:"log_metric_group" xorm:"log_metric_group"` + LogMonitorTemplate string `json:"log_monitor_template" xorm:"log_monitor_template"` + LogParamName string `json:"log_param_name" xorm:"log_param_name"` + ValueType string `json:"value_type" xorm:"value_type"` + SourceValue string `json:"source_value" xorm:"source_value"` + Regulative int `json:"regulative" xorm:"regulative"` + TargetValue string `json:"target_value" xorm:"target_value"` + UpdateTime string `json:"update_time" xorm:"update_time"` } type LogMetricEndpointRelTable struct { @@ -210,15 +211,16 @@ type CheckRegExpResult struct { type LogMetricGroupObj struct { LogMonitorTemplateGuid string `json:"log_monitor_template_guid"` LogMetricGroup - LogMonitorTemplateName string `json:"log_monitor_template_name"` - ServiceGroup string `json:"service_group"` - MonitorType string `json:"monitor_type"` - JsonRegular string `json:"json_regular"` - ParamList []*LogMetricParamObj `json:"param_list"` - MetricList []*LogMetricConfigDto `json:"metric_list"` - AutoCreateWarn bool `json:"auto_create_warn"` //自动创建告警 - AutoCreateDashboard bool `json:"auto_create_dashboard"` //自动创建自定义看板 - LogMonitorTemplateDto *LogMonitorTemplateDto `json:"log_monitor_template_data"` + LogMonitorTemplateName string `json:"log_monitor_template_name"` + ServiceGroup string `json:"service_group"` + MonitorType string `json:"monitor_type"` + JsonRegular string `json:"json_regular"` + ParamList []*LogMetricParamObj `json:"param_list"` + MetricList []*LogMetricConfigDto `json:"metric_list"` + AutoCreateWarn bool `json:"auto_create_warn"` //自动创建告警 + AutoCreateDashboard bool `json:"auto_create_dashboard"` //自动创建自定义看板 + LogMonitorTemplateDto *LogMonitorTemplateDto `json:"log_monitor_template_data"` + LogMonitorTemplateVersion string `json:"log_monitor_template_version"` } type LogMetricGroupWithTemplate struct { @@ -242,6 +244,7 @@ type LogMetricThreshold struct { Metric string DisplayName string TagConfig []string + AutoWarn bool *ThresholdConfig } @@ -271,3 +274,7 @@ type CreateLogMetricGroupDto struct { AlarmList []string `json:"alarm_list"` CustomDashboard string `json:"custom_dashboard"` } + +type IdsParam struct { + Ids []string `json:"ids"` +} diff --git a/monitor-server/models/log_template.go b/monitor-server/models/log_template.go index ea15a4bb4..69aed22c7 100644 --- a/monitor-server/models/log_template.go +++ b/monitor-server/models/log_template.go @@ -97,7 +97,7 @@ func (l *LogMetricTemplate) TransToLogMetric() (output *LogMetricConfigDto) { type LogMonitorTemplateDto struct { LogMonitorTemplate CalcResultObj *CheckRegExpResult `json:"calc_result"` - ParamList []*LogParamTemplate `json:"param_list"` + ParamList []*LogParamTemplateObj `json:"param_list"` MetricList []*LogMetricTemplate `json:"metric_list"` Permission *LogMonitorTemplatePermission `json:"permission"` LogMonitorTemplateVersion string `json:"log_monitor_template_version"` @@ -141,6 +141,8 @@ type LogMetricGroup struct { UpdateTimeString string `json:"update_time"` TemplateSnapshot string `json:"template_snapshot" xorm:"template_snapshot"` RefTemplateVersion string `json:"log_monitor_template_version" xorm:"ref_template_version"` + AutoAlarm int `json:"auto_alarm" xorm:"auto_alarm"` + AutoDashboard int `json:"auto_dashboard" xorm:"auto_dashboard"` } type LogMetricParam struct { diff --git a/monitor-server/models/service_group.go b/monitor-server/models/service_group.go index dbf0c246b..7f11e5df6 100644 --- a/monitor-server/models/service_group.go +++ b/monitor-server/models/service_group.go @@ -66,14 +66,23 @@ type PluginUpdateServicePathRequest struct { } type PluginUpdateServicePathRequestObj struct { - CallbackParameter string `json:"callbackParameter"` - Guid string `json:"guid"` - SystemName string `json:"systemName"` - LogPathList interface{} `json:"logPathList"` - MonitorType string `json:"monitorType"` - LogMonitorTemplate string `json:"logMonitorTemplate"` - DeployPath string `json:"deployPath"` - PathType string `json:"pathType"` // 日志类型-> logMonitor|logKeyword + CallbackParameter string `json:"callbackParameter"` + Guid string `json:"guid"` + SystemName string `json:"systemName"` + LogPathList interface{} `json:"logPathList"` + MonitorType string `json:"monitorType"` + LogMonitorTemplate string `json:"logMonitorTemplate"` // 日志监控模版id,单值 + LogMonitorPrefixCode string `json:"logMonitorPrefixCode"` // 日志监控指标前缀 + LogMonitorName string `json:"logMonitorName"` // 日志监控配置名 + DeployPath string `json:"deployPath"` + PathType string `json:"pathType"` // 日志类型-> logMonitor|logKeyword + LogServiceCodeList interface{} `json:"logServiceCodeList"` // 业务服务码列表 +} + +type PluginUpdateServiceCodeObj struct { + Regulative int `json:"regulative"` + SourceValue string `json:"source_value"` + TargetValue string `json:"target_value"` } type PluginUpdateServicePathResp struct { diff --git a/monitor-server/models/type_config.go b/monitor-server/models/type_config.go index 8f8da8f22..93b14be82 100644 --- a/monitor-server/models/type_config.go +++ b/monitor-server/models/type_config.go @@ -13,3 +13,7 @@ type TypeConfig struct { type BatchAddTypeConfigParam struct { DisplayNameList []string `json:"displayNameList"` } + +type CommonNameParam struct { + Names []string `json:"names"` +} diff --git a/monitor-server/services/datasource/prometheus.go b/monitor-server/services/datasource/prometheus.go index 2b780ea4a..6c5515e3a 100644 --- a/monitor-server/services/datasource/prometheus.go +++ b/monitor-server/services/datasource/prometheus.go @@ -385,6 +385,9 @@ func GetSerialName(query *m.QueryMonitorData, tagMap map[string]string, dataLeng } tmpTagList := query.Tags tmpTagList = append(tmpTagList, "t_endpoint", "instance", "calc_type") + if query.ServiceConfiguration == "custom" { + tagMap = ResetPrometheusMetricMap(tagMap) + } tmpName = appendTagString(tmpName, tagMap, tmpTagList) } else { tmpName = metric @@ -577,3 +580,17 @@ func QueryPrometheusRange(promQL string, start, end, step int64) (result *m.Prom result = &data.Data return } + +// ResetPrometheusMetricMap 重置 Prometheus返回的metric +func ResetPrometheusMetricMap(tagMap map[string]string) map[string]string { + // 此处查询指标 对应的业务配置,如果是自定义业务配置, tags内容: tags="test_service_code=addUser,test_retcode=200",需要做特殊解析处理 + if tagMap["tags"] != "" { + strArr := strings.Split(tagMap["tags"], ",") + for _, str := range strArr { + if index := strings.Index(str, "="); index > 0 { + tagMap[str[:index]] = str[index+1:] + } + } + } + return tagMap +} diff --git a/monitor-server/services/db/agent.go b/monitor-server/services/db/agent.go index d751380b0..41be82f0f 100644 --- a/monitor-server/services/db/agent.go +++ b/monitor-server/services/db/agent.go @@ -309,8 +309,24 @@ func UpdateRecursiveEndpoint(guid, operator string, endpoint []string) error { func DeleteRecursivePanel(guid string) (err error) { var actions []*Action - actions = append(actions, &Action{Sql: "DELETE FROM panel_recursive WHERE guid=?", Param: []interface{}{guid}}) - actions = append(actions, getDeleteServiceGroupAction(guid)...) + var tableData []*m.PanelRecursiveTable + x.SQL("SELECT guid,display_name,parent FROM panel_recursive").Find(&tableData) + if len(tableData) == 0 { + return nil + } + guidList := getNodeFromParent(tableData, []string{guid}, guid) + tmpMap := make(map[string]bool) + for _, v := range guidList { + tmpMap[v] = true + } + guidList = []string{} + for k, _ := range tmpMap { + if k != "" { + guidList = append(guidList, k) + } + } + actions = append(actions, &Action{Sql: "DELETE FROM panel_recursive WHERE guid in ('" + strings.Join(guidList, "','") + "')", Param: []interface{}{}}) + actions = append(actions, getDeleteServiceGroupAction(guid, guidList)...) err = Transaction(actions) if err == nil { DeleteServiceWithChildConfig(guid) diff --git a/monitor-server/services/db/alarm.go b/monitor-server/services/db/alarm.go index 626cc2762..3a299a21b 100644 --- a/monitor-server/services/db/alarm.go +++ b/monitor-server/services/db/alarm.go @@ -614,7 +614,7 @@ func GetAlarms(cond m.QueryAlarmCondition) (error, m.AlarmProblemList) { } } } - if cond.ExtOpenAlarm { + if cond.ExtOpenAlarm && len(cond.MetricFilterList) == 0 && len(cond.EndpointFilterList) == 0 { for _, v := range GetOpenAlarm(m.CustomAlarmQueryParam{Enable: true, Status: "problem", Start: "", End: "", Level: cond.PriorityList, AlterTitleList: cond.AlarmNameFilterList, Query: cond.Query}) { result = append(result, v) } @@ -2049,39 +2049,54 @@ func matchAlarmGroups(alarmStrategyList, endpointList []string) (strategyGroupMa } func GetAlarmNameList(status, alarmName string) (list []string, err error) { - var newList []string + var alarmList, customAlarmList []*m.SimpleAlarm var closed = 1 if status == "firing" { closed = 0 } - baseSQL := "select distinct alarm_name from (select distinct alarm_name,start from alarm where 1=1" + alarmSQL := "select alarm_name as alarm_name,start as update_at from alarm where 1=1" var params []interface{} if status != "" { - baseSQL = baseSQL + " and status=?" + alarmSQL = alarmSQL + " and status=?" params = append(params, status) } if strings.TrimSpace(alarmName) != "" { - baseSQL = baseSQL + " and alarm_name like '%" + alarmName + "%'" + alarmSQL = alarmSQL + " and alarm_name like '%" + alarmName + "%'" } - baseSQL = baseSQL + " union select distinct alert_title as alarm_name,update_at as start from alarm_custom where 1=1" + alarmSQL = alarmSQL + " order by start desc limit 100" + if err = x.SQL(alarmSQL, params...).Find(&alarmList); err != nil { + return + } + + var customAlarmParams []interface{} + customAlarmSQL := "select alert_title as alarm_name,update_at from alarm_custom where 1=1" if status != "" { - baseSQL = baseSQL + " and closed=?" - params = append(params, closed) + customAlarmSQL = customAlarmSQL + " and closed=?" + customAlarmParams = append(customAlarmParams, closed) } if strings.TrimSpace(alarmName) != "" { - baseSQL = baseSQL + " and alert_title like '%" + alarmName + "%'" + customAlarmSQL = customAlarmSQL + " and alert_title like '%" + alarmName + "%'" } - baseSQL = baseSQL + " ) t order by t.start desc limit 20" + customAlarmSQL = customAlarmSQL + " order by update_at desc limit 100" - if err = x.SQL(baseSQL, params...).Find(&list); err != nil { + if err = x.SQL(customAlarmSQL, customAlarmParams...).Find(&customAlarmList); err != nil { return } - if len(list) > 0 { - for _, s := range list { - if strings.TrimSpace(s) == "" { - continue - } - newList = append(newList, s) + + alarmList = append(alarmList, customAlarmList...) + // 数据去重复 + alarmList = filterRepeatAndEmptyNameAlarm(alarmList) + // 按最新时间在前面 + sort.Sort(m.SimpleAlarmSort(alarmList)) + + for _, alarm := range alarmList { + if strings.TrimSpace(alarm.AlarmName) == "" { + continue + } + list = append(list, alarm.AlarmName) + // 最长20条数据 + if len(list) >= 20 { + break } } return @@ -2098,6 +2113,24 @@ func convertString2Map(list []string) map[string]bool { return hashMap } +func filterRepeatAndEmptyNameAlarm(list []*m.SimpleAlarm) []*m.SimpleAlarm { + var newList []*m.SimpleAlarm + hashMap := make(map[string]*m.SimpleAlarm) + for _, alarm := range list { + if alarm.AlarmName == "" { + continue + } + if _, ok := hashMap[alarm.AlarmName]; ok { + continue + } + hashMap[alarm.AlarmName] = alarm + } + for _, value := range hashMap { + newList = append(newList, value) + } + return newList +} + func getLevelSQL(levelMap map[string]bool) string { var levelFilterSql string switch len(levelMap) { diff --git a/monitor-server/services/db/alarm_strategy.go b/monitor-server/services/db/alarm_strategy.go index 3c6a525db..cec620a22 100644 --- a/monitor-server/services/db/alarm_strategy.go +++ b/monitor-server/services/db/alarm_strategy.go @@ -158,9 +158,10 @@ func GetAlarmStrategy(strategyGuid, conditionCrc string) (result models.AlarmStr } func CreateAlarmStrategy(param *models.GroupStrategyObj, operator string) error { + var err error + var actions []*Action nowTime := time.Now().Format(models.DatetimeFormat) - actions, err := getCreateAlarmStrategyActions(param, nowTime, operator) - if err != nil { + if actions, err = getCreateAlarmStrategyActions(param, nowTime, operator); err != nil { return err } return Transaction(actions) @@ -178,6 +179,19 @@ func getCreateAlarmStrategyActions(param *models.GroupStrategyObj, nowTime, oper actions = append(actions, getNotifyListInsertAction(param.NotifyList)...) } if len(param.Conditions) > 0 { + for _, condition := range param.Conditions { + // 创建时候 metric_name web传递可能为空,从metric截取下 + if strings.TrimSpace(condition.MetricName) == "" && len(condition.Metric) > 0 { + condition.MetricName = strings.Split(condition.Metric, "__")[0] + } + if condition.LogType == "" { + if logType, err2 := GetLogTypeByMetric(condition.MetricName); err != nil { + log.Logger.Error("GetLogTypeByMetric err", log.Error(err2)) + } else { + condition.LogType = logType + } + } + } insertConditionActions, buildActionErr := getStrategyConditionInsertAction(param.Guid, param.Conditions) if buildActionErr != nil { err = buildActionErr @@ -202,17 +216,16 @@ func ValidateAlarmStrategyName(param *models.GroupStrategyObj) (err error) { func UpdateAlarmStrategy(param *models.GroupStrategyObj, operator string) error { nowTime := time.Now().Format(models.DatetimeFormat) - var actions []*Action + var updateConditionActions, actions []*Action + var err error updateAction := Action{Sql: "update alarm_strategy set name=?,priority=?,content=?,notify_enable=?,notify_delay_second=?,active_window=?,update_time=?,update_user=? where guid=?"} updateAction.Param = []interface{}{param.Name, param.Priority, param.Content, param.NotifyEnable, param.NotifyDelaySecond, param.ActiveWindow, nowTime, operator, param.Guid} actions = append(actions, &updateAction) for _, v := range param.NotifyList { v.AlarmStrategy = param.Guid } - //actions = append(actions, getNotifyListDeleteAction(param.Guid, "", "")...) - //actions = append(actions, getNotifyListInsertAction(param.NotifyList)...) actions = append(actions, getNotifyListUpdateAction(param.NotifyList)...) - updateConditionActions, err := getStrategyConditionUpdateAction(param.Guid, param.Conditions) + updateConditionActions, err = getStrategyConditionUpdateAction(param.Guid, param.Conditions) if err != nil { return err } @@ -470,8 +483,8 @@ func getStrategyConditionInsertAction(alarmStrategyGuid string, conditions []*mo if _, ok := monitorEngineMetricMap[metricRow.Metric]; ok { monitorEngineFlag = 1 } - actions = append(actions, &Action{Sql: "insert into alarm_strategy_metric(guid,alarm_strategy,metric,`condition`,`last`,create_time,crc_hash,monitor_engine) values (?,?,?,?,?,?,?,?)", Param: []interface{}{ - metricGuidList[i], alarmStrategyGuid, metricRow.Metric, metricRow.Condition, metricRow.Last, nowTime, tmpCrcHash, monitorEngineFlag, + actions = append(actions, &Action{Sql: "insert into alarm_strategy_metric(guid,alarm_strategy,metric,`condition`,`last`,create_time,crc_hash,monitor_engine,log_type) values (?,?,?,?,?,?,?,?,?)", Param: []interface{}{ + metricGuidList[i], alarmStrategyGuid, metricRow.Metric, metricRow.Condition, metricRow.Last, nowTime, tmpCrcHash, monitorEngineFlag, metricRow.LogType, }}) if len(metricRow.Tags) > 0 { tagGuidList := guid.CreateGuidList(len(metricRow.Tags)) @@ -488,6 +501,15 @@ func getStrategyConditionInsertAction(alarmStrategyGuid string, conditions []*mo func getStrategyConditionUpdateAction(alarmStrategyGuid string, conditions []*models.StrategyConditionObj) (actions []*Action, err error) { actions = append(actions, getStrategyConditionDeleteAction(alarmStrategyGuid)...) + for _, condition := range conditions { + if condition.LogType == "" { + if logType, err2 := GetLogTypeByMetric(condition.MetricName); err != nil { + log.Logger.Error("GetLogTypeByMetric err", log.Error(err2)) + } else { + condition.LogType = logType + } + } + } insertConditionActions, getErr := getStrategyConditionInsertAction(alarmStrategyGuid, conditions) if getErr != nil { err = getErr @@ -600,7 +622,7 @@ func getAlarmStrategyWithExprNew(endpointGroup string) (result, monitorEngineStr return } var strategyMetricRows []*models.AlarmStrategyMetricWithExpr - err = x.SQL("select t1.guid,t1.alarm_strategy,t1.metric,t1.`condition`,t1.`last`,t1.crc_hash,t2.metric as 'metric_name',t2.prom_expr as 'metric_expr',t2.monitor_type as 'metric_type',t1.monitor_engine from alarm_strategy_metric t1 left join metric t2 on t1.metric=t2.guid where t1.alarm_strategy in (select guid from alarm_strategy where endpoint_group=?)", endpointGroup).Find(&strategyMetricRows) + err = x.SQL("select t1.guid,t1.alarm_strategy,t1.metric,t1.`condition`,t1.`last`,t1.crc_hash,t1.log_type,t2.metric as 'metric_name',t2.prom_expr as 'metric_expr',t2.monitor_type as 'metric_type',t1.monitor_engine from alarm_strategy_metric t1 left join metric t2 on t1.metric=t2.guid where t1.alarm_strategy in (select guid from alarm_strategy where endpoint_group=?)", endpointGroup).Find(&strategyMetricRows) if err != nil { err = fmt.Errorf("query alarm strategy metric with endpointGroup:%s fail,%s ", endpointGroup, err.Error()) return @@ -660,6 +682,7 @@ func getAlarmStrategyWithExprNew(endpointGroup string) (result, monitorEngineStr tmpStrategyObj.MetricExpr = metricRow.MetricExpr tmpStrategyObj.MetricType = metricRow.MetricType tmpStrategyObj.ConditionCrc = metricRow.CrcHash + tmpStrategyObj.LogType = metricRow.LogType tmpStrategyObj.Tags = []*models.MetricTag{} for _, tagRow := range tagRows { if tagRow.AlarmStrategyMetric == metricRow.Guid { @@ -764,17 +787,30 @@ func buildStrategyAlarmRuleExpr(guidExpr, addressExpr, ipExpr string, strategy * } } if len(strategy.Tags) > 0 { - for _, tagObj := range strategy.Tags { - tagSourceString := "$t_" + tagObj.TagName - if strings.Contains(strategy.MetricExpr, tagSourceString) { - if len(tagObj.TagValue) == 0 { - strategy.MetricExpr = strings.Replace(strategy.MetricExpr, "=\""+tagSourceString+"\"", "=~\".*\"", -1) + if strategy.LogType == models.LogMonitorCustomType { + originPromQl := strategy.MetricExpr + // 原表达式: node_log_metric_monitor_value{key="key",agg="max",service_group="log_sys",tags="$t_tags"} + // 正则字符串匹配 替换成 node_log_metric_monitor_value{key="key",agg="max",service_group="log_sys",tags!~".*test_service_code=addUser.*"} + for i, tagObj := range strategy.Tags { + if i == 0 { + strategy.MetricExpr = getTagPromQl(convertMetricTag2Dto(tagObj), originPromQl) } else { - tmpEqual := "=~" - if tagObj.Equal == "notin" { - tmpEqual = "!~" + strategy.MetricExpr = "(" + strategy.MetricExpr + ") and (" + getTagPromQl(convertMetricTag2Dto(tagObj), originPromQl) + ")" + } + } + } else { + for _, tagObj := range strategy.Tags { + tagSourceString := "$t_" + tagObj.TagName + if strings.Contains(strategy.MetricExpr, tagSourceString) { + if len(tagObj.TagValue) == 0 { + strategy.MetricExpr = strings.Replace(strategy.MetricExpr, "=\""+tagSourceString+"\"", "=~\".*\"", -1) + } else { + tmpEqual := "=~" + if tagObj.Equal == "notin" { + tmpEqual = "!~" + } + strategy.MetricExpr = strings.Replace(strategy.MetricExpr, "=\""+tagSourceString+"\"", tmpEqual+"\""+strings.Join(tagObj.TagValue, "|")+"\"", -1) } - strategy.MetricExpr = strings.Replace(strategy.MetricExpr, "=\""+tagSourceString+"\"", tmpEqual+"\""+strings.Join(tagObj.TagValue, "|")+"\"", -1) } } } @@ -784,10 +820,24 @@ func buildStrategyAlarmRuleExpr(guidExpr, addressExpr, ipExpr string, strategy * } } +func convertMetricTag2Dto(tag *models.MetricTag) *models.TagDto { + if tag == nil { + return &models.TagDto{} + } + return &models.TagDto{ + TagName: tag.TagName, + Equal: tag.Equal, + TagValue: tag.TagValue, + } +} + func copyStrategyListNew(inputs []*models.AlarmStrategyMetricObj) (result []*models.AlarmStrategyMetricObj) { result = []*models.AlarmStrategyMetricObj{} for _, strategy := range inputs { - tmpStrategy := models.AlarmStrategyMetricObj{Guid: strategy.Guid, Metric: strategy.Metric, Condition: strategy.Condition, Last: strategy.Last, Priority: strategy.Priority, Content: strategy.Content, NotifyEnable: strategy.NotifyEnable, NotifyDelaySecond: strategy.NotifyDelaySecond, MetricName: strategy.MetricName, MetricExpr: strategy.MetricExpr, MetricType: strategy.MetricType, ConditionCrc: strategy.ConditionCrc, Tags: strategy.Tags} + tmpStrategy := models.AlarmStrategyMetricObj{Guid: strategy.Guid, Metric: strategy.Metric, Condition: strategy.Condition, + Last: strategy.Last, Priority: strategy.Priority, Content: strategy.Content, NotifyEnable: strategy.NotifyEnable, + NotifyDelaySecond: strategy.NotifyDelaySecond, MetricName: strategy.MetricName, MetricExpr: strategy.MetricExpr, + MetricType: strategy.MetricType, ConditionCrc: strategy.ConditionCrc, Tags: strategy.Tags, LogType: strategy.LogType} result = append(result, &tmpStrategy) } return result @@ -1208,7 +1258,7 @@ func getRoleMail(roleList []string) (mailList []string) { return } -func ImportAlarmStrategy(queryType, inputGuid string, param []*models.EndpointStrategyObj, operator string) (err error, metricNotFound, nameDuplicate []string) { +func ImportAlarmStrategy(queryType, inputGuid string, param []*models.EndpointStrategyObj, operator, importRule string) (err error, metricNotFound, nameDuplicate []string) { if len(param) == 0 { err = fmt.Errorf("import content empty ") return @@ -1239,7 +1289,7 @@ func ImportAlarmStrategy(queryType, inputGuid string, param []*models.EndpointSt return } endpointGroupList = append(endpointGroupList, inputGuid) - tmpActions, tmpErr, tmpMetricNotFound, tmpNameDuplicate := getAlarmStrategyImportActions(inputGuid, "", endpointGroupTable[0].MonitorType, nowTime, operator, param[0], metricMap) + tmpActions, tmpErr, tmpMetricNotFound, tmpNameDuplicate := getAlarmStrategyImportActions(inputGuid, nowTime, operator, importRule, param[0], metricMap) if tmpErr != nil { metricNotFound = tmpMetricNotFound nameDuplicate = tmpNameDuplicate @@ -1268,7 +1318,7 @@ func ImportAlarmStrategy(queryType, inputGuid string, param []*models.EndpointSt return } endpointGroupList = append(endpointGroupList, tmpMatchEndpointGroup) - tmpActions, tmpErr, tmpMetricNotFound, tmpNameDuplicate := getAlarmStrategyImportActions(tmpMatchEndpointGroup, inputGuid, v.MonitorType, nowTime, operator, v, metricMap) + tmpActions, tmpErr, tmpMetricNotFound, tmpNameDuplicate := getAlarmStrategyImportActions(tmpMatchEndpointGroup, nowTime, operator, importRule, v, metricMap) if tmpErr != nil { metricNotFound = tmpMetricNotFound nameDuplicate = tmpNameDuplicate @@ -1298,7 +1348,7 @@ func ImportAlarmStrategy(queryType, inputGuid string, param []*models.EndpointSt return } -func getAlarmStrategyImportActions(endpointGroup, serviceGroup, monitorType, nowTime, operator string, param *models.EndpointStrategyObj, metricMap map[string]*models.MetricTable) (actions []*Action, err error, metricNotFound, nameDuplicate []string) { +func getAlarmStrategyImportActions(endpointGroup, nowTime, operator, importRule string, param *models.EndpointStrategyObj, metricMap map[string]*models.MetricTable) (actions []*Action, err error, metricNotFound, nameDuplicate []string) { var existStrategyTable []*models.AlarmStrategyTable var systemAlarmStrategyMap = convertString2Map(systemAlarmStrategyIds) var list []*models.GroupStrategyObj @@ -1325,12 +1375,21 @@ func getAlarmStrategyImportActions(endpointGroup, serviceGroup, monitorType, now } for _, strategy := range param.Strategy { strategy.EndpointGroup = endpointGroup - if _, ok := existNameMap[strategy.Name]; ok { - strategy.Name = strategy.Name + "_1" - if _, doubleCheck := existNameMap[strategy.Name]; doubleCheck { - err = fmt.Errorf("name: %s duplicate", strategy.Name) - nameDuplicate = append(nameDuplicate, strategy.Name) - return + if guid, ok := existNameMap[strategy.Name]; ok { + // 覆盖模式 + if importRule == string(models.ImportRuleCover) { + var delAlarmStrategyActions []*Action + if delAlarmStrategyActions, _, err = GetDeleteAlarmStrategyActions(guid); err != nil { + return + } + actions = append(actions, delAlarmStrategyActions...) + } else { + strategy.Name = strategy.Name + "_1" + if _, doubleCheck := existNameMap[strategy.Name]; doubleCheck { + err = fmt.Errorf("name: %s duplicate", strategy.Name) + nameDuplicate = append(nameDuplicate, strategy.Name) + return + } } } // 检测策略上的指标在不在 @@ -1349,6 +1408,11 @@ func getAlarmStrategyImportActions(endpointGroup, serviceGroup, monitorType, now err = fmt.Errorf("Metric:%s not found ", tmpMetricName) break } + if logType, err2 := GetLogTypeByMetric(tmpMetricName); err2 != nil { + log.Logger.Error("GetLogTypeByMetric err", log.Error(err2)) + } else { + v.LogType = logType + } } } else { tmpMetricName := strategy.MetricName @@ -1367,6 +1431,7 @@ func getAlarmStrategyImportActions(endpointGroup, serviceGroup, monitorType, now if err != nil { return } + newAction, buildErr := getCreateAlarmStrategyActions(strategy, nowTime, operator) if buildErr != nil { err = buildErr @@ -1476,6 +1541,14 @@ func GetMonitorEngineAlarmList() (alarmList []*models.AlarmTable, err error) { } func GetAlarmStrategyNotifyWorkflowList() (result []*models.WorkflowDto, err error) { - err = x.SQL("select distinct proc_callback_name as name,proc_callback_key as 'key' from notify where proc_callback_key!='' and proc_callback_key is not null").Find(&result) + result = []*models.WorkflowDto{} + var tempList []*models.WorkflowDto + err = x.SQL("select distinct proc_callback_name as name,proc_callback_key as 'key' from notify where proc_callback_key!='' and proc_callback_name!=''").Find(&tempList) + for _, dto := range tempList { + if strings.TrimSpace(dto.Name) == "" || strings.TrimSpace(dto.Key) == "" { + continue + } + result = append(result, &models.WorkflowDto{Key: dto.Key, Name: dto.Name}) + } return } diff --git a/monitor-server/services/db/auto_generate_aram.go b/monitor-server/services/db/auto_generate_aram.go index 413d72de6..c285f28b5 100644 --- a/monitor-server/services/db/auto_generate_aram.go +++ b/monitor-server/services/db/auto_generate_aram.go @@ -47,6 +47,9 @@ func autoGenerateAlarmStrategy(alarmStrategyParam models.AutoAlarmStrategyParam) codeList = append(codeList, constOtherCode) for _, code := range codeList { for _, alarmMetric := range autoAlarmMetricList { + if !alarmMetric.AutoWarn { + continue + } // 添加告警配置基础信息 alarmStrategy := &models.GroupStrategyObj{NotifyList: make([]*models.NotifyObj, 0), Conditions: make([]*models.StrategyConditionObj, 0)} metricTags := make([]*models.MetricTag, 0) @@ -139,6 +142,9 @@ func autoGenerateSimpleAlarmStrategy(alarmStrategyParam models.AutoSimpleAlarmSt } autoAlarmMetricList := getAutoSimpleAlarmMetricList(alarmStrategyParam.MetricList, alarmStrategyParam.ServiceGroup, alarmStrategyParam.MetricPrefixCode) for _, alarmMetric := range autoAlarmMetricList { + if !alarmMetric.AutoWarn { + continue + } // 添加告警配置基础信息 alarmStrategy := &models.GroupStrategyObj{NotifyList: make([]*models.NotifyObj, 0), Conditions: make([]*models.StrategyConditionObj, 0)} metricTags := make([]*models.MetricTag, 0) @@ -171,6 +177,7 @@ func autoGenerateSimpleAlarmStrategy(alarmStrategyParam models.AutoSimpleAlarmSt Condition: fmt.Sprintf("%s%s", alarmMetric.Operator, alarmMetric.Threshold), Last: fmt.Sprintf("%s%s", alarmMetric.Time, alarmMetric.TimeUnit), Tags: metricTags, + LogType: alarmStrategyParam.LogType, }) alarmStrategy.Condition = fmt.Sprintf("%s%s", alarmMetric.Operator, alarmMetric.Threshold) alarmStrategy.Last = fmt.Sprintf("%s%s", alarmMetric.Time, alarmMetric.TimeUnit) @@ -452,7 +459,7 @@ func generateMetricGuidDisplayName(metricPrefixCode, metric, displayServiceGroup func getServiceGroupRoles(serviceGroup string) []string { var optionModels []*models.OptionModel var roles []string - optionModels, _ = GetOrgRole(serviceGroup) + optionModels, _ = GetOrgRoleNew(serviceGroup) if len(optionModels) == 0 { return roles } @@ -508,12 +515,18 @@ func getAutoAlarmMetricList(list []*models.LogMetricTemplate, serviceGroup, metr if logMetricTemplate.AutoAlarm && logMetricTemplate.RangeConfig != "" { temp := &models.ThresholdConfig{} json.Unmarshal([]byte(logMetricTemplate.RangeConfig), temp) + // 此处添加数据校验,强制校验阈值数据,防止Prometheus解析数据失败挂掉 + if strings.TrimSpace(temp.Operator) == "" || strings.TrimSpace(temp.Threshold) == "" || strings.TrimSpace(temp.Time) == "" || strings.TrimSpace(temp.TimeUnit) == "" { + log.Logger.Warn("getAutoAlarmMetricList strategy format invalid", log.JsonObj("strategy", temp)) + continue + } metricThresholdList = append(metricThresholdList, &models.LogMetricThreshold{ MetricId: generateMetricGuid(metric, serviceGroup), Metric: logMetricTemplate.Metric, DisplayName: logMetricTemplate.DisplayName, ThresholdConfig: temp, TagConfig: logMetricTemplate.TagConfigList, + AutoWarn: logMetricTemplate.AutoAlarm, }) } } @@ -541,6 +554,7 @@ func getAutoSimpleAlarmMetricList(list []*models.LogMetricConfigDto, serviceGrou DisplayName: logMetricTemplate.DisplayName, ThresholdConfig: temp, TagConfig: logMetricTemplate.TagConfigList, + AutoWarn: logMetricTemplate.AutoAlarm, }) } } diff --git a/monitor-server/services/db/custom_dashboard.go b/monitor-server/services/db/custom_dashboard.go index 7bbd19a51..4a1925b36 100644 --- a/monitor-server/services/db/custom_dashboard.go +++ b/monitor-server/services/db/custom_dashboard.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "github.com/WeBankPartners/go-common-lib/guid" + "github.com/WeBankPartners/open-monitor/monitor-server/middleware/log" "github.com/WeBankPartners/open-monitor/monitor-server/models" "sort" "strconv" @@ -661,8 +662,21 @@ func handleDashboardChart(param *models.CustomDashboardExportDto, newDashboardId return } -func deleteCustomDashboard(customDashboardId int64) (err error) { - _, err = x.Exec("delete from custom_dashboard where id=?", customDashboardId) +func deleteCustomDashboard(customDashboardId int64) { + var err error + if _, err = x.Exec("delete from custom_dashboard where id=?", customDashboardId); err != nil { + log.Logger.Error("deleteCustomDashboard fail", log.Error(err)) + } + return +} + +func deleteCustomDashboardList(customDashboardIdList []int64) { + var err error + for _, id := range customDashboardIdList { + if _, err = x.Exec("delete from custom_dashboard where id=?", id); err != nil { + log.Logger.Error("deleteCustomDashboard fail", log.Error(err)) + } + } return } @@ -736,6 +750,14 @@ func handleAutoCreateChart(chart *models.CustomChartDto, newDashboardId int64, u return } +func BatchGetCustomDashboardByIds(ids []string) (list []*models.CustomDashboardTable, err error) { + err = x.SQL(fmt.Sprintf("select id,name,update_user,update_at from custom_dashboard where id in (%s)", strings.Join(ids, ","))).Find(&list) + for _, dashboard := range list { + dashboard.UpdateAtStr = dashboard.UpdateAt.Format(models.DatetimeFormat) + } + return +} + func convertLineTypeIntToString(lineType int) string { switch lineType { case 1: diff --git a/monitor-server/services/db/dashboard.go b/monitor-server/services/db/dashboard.go index 9f73480e7..faa250245 100644 --- a/monitor-server/services/db/dashboard.go +++ b/monitor-server/services/db/dashboard.go @@ -172,7 +172,8 @@ func GetPromMetric(endpoint []string, metric string) (error, string) { } func ReplacePromQlKeyword(promQl, metric string, host *m.EndpointNewTable, tagList []*m.TagDto) string { - var tmpTag string + var tmpTag, logType string + var err error if strings.Contains(metric, "/") { tmpTag = metric[strings.Index(metric, "/")+1:] } @@ -204,24 +205,35 @@ func ReplacePromQlKeyword(promQl, metric string, host *m.EndpointNewTable, tagLi if strings.Contains(promQl, "$pod") { promQl = strings.Replace(promQl, "$pod", host.Name, -1) } - //if strings.Contains(promQl, "$k8s_namespace") { - // promQl = strings.Replace(promQl, "$k8s_namespace", host.ExportVersion, -1) - //} - //if strings.Contains(promQl, "$k8s_cluster") { - // promQl = strings.Replace(promQl, "$k8s_cluster", host.OsType, -1) - //} + // 看指标是否为 业务配置过来的,查询业务配置类型,自定义类型需要特殊处理 tags,tags="test_service_code=deleteUser,test_retcode=304" + if logType, err = GetLogTypeByMetric(metric); err != nil { + log.Logger.Error("GetLogTypeByMetric err", log.Error(err)) + } if len(tagList) > 0 { - for _, tagObj := range tagList { - tagSourceString := "$t_" + tagObj.TagName - if strings.Contains(promQl, tagSourceString) { - if len(tagObj.TagValue) == 0 { - promQl = strings.Replace(promQl, "=\""+tagSourceString+"\"", "=~\".*\"", -1) + if logType == m.LogMonitorCustomType { + originPromQl := promQl + // 原表达式: node_log_metric_monitor_value{key="key",agg="max",service_group="log_sys",tags="$t_tags"} + // 正则字符串匹配 替换成 node_log_metric_monitor_value{key="key",agg="max",service_group="log_sys",tags!~".*test_service_code=addUser.*"} + for i, tagObj := range tagList { + if i == 0 { + promQl = getTagPromQl(tagObj, originPromQl) } else { - tmpEqual := "=~" - if tagObj.Equal == "notin" { - tmpEqual = "!~" + promQl = "(" + promQl + ") and (" + getTagPromQl(tagObj, originPromQl) + ")" + } + } + } else { + for _, tagObj := range tagList { + tagSourceString := "$t_" + tagObj.TagName + if strings.Contains(promQl, tagSourceString) { + if len(tagObj.TagValue) == 0 { + promQl = strings.Replace(promQl, "=\""+tagSourceString+"\"", "=~\".*\"", -1) + } else { + tmpEqual := "=~" + if tagObj.Equal == "notin" { + tmpEqual = "!~" + } + promQl = strings.Replace(promQl, "=\""+tagSourceString+"\"", tmpEqual+"\""+strings.Join(tagObj.TagValue, "|")+"\"", -1) } - promQl = strings.Replace(promQl, "=\""+tagSourceString+"\"", tmpEqual+"\""+strings.Join(tagObj.TagValue, "|")+"\"", -1) } } } @@ -236,6 +248,22 @@ func ReplacePromQlKeyword(promQl, metric string, host *m.EndpointNewTable, tagLi return promQl } +func getTagPromQl(tagObj *m.TagDto, originPromQl string) (promQl string) { + if len(tagObj.TagValue) == 0 { + promQl = strings.Replace(originPromQl, "=\"$t_tags\"", "=~\".*"+tagObj.TagName+".*\"", -1) + } else { + tmpEqual := "=~" + if tagObj.Equal == "notin" { + tmpEqual = "!~" + } + for i, tagValue := range tagObj.TagValue { + tagObj.TagValue[i] = fmt.Sprintf(".*%s=%s.*", tagObj.TagName, tagValue) + } + promQl = strings.Replace(originPromQl, "=\"$t_tags\"", tmpEqual+"\""+strings.Join(tagObj.TagValue, "|")+"\"", -1) + } + return +} + func GetDbPromMetric(endpoint, metric, legend string) (error, string) { promQL := "db_monitor_count" var query []*m.PromMetricTable diff --git a/monitor-server/services/db/entity_model.go b/monitor-server/services/db/entity_model.go index 5e74e9384..12c3ce836 100644 --- a/monitor-server/services/db/entity_model.go +++ b/monitor-server/services/db/entity_model.go @@ -134,15 +134,13 @@ func AnalyzeTransExportData(param *models.AnalyzeTransParam) (result *models.Ana for _, row := range logMetricMonitorRows { result.LogMonitorServiceGroup = append(result.LogMonitorServiceGroup, row.ServiceGroup) } - var logMetricGroupRows []*models.LogMetricGroup - err = x.SQL("select distinct log_monitor_template from log_metric_group where log_monitor_template<>'' and log_metric_monitor in (select guid from log_metric_monitor where service_group in ("+serviceGroupFilterSql+"))", serviceGroupFilterParams...).Find(&logMetricGroupRows) + var logMonitorTemplates []string + err = x.SQL("select guid from log_monitor_template").Find(&logMonitorTemplates) if err != nil { err = fmt.Errorf("query log metric group table fail,%s ", err.Error()) return } - for _, row := range logMetricGroupRows { - result.LogMonitorTemplate = append(result.LogMonitorTemplate, row.LogMonitorTemplate) - } + result.LogMonitorTemplate = logMonitorTemplates var logKeywordMonitorRows []*models.LogKeywordMonitorTable err = x.SQL("select t1.service_group from (select service_group from log_keyword_monitor union select service_group from db_keyword_monitor) t1 where t1.service_group in ("+serviceGroupFilterSql+")", serviceGroupFilterParams...).Find(&logKeywordMonitorRows) if err != nil { diff --git a/monitor-server/services/db/log_metric.go b/monitor-server/services/db/log_metric.go index f26f41e57..92f22461f 100644 --- a/monitor-server/services/db/log_metric.go +++ b/monitor-server/services/db/log_metric.go @@ -768,8 +768,10 @@ func regexp2FindStringMatch(re *regexp2.Regexp, lineText string) (matchString st return } -func ImportLogMetric(param *models.LogMetricQueryObj, operator string, errMsgObj *models.ErrorMessageObj) (err error) { +func ImportLogMetric(param *models.LogMetricQueryObj, operator string, roles []string, errMsgObj *models.ErrorMessageObj) (err error) { var actions []*Action + var dashboardIdList []int64 + var existLogMetricMonitorMap = make(map[string]*models.LogMetricMonitorObj) existData, queryErr := GetLogMetricByServiceGroup(param.Guid, "") if queryErr != nil { return fmt.Errorf("get exist log metric data fail,%s ", queryErr.Error()) @@ -782,8 +784,29 @@ func ImportLogMetric(param *models.LogMetricQueryObj, operator string, errMsgObj err = getErr return } + // delete action + if len(param.Config) > 0 { + for _, existLogMonitor := range existData.Config { + existLogMetricMonitorMap[existLogMonitor.LogPath] = existLogMonitor + for _, v := range existLogMonitor.EndpointRel { + affectHostMap[v.SourceEndpoint] = 1 + } + tmpDeleteActions, affectHost, affectEndpointGroup := getDeleteLogMetricMonitor(existLogMonitor.Guid) + actions = append(actions, tmpDeleteActions...) + for _, v := range affectHost { + affectHostMap[v] = 1 + } + for _, v := range affectEndpointGroup { + affectEndpointGroupMap[v] = 1 + } + } + } for _, inputLogMonitor := range param.Config { existObj := &models.LogMetricMonitorObj{} + // 业务配置映射关系需要保留 + if v, ok := existLogMetricMonitorMap[inputLogMonitor.LogPath]; ok { + existObj.EndpointRel = v.EndpointRel + } if existObj.Guid != "" { if existObj.LogPath != inputLogMonitor.LogPath || existObj.MonitorType != inputLogMonitor.MonitorType { actions = append(actions, &Action{Sql: "update log_metric_monitor set log_path=?,monitor_type=? where guid=?", Param: []interface{}{inputLogMonitor.LogPath, inputLogMonitor.MonitorType, inputLogMonitor.Guid}}) @@ -791,10 +814,26 @@ func ImportLogMetric(param *models.LogMetricQueryObj, operator string, errMsgObj } else { inputLogMonitor.Guid = "lmm_" + guid.CreateGuid() actions = append(actions, &Action{Sql: "insert into log_metric_monitor(guid,service_group,log_path,metric_type,monitor_type,update_time) value (?,?,?,?,?,?)", Param: []interface{}{inputLogMonitor.Guid, param.Guid, inputLogMonitor.LogPath, inputLogMonitor.MetricType, inputLogMonitor.MonitorType, nowTime}}) + if len(existObj.EndpointRel) > 0 { + for _, endpointRel := range existObj.EndpointRel { + actions = append(actions, &Action{Sql: "insert into log_metric_endpoint_rel(guid,log_metric_monitor,source_endpoint,target_endpoint) value (?,?,?,?)", Param: []interface{}{guid.CreateGuid(), inputLogMonitor.Guid, endpointRel.SourceEndpoint, endpointRel.TargetEndpoint}}) + } + } else { + // 如果 不存在对象映射关系,查询所有可以添加的映射关系 + var endpointRelList []*models.LogMetricEndpointRelTable + if endpointRelList, err = GetServiceGroupEndpointRel(param.ServiceGroupTable.Guid, "host", inputLogMonitor.MonitorType); err != nil { + log.Logger.Error("GetServiceGroupEndpointRel err", log.Error(err)) + } + for _, endpointRel := range endpointRelList { + actions = append(actions, &Action{Sql: "insert into log_metric_endpoint_rel(guid,log_metric_monitor,source_endpoint,target_endpoint) value (?,?,?,?)", Param: []interface{}{guid.CreateGuid(), inputLogMonitor.Guid, endpointRel.SourceEndpoint, endpointRel.TargetEndpoint}}) + } + } } - tmpActions, tmpAffectHosts, tmpAffectEndpointGroup, tmpErr := getUpdateLogMetricMonitorByImport(existObj, inputLogMonitor, nowTime, operator, serviceGroupMetricMap, errMsgObj) + tmpActions, subDashboardIdList, tmpAffectHosts, tmpAffectEndpointGroup, tmpErr := getUpdateLogMetricMonitorByImport(existObj, inputLogMonitor, nowTime, operator, serviceGroupMetricMap, errMsgObj, roles) + dashboardIdList = append(dashboardIdList, subDashboardIdList...) if tmpErr != nil { err = tmpErr + deleteCustomDashboardList(dashboardIdList) return } actions = append(actions, tmpActions...) @@ -805,38 +844,28 @@ func ImportLogMetric(param *models.LogMetricQueryObj, operator string, errMsgObj affectEndpointGroupMap[v] = 1 } } - // delete action - for _, existLogMonitor := range existData.Config { - for _, v := range existLogMonitor.EndpointRel { - affectHostMap[v.SourceEndpoint] = 1 - } - deleteFlag := true - //for _, inputLogMonitor := range param.Config { - // if existLogMonitor.Guid == inputLogMonitor.Guid { - // deleteFlag = false - // break - // } - //} - if deleteFlag { - tmpDeleteActions, affectHost, affectEndpointGroup := getDeleteLogMetricMonitor(existLogMonitor.Guid) - actions = append(actions, tmpDeleteActions...) - for _, v := range affectHost { - affectHostMap[v] = 1 - } - for _, v := range affectEndpointGroup { - affectEndpointGroupMap[v] = 1 - } - } - } + for _, dbConfig := range param.DBConfig { tmpDBMetricGuid := fmt.Sprintf("%s__%s", dbConfig.Metric, dbConfig.ServiceGroup) if tmpMetric, existFlag := serviceGroupMetricMap[tmpDBMetricGuid]; existFlag { err = fmt.Errorf("Metric: %s duplicate ", tmpMetric) + deleteCustomDashboardList(dashboardIdList) return } + // 查询所有可以添加的映射关系,重写导入EndpointRel关系 + var endpointRelList []*models.LogMetricEndpointRelTable + if endpointRelList, err = GetServiceGroupEndpointRel(param.ServiceGroupTable.Guid, "mysql", dbConfig.MonitorType); err != nil { + log.Logger.Error("GetServiceGroupEndpointRel err", log.Error(err)) + deleteCustomDashboardList(dashboardIdList) + } + dbConfig.EndpointRel = []*models.DbMetricEndpointRelTable{} + for _, endpointRel := range endpointRelList { + dbConfig.EndpointRel = append(dbConfig.EndpointRel, &models.DbMetricEndpointRelTable{SourceEndpoint: endpointRel.SourceEndpoint, TargetEndpoint: endpointRel.TargetEndpoint}) + } tmpActions, tmpAffectHosts, tmpAffectEndpointGroup, tmpErr := getCreateDBMetricMonitorByImport(dbConfig, nowTime, operator) if tmpErr != nil { err = tmpErr + deleteCustomDashboardList(dashboardIdList) return } actions = append(actions, tmpActions...) @@ -858,6 +887,7 @@ func ImportLogMetric(param *models.LogMetricQueryObj, operator string, errMsgObj err = Transaction(actions) if err != nil { log.Logger.Error("import log monitor exec database fail", log.Error(err)) + deleteCustomDashboardList(dashboardIdList) return } if tmpErr := SyncLogMetricExporterConfig(affectHostList); tmpErr != nil { @@ -871,7 +901,8 @@ func ImportLogMetric(param *models.LogMetricQueryObj, operator string, errMsgObj return } -func getUpdateLogMetricMonitorByImport(existObj, inputObj *models.LogMetricMonitorObj, nowTime, operator string, existMetricMap map[string]string, errMsgObj *models.ErrorMessageObj) (actions []*Action, affectHost []string, affectEndpointGroup []string, err error) { +func getUpdateLogMetricMonitorByImport(existObj, inputObj *models.LogMetricMonitorObj, nowTime, operator string, existMetricMap map[string]string, errMsgObj *models.ErrorMessageObj, roles []string) (actions []*Action, dashboardId []int64, affectHost []string, affectEndpointGroup []string, err error) { + dashboardId = []int64{} if existObj.Guid != "" { // compare log json monitor for _, inputJsonObj := range inputObj.JsonConfigList { @@ -934,7 +965,8 @@ func getUpdateLogMetricMonitorByImport(existObj, inputObj *models.LogMetricMonit actions = append(actions, tmpMetricGroupActions...) affectEndpointGroup = append(affectEndpointGroup, tmpAffect...) } else { - tmpMetricGroupActions, tmpErr := getCreateLogMetricGroupByImport(inputMetricGroup, operator, existMetricMap, errMsgObj) + tmpMetricGroupActions, newDashboardId, tmpErr := getCreateLogMetricGroupByImport(inputMetricGroup, operator, existMetricMap, errMsgObj, roles) + dashboardId = append(dashboardId, newDashboardId) if tmpErr != nil { err = tmpErr return @@ -994,7 +1026,8 @@ func getUpdateLogMetricMonitorByImport(existObj, inputObj *models.LogMetricMonit metricGroup.LogMetricMonitor = inputObj.Guid metricGroup.ServiceGroup = inputObj.ServiceGroup metricGroup.MonitorType = inputObj.MonitorType - tmpActions, tmpErr := getCreateLogMetricGroupByImport(metricGroup, operator, existMetricMap, errMsgObj) + tmpActions, newDashboardId, tmpErr := getCreateLogMetricGroupByImport(metricGroup, operator, existMetricMap, errMsgObj, roles) + dashboardId = append(dashboardId, newDashboardId) if tmpErr != nil { err = tmpErr return @@ -1010,7 +1043,8 @@ func getCreateDBMetricMonitorByImport(inputObj *models.DbMetricMonitorObj, nowTi return } -func getCreateLogMetricGroupByImport(metricGroup *models.LogMetricGroupObj, operator string, existMetricMap map[string]string, errMsgObj *models.ErrorMessageObj) (actions []*Action, err error) { +func getCreateLogMetricGroupByImport(metricGroup *models.LogMetricGroupObj, operator string, existMetricMap map[string]string, errMsgObj *models.ErrorMessageObj, roles []string) (actions []*Action, newDashboardId int64, err error) { + var tmpActions []*Action if metricGroup.LogMonitorTemplate != "" && (metricGroup.LogType == models.LogMonitorRegularType || metricGroup.LogType == models.LogMonitorJsonType) { metricGroup.LogMonitorTemplate, err = GetLogTemplateGuidByName(metricGroup.LogMonitorTemplateName) if err != nil { @@ -1025,6 +1059,8 @@ func getCreateLogMetricGroupByImport(metricGroup *models.LogMetricGroupObj, oper ServiceGroup: metricGroup.ServiceGroup, MonitorType: metricGroup.MonitorType, LogMonitorTemplate: metricGroup.LogMonitorTemplateDto, + AutoCreateWarn: metricGroup.AutoCreateWarn, + AutoCreateDashboard: metricGroup.AutoCreateDashboard, } for _, mgParamObj := range metricGroup.ParamList { if mgParamObj.Name == "code" { @@ -1033,22 +1069,12 @@ func getCreateLogMetricGroupByImport(metricGroup *models.LogMetricGroupObj, oper tmpCreateParam.RetCodeStringMap = mgParamObj.StringMap } } - tmpActions, _, newDashboardId, tmpErr := getCreateLogMetricGroupActions(&tmpCreateParam, operator, []string{}, existMetricMap, errMsgObj) - if tmpErr != nil { - err = tmpErr - if err2 := deleteCustomDashboard(newDashboardId); err2 != nil { - log.Logger.Error("deleteCustomDashboard fail", log.Error(err2)) - } + if tmpActions, _, newDashboardId, err = getCreateLogMetricGroupActions(&tmpCreateParam, operator, roles, existMetricMap, errMsgObj, true); err != nil { return } actions = append(actions, tmpActions...) } else { - tmpActions, _, newDashboardId, tmpErr := getCreateLogMetricCustomGroupActions(metricGroup, operator, existMetricMap, []string{}, errMsgObj) - if tmpErr != nil { - err = tmpErr - if err2 := deleteCustomDashboard(newDashboardId); err2 != nil { - log.Logger.Error("deleteCustomDashboard fail", log.Error(err2)) - } + if tmpActions, _, newDashboardId, err = getCreateLogMetricCustomGroupActions(metricGroup, operator, existMetricMap, roles, errMsgObj, true); err != nil { return } actions = append(actions, tmpActions...) @@ -1064,6 +1090,8 @@ func getUpdateLogMetricGroupByImport(metricGroup *models.LogMetricGroupObj, oper LogMetricMonitorGuid: metricGroup.LogMetricMonitor, LogMonitorTemplateGuid: metricGroup.LogMonitorTemplate, MetricPrefixCode: metricGroup.MetricPrefixCode, + AutoCreateDashboard: metricGroup.AutoCreateDashboard, + AutoCreateWarn: metricGroup.AutoCreateWarn, } for _, mgParamObj := range metricGroup.ParamList { if mgParamObj.Name == "code" { @@ -1240,29 +1268,32 @@ func CreateLogMetricGroup(param *models.LogMetricGroupWithTemplate, operator str param.LogMetricGroupGuid = "" var actions []*Action var newDashboardId int64 - actions, result, newDashboardId, err = getCreateLogMetricGroupActions(param, operator, roles, make(map[string]string), errMsgObj) + actions, result, newDashboardId, err = getCreateLogMetricGroupActions(param, operator, roles, make(map[string]string), errMsgObj, false) if err != nil { - if err2 := deleteCustomDashboard(newDashboardId); err2 != nil { - log.Logger.Error("deleteCustomDashboard fail", log.Error(err2)) - } + deleteCustomDashboard(newDashboardId) return } if err = Transaction(actions); err != nil { - if err2 := deleteCustomDashboard(newDashboardId); err2 != nil { - log.Logger.Error("deleteCustomDashboard fail", log.Error(err2)) - } + deleteCustomDashboard(newDashboardId) } return } -func getCreateLogMetricGroupActions(param *models.LogMetricGroupWithTemplate, operator string, roles []string, existMetricMap map[string]string, errMsgObj *models.ErrorMessageObj) (actions []*Action, result *models.CreateLogMetricGroupDto, newDashboardId int64, err error) { +func getCreateLogMetricGroupActions(param *models.LogMetricGroupWithTemplate, operator string, roles []string, existMetricMap map[string]string, errMsgObj *models.ErrorMessageObj, doImport bool) (actions []*Action, result *models.CreateLogMetricGroupDto, newDashboardId int64, err error) { var templateSnapshot []byte var refTemplateVersion, endpointGroup string var subCreateAlarmStrategyActions, subCreateDashboardActions []*Action var serviceGroupsRoles, alarmStrategyList []string + var autoAlarm, autoDashboard int if param.LogMetricGroupGuid == "" { param.LogMetricGroupGuid = "lmg_" + guid.CreateGuid() } + if param.AutoCreateWarn { + autoAlarm = 1 + } + if param.AutoCreateDashboard { + autoDashboard = 1 + } result = &models.CreateLogMetricGroupDto{AlarmList: make([]string, 0)} logMonitorTemplateObj, getErr := GetLogMonitorTemplate(param.LogMonitorTemplateGuid) if getErr != nil { @@ -1289,8 +1320,20 @@ func getCreateLogMetricGroupActions(param *models.LogMetricGroupWithTemplate, op if param.Name == "" { param.Name = logMonitorTemplateObj.Name } - actions = append(actions, &Action{Sql: "insert into log_metric_group(guid,name,metric_prefix_code,log_type,log_metric_monitor,log_monitor_template,create_user,create_time,update_user,update_time,template_snapshot,ref_template_version) values (?,?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{ - param.LogMetricGroupGuid, param.Name, param.MetricPrefixCode, logMonitorTemplateObj.LogType, param.LogMetricMonitorGuid, param.LogMonitorTemplateGuid, operator, nowTime, operator, nowTime, templateSnapshot, refTemplateVersion, + // 如果输入的映射为空,尝试拿模版的成功映射,为了插件服务自动化生成配置 + if len(param.RetCodeStringMap) == 0 && logMonitorTemplateObj.SuccessCode != "" { + var templateRetCodeMap models.LogMetricStringMapTable + if unmarshalErr := json.Unmarshal([]byte(logMonitorTemplateObj.SuccessCode), &templateRetCodeMap); unmarshalErr == nil { + templateRetCodeMap.ValueType = "success" + param.RetCodeStringMap = append(param.RetCodeStringMap, &templateRetCodeMap) + } else { + log.Logger.Warn("json unmarshal log template success code fail", log.String("successCode", logMonitorTemplateObj.SuccessCode), log.Error(unmarshalErr)) + } + } + actions = append(actions, &Action{Sql: "insert into log_metric_group(guid,name,metric_prefix_code,log_type,log_metric_monitor,log_monitor_template,create_user," + + "create_time,update_user,update_time,template_snapshot,ref_template_version,auto_alarm,auto_dashboard) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{ + param.LogMetricGroupGuid, param.Name, param.MetricPrefixCode, logMonitorTemplateObj.LogType, param.LogMetricMonitorGuid, param.LogMonitorTemplateGuid, operator, + nowTime, operator, nowTime, templateSnapshot, refTemplateVersion, autoAlarm, autoDashboard, }}) sucRetCode, createMapActions := getCreateLogMetricGroupMapAction(param, nowTime) actions = append(actions, createMapActions...) @@ -1313,27 +1356,21 @@ func getCreateLogMetricGroupActions(param *models.LogMetricGroupWithTemplate, op promExpr = getLogMetricExprByAggType(tmpMetricWithPrefix, v.AggType, serviceGroup, v.TagConfigList) } tmpMetricGuid := generateMetricGuid(tmpMetricWithPrefix, serviceGroup) - if duplicateMetric, ok := existMetricMap[tmpMetricGuid]; ok { + if duplicateMetric, ok := existMetricMap[tmpMetricGuid]; ok && !doImport { err = fmt.Errorf("Metric: %s duplicate ", duplicateMetric) return } actions = append(actions, &Action{Sql: "insert into metric(guid,metric,monitor_type,prom_expr,service_group,workspace,update_time,log_metric_template,log_metric_group,create_time,create_user,update_user) value (?,?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{tmpMetricGuid, tmpMetricWithPrefix, monitorType, promExpr, serviceGroup, models.MetricWorkspaceService, nowTime, v.Guid, param.LogMetricGroupGuid, nowTime, operator, operator}}) } - if param.LogMetricMonitorGuid != "" { - var logMetricMonitor = &models.LogMetricMonitorTable{} + if serviceGroup != "" && monitorType != "" { var endpointGroupIds []string - if _, err = x.SQL("select service_group,monitor_type from log_metric_monitor where guid=?", param.LogMetricMonitorGuid).Get(logMetricMonitor); err != nil { + serviceGroupsRoles = getServiceGroupRoles(serviceGroup) + if err = x.SQL("select guid from endpoint_group where service_group=? and monitor_type=?", serviceGroup, monitorType).Find(&endpointGroupIds); err != nil { return } - if logMetricMonitor != nil { - serviceGroupsRoles = getServiceGroupRoles(logMetricMonitor.ServiceGroup) - if err = x.SQL("select guid from endpoint_group where service_group=? and monitor_type=?", logMetricMonitor.ServiceGroup, logMetricMonitor.MonitorType).Find(&endpointGroupIds); err != nil { - return - } - if len(endpointGroupIds) > 0 { - endpointGroup = endpointGroupIds[0] - } + if len(endpointGroupIds) > 0 { + endpointGroup = endpointGroupIds[0] } } if len(serviceGroupsRoles) == 0 && len(roles) > 0 { @@ -1579,8 +1616,8 @@ func ListLogMetricGroups(logMetricMonitor, metricKey string) (result []*models.L for _, v := range logMetricGroupTable { v.CreateTimeString = v.CreateTime.Format(models.DatetimeFormat) v.UpdateTimeString = v.UpdateTime.Format(models.DatetimeFormat) - logMetricGroupData := &models.LogMetricGroupObj{LogMetricGroup: *v} - if v.LogMonitorTemplate != "" { + logMetricGroupData := &models.LogMetricGroupObj{LogMetricGroup: *v, AutoCreateDashboard: v.AutoDashboard == 1, AutoCreateWarn: v.AutoAlarm == 1} + if v.LogMonitorTemplate != "" && v.LogType != "custom" { tmpTemplateObj, tmpGetTemplateErr := GetLogMonitorTemplate(v.LogMonitorTemplate) if tmpGetTemplateErr != nil { log.Logger.Error("ListLogMetricGroups fail get template data ", log.String("templateGuid", v.LogMonitorTemplate), log.Error(tmpGetTemplateErr)) @@ -1591,6 +1628,7 @@ func ListLogMetricGroups(logMetricMonitor, metricKey string) (result []*models.L log.Logger.Error("ListLogMetricGroups getLogMetricGroupMapData fail ", log.String("logMetricGroupGuid", v.Guid), log.Error(getStringMapErr)) } logMetricGroupData.LogMonitorTemplateName = tmpTemplateObj.Name + // 读取 模版的 ParamList for _, tplParam := range tmpTemplateObj.ParamList { tmpLogMetricParamObj := tplParam.TransToLogParam() tmpLogMetricParamObj.StringMap = logMetricStringMapData[tmpLogMetricParamObj.Name] @@ -1612,6 +1650,17 @@ func ListLogMetricGroups(logMetricMonitor, metricKey string) (result []*models.L log.Logger.Error("ListLogMetricGroups fail get custom metric group data ", log.String("logMetricGroupGuid", v.Guid), log.Error(getCustomErr)) } else { logMetricGroupData = customGroupData + logMetricGroupData.AutoCreateDashboard = v.AutoDashboard == 1 + logMetricGroupData.AutoCreateWarn = v.AutoAlarm == 1 + } + if v.LogMonitorTemplate != "" { + tmpTemplateObj, tmpGetTemplateErr := GetLogMonitorTemplate(v.LogMonitorTemplate) + if tmpGetTemplateErr != nil { + log.Logger.Error("ListLogMetricGroups fail get template data ", log.String("templateGuid", v.LogMonitorTemplate), log.Error(tmpGetTemplateErr)) + } else { + logMetricGroupData.LogMonitorTemplateName = tmpTemplateObj.Name + logMetricGroupData.LogMonitorTemplateGuid = v.LogMonitorTemplate + } } } if metricKey != "" && len(logMetricGroupData.MetricList) > 0 { @@ -1629,14 +1678,28 @@ func ListLogMetricGroups(logMetricMonitor, metricKey string) (result []*models.L } func GetLogMetricCustomGroup(logMetricGroupGuid string) (result *models.LogMetricGroupObj, err error) { + var logMonitorTemplate = &models.LogMonitorTemplateDto{} metricGroupObj, getGroupErr := GetSimpleLogMetricGroup(logMetricGroupGuid) if getGroupErr != nil { err = getGroupErr return } + if metricGroupObj.TemplateSnapshot != "" { + if err = json.Unmarshal([]byte(metricGroupObj.TemplateSnapshot), logMonitorTemplate); err != nil { + return + } + } else { + // 历史数据 模版查询兜底 + if logMonitorTemplate, err = GetLogMonitorTemplate(metricGroupObj.LogMonitorTemplate); err != nil { + return + } + metricGroupObj.RefTemplateVersion = logMonitorTemplate.UpdateTime.Format(models.DatetimeDigitFormat) + } result = &models.LogMetricGroupObj{LogMetricGroup: *metricGroupObj, ParamList: []*models.LogMetricParamObj{}, MetricList: []*models.LogMetricConfigDto{}} result.CreateTimeString = result.CreateTime.Format(models.DatetimeFormat) result.UpdateTimeString = result.UpdateTime.Format(models.DatetimeFormat) + result.AutoAlarm = metricGroupObj.AutoAlarm + result.AutoDashboard = metricGroupObj.AutoDashboard logMetricStringMapData, getStringMapErr := getLogMetricGroupMapData(logMetricGroupGuid) if getStringMapErr != nil { err = getStringMapErr @@ -1661,10 +1724,9 @@ func GetLogMetricCustomGroup(logMetricGroupGuid string) (result *models.LogMetri } for _, row := range logMetricConfigRows { json.Unmarshal([]byte(row.TagConfig), &row.TagConfigList) - if strings.TrimSpace(metricGroupObj.MetricPrefixCode) == "" { - row.FullMetric = row.Metric - } else { - row.FullMetric = fmt.Sprintf("%s_%s", metricGroupObj.MetricPrefixCode, row.Metric) + row.FullMetric = row.Metric + if strings.TrimSpace(metricGroupObj.MetricPrefixCode) != "" && len(row.Metric) > len(metricGroupObj.MetricPrefixCode) { + row.Metric = row.Metric[len(metricGroupObj.MetricPrefixCode)+1:] } result.MetricList = append(result.MetricList, convertLogMetricConfigTable2Dto(row)) } @@ -1701,37 +1763,59 @@ func CreateLogMetricCustomGroup(param *models.LogMetricGroupObj, operator string param.Guid = "" var actions []*Action var newDashboardId int64 - if actions, result, newDashboardId, err = getCreateLogMetricCustomGroupActions(param, operator, make(map[string]string), roles, errMsgObj); err != nil { - if err2 := deleteCustomDashboard(newDashboardId); err2 != nil { - log.Logger.Error("deleteCustomDashboard fail", log.Error(err2)) - } + if actions, result, newDashboardId, err = getCreateLogMetricCustomGroupActions(param, operator, make(map[string]string), roles, errMsgObj, false); err != nil { + deleteCustomDashboard(newDashboardId) return } if err = Transaction(actions); err != nil { - if err2 := deleteCustomDashboard(newDashboardId); err2 != nil { - log.Logger.Error("deleteCustomDashboard fail", log.Error(err2)) - } + deleteCustomDashboard(newDashboardId) } return } -func getCreateLogMetricCustomGroupActions(param *models.LogMetricGroupObj, operator string, existMetricMap map[string]string, roles []string, errMsgObj *models.ErrorMessageObj) (actions []*Action, result *models.CreateLogMetricGroupDto, newDashboardId int64, err error) { - var endpointGroup string +func getCreateLogMetricCustomGroupActions(param *models.LogMetricGroupObj, operator string, existMetricMap map[string]string, roles []string, errMsgObj *models.ErrorMessageObj, doImport bool) (actions []*Action, result *models.CreateLogMetricGroupDto, newDashboardId int64, err error) { + var endpointGroup, refTemplateVersion string var subCreateAlarmStrategyActions, subCreateDashboardActions []*Action var serviceGroupsRoles, alarmStrategyList []string + var logMonitorTemplate *models.LogMonitorTemplateDto + var templateSnapshot []byte + var autoAlarm, autoDashboard int param.LogType = "custom" if param.Guid == "" { param.Guid = "lmg_" + guid.CreateGuid() } + if param.AutoCreateWarn { + autoAlarm = 1 + } + if param.AutoCreateDashboard { + autoDashboard = 1 + } nowTime := time.Now() result = &models.CreateLogMetricGroupDto{AlarmList: []string{}} + if strings.TrimSpace(param.LogMonitorTemplateGuid) == "" && strings.TrimSpace(param.LogMonitorTemplate) != "" { + param.LogMonitorTemplateGuid = param.LogMonitorTemplate + } if strings.TrimSpace(param.LogMonitorTemplateGuid) != "" { - actions = append(actions, &Action{Sql: "insert into log_metric_group(guid,name,log_type,log_metric_monitor,log_monitor_template,demo_log,calc_result,create_user,create_time,update_user,update_time,metric_prefix_code) values (?,?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{ - param.Guid, param.Name, param.LogType, param.LogMetricMonitor, param.LogMonitorTemplateGuid, param.DemoLog, param.CalcResult, operator, nowTime, operator, nowTime, param.MetricPrefixCode, + if logMonitorTemplate, err = GetLogMonitorTemplate(param.LogMonitorTemplateGuid); err != nil { + return + } + if logMonitorTemplate == nil { + err = fmt.Errorf("logMonitorTemplate id:%s is valid", param.LogMonitorTemplateGuid) + return + } + if templateSnapshot, err = json.Marshal(logMonitorTemplate); err != nil { + return + } + refTemplateVersion = logMonitorTemplate.UpdateTime.Format(models.DatetimeDigitFormat) + actions = append(actions, &Action{Sql: "insert into log_metric_group(guid,name,log_type,log_metric_monitor,log_monitor_template,demo_log,calc_result,create_user," + + "create_time,update_user,update_time,metric_prefix_code,template_snapshot,ref_template_version,auto_alarm,auto_dashboard) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{ + param.Guid, param.Name, param.LogType, param.LogMetricMonitor, param.LogMonitorTemplateGuid, param.DemoLog, param.CalcResult, operator, nowTime, operator, + nowTime, param.MetricPrefixCode, templateSnapshot, refTemplateVersion, autoAlarm, autoDashboard, }}) } else { - actions = append(actions, &Action{Sql: "insert into log_metric_group(guid,name,log_type,log_metric_monitor,demo_log,calc_result,create_user,create_time,update_user,update_time,metric_prefix_code) values (?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{ - param.Guid, param.Name, param.LogType, param.LogMetricMonitor, param.DemoLog, param.CalcResult, operator, nowTime, operator, nowTime, param.MetricPrefixCode, + actions = append(actions, &Action{Sql: "insert into log_metric_group(guid,name,log_type,log_metric_monitor,demo_log,calc_result,create_user,create_time,update_user," + + "update_time,metric_prefix_code,auto_alarm,auto_dashboard) values (?,?,?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{ + param.Guid, param.Name, param.LogType, param.LogMetricMonitor, param.DemoLog, param.CalcResult, operator, nowTime, operator, nowTime, param.MetricPrefixCode, autoAlarm, autoDashboard, }}) } paramGuidList := guid.CreateGuidList(len(param.ParamList)) @@ -1771,7 +1855,7 @@ func getCreateLogMetricCustomGroupActions(param *models.LogMetricGroupObj, opera tmpMetricTags = []string{"tags"} } tmpMetricGuid := generateMetricGuid(tmpMetricWithPrefix, serviceGroup) - if duplicateMetric, ok := existMetricMap[tmpMetricGuid]; ok { + if duplicateMetric, ok := existMetricMap[tmpMetricGuid]; ok && !doImport { err = fmt.Errorf("Metric: %s duplicate ", duplicateMetric) return } @@ -1794,11 +1878,23 @@ func getCreateLogMetricCustomGroupActions(param *models.LogMetricGroupObj, opera } } } + // endpointGroup为空 + if strings.TrimSpace(endpointGroup) == "" { + var endpointGroupIds []string + serviceGroupsRoles = getServiceGroupRoles(param.ServiceGroup) + if err = x.SQL("select guid from endpoint_group where service_group=? and monitor_type=?", param.ServiceGroup, param.MonitorType).Find(&endpointGroupIds); err != nil { + return + } + if len(endpointGroupIds) > 0 { + endpointGroup = endpointGroupIds[0] + } + } if len(serviceGroupsRoles) == 0 && len(roles) > 0 { serviceGroupsRoles = roles[:1] } alarmStrategyParam := models.AutoSimpleAlarmStrategyParam{MetricList: param.MetricList, ServiceGroupsRoles: serviceGroupsRoles, LogMetricGroupGuid: param.LogMetricGroup.Guid, - ServiceGroup: serviceGroup, EndpointGroup: endpointGroup, Operator: operator, ErrMsgObj: errMsgObj, AutoCreateWarn: param.AutoCreateWarn, MetricPrefixCode: param.LogMetricGroup.MetricPrefixCode} + ServiceGroup: serviceGroup, EndpointGroup: endpointGroup, Operator: operator, ErrMsgObj: errMsgObj, AutoCreateWarn: param.AutoCreateWarn, + MetricPrefixCode: param.LogMetricGroup.MetricPrefixCode, LogType: models.LogMonitorCustomType} if subCreateAlarmStrategyActions, alarmStrategyList, err = autoGenerateSimpleAlarmStrategy(alarmStrategyParam); err != nil { return } @@ -2029,6 +2125,10 @@ func convertLogMetricConfigTable2Dto(config *models.LogMetricConfigTable) (dto * if config.TagConfig != "" && len(config.TagConfigList) == 0 { config.TagConfigList = strings.Split(config.TagConfig, ",") } + var rangeConfig interface{} + if config.RangeConfig != "" { + json.Unmarshal([]byte(config.RangeConfig), &rangeConfig) + } dto = &models.LogMetricConfigDto{ Guid: config.Guid, LogMetricMonitor: config.LogMetricMonitor, @@ -2048,7 +2148,7 @@ func convertLogMetricConfigTable2Dto(config *models.LogMetricConfigTable) (dto * CreateTime: config.CreateTime.Format(models.DatetimeFormat), UpdateTime: config.UpdateTime.Format(models.DatetimeFormat), AutoAlarm: config.AutoAlarm, - RangeConfig: config.RangeConfig, + RangeConfig: rangeConfig, ColorGroup: config.ColorGroup, FullMetric: config.FullMetric, } diff --git a/monitor-server/services/db/log_template.go b/monitor-server/services/db/log_template.go index ff00617e8..0803705f3 100644 --- a/monitor-server/services/db/log_template.go +++ b/monitor-server/services/db/log_template.go @@ -59,7 +59,7 @@ func GetLogMonitorTemplate(logMonitorTemplateGuid string) (result *models.LogMon } logMonitorTemplateRow.CreateTimeString = logMonitorTemplateRow.CreateTime.Format(models.DatetimeFormat) logMonitorTemplateRow.UpdateTimeString = logMonitorTemplateRow.UpdateTime.Format(models.DatetimeFormat) - result = &models.LogMonitorTemplateDto{LogMonitorTemplate: *logMonitorTemplateRow, CalcResultObj: &models.CheckRegExpResult{}, ParamList: []*models.LogParamTemplate{}, MetricList: []*models.LogMetricTemplate{}} + result = &models.LogMonitorTemplateDto{LogMonitorTemplate: *logMonitorTemplateRow, CalcResultObj: &models.CheckRegExpResult{}, ParamList: []*models.LogParamTemplateObj{}, MetricList: []*models.LogMetricTemplate{}} result.LogMonitorTemplateVersion = logMonitorTemplateRow.UpdateTime.Format(models.DatetimeDigitFormat) if result.CalcResult != "" { if err = json.Unmarshal([]byte(result.CalcResult), result.CalcResultObj); err != nil { @@ -73,8 +73,31 @@ func GetLogMonitorTemplate(logMonitorTemplateGuid string) (result *models.LogMon err = fmt.Errorf("query log_param_template table fail,%s ", err.Error()) return } + var logMetricStringMaps []*models.LogMetricStringMapTable + var stringCodeMap = make(map[string][]*models.LogMetricStringMapTable) + err = x.SQL("select * from log_metric_string_map where log_monitor_template=?", logMonitorTemplateGuid).Find(&logMetricStringMaps) + if err != nil { + err = fmt.Errorf("query log_metric_string_map table fail,%s ", err.Error()) + return + } + for _, logMetricString := range logMetricStringMaps { + if _, ok := stringCodeMap[logMetricString.LogParamName]; ok { + stringCodeMap[logMetricString.LogParamName] = append(stringCodeMap[logMetricString.LogParamName], logMetricString) + } else { + stringCodeMap[logMetricString.LogParamName] = []*models.LogMetricStringMapTable{logMetricString} + } + } for _, row := range logParamRows { - result.ParamList = append(result.ParamList, row) + if row == nil { + continue + } + logParamTemplateObj := &models.LogParamTemplateObj{ + LogParamTemplate: *row, + } + if v, ok := stringCodeMap[row.Name]; ok { + logParamTemplateObj.StringMap = v + } + result.ParamList = append(result.ParamList, logParamTemplateObj) } var logMetricRows []*models.LogMetricTemplate if logMetricRows, err = getLogMetricTemplateWithMonitor(logMonitorTemplateGuid); err != nil { @@ -136,6 +159,12 @@ func getCreateLogMonitorTemplateActions(param *models.LogMonitorTemplateDto, ope actions = append(actions, &Action{Sql: "insert into log_param_template(guid,log_monitor_template,name,display_name,json_key,regular,demo_match_value,create_user,update_user,create_time,update_time) values (?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{ "lpt_" + logParamGuidList[i], param.Guid, logParamObj.Name, logParamObj.DisplayName, logParamObj.JsonKey, logParamObj.Regular, logParamObj.DemoMatchValue, operator, operator, nowTime, nowTime, }}) + tmpStringMapGuidList := guid.CreateGuidList(len(logParamObj.StringMap)) + for stringMapIndex, stringMapObj := range logParamObj.StringMap { + actions = append(actions, &Action{Sql: "insert into log_metric_string_map(guid,log_monitor_template,log_param_name,value_type,source_value,regulative,target_value,update_time) values (?,?,?,?,?,?,?,?)", Param: []interface{}{ + "lmsm_" + tmpStringMapGuidList[stringMapIndex], param.Guid, logParamObj.Name, stringMapObj.ValueType, stringMapObj.SourceValue, stringMapObj.Regulative, stringMapObj.TargetValue, nowTime.Format(models.DatetimeFormat), + }}) + } } logMetricGuidList := guid.CreateGuidList(len(param.MetricList)) for i, logMetricObj := range param.MetricList { @@ -181,6 +210,12 @@ func getUpdateLogMonitorTemplateActions(param *models.LogMonitorTemplateDto, ope logParamObj.Name, logParamObj.DisplayName, logParamObj.JsonKey, logParamObj.Regular, logParamObj.DemoMatchValue, operator, nowTime, logParamObj.Guid, }}) } + tmpStringMapGuidList := guid.CreateGuidList(len(logParamObj.StringMap)) + for stringMapIndex, stringMapObj := range logParamObj.StringMap { + actions = append(actions, &Action{Sql: "insert into log_metric_string_map(guid,log_monitor_template,log_param_name,value_type,source_value,regulative,target_value,update_time) values (?,?,?,?,?,?,?,?)", Param: []interface{}{ + "lmsm_" + tmpStringMapGuidList[stringMapIndex], param.Guid, logParamObj.Name, stringMapObj.ValueType, stringMapObj.SourceValue, stringMapObj.Regulative, stringMapObj.TargetValue, nowTime.Format(models.DatetimeFormat), + }}) + } } for _, existParamObj := range existLogMonitorObj.ParamList { deleteFlag := true @@ -193,6 +228,9 @@ func getUpdateLogMonitorTemplateActions(param *models.LogMonitorTemplateDto, ope if deleteFlag { actions = append(actions, &Action{Sql: "delete from log_param_template where guid=?", Param: []interface{}{existParamObj.Guid}}) } + for _, stringMapObj := range existParamObj.StringMap { + actions = append(actions, &Action{Sql: "delete from log_metric_string_map where guid=?", Param: []interface{}{stringMapObj.Guid}}) + } } logMetricGuidList := guid.CreateGuidList(len(param.MetricList)) for i, logMetricObj := range param.MetricList { @@ -307,19 +345,26 @@ func ImportLogMonitorTemplate(params []*models.LogMonitorTemplateDto, operator s inputParam.Guid = "" } // 看下ID 能否复用 - if existLogMonitorTemplate, err = GetLogMonitorTemplateByName("", inputParam.Name); err != nil { + if existLogMonitorTemplate, err = GetLogMonitorTemplateById(inputParam.Guid); err != nil { return } - // 看下名称能否复用 if existLogMonitorTemplate != nil { - inputParam.Name = fmt.Sprintf("%s(1)", inputParam.Name) + inputParam.Guid = "" } + // 看下名称能否复用 if existLogMonitorTemplate, err = GetLogMonitorTemplateByName("", inputParam.Name); err != nil { return } if existLogMonitorTemplate != nil { - err = fmt.Errorf("log monitor template name:%s duplicate", inputParam.Name) - return + // 名称已有,追加名称 + inputParam.Name = fmt.Sprintf("%s(1)", inputParam.Name) + if existLogMonitorTemplate, err = GetLogMonitorTemplateByName("", inputParam.Name); err != nil { + return + } + if existLogMonitorTemplate != nil { + err = fmt.Errorf("log monitor template name:%s duplicate", inputParam.Name) + return + } } calcResultBytes, _ := json.Marshal(inputParam.CalcResultObj) inputParam.CalcResult = string(calcResultBytes) @@ -368,3 +413,11 @@ func GetLogMetricGroupById(id string) (result *models.LogMetricGroup, err error) } return } + +func BatchGetLogTemplateByGuid(ids []string) (list []*models.LogMonitorTemplate, err error) { + err = x.SQL(fmt.Sprintf("select name,log_type,update_user,update_time from log_monitor_template where guid in ('%s')", strings.Join(ids, "','"))).Find(&list) + for _, logMonitorTemplate := range list { + logMonitorTemplate.UpdateTimeString = logMonitorTemplate.UpdateTime.Format(models.DatetimeFormat) + } + return +} diff --git a/monitor-server/services/db/metric.go b/monitor-server/services/db/metric.go index b441c1e21..1e9465c97 100644 --- a/monitor-server/services/db/metric.go +++ b/monitor-server/services/db/metric.go @@ -416,8 +416,16 @@ func MetricComparisonImport(operator string, inputMetrics []*models.MetricCompar return } if targetMetric == nil || targetMetric.Metric == "" { - failList = append(failList, metric.Metric) - continue + // 同环比导入,使用指标名称兼容一下,存在 metricId 对应不上的情况 + if _, err = x.SQL("select * from metric where metric =? limit 1", metric.OriginMetric).Get(targetMetric); err != nil { + return + } + if targetMetric == nil || targetMetric.Metric == "" { + failList = append(failList, metric.Metric) + continue + } else { + metric.MetricId = targetMetric.Guid + } } // 2. 查询同环比指标是否存在 guid := "" @@ -484,10 +492,12 @@ func MetricImport(monitorType, serviceGroup, endPointGroup, operator string, inp if metricRow != nil && metricRow.Guid != "" && (metricRow.LogMetricGroup != "" || metricRow.DbMetricMonitor != "") { continue } - } else { + } else if endPointGroup != "" { var originMonitorType string x.SQL("select monitor_type from endpoint_group where guid=?", endPointGroup).Get(&originMonitorType) inputMetric.Guid = fmt.Sprintf("%s__%s", inputMetric.Metric, originMonitorType) + } else { + inputMetric.Guid = fmt.Sprintf("%s__%s", inputMetric.Metric, monitorType) } matchMetric := &models.MetricTable{} for _, existMetric := range existMetrics { @@ -501,10 +511,12 @@ func MetricImport(monitorType, serviceGroup, endPointGroup, operator string, inp inputMetric.Metric = inputMetric.Metric + "_1" if serviceGroup != "" { inputMetric.Guid = fmt.Sprintf("%s__%s", inputMetric.Metric, serviceGroup) - } else { + } else if endPointGroup != "" { var originMonitorType string x.SQL("select monitor_type from endpoint_group where guid=?", endPointGroup).Get(&originMonitorType) inputMetric.Guid = fmt.Sprintf("%s__%s", inputMetric.Metric, originMonitorType) + } else { + inputMetric.Guid = fmt.Sprintf("%s__%s", inputMetric.Metric, monitorType) } matchMetric = &models.MetricTable{} for _, existMetric := range existMetrics { @@ -935,3 +947,21 @@ func getMetricComparisonDeleteAction(sourceMetricGuid string) (actions []*Action } return } + +func GetLogTypeByMetric(metric string) (logType string, err error) { + var result []string + err = x.SQL("select log_type from log_metric_group where guid in (select log_metric_group from metric where metric = ?)", metric).Find(&result) + if len(result) > 0 { + logType = result[0] + } + return +} + +func GetLogTypeByLogMetricGroup(id string) (logType string, err error) { + var result []string + err = x.SQL("select log_type from log_metric_group where guid =?", id).Find(&result) + if len(result) > 0 { + logType = result[0] + } + return +} diff --git a/monitor-server/services/db/organization.go b/monitor-server/services/db/organization.go index 7563df431..5fc5842e9 100644 --- a/monitor-server/services/db/organization.go +++ b/monitor-server/services/db/organization.go @@ -228,7 +228,7 @@ func UpdateOrganization(operation string, param m.UpdateOrgPanelParam) (err erro } } } - actions = append(actions, getDeleteServiceGroupAction(param.Guid)...) + actions = append(actions, getDeleteServiceGroupAction(param.Guid, guidList)...) err = Transaction(actions) if err == nil { DeleteServiceWithChildConfig(param.Guid) @@ -270,6 +270,28 @@ func getNodeFromParent(data []*m.PanelRecursiveTable, input []string, guid strin return tmpInput } +func GetOrgRoleNew(guid string) (result []*m.OptionModel, err error) { + var serviceGroupRoleRows []*m.ServiceGroupRoleRelTable + err = x.SQL("select * from service_group_role_rel where service_group=?", guid).Find(&serviceGroupRoleRows) + if err != nil { + err = fmt.Errorf("query service group role rel table fail,%s ", err.Error()) + return + } + var roleData []*m.RoleTable + x.SQL("SELECT id,name,display_name FROM role").Find(&roleData) + if len(roleData) == 0 { + return result, nil + } + roleIdMap := make(map[string]int) + for _, row := range roleData { + roleIdMap[row.Name] = row.Id + } + for _, row := range serviceGroupRoleRows { + result = append(result, &m.OptionModel{OptionText: row.Role, OptionValue: fmt.Sprintf("%d", roleIdMap[row.Role]), Id: roleIdMap[row.Role], OptionName: row.Role}) + } + return +} + func GetOrgRole(guid string) (result []*m.OptionModel, err error) { var tableData []*m.PanelRecursiveTable err = x.SQL("SELECT role FROM panel_recursive WHERE guid=?", guid).Find(&tableData) @@ -473,3 +495,8 @@ func GetPanelRecursiveEndpoints(guid, endpointType string) (result []*m.Endpoint err = x.SQL("select * from endpoint where guid in ('" + strings.ReplaceAll(panelRecursiveTable[0].Endpoint, "^", "','") + "')").Find(&result) return } + +func BatchGetServiceGroupByIds(ids []string) (list []*m.ServiceGroupTable, err error) { + err = x.SQL(fmt.Sprintf("select * from service_group where guid in ('%s')", strings.Join(ids, "','"))).Find(&list) + return +} diff --git a/monitor-server/services/db/service_group.go b/monitor-server/services/db/service_group.go index 2d91bedc0..dd69aae7e 100644 --- a/monitor-server/services/db/service_group.go +++ b/monitor-server/services/db/service_group.go @@ -268,10 +268,15 @@ func GetDeleteServiceGroupAffectList(serviceGroup string) (result []string, err return } -func getDeleteServiceGroupAction(serviceGroupGuid string) (actions []*Action) { - guidList := []string{serviceGroupGuid} - if sNode, b := globalServiceGroupMap[serviceGroupGuid]; b { - guidList = sNode.FetchChildGuid() +func getDeleteServiceGroupAction(serviceGroupGuid string, subNodeList []string) (actions []*Action) { + var guidList []string + if len(subNodeList) > 0 { + guidList = subNodeList + } else { + guidList = []string{serviceGroupGuid} + if sNode, b := globalServiceGroupMap[serviceGroupGuid]; b { + guidList = sNode.FetchChildGuid() + } } guidFilterString := strings.Join(guidList, "','") var endpointGroup []*models.EndpointGroupTable @@ -710,16 +715,16 @@ func DeleteServiceConfig(serviceGroup string) { } func getUpdateServiceGroupNotifyActions(serviceGroup, firingCallback, recoverCallback string, roleList []string) (actions []*Action) { - actions = append(actions, &Action{Sql: "delete from notify_role_rel where notify in (select guid from notify where service_group=?)", Param: []interface{}{serviceGroup}}) - actions = append(actions, &Action{Sql: "delete from notify where service_group=?", Param: []interface{}{serviceGroup}}) - if firingCallback != "" { - firingActionGuid := guid.CreateGuid() - actions = append(actions, &Action{Sql: "insert into notify(guid,service_group,alarm_action,proc_callback_key) value (?,?,?,?)", Param: []interface{}{firingActionGuid, serviceGroup, "firing", firingCallback}}) - } - if recoverCallback != "" { - recoverActionGuid := guid.CreateGuid() - actions = append(actions, &Action{Sql: "insert into notify(guid,service_group,alarm_action,proc_callback_key) value (?,?,?,?)", Param: []interface{}{recoverActionGuid, serviceGroup, "ok", recoverCallback}}) - } + //actions = append(actions, &Action{Sql: "delete from notify_role_rel where notify in (select guid from notify where service_group=?)", Param: []interface{}{serviceGroup}}) + //actions = append(actions, &Action{Sql: "delete from notify where service_group=?", Param: []interface{}{serviceGroup}}) + //if firingCallback != "" { + // firingActionGuid := guid.CreateGuid() + // actions = append(actions, &Action{Sql: "insert into notify(guid,service_group,alarm_action,proc_callback_key) value (?,?,?,?)", Param: []interface{}{firingActionGuid, serviceGroup, "firing", firingCallback}}) + //} + //if recoverCallback != "" { + // recoverActionGuid := guid.CreateGuid() + // actions = append(actions, &Action{Sql: "insert into notify(guid,service_group,alarm_action,proc_callback_key) value (?,?,?,?)", Param: []interface{}{recoverActionGuid, serviceGroup, "ok", recoverCallback}}) + //} actions = append(actions, &Action{Sql: "delete from service_group_role_rel where service_group=?", Param: []interface{}{serviceGroup}}) for _, role := range roleList { if role == "" { diff --git a/monitor-server/services/db/service_plugin.go b/monitor-server/services/db/service_plugin.go index 0fc08be2d..a2c8ed1bc 100644 --- a/monitor-server/services/db/service_plugin.go +++ b/monitor-server/services/db/service_plugin.go @@ -1,6 +1,7 @@ package db import ( + "encoding/json" "fmt" "github.com/WeBankPartners/go-common-lib/guid" "github.com/WeBankPartners/open-monitor/monitor-server/middleware/log" @@ -9,8 +10,8 @@ import ( "time" ) -func PluginUpdateServicePathAction(input *models.PluginUpdateServicePathRequestObj) (result *models.PluginUpdateServicePathOutputObj, err error) { - log.Logger.Info("PluginUpdateServicePathAction", log.JsonObj("input", input)) +func PluginUpdateServicePathAction(input *models.PluginUpdateServicePathRequestObj, operator string, roles []string, errMsgObj *models.ErrorMessageObj) (result *models.PluginUpdateServicePathOutputObj, err error) { + log.Logger.Info("PluginUpdateServicePathAction", log.JsonObj("input", input), log.String("operator", operator), log.StringList("roles", roles)) result = &models.PluginUpdateServicePathOutputObj{CallbackParameter: input.CallbackParameter, ErrorCode: "0", ErrorMessage: "", Guid: input.Guid} input.SystemName = input.Guid monitorTypeQuery, _ := x.QueryString("select guid from monitor_type where guid=?", input.MonitorType) @@ -31,6 +32,15 @@ func PluginUpdateServicePathAction(input *models.PluginUpdateServicePathRequestO } pathList = newPathList } + var logServiceCodeList []*models.PluginUpdateServiceCodeObj + if input.LogServiceCodeList != nil { + if codeBytes, codeMarshalErr := json.Marshal(input.LogServiceCodeList); codeMarshalErr == nil { + if codeUnmarshalErr := json.Unmarshal(codeBytes, &logServiceCodeList); codeUnmarshalErr != nil { + err = fmt.Errorf("param logServiceCode illegal,fail to parse json struct,%s ", codeUnmarshalErr.Error()) + return + } + } + } endpointTypeMap := getServiceGroupEndpointWithChild(input.SystemName) sourceTargetMap := make(map[string]string) var hostEndpoint, targetEndpoint []string @@ -62,7 +72,7 @@ func PluginUpdateServicePathAction(input *models.PluginUpdateServicePathRequestO err = fmt.Errorf("Update logKeyword config fail,%s ", err.Error()) } } else { - err = updateServiceLogMetricPath(pathList, serviceGroupObj.Guid, input.MonitorType, sourceTargetMap) + err = updateServiceLogMetricPath(pathList, serviceGroupObj.Guid, input.MonitorType, sourceTargetMap, input.LogMonitorTemplate, input.LogMonitorPrefixCode, input.LogMonitorName, operator, roles, errMsgObj, logServiceCodeList) if err != nil { err = fmt.Errorf("Update logMetric config fail,%s ", err.Error()) return @@ -71,7 +81,7 @@ func PluginUpdateServicePathAction(input *models.PluginUpdateServicePathRequestO return } -func updateServiceLogMetricPath(pathList []string, serviceGroup, monitorType string, sourceTargetMap map[string]string) (err error) { +func updateServiceLogMetricPath(pathList []string, serviceGroup, monitorType string, sourceTargetMap map[string]string, logMonitorTemplateGuid, logMonitorPrefixCode, logMonitorName, operator string, roles []string, errMsgObj *models.ErrorMessageObj, logServiceCodeList []*models.PluginUpdateServiceCodeObj) (err error) { var logMetricTable []*models.LogMetricMonitorTable err = x.SQL("select * from log_metric_monitor where service_group=?", serviceGroup).Find(&logMetricTable) if err != nil { @@ -80,6 +90,7 @@ func updateServiceLogMetricPath(pathList []string, serviceGroup, monitorType str nowTime := time.Now().Format(models.DatetimeFormat) var actions []*Action var affectHostList, affectEndpointGroup []string + var newCustomDashboardIdList []int64 existPathTypeMap := make(map[string]string) existPathGuidMap := make(map[string]string) for _, v := range logMetricTable { @@ -100,7 +111,7 @@ func updateServiceLogMetricPath(pathList []string, serviceGroup, monitorType str actions = append(actions, tmpActions...) } } - for _, path := range pathList { + for i, path := range pathList { if existMonitorType, b := existPathTypeMap[path]; b { if existMonitorType != monitorType { // change monitor type @@ -111,34 +122,78 @@ func updateServiceLogMetricPath(pathList []string, serviceGroup, monitorType str continue } // add path - tmpAffectList, tmpActions := getLogMetricPathAddAction(path, serviceGroup, monitorType, nowTime, sourceTargetMap) + tmpAffectList, tmpActions, newLogMetricMonitorGuid := getLogMetricPathAddAction(path, serviceGroup, monitorType, nowTime, sourceTargetMap) affectHostList = append(affectHostList, tmpAffectList...) actions = append(actions, tmpActions...) + if logMonitorTemplateGuid != "" { + autoCreateLogMetricGroupParam := models.LogMetricGroupWithTemplate{ + AutoCreateDashboard: true, + AutoCreateWarn: true, + LogMetricMonitorGuid: newLogMetricMonitorGuid, + LogMonitorTemplateGuid: logMonitorTemplateGuid, + MetricPrefixCode: logMonitorPrefixCode, + Name: logMonitorName, + ServiceGroup: serviceGroup, + MonitorType: monitorType, + CodeStringMap: []*models.LogMetricStringMapTable{}, + } + if len(pathList) > 1 { + autoCreateLogMetricGroupParam.MetricPrefixCode += fmt.Sprintf("%d", i+1) + autoCreateLogMetricGroupParam.Name += fmt.Sprintf("_%d", i+1) + } + for _, codeRow := range logServiceCodeList { + autoCreateLogMetricGroupParam.CodeStringMap = append(autoCreateLogMetricGroupParam.CodeStringMap, &models.LogMetricStringMapTable{ + Regulative: codeRow.Regulative, + SourceValue: codeRow.SourceValue, + TargetValue: codeRow.TargetValue, + }) + } + createLogMetricGroupActions, _, newDashboardId, createLogMetricGroupErr := getCreateLogMetricGroupActions(&autoCreateLogMetricGroupParam, operator, roles, make(map[string]string), errMsgObj, false) + if createLogMetricGroupErr != nil { + err = fmt.Errorf("try to create logMetricGroup with template fail,%s ", createLogMetricGroupErr.Error()) + break + } + if newDashboardId > 0 { + newCustomDashboardIdList = append(newCustomDashboardIdList, newDashboardId) + } + actions = append(actions, createLogMetricGroupActions...) + } + } + if err != nil { + return } + if len(actions) > 0 { err = Transaction(actions) if err != nil { + for _, newDashboardId := range newCustomDashboardIdList { + x.Exec("delete from custom_dashboard where id=?", newDashboardId) + } return err } } if len(affectHostList) > 0 { - err = SyncLogMetricExporterConfig(affectHostList) + if syncErr := SyncLogMetricExporterConfig(affectHostList); syncErr != nil { + log.Logger.Error("updateServiceLogMetricPath sync log metric exporter config fail", log.StringList("hostList", affectHostList), log.Error(syncErr)) + } } if len(affectEndpointGroup) > 0 { for _, v := range affectEndpointGroup { - SyncPrometheusRuleFile(v, false) + if tmpErr := SyncPrometheusRuleFile(v, false); tmpErr != nil { + log.Logger.Error("updateServiceLogMetricPath sync endpointGroup prometheus rule file fail", log.String("endpointGroup", v), log.Error(tmpErr)) + } } } return err } -func getLogMetricPathAddAction(path, serviceGroup, monitorType, nowTime string, sourceTargetMap map[string]string) (hostList []string, actions []*Action) { - newLogMetricGuid := guid.CreateGuid() +func getLogMetricPathAddAction(path, serviceGroup, monitorType, nowTime string, sourceTargetMap map[string]string) (hostList []string, actions []*Action, newLogMetricMonitorGuid string) { + newLogMetricMonitorGuid = "lmm_" + guid.CreateGuid() path = strings.TrimSpace(path) - actions = append(actions, &Action{Sql: "insert into log_metric_monitor(guid,service_group,log_path,monitor_type,update_time) value (?,?,?,?,?)", Param: []interface{}{newLogMetricGuid, serviceGroup, path, monitorType, nowTime}}) + actions = append(actions, &Action{Sql: "insert into log_metric_monitor(guid,service_group,log_path,monitor_type,update_time) value (?,?,?,?,?)", Param: []interface{}{newLogMetricMonitorGuid, serviceGroup, path, monitorType, nowTime}}) for k, v := range sourceTargetMap { hostList = append(hostList, k) - actions = append(actions, &Action{Sql: "insert into log_metric_endpoint_rel(guid,log_metric_monitor,source_endpoint,target_endpoint) value (?,?,?,?)", Param: []interface{}{guid.CreateGuid(), newLogMetricGuid, k, v}}) + actions = append(actions, &Action{Sql: "insert into log_metric_endpoint_rel(guid,log_metric_monitor,source_endpoint,target_endpoint) value (?,?,?,?)", Param: []interface{}{guid.CreateGuid(), newLogMetricMonitorGuid, k, v}}) } return } diff --git a/monitor-server/services/db/type_config.go b/monitor-server/services/db/type_config.go index 1caa9fd9a..fd032e817 100644 --- a/monitor-server/services/db/type_config.go +++ b/monitor-server/services/db/type_config.go @@ -3,6 +3,7 @@ package db import ( "fmt" "github.com/WeBankPartners/open-monitor/monitor-server/models" + "strings" "time" ) @@ -33,6 +34,29 @@ func GetTypeConfigList(name string) (list []*models.TypeConfig, err error) { return } +func GetTypeConfigListByNames(names []string) (list []*models.TypeConfig, err error) { + var endpointList []*models.EndpointNewTable + err = x.SQL(fmt.Sprintf("select * from monitor_type where display_name in ('%s') order by create_time desc", strings.Join(names, "','"))).Find(&list) + if err != nil { + return + } + if len(list) > 0 { + if err = x.SQL("select * from endpoint_new ").Find(&endpointList); err != nil { + return + } + if len(endpointList) > 0 { + for _, typeConf := range list { + for _, endpoint := range endpointList { + if endpoint.MonitorType == typeConf.DisplayName && endpoint.MonitorType != "" { + typeConf.ObjectCount++ + } + } + } + } + } + return +} + func AddTypeConfig(param models.TypeConfig) (err error) { _, err = x.Exec("insert into monitor_type(guid,display_name,system_type,create_user,create_time) values(?,?,?,?,?)", param.Guid, param.DisplayName, param.SystemType, param.CreateUser, time.Now().Format(models.DatetimeFormat)) diff --git a/monitor-server/services/db/user.go b/monitor-server/services/db/user.go index 762dffb55..0ee34ad32 100644 --- a/monitor-server/services/db/user.go +++ b/monitor-server/services/db/user.go @@ -278,6 +278,10 @@ func ListManageRole(roles []string) (result []*m.RoleTable, err error) { } func StartCronJob() { + go func() { + SyncCoreRole() + SyncCoreRoleList() + }() if !m.Config().CronJob.Enable { return } @@ -600,6 +604,7 @@ func GetGrpRole(grpId int) (err error, result []*m.OptionModel) { } func GetRoleMap() (roleMap map[string]string) { + SyncCoreRole() SyncCoreRoleList() roleMap = make(map[string]string) var roleTable []*m.RoleNewTable diff --git a/monitor-ui/src/assets/locale/lang/zh-CN.json b/monitor-ui/src/assets/locale/lang/zh-CN.json index f573f3bb9..4b6df5679 100644 --- a/monitor-ui/src/assets/locale/lang/zh-CN.json +++ b/monitor-ui/src/assets/locale/lang/zh-CN.json @@ -173,7 +173,7 @@ "m_configuration_information": "配置信息", "m_cannot_be_empty": "不能为空", "m_cannot_be_repeated": "不能重复", - "m_fields_cannot_be_empty": "不能存在字段为空", + "m_fields_cannot_be_empty": "必填字段不能为空", "m_filtering_info": "请输入过滤信息", "m_success": "成功", "m_fail": "失败", diff --git a/monitor-ui/src/components/custom-chart.vue b/monitor-ui/src/components/custom-chart.vue index e557c7882..768f1490a 100644 --- a/monitor-ui/src/components/custom-chart.vue +++ b/monitor-ui/src/components/custom-chart.vue @@ -49,7 +49,7 @@ export default { } }, mounted() { - this.getchartdata() + this.getchartdata('mounted') this.isAutoRefresh() window.addEventListener('scroll', this.scrollHandle, true) window.addEventListener('visibilitychange', this.isTabActive, true) @@ -106,7 +106,7 @@ export default { },this.params.autoRefresh * 1000) } }, - getchartdata() { + getchartdata(type = '') { this.noDataType = 'normal' if (this.chartInfo.chartParams.data.length === 0) { this.noDataType = 'noConfig' @@ -118,27 +118,34 @@ export default { } this.elId = this.chartInfo.elId window.intervalFrom = 'custom-chart' - this.$root.$httpRequestEntrance.httpRequestEntrance('POST',this.$root.apiCenter.metricConfigView.api, params, responseData => { - if (responseData.legend.length === 0) { - this.noDataType = 'noData' - } else { - responseData.yaxis.unit = this.chartInfo.panalUnit - this.noDataType = 'normal' - const chartConfig = { - title: false, - eye: false, - clear: true, - dataZoom: false, - lineBarSwitch: true, - chartType: this.chartInfo.chartType, - params: this.chartInfo.chartParams + const modalElement = document.querySelector('#edit-view') + const offset = this.$el.getBoundingClientRect() + const offsetTop = offset.top + const offsetBottom = offset.bottom + // 进入可视区域 + if ((offsetTop <= window.innerHeight && offsetBottom >= 0 && !modalElement) || type === 'mounted') { + this.$root.$httpRequestEntrance.httpRequestEntrance('POST',this.$root.apiCenter.metricConfigView.api, params, responseData => { + if (responseData.legend.length === 0) { + this.noDataType = 'noData' + } else { + responseData.yaxis.unit = this.chartInfo.panalUnit + this.noDataType = 'normal' + const chartConfig = { + title: false, + eye: false, + clear: true, + dataZoom: false, + lineBarSwitch: true, + chartType: this.chartInfo.chartType, + params: this.chartInfo.chartParams + } + this.$nextTick(() => { + this.chartInstance = readyToDraw(this, responseData, this.chartIndex, chartConfig) + this.scrollHandle() + }) } - this.$nextTick(() => { - this.chartInstance = readyToDraw(this, responseData, this.chartIndex, chartConfig) - this.scrollHandle() - }) - } - }, { isNeedloading: false }) + }, { isNeedloading: false }) + } }, onMouseLeaveContent() { if (this.chartInstance) { diff --git a/monitor-ui/src/components/custom-pie-chart.vue b/monitor-ui/src/components/custom-pie-chart.vue index c49849ec0..4d8a5949e 100644 --- a/monitor-ui/src/components/custom-pie-chart.vue +++ b/monitor-ui/src/components/custom-pie-chart.vue @@ -44,7 +44,7 @@ export default { } }, mounted() { - this.getchartdata() + this.getchartdata('mounted') }, destroyed() { clearInterval(this.interval) @@ -58,7 +58,7 @@ export default { },this.params.autoRefresh*1000) } }, - getchartdata() { + getchartdata(type = '') { this.noDataType = 'normal' if (this.chartInfo.chartParams.data.length === 0) { this.noDataType = 'noConfig' @@ -73,20 +73,27 @@ export default { p.custom_chart_guid = this.chartInfo.elId }) this.elId = this.chartInfo.elId - this.$root.$httpRequestEntrance.httpRequestEntrance( - 'POST', - this.$root.apiCenter.metricConfigPieView.api, - params, - responseData => { - if (responseData.legend && responseData.legend.length === 0) { - this.noDataType = 'noData' - } else { - this.noDataType = 'normal' - drawPieChart(this, responseData) - } - }, - { isNeedloading: false } - ) + + const modalElement = document.querySelector('#edit-view') + const offset = this.$el.getBoundingClientRect() + const offsetTop = offset.top + const offsetBottom = offset.bottom + if ((offsetTop <= window.innerHeight && offsetBottom >= 0 && !modalElement) || type === 'mounted') { + this.$root.$httpRequestEntrance.httpRequestEntrance( + 'POST', + this.$root.apiCenter.metricConfigPieView.api, + params, + responseData => { + if (responseData.legend && responseData.legend.length === 0) { + this.noDataType = 'noData' + } else { + this.noDataType = 'normal' + drawPieChart(this, responseData) + } + }, + { isNeedloading: false } + ) + } } }, components: {}, diff --git a/monitor-ui/src/components/search-badge.vue b/monitor-ui/src/components/search-badge.vue index c5ac27114..f95a29086 100644 --- a/monitor-ui/src/components/search-badge.vue +++ b/monitor-ui/src/components/search-badge.vue @@ -193,7 +193,7 @@ export default ({ methods: { onFilterOptions: debounce(function () { this.getFilterAllOptions() - }, 400), + }, 800), getFilterAllOptions() { const api = '/monitor/api/v1/alarm/problem/options' this.request('POST', api, this.filterParams, res => { diff --git a/monitor-ui/src/views/custom-view/edit-view.vue b/monitor-ui/src/views/custom-view/edit-view.vue index 006b6059e..3b1e976cb 100644 --- a/monitor-ui/src/views/custom-view/edit-view.vue +++ b/monitor-ui/src/views/custom-view/edit-view.vue @@ -781,7 +781,7 @@ export default { }, methods: { async getTableData() { - await this.getEndpointList() + this.getEndpointList() this.request('GET', '/monitor/api/v2/chart/custom', { chart_id: this.chartId }, async res => { @@ -916,7 +916,7 @@ export default { debounceGetEndpointList: debounce(async function () { await this.getEndpointList() - }, 300), + }, 200), getEndpointList() { return new Promise(resolve => { @@ -1310,7 +1310,7 @@ export default { debounceDrawChart: debounce(function () { this.drawChartContent() - }, 300), + }, 50), generateLineParamsData() { if (isEmpty(this.tableData)) { diff --git a/monitor-ui/src/views/custom-view/view-config-index.vue b/monitor-ui/src/views/custom-view/view-config-index.vue index 4e86d5a23..145b76c74 100644 --- a/monitor-ui/src/views/custom-view/view-config-index.vue +++ b/monitor-ui/src/views/custom-view/view-config-index.vue @@ -222,32 +222,31 @@ - -
-
-
-
- {{$t('m_tableKey_role')}}: -
-
- {{$t('m_custom_dashboard')}}: -
+ + +
+
+
+ {{$t('m_tableKey_role')}}:
-
-
+
+ {{$t('m_custom_dashboard')}}: +
+
+
-
- +
-
+ { this.$Message.success(this.$t('m_tips_success')) - this.$root.JQ('#set_dashboard_modal').modal('hide') + this.isShowProcessConfigModel = false + // this.$root.JQ('#set_dashboard_modal').modal('hide') this.getViewList() }) }, @@ -533,7 +534,7 @@ export default { setDashboard() { this.request('GET',this.pathMap.portalConfig, '', responseData => { this.processConfigModel.dashboardConfig = responseData - this.$root.JQ('#set_dashboard_modal').modal('show') + this.isShowProcessConfigModel = true }) }, goToPanal(panalItem, type) { @@ -667,6 +668,13 @@ body::-webkit-scrollbar { // display: none; } +.dashboard-config-selection { + width:200px; + .ivu-select-selection { + height: 34px !important; + } +} +