-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
154 lines (127 loc) · 4.4 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
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
// Configuration management for the http server part
import (
"encoding/json"
"os"
)
// Main configuration struct
type Configuration struct {
// IP on which the webserver should listen on. By default it's 127.0.0.1.
// Name in the config file: listener_ip
IP string `json:"listener_ip"`
// Port on which the webserver should listen on. By default it's 8080.
// Name in the config file: listener_port
Port uint16 `json:"listener_port"`
// Comma-separated list of IP to only listen from. Queries from other
// IPs will be discarded. Empty to serve all the clients without restrictions
// which is the default value.
// For the list of the current IP Catchpoint is pushing from, see:
// https://support.catchpoint.com/hc/en-us/articles/202459889-Alerts-API
// Name in the config file: authorized_ips
AuthIPs string `json:"authorized_ips"`
// The number of concurrent CPUs the HTTP server is allowed to use.
// This sets GOMAXPROCS at the run time.
// If the GOMAXPROCS environment variable is set and to a bigger value than
// this number, the value of the environment variable will be taken.
// For more details, see https://golang.org/src/runtime/debug.go?s=995:1021#L16
// By default this configuration value is set to 1.
// Name in the config file: max_procs
Procs int `json:"max_procs"`
// Path to the log file you want to have the output to. Keep it empty if you
// want to log to the console.
// Defaults to empty so console logging.
LogFile string `json:"log_file"`
// Endpoints list specifying which plugin should handle which endpoint
Endpoints []Endpoint `json:"endpoints"`
// Emitter. Listener to give results back
// Contains: if emitter is enabled, templateDir,
// templateName, listenerURI
Emitter Emitter `json:"emitter"`
// Configuration of the nsca plugin
NSCA Nsca `json:"nsca"`
}
// The endpoints define which plugin is used for each supported endpoint
type Endpoint struct {
// The definition of the Path of the endpoint (for example "/catchpoint/alerts")
URIPath string `json:"uri_path"`
// The name of the plugin that is supposed to handle this endpoint.
// Currently supported values:
// - catchpoint_alerts
PluginName string `json:"plugin_name"`
}
// Configuration of NSCA
type Nsca struct {
// Wether or not we want to send alerts with this method. If empty the default
// value will be false (as it is the default value of a boolean in Go)
Enabled bool `json:"enabled"`
// The name of the NSCA server to send the data to. No default value.
Server string `json:"server"`
// Full path of the send_nsca command on the system
// Defaults to "/usr/sbin/send_nsca"
OsCommand string `json:"os_command_path"`
// Configuration file path for the send_nsca command
// Defaults to "/etc/send_nsca.cfg"
ConfigFile string `json:"config_file"`
// The name of the host you want to use when sending the nsca messages
ClientHost string `json:"client_host"`
}
type Emitter struct {
// Wether or not swith alerts emitter for TM Health
Enabled bool `json:"enabled"`
// Queue for checks.
// Actually a placeholder for host value
Queue string `json:"queue"`
// URI for health check emission
URI []Listener `json:"uri"`
// Directory for templates.
// Templates are used for TM Health check
TemplateDir string `json:"template_dir"`
// Temaplate name
Template string `json:"template"`
}
type Listener struct {
// Path to the emitter endpoint
// For example, /catchpoint/health
URIPath string `json:"uri_path"`
}
// This function loads the configuration file given in parameter and returns a
// pointer to a Configuration object
func (cfg *Configuration) loadConfig(confFilePath string) error {
file, oserr := os.Open(confFilePath)
if oserr != nil {
return oserr
}
decoder := json.NewDecoder(file)
err := decoder.Decode(&cfg)
if err != nil {
return err
}
// Loading the default configurations
if cfg.IP == "" {
cfg.IP = "127.0.0.1"
}
if cfg.Port == 0 {
cfg.Port = 8080
}
if cfg.Procs == 0 {
cfg.Procs = 1
}
if len(cfg.NSCA.ClientHost) == 0 {
cfg.NSCA.ClientHost, err = os.Hostname()
return err
}
if len(cfg.NSCA.OsCommand) == 0 {
cfg.NSCA.OsCommand = "/usr/sbin/send_nsca"
}
if len(cfg.NSCA.ConfigFile) == 0 {
cfg.NSCA.ConfigFile = "/etc/send_nsca.cfg"
}
if len(cfg.Emitter.Queue) == 0 {
cfg.Emitter.Queue = "Catchpoint"
}
if len(cfg.Emitter.URI) == 0 {
u := Listener{URIPath: "/api/reports"}
cfg.Emitter.URI = append(cfg.Emitter.URI, u)
}
return nil
}