config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package g
  2. import (
  3. "fmt"
  4. "log"
  5. "sync"
  6. "github.com/toolkits/file"
  7. "gopkg.in/yaml.v2"
  8. )
  9. type LogConfig struct {
  10. Path string `yaml:"path"`
  11. Filename string `yaml:"filename"`
  12. Level string `yaml:"level"`
  13. }
  14. type MysqlConfig struct {
  15. Addr string `yaml:"addr"`
  16. Idle int `yaml:"idle"`
  17. Max int `yaml:"max"`
  18. }
  19. type HttpConfig struct {
  20. Listen string `yaml:"listen"`
  21. Secret string `yaml:"secret"`
  22. }
  23. type RpcConfig struct {
  24. Listen string `yaml:"listen"`
  25. }
  26. type AlarmConfig struct {
  27. Enable bool `yaml:"enable"`
  28. Batch int `yaml:"batch"`
  29. Replicas int `yaml:"replicas"`
  30. ConnTimeout int `yaml:"connTimeout"`
  31. CallTimeout int `yaml:"callTimeout"`
  32. MaxConns int `yaml:"maxConns"`
  33. MaxIdle int `yaml:"maxIdle"`
  34. SleepTime int `yaml:"sleepTime"`
  35. Cluster map[string]string `yaml:"cluster"`
  36. }
  37. type FalconConfig struct {
  38. Enable bool `yaml:"enable"`
  39. Addr string `yaml:"addr"`
  40. Interval int `yaml:"interval"`
  41. }
  42. type StatsdConfig struct {
  43. Enable bool `yaml:"enable"`
  44. Addr string `yaml:"addr"`
  45. }
  46. type PromConfig struct {
  47. Enable bool `yaml:"enable"`
  48. Addr string `yaml:"addr"`
  49. }
  50. type LdapConfig struct {
  51. Enabled bool `yaml:"enabled"`
  52. Addr string `yaml:"addr"`
  53. BindDN string `yaml:"bindDN"`
  54. BaseDN string `yaml:"baseDN`
  55. BindPasswd string `yaml:"bindPasswd"`
  56. UserField string `yaml:"userField"`
  57. Attributes []string `yaml:attributes`
  58. }
  59. type InternalDnsConfig struct {
  60. Enable bool `yaml:"enable"`
  61. CMD string `yaml:"cmd"`
  62. }
  63. type GlobalConfig struct {
  64. Debug bool `yaml:"debug"`
  65. Admins []string `yaml:"admins"`
  66. Salt string `yaml:"salt"`
  67. Register bool `yaml:"register"`
  68. ShowDurationMin int `yaml:"showDurationMin"` //查看最近几分钟内的报警历史和绘图,默认为30分钟
  69. KeepDurationHour int `yaml:"keepDurationHour"` //保留历史数据时间长度,默认为12小时
  70. Http *HttpConfig `yaml:"http"`
  71. Rpc *RpcConfig `yaml:"rpc"`
  72. Ldap *LdapConfig `yaml:"ldap"`
  73. Mysql *MysqlConfig `yaml:"mysql"`
  74. Alarm *AlarmConfig `yaml:"alarm"`
  75. Falcon *FalconConfig `yaml:"falcon"`
  76. Statsd *StatsdConfig `yaml:"statsd"`
  77. Prom *PromConfig `yaml:"prom"`
  78. IDC []string `yaml:"idc"`
  79. }
  80. var (
  81. Config *GlobalConfig
  82. configLock = new(sync.RWMutex)
  83. )
  84. func Parse(cfg string) error {
  85. if cfg == "" {
  86. return fmt.Errorf("use -c to specify configuration file")
  87. }
  88. if !file.IsExist(cfg) {
  89. return fmt.Errorf("configuration file %s is nonexistent", cfg)
  90. }
  91. configContent, err := file.ToTrimString(cfg)
  92. if err != nil {
  93. return fmt.Errorf("read configuration file %s fail %s", cfg, err.Error())
  94. }
  95. var c GlobalConfig
  96. err = yaml.Unmarshal([]byte(configContent), &c)
  97. if err != nil {
  98. return fmt.Errorf("parse configuration file %s fail %s", cfg, err.Error())
  99. }
  100. configLock.Lock()
  101. defer configLock.Unlock()
  102. Config = &c
  103. log.Println(Config)
  104. if len(Config.IDC) < 1 {
  105. return fmt.Errorf("idc is null")
  106. }
  107. log.Println("load configuration file", cfg, "successfully")
  108. return nil
  109. }