| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package config
- import (
- "fmt"
- "github.com/coroot/coroot-node-agent/flags"
- "github.com/coroot/coroot-node-agent/utils"
- "github.com/fsnotify/fsnotify"
- log "github.com/sirupsen/logrus"
- "github.com/spf13/viper"
- "path/filepath"
- "reflect"
- )
- type commonCfgRecord struct {
- ConfigServer string `mapstructure:"config_server"`
- DataServer string `mapstructure:"data_server"`
- //FileServer string `ini:"file_server"`
- ProxyConfigServer string `mapstructure:"proxy_config_server"`
- ProxyDataServer string `mapstructure:"proxy_data_server"`
- //ProxyFileServer string `ini:"proxy_file_server"`
- //Ip string `ini:"ip"`
- //Token string `ini:"token"`
- //HostId string `ini:"host_id"`
- //HostTag string `ini:"host_tag"`
- //RealAccountId string `ini:"real_account_id"`
- }
- type omniCommonCfg struct {
- omniCommonV *viper.Viper
- commonSecV *viper.Viper
- cfgRecordLast *commonCfgRecord
- }
- func initOmniCommonCfg() (*omniCommonCfg, error) {
- var (
- omniConfDirAbs string
- omniConfDirAbs2 string
- err error
- )
- v := viper.New()
- if *flags.CommonIni != "" {
- // *flags.CommonIni默认值 => /opt/cloudwise/omniagent/conf/common.ini
- omniConfDirAbs, err = filepath.Abs(filepath.Dir(*flags.CommonIni))
- if err != nil {
- omniConfDirAbs = ""
- log.Warningf("get filepath.Abs for the flags.CommonIni[%s] occurs error: %s.", *flags.CommonIni, err.Error())
- } else {
- v.AddConfigPath(omniConfDirAbs)
- }
- }
- //结合部署路径,获取omniagent common.ini文件所在路径 => eg: /xxx/cloudwise/omniagent/agents/euspace/0.0.7/../../../conf
- omniConfDir := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(utils.GetRootPath()))), "conf")
- omniConfDirAbs2, err = filepath.Abs(omniConfDir)
- if err != nil {
- omniConfDirAbs2 = ""
- log.Warningf("get filepath.Abs for omniConfDir[%s] occurs error: %s.", omniConfDir, err.Error())
- } else {
- if omniConfDirAbs2 != "" && omniConfDirAbs != omniConfDirAbs2 {
- v.AddConfigPath(omniConfDirAbs2)
- }
- }
- v.SetConfigName("common")
- v.SetConfigType("ini")
- if err = v.ReadInConfig(); err != nil {
- if _, ok := err.(viper.ConfigFileNotFoundError); ok {
- return nil, nil
- }
- return nil, err
- }
- commonSecV := v.Sub("common")
- if commonSecV == nil {
- return nil, fmt.Errorf("`common` section not found in Config file [%s]", v.ConfigFileUsed())
- }
- //fmt.Printf("v.ConfigFileUsed() =====> %s \n", v.ConfigFileUsed())
- //set RunInOmniagent at first
- *flags.RunInOmniagent = true
- //set CommonIni
- if v.ConfigFileUsed() != *flags.CommonIni {
- *flags.CommonIni = v.ConfigFileUsed()
- }
- // set ServerPrefix
- if *flags.ServerPrefix == "" {
- *flags.ServerPrefix = "/apm"
- }
- omc := &omniCommonCfg{
- omniCommonV: v,
- commonSecV: commonSecV,
- cfgRecordLast: new(commonCfgRecord),
- }
- return omc, nil
- }
- func (omc *omniCommonCfg) getConfigServer() string {
- omniPrxCfgSvr := omc.commonSecV.GetString("proxy_config_server")
- if omniPrxCfgSvr != "" {
- return omniPrxCfgSvr
- } else {
- return omc.commonSecV.GetString("config_server")
- }
- }
- func (omc *omniCommonCfg) getDataServer() string {
- omniPrxDataSvr := omc.commonSecV.GetString("proxy_data_server")
- if omniPrxDataSvr != "" {
- return omniPrxDataSvr
- } else {
- return omc.commonSecV.GetString("data_server")
- }
- }
- func (omc *omniCommonCfg) getFileServer() string {
- omniPrxFileSvr := omc.commonSecV.GetString("proxy_file_server")
- if omniPrxFileSvr != "" {
- return omniPrxFileSvr
- } else {
- return omc.commonSecV.GetString("file_server")
- }
- }
- func (omc *omniCommonCfg) watchConfig(c *Config) {
- omc.omniCommonV.OnConfigChange(func(e fsnotify.Event) {
- //fmt.Printf("Config file changed [%s],Op:%d ,AllKeys:%#v======> \n", e.Name, e.Op, omc.omniCommonV.AllSettings())
- if !e.Op.Has(fsnotify.Create) && !e.Op.Has(fsnotify.Write) {
- return
- }
- c.m.Lock()
- //defer c.m.Unlock()
- omc.commonSecV = omc.omniCommonV.Sub("common")
- if omc.commonSecV == nil {
- //`common` section not found
- c.m.Unlock()
- return
- }
- ccr := new(commonCfgRecord)
- if err := omc.commonSecV.Unmarshal(ccr); err != nil {
- log.Warningf("on-Config-change,`omniCommonV` unmarshal `commonCfgRecord` failed : %s", err.Error())
- c.m.Unlock()
- return
- }
- if reflect.DeepEqual(omc.cfgRecordLast, ccr) {
- c.m.Unlock()
- return
- }
- //update cfgRecordLast
- omc.cfgRecordLast = ccr
- c.m.Unlock()
- hasChange := false
- omniCfgSvr := omc.getConfigServer()
- if omniCfgSvr != "" && omniCfgSvr != c.euspaceCfg.cfgRecordLast.ConfigServer {
- hasChange = true
- c.euspaceCfg.cfgRecordLast.ConfigServer = omniCfgSvr
- if *flags.ConfigServer == "" {
- c.m.Lock()
- c.euspaceCfg.runtimeV.Set("configServer", omniCfgSvr)
- c.m.Unlock()
- c.noticeSubscribers(ScbTypeConfigServerChg)
- }
- }
- omniDataSvr := omc.getDataServer()
- if omniDataSvr != "" && omniDataSvr != c.euspaceCfg.cfgRecordLast.DataServer {
- hasChange = true
- c.euspaceCfg.cfgRecordLast.DataServer = omniDataSvr
- if *flags.DataServer == "" {
- c.m.Lock()
- c.euspaceCfg.runtimeV.Set("dataServer", omniDataSvr)
- c.m.Unlock()
- c.noticeSubscribers(ScbTypeDataServerChg)
- c.noticeSubscribers(ScbTypeTracesEndpointChg)
- }
- }
- // sync common.ini to euspace Config.ymal
- if hasChange {
- c.m.Lock()
- if err := c.euspaceCfg.syncConfToFile(c.euspaceCfg.cfgRecordLast); err != nil {
- log.Warningf("on-Config-change,%s", err.Error())
- }
- c.m.Unlock()
- }
- })
- omc.omniCommonV.WatchConfig()
- }
|