module.go 1.7 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
package monitor

import (
	"github.com/eolinker/goku-api-gateway/ksitigarbha"

	console_sqlite3 "github.com/eolinker/goku-api-gateway/server/dao/console-sqlite3"
	"github.com/pkg/errors"
)

type MonitorModule struct {
	Name         string      `json:"moduleName"`
	Config       interface{} `json:"config,omitempty"`
	ModuleStatus int         `json:"moduleStatus"`
	Desc         string      `json:"moduleDesc"`
	Models       interface{} `json:"layer"`
}

//GetMonitorModules 获取监控模块列表
func GetMonitorModules() ([]*MonitorModule, error) {
	m, err := console_sqlite3.GetMonitorModules()
	if err != nil {
		return make([]*MonitorModule, 0), nil
	}

	names := ksitigarbha.GetMonitorModuleNames()
	modules := make([]*MonitorModule, 0, len(names))

	for _, name := range names {
		model,_ := ksitigarbha.GetMonitorModuleModel(name)
		  mod :=&MonitorModule{
			  Name:         name,
			  Config:       model.GetDefaultConfig(),
			  ModuleStatus: 0,
			  Desc:          model.GetDesc(),
			  Models:        model.GetModel(),
		  }

		v, ok := m[name]
		if ok {
			mod.ModuleStatus = v.ModuleStatus
			c ,err := model.Decode(v.Config)
			if err == nil {
				mod.Config = c
			}
		}

		modules = append(modules, mod)
	}
	return modules, nil
}

func SetMonitorModule(moduleName string, config string, moduleStatus int) error {

	model,has := ksitigarbha.GetMonitorModuleModel(moduleName)
	if !has {
		return errors.New("[error]the module does not exist")
	}

	if moduleStatus == 1  {

		_ ,err:= model.Decode(config)
		if err != nil{
			//errInfo := "[error]invalid config"
			return err
		}

	}

	err := console_sqlite3.SetMonitorModule(moduleName, config, moduleStatus)
	if err != nil {
		return err
	}
	return nil
}