diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 8f217cb..ed745a2 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -55,6 +55,10 @@ func routers(r *gin.Engine) { optionalAuth.GET("/ws/server", commonHandler(serverStream)) optionalAuth.GET("/server-group", commonHandler(listServerGroup)) + optionalAuth.GET("/service", commonHandler(listService)) + optionalAuth.GET("/service/:id", commonHandler(listServiceHistory)) + optionalAuth.GET("/service/server", commonHandler(listServerWithServices)) + optionalAuth.GET("/setting", commonHandler(listConfig)) auth := api.Group("", authMiddleware.MiddlewareFunc()) @@ -71,7 +75,6 @@ func routers(r *gin.Engine) { auth.POST("/user", commonHandler(createUser)) auth.POST("/batch-delete/user", commonHandler(batchDeleteUser)) - auth.GET("/service", commonHandler(listService)) auth.POST("/service", commonHandler(createService)) auth.PATCH("/service/:id", commonHandler(updateService)) auth.POST("/batch-delete/service", commonHandler(batchDeleteService)) diff --git a/cmd/dashboard/controller/service.go b/cmd/dashboard/controller/service.go index b17d302..2986dda 100644 --- a/cmd/dashboard/controller/service.go +++ b/cmd/dashboard/controller/service.go @@ -1,12 +1,15 @@ package controller import ( + "errors" "fmt" "strconv" "strings" + "time" "github.com/gin-gonic/gin" "github.com/jinzhu/copier" + "github.com/naiba/nezha/model" "github.com/naiba/nezha/service/singleton" "gorm.io/gorm" @@ -17,7 +20,7 @@ import ( // @Security BearerAuth // @Schemes // @Description List service -// @Tags auth required +// @Tags common // @Produce json // @Success 200 {object} model.CommonResponse[model.ServiceResponse] // @Router /service [get] @@ -29,10 +32,16 @@ func listService(c *gin.Context) (*model.ServiceResponse, error) { var statsStore map[uint64]model.CycleTransferStats copier.Copy(&stats, singleton.ServiceSentinelShared.LoadStats()) copier.Copy(&statsStore, singleton.AlertsCycleTransferStatsStore) + _, isMember := c.Get(model.CtxKeyAuthorizedUser) + authorized := isMember // TODO || isViewPasswordVerfied for k, service := range stats { if !service.Service.EnableShowInService { delete(stats, k) } + if !authorized { + service.Service = &model.Service{Name: service.Service.Name} + stats[k] = service + } } return []interface { }{ @@ -49,6 +58,114 @@ func listService(c *gin.Context) (*model.ServiceResponse, error) { }, nil } +// List service histories by server id +// @Summary List service histories by server id +// @Security BearerAuth +// @Schemes +// @Description List service histories by server id +// @Tags common +// @param id path uint true "Server ID" +// @Produce json +// @Success 200 {object} model.CommonResponse[[]model.ServiceInfos] +// @Router /service/{id} [get] +func listServiceHistory(c *gin.Context) ([]*model.ServiceInfos, error) { + idStr := c.Param("id") + id, err := strconv.ParseUint(idStr, 10, 64) + if err != nil { + return nil, err + } + + singleton.ServerLock.RLock() + server, ok := singleton.ServerList[id] + if !ok { + return nil, errors.New("server not found") + } + + _, isMember := c.Get(model.CtxKeyAuthorizedUser) + authorized := isMember // TODO || isViewPasswordVerfied + + if server.HideForGuest && !authorized { + return nil, errors.New("unauthorized") + } + singleton.ServerLock.RUnlock() + + var serviceHistories []*model.ServiceHistory + if err := singleton.DB.Model(&model.ServiceHistory{}).Select("service_id, created_at, server_id, avg_delay"). + Where("server_id = ?", id).Where("created_at >= ?", time.Now().Add(-24*time.Hour)).Order("service_id, created_at"). + Scan(&serviceHistories).Error; err != nil { + return nil, err + } + + singleton.ServiceSentinelShared.ServicesLock.RLock() + defer singleton.ServiceSentinelShared.ServicesLock.RUnlock() + singleton.ServerLock.RLock() + defer singleton.ServerLock.RUnlock() + + var sortedServiceIDs []uint64 + resultMap := make(map[uint64]*model.ServiceInfos) + for _, history := range serviceHistories { + infos, ok := resultMap[history.ServiceID] + if !ok { + infos = &model.ServiceInfos{ + ServiceID: history.ServiceID, + ServerID: history.ServerID, + // ServiceName: singleton.ServiceSentinel.Services[history.ServiceID].Name, + ServerName: singleton.ServerList[history.ServerID].Name, + } + resultMap[history.ServiceID] = infos + sortedServiceIDs = append(sortedServiceIDs, history.ServiceID) + } + infos.CreatedAt = append(infos.CreatedAt, history.CreatedAt.Truncate(time.Minute).Unix()*1000) + infos.AvgDelay = append(infos.AvgDelay, history.AvgDelay) + } + + ret := make([]*model.ServiceInfos, 0, len(sortedServiceIDs)) + for _, id := range sortedServiceIDs { + ret = append(ret, resultMap[id]) + } + + return ret, nil +} + +// List server with service +// @Summary List server with service +// @Security BearerAuth +// @Schemes +// @Description List server with service +// @Tags common +// @Produce json +// @Success 200 {object} model.CommonResponse[[]uint64] +// @Router /service/server [get] +func listServerWithServices(c *gin.Context) ([]uint64, error) { + var serverIdsWithService []uint64 + if err := singleton.DB.Model(&model.ServiceHistory{}). + Select("distinct(server_id)"). + Where("server_id != 0"). + Find(&serverIdsWithService).Error; err != nil { + return nil, newGormError("%v", err) + } + + _, isMember := c.Get(model.CtxKeyAuthorizedUser) + authorized := isMember // TODO || isViewPasswordVerfied + + var ret []uint64 + for _, id := range serverIdsWithService { + singleton.ServerLock.RLock() + server, ok := singleton.ServerList[id] + if !ok { + singleton.ServerLock.RUnlock() + return nil, errors.New("server not found") + } + + if !server.HideForGuest || authorized { + ret = append(ret, id) + } + singleton.ServerLock.RUnlock() + } + + return ret, nil +} + // Create service // @Summary Create service // @Security BearerAuth diff --git a/model/service_history_api.go b/model/service_history_api.go new file mode 100644 index 0000000..88337a7 --- /dev/null +++ b/model/service_history_api.go @@ -0,0 +1,10 @@ +package model + +type ServiceInfos struct { + ServiceID uint64 `json:"monitor_id"` + ServerID uint64 `json:"server_id"` + ServiceName string `json:"monitor_name"` + ServerName string `json:"server_name"` + CreatedAt []int64 `json:"created_at"` + AvgDelay []float32 `json:"avg_delay"` +} diff --git a/service/singleton/servicesentinel.go b/service/singleton/servicesentinel.go index ef2f5f1..16ac5f9 100644 --- a/service/singleton/servicesentinel.go +++ b/service/singleton/servicesentinel.go @@ -42,7 +42,7 @@ func NewServiceSentinel(serviceSentinelDispatchBus chan<- model.Service) { serviceResponseDataStoreCurrentDown: make(map[uint64]uint64), serviceResponseDataStoreCurrentAvgDelay: make(map[uint64]float32), serviceResponsePing: make(map[uint64]map[uint64]*pingStore), - services: make(map[uint64]*model.Service), + Services: make(map[uint64]*model.Service), sslCertCache: make(map[uint64]string), // 30天数据缓存 monthlyStatus: make(map[uint64]*model.ServiceResponseItem), @@ -104,8 +104,8 @@ type ServiceSentinel struct { lastStatus map[uint64]int sslCertCache map[uint64]string - servicesLock sync.RWMutex - services map[uint64]*model.Service + ServicesLock sync.RWMutex + Services map[uint64]*model.Service // 30天数据缓存 monthlyStatusLock sync.Mutex @@ -157,11 +157,11 @@ func (ss *ServiceSentinel) Dispatch(r ReportData) { ss.serviceReportChannel <- r } -func (ss *ServiceSentinel) Services() []*model.Service { - ss.servicesLock.RLock() - defer ss.servicesLock.RUnlock() +func (ss *ServiceSentinel) GetServiceList() []*model.Service { + ss.ServicesLock.RLock() + defer ss.ServicesLock.RUnlock() var services []*model.Service - for _, v := range ss.services { + for _, v := range ss.Services { services = append(services, v) } sort.SliceStable(services, func(i, j int) bool { @@ -182,8 +182,8 @@ func (ss *ServiceSentinel) loadServiceHistory() { defer ss.serviceResponseDataStoreLock.Unlock() ss.monthlyStatusLock.Lock() defer ss.monthlyStatusLock.Unlock() - ss.servicesLock.Lock() - defer ss.servicesLock.Unlock() + ss.ServicesLock.Lock() + defer ss.ServicesLock.Unlock() for i := 0; i < len(services); i++ { task := *services[i] @@ -194,7 +194,7 @@ func (ss *ServiceSentinel) loadServiceHistory() { if err != nil { panic(err) } - ss.services[services[i].ID] = services[i] + ss.Services[services[i].ID] = services[i] ss.serviceCurrentStatusData[services[i].ID] = make([]*pb.TaskResult, _CurrentStatusSize) ss.serviceStatusToday[services[i].ID] = &_TodayStatsOfService{} } @@ -234,8 +234,8 @@ func (ss *ServiceSentinel) OnServiceUpdate(m model.Service) error { defer ss.serviceResponseDataStoreLock.Unlock() ss.monthlyStatusLock.Lock() defer ss.monthlyStatusLock.Unlock() - ss.servicesLock.Lock() - defer ss.servicesLock.Unlock() + ss.ServicesLock.Lock() + defer ss.ServicesLock.Unlock() var err error // 写入新任务 @@ -245,9 +245,9 @@ func (ss *ServiceSentinel) OnServiceUpdate(m model.Service) error { if err != nil { return err } - if ss.services[m.ID] != nil { + if ss.Services[m.ID] != nil { // 停掉旧任务 - Cron.Remove(ss.services[m.ID].CronJobID) + Cron.Remove(ss.Services[m.ID].CronJobID) } else { // 新任务初始化数据 ss.monthlyStatus[m.ID] = &model.ServiceResponseItem{ @@ -260,7 +260,7 @@ func (ss *ServiceSentinel) OnServiceUpdate(m model.Service) error { ss.serviceStatusToday[m.ID] = &_TodayStatsOfService{} } // 更新这个任务 - ss.services[m.ID] = &m + ss.Services[m.ID] = &m return nil } @@ -269,8 +269,8 @@ func (ss *ServiceSentinel) OnServiceDelete(ids []uint64) { defer ss.serviceResponseDataStoreLock.Unlock() ss.monthlyStatusLock.Lock() defer ss.monthlyStatusLock.Unlock() - ss.servicesLock.Lock() - defer ss.servicesLock.Unlock() + ss.ServicesLock.Lock() + defer ss.ServicesLock.Unlock() for _, id := range ids { delete(ss.serviceCurrentStatusIndex, id) @@ -283,8 +283,8 @@ func (ss *ServiceSentinel) OnServiceDelete(ids []uint64) { delete(ss.serviceStatusToday, id) // 停掉定时任务 - Cron.Remove(ss.services[id].CronJobID) - delete(ss.services, id) + Cron.Remove(ss.Services[id].CronJobID) + delete(ss.Services, id) delete(ss.monthlyStatus, id) } @@ -297,8 +297,8 @@ func (ss *ServiceSentinel) LoadStats() map[uint64]*model.ServiceResponseItem { defer ss.monthlyStatusLock.Unlock() // 刷新最新一天的数据 - for k := range ss.services { - ss.monthlyStatus[k].Service = ss.services[k] + for k := range ss.Services { + ss.monthlyStatus[k].Service = ss.Services[k] v := ss.serviceStatusToday[k] // 30 天在线率, @@ -329,7 +329,7 @@ func (ss *ServiceSentinel) LoadStats() map[uint64]*model.ServiceResponseItem { func (ss *ServiceSentinel) worker() { // 从服务状态汇报管道获取汇报的服务数据 for r := range ss.serviceReportChannel { - if ss.services[r.Data.GetId()] == nil || ss.services[r.Data.GetId()].ID == 0 { + if ss.Services[r.Data.GetId()] == nil || ss.Services[r.Data.GetId()].ID == 0 { log.Printf("NEZHA>> 错误的服务监控上报 %+v", r) continue } @@ -427,23 +427,23 @@ func (ss *ServiceSentinel) worker() { // 延迟报警 if mh.Delay > 0 { - ss.servicesLock.RLock() - if ss.services[mh.GetId()].LatencyNotify { - notificationGroupID := ss.services[mh.GetId()].NotificationGroupID + ss.ServicesLock.RLock() + if ss.Services[mh.GetId()].LatencyNotify { + notificationGroupID := ss.Services[mh.GetId()].NotificationGroupID minMuteLabel := NotificationMuteLabel.ServiceLatencyMin(mh.GetId()) maxMuteLabel := NotificationMuteLabel.ServiceLatencyMax(mh.GetId()) - if mh.Delay > ss.services[mh.GetId()].MaxLatency { + if mh.Delay > ss.Services[mh.GetId()].MaxLatency { // 延迟超过最大值 ServerLock.RLock() reporterServer := ServerList[r.Reporter] - msg := fmt.Sprintf("[Latency] %s %2f > %2f, Reporter: %s", ss.services[mh.GetId()].Name, mh.Delay, ss.services[mh.GetId()].MaxLatency, reporterServer.Name) + msg := fmt.Sprintf("[Latency] %s %2f > %2f, Reporter: %s", ss.Services[mh.GetId()].Name, mh.Delay, ss.Services[mh.GetId()].MaxLatency, reporterServer.Name) go SendNotification(notificationGroupID, msg, minMuteLabel) ServerLock.RUnlock() - } else if mh.Delay < ss.services[mh.GetId()].MinLatency { + } else if mh.Delay < ss.Services[mh.GetId()].MinLatency { // 延迟低于最小值 ServerLock.RLock() reporterServer := ServerList[r.Reporter] - msg := fmt.Sprintf("[Latency] %s %2f < %2f, Reporter: %s", ss.services[mh.GetId()].Name, mh.Delay, ss.services[mh.GetId()].MinLatency, reporterServer.Name) + msg := fmt.Sprintf("[Latency] %s %2f < %2f, Reporter: %s", ss.Services[mh.GetId()].Name, mh.Delay, ss.Services[mh.GetId()].MinLatency, reporterServer.Name) go SendNotification(notificationGroupID, msg, maxMuteLabel) ServerLock.RUnlock() } else { @@ -452,24 +452,24 @@ func (ss *ServiceSentinel) worker() { UnMuteNotification(notificationGroupID, maxMuteLabel) } } - ss.servicesLock.RUnlock() + ss.ServicesLock.RUnlock() } // 状态变更报警+触发任务执行 if stateCode == StatusDown || stateCode != ss.lastStatus[mh.GetId()] { - ss.servicesLock.Lock() + ss.ServicesLock.Lock() lastStatus := ss.lastStatus[mh.GetId()] // 存储新的状态值 ss.lastStatus[mh.GetId()] = stateCode // 判断是否需要发送通知 - isNeedSendNotification := ss.services[mh.GetId()].Notify && (lastStatus != 0 || stateCode == StatusDown) + isNeedSendNotification := ss.Services[mh.GetId()].Notify && (lastStatus != 0 || stateCode == StatusDown) if isNeedSendNotification { ServerLock.RLock() reporterServer := ServerList[r.Reporter] - notificationGroupID := ss.services[mh.GetId()].NotificationGroupID - notificationMsg := fmt.Sprintf("[%s] %s Reporter: %s, Error: %s", StatusCodeToString(stateCode), ss.services[mh.GetId()].Name, reporterServer.Name, mh.Data) + notificationGroupID := ss.Services[mh.GetId()].NotificationGroupID + notificationMsg := fmt.Sprintf("[%s] %s Reporter: %s, Error: %s", StatusCodeToString(stateCode), ss.Services[mh.GetId()].Name, reporterServer.Name, mh.Data) muteLabel := NotificationMuteLabel.ServiceStateChanged(mh.GetId()) // 状态变更时,清除静音缓存 @@ -482,7 +482,7 @@ func (ss *ServiceSentinel) worker() { } // 判断是否需要触发任务 - isNeedTriggerTask := ss.services[mh.GetId()].EnableTriggerTask && lastStatus != 0 + isNeedTriggerTask := ss.Services[mh.GetId()].EnableTriggerTask && lastStatus != 0 if isNeedTriggerTask { ServerLock.RLock() reporterServer := ServerList[r.Reporter] @@ -490,14 +490,14 @@ func (ss *ServiceSentinel) worker() { if stateCode == StatusGood && lastStatus != stateCode { // 当前状态正常 前序状态非正常时 触发恢复任务 - go SendTriggerTasks(ss.services[mh.GetId()].RecoverTriggerTasks, reporterServer.ID) + go SendTriggerTasks(ss.Services[mh.GetId()].RecoverTriggerTasks, reporterServer.ID) } else if lastStatus == StatusGood && lastStatus != stateCode { // 前序状态正常 当前状态非正常时 触发失败任务 - go SendTriggerTasks(ss.services[mh.GetId()].FailTriggerTasks, reporterServer.ID) + go SendTriggerTasks(ss.Services[mh.GetId()].FailTriggerTasks, reporterServer.ID) } } - ss.servicesLock.Unlock() + ss.ServicesLock.Unlock() } ss.serviceResponseDataStoreLock.Unlock() @@ -509,22 +509,22 @@ func (ss *ServiceSentinel) worker() { !strings.HasSuffix(mh.Data, "EOF") && !strings.HasSuffix(mh.Data, "timed out") { errMsg = mh.Data - ss.servicesLock.RLock() - if ss.services[mh.GetId()].Notify { + ss.ServicesLock.RLock() + if ss.Services[mh.GetId()].Notify { muteLabel := NotificationMuteLabel.ServiceSSL(mh.GetId(), "network") - go SendNotification(ss.services[mh.GetId()].NotificationGroupID, fmt.Sprintf("[SSL] Fetch cert info failed, %s %s", ss.services[mh.GetId()].Name, errMsg), muteLabel) + go SendNotification(ss.Services[mh.GetId()].NotificationGroupID, fmt.Sprintf("[SSL] Fetch cert info failed, %s %s", ss.Services[mh.GetId()].Name, errMsg), muteLabel) } - ss.servicesLock.RUnlock() + ss.ServicesLock.RUnlock() } } else { // 清除网络错误静音缓存 - UnMuteNotification(ss.services[mh.GetId()].NotificationGroupID, NotificationMuteLabel.ServiceSSL(mh.GetId(), "network")) + UnMuteNotification(ss.Services[mh.GetId()].NotificationGroupID, NotificationMuteLabel.ServiceSSL(mh.GetId(), "network")) var newCert = strings.Split(mh.Data, "|") if len(newCert) > 1 { - ss.servicesLock.Lock() - enableNotify := ss.services[mh.GetId()].Notify + ss.ServicesLock.Lock() + enableNotify := ss.Services[mh.GetId()].Notify // 首次获取证书信息时,缓存证书信息 if ss.sslCertCache[mh.GetId()] == "" { @@ -542,9 +542,9 @@ func (ss *ServiceSentinel) worker() { ss.sslCertCache[mh.GetId()] = mh.Data } - notificationGroupID := ss.services[mh.GetId()].NotificationGroupID - serviceName := ss.services[mh.GetId()].Name - ss.servicesLock.Unlock() + notificationGroupID := ss.Services[mh.GetId()].NotificationGroupID + serviceName := ss.Services[mh.GetId()].Name + ss.ServicesLock.Unlock() // 需要发送提醒 if enableNotify {