From e267db829aae224916a75c066f8e47c0562d4a36 Mon Sep 17 00:00:00 2001 From: barryz Date: Tue, 15 May 2018 18:46:07 +0800 Subject: [PATCH] change config file format to yaml --- cfg.json.example | 48 ------------------------------- config.example.yml | 56 ++++++++++++++++++++++++++++++++++++ falcon/falcon_test.go | 2 +- funcs/funcs_test.go | 2 +- g/cfg.go | 66 +++++++++++++++++++++---------------------- witch/witch_test.go | 2 +- 6 files changed, 92 insertions(+), 84 deletions(-) delete mode 100644 cfg.json.example create mode 100644 config.example.yml diff --git a/cfg.json.example b/cfg.json.example deleted file mode 100644 index 71b3385..0000000 --- a/cfg.json.example +++ /dev/null @@ -1,48 +0,0 @@ -{ - "debug": false, - "hostname": "", - "interval": 10, - "batchsize": 100, - "details": true, - "enabled": { - "collect": true, - "cmdb": true, - "witch": true, - "log_rotate": true - }, - "http": { - "conn_timeout": 10, - "response_timeout": 10 - }, - "rabbitmq": { - "host": "127.0.0.1", - "port": 15672, - "user": "admin", - "password": "admin" - }, - "falcon": { - "api": "http://127.0.0.1:1988/v1/push" - }, - "scheduler": { - "log_rotate": "0 11 18 ? * 0-6" - }, - "witch": { - "listen": ":5671", - "control": "buildin", - "service": "", - "command": "sleep 120", - "process": "beam", - "pid_file": "/var/run/witch.pid", - "auth": { - "admin": "ADMIN" - } - }, - "ignore_queue": [ - "test", - "celery" - ], - "qrunning": [ - "idle", - "running" - ] -} diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..339a7f3 --- /dev/null +++ b/config.example.yml @@ -0,0 +1,56 @@ +# debug mode +debug: false +# getting metric details +details: true +# hostname specified +hostname: "" +# agent collect interval +interval: 10 +# push batch-size metrics per-operation +batchsize: 100 +# enable functions +enabled: + collect: true + witch: true + log_rotate: true + +# http client settings (timeout in seconds) +http: + conn_timeout: 10 + response_timeout: 10 + +# RabbitMQ settings +rabbitmq: + host: 127.0.0.1 + port: 15672 + user: admin + password: admin + +# falcon settings +falcon: + api: "http://127.0.0.1:1988/v1/push" + +# scheduler config +scheduler: + log_rotate: "0 11 18 ? * 0-6" + +# witch config +witch: + listen: ":5671" + control: "buildin" + service: "" + command: "sleep 120" + process: "beam" + pid_file: "var/run/witch.pid" + auth: + admin: "ADMIN" + +# filter for queues +ignore_queue: + - "test" + - "celery" + +# status which indicate queue's running state +qrunning: + - "idle" + - "running" diff --git a/falcon/falcon_test.go b/falcon/falcon_test.go index 42c4008..1e88103 100644 --- a/falcon/falcon_test.go +++ b/falcon/falcon_test.go @@ -7,7 +7,7 @@ import ( ) func TestGetStatsDB(t *testing.T) { - g.ParseConfig("../cfg.json.example") + g.ParseConfig("../config.example.yml") //ov, err := funcs.GetOverview() //if err != nil { diff --git a/funcs/funcs_test.go b/funcs/funcs_test.go index 9ee67b7..9c214c3 100644 --- a/funcs/funcs_test.go +++ b/funcs/funcs_test.go @@ -7,7 +7,7 @@ import ( ) func TestGetExchanges(t *testing.T) { - g.ParseConfig("../cfg.json.example") + g.ParseConfig("../config.example.yml") //exchs, err := GetExchanges() //if err != nil { // t.Error(err.Error()) diff --git a/g/cfg.go b/g/cfg.go index 74f4189..e7636cc 100644 --- a/g/cfg.go +++ b/g/cfg.go @@ -1,71 +1,71 @@ package g import ( - "encoding/json" "log" "os" "sync" "github.com/toolkits/file" + "gopkg.in/yaml.v2" ) // EnableConfig configs which can be used type EnableConfig struct { - Collect bool `json:"collect"` - LogRotate bool `json:"log_rotate"` - Witch bool `json:"witch"` + Collect bool `yaml:"collect"` + LogRotate bool `yaml:"log_rotate"` + Witch bool `yaml:"witch"` } // RabbitConfig ... type RabbitConfig struct { - Host string `json:"host"` - Port int `json:"port"` - User string `json:"user"` - Password string `json:"password"` + Host string `yaml:"host"` + Port int `yaml:"port"` + User string `yaml:"user"` + Password string `yaml:"password"` } // FalconConfig ... type FalconConfig struct { - API string `json:"api"` + API string `yaml:"api"` } // HTTPConfig ... type HTTPConfig struct { - ConnTimeout int `json:"conn_timeout"` - RespTimeout int `json:"response_timeout"` + ConnTimeout int `yaml:"conn_timeout"` + RespTimeout int `yaml:"response_timeout"` } // SchedulerConfig ... type SchedulerConfig struct { - LogRotate string `json:"log_rotate"` + LogRotate string `yaml:"log_rotate"` } // WitchConfig Program Config ... type WitchConfig struct { - ListenAddr string `json:"listen"` - Control string `json:"control"` - Service string `json:"service"` - Process string `json:"process"` - Command string `json:"command"` - PidFile string `json:"pid_file"` - Auth map[string]string `json:"auth"` + ListenAddr string `yaml:"listen"` + Control string `yaml:"control"` + Service string `yaml:"service"` + Process string `yaml:"process"` + Command string `yaml:"command"` + PidFile string `yaml:"pid_file"` + Auth map[string]string `yaml:"auth"` } // GlobalConfig ... type GlobalConfig struct { - Debug bool `json:"debug"` - Details bool `json:"details"` - Hostname string `json:"hostname"` - Batchsize int `json:"batchsize"` - Interval int64 `json:"interval"` - Rabbit *RabbitConfig `json:"rabbitmq"` - Falcon *FalconConfig `json:"falcon"` - HTTP *HTTPConfig `json:"http"` - Cron *SchedulerConfig `json:"scheduler"` - Enabled *EnableConfig `json:"enabled"` - Ignores []string `json:"ignore_queue"` - Qrunning []string `json:"qrunning"` - Witch *WitchConfig `json:"witch"` + Debug bool `yaml:"debug"` + Details bool `yaml:"details"` + Hostname string `yaml:"hostname"` + Batchsize int `yaml:"batchsize"` + Interval int64 `yaml:"interval"` + Rabbit *RabbitConfig `yaml:"rabbitmq"` + Falcon *FalconConfig `yaml:"falcon"` + HTTP *HTTPConfig `yaml:"http"` + Cron *SchedulerConfig `yaml:"scheduler"` + Enabled *EnableConfig `yaml:"enabled"` + Ignores []string `yaml:"ignore_queue"` + Qrunning []string `yaml:"qrunning"` + Witch *WitchConfig `yaml:"witch"` } var ( @@ -93,7 +93,7 @@ func ParseConfig(cfg string) { log.Fatalln("[ERROR]: read config file:", cfg, "fail:", err) } - err = json.Unmarshal([]byte(configContent), &c) + err = yaml.Unmarshal([]byte(configContent), &c) if err != nil { log.Fatalln("[ERROR]: read config file:", cfg, "fail:", err) } diff --git a/witch/witch_test.go b/witch/witch_test.go index 2f1b026..5418c4b 100644 --- a/witch/witch_test.go +++ b/witch/witch_test.go @@ -7,7 +7,7 @@ import ( ) func Test_SystemLauncher(t *testing.T) { - g.ParseConfig("../cfg.json.example") + g.ParseConfig("../config.example.yml") //Launch() //