-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
141 lines (133 loc) · 3.21 KB
/
config.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* config.go - the structure of configuration and related functions
*
* history
* --------------------
* 2017/8/18, by Ye Zhiqin, create
* 2017/9/30, by Ye Zhiqin, modify
* 2018/1/3, by Ye Zhiqin, modify
*
* DESCRIPTION
* This file contains the definition of configuration data structure
* and the functions to load configuration file and check md5sum of
* the configuration file
*/
package main
import (
"crypto/md5"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"log"
"os"
)
type Config struct {
Falcon FalconConfig `yaml:"falcon"`
Logs []LogConfig `yaml:"logs"`
}
type FalconConfig struct {
Url string `yaml:"url"`
Endpoint string `yaml:"endpoint"`
}
type LogConfig struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
Delimiter string `yaml:"delimiter"`
TsEnabled bool `yaml:"tsEnabled"`
TsPattern string `yaml:"tsPattern"`
InotifyEnabled bool `yaml:"inotifyEnabled"`
Items []ItemConfig `yaml:"items"`
}
type ItemConfig struct {
Metric string `yaml:"metric"`
Tags string `yaml:"tags"`
CounterType string `yaml:"counterType"`
Step int64 `yaml:"step"`
Pattern string `yaml:"pattern"`
Reversed bool `yaml:"reversed"`
Threshold float64 `yaml:"threshold"`
Method string `yaml:"method"`
}
var config *Config
var configMD5Sum []byte
/*
* CheckConfigMD5 - calculate the md5sum of configuration file
*
* PARAMS:
* No paramter
*
* RETURNS:
* []byte, nil, if succeed
* nil, error, if fail
*/
func CheckConfigMD5() ([]byte, error) {
f, err := os.Open("config.yaml")
if err != nil {
log.Printf("configuration file opening FAIL: %v", err)
return nil, err
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Printf("md5 copying FAIL: %v", err)
return nil, err
}
return h.Sum(nil), nil
}
/*
* LoadConfig - load configuration file to Config struct
*
* PARAMS:
* No paramter
*
* RETURNS:
* nil, if error ocurred
* *Config, if succeed
*/
func LoadConfig() *Config {
cfg := new(Config)
buf, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Printf("configuration file reading FAIL: %v", err)
return nil
}
if err := yaml.Unmarshal(buf, cfg); err != nil {
log.Printf("yaml file unmarshal FAIL: %v", err)
return nil
}
log.Printf("config: %v", cfg)
// check configuration
if cfg.Falcon.Url == "" {
log.Printf("Url of falcon agent api should not EMPTY!")
return nil
}
for _, one := range cfg.Logs {
if one.Name == "" {
log.Printf("Name of log should not EMPTY!")
return nil
}
if one.Path == "" {
log.Printf("Path of log should not EMPTY!")
return nil
}
for _, item := range one.Items {
if item.Metric == "" {
log.Printf("Metric of item should not EMPTY!")
return nil
}
if item.CounterType != "GAUGE" && item.CounterType != "COUNTER" {
log.Printf("CouterType of item should be 'GAUGE' or 'COUNTER'")
return nil
}
if item.Pattern == "" {
log.Printf("Pattern of item should not EMPTY!")
return nil
}
if item.Method != "count" && item.Method != "Tcount" && item.Method != "statistic" {
log.Printf("Method of item should be 'count'/'Tcount'/'statistic'")
return nil
}
}
}
return cfg
}