forked from JustinBack/cachet-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
275 lines (219 loc) · 5.89 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"github.com/urfave/cli"
"github.com/Sirupsen/logrus"
cachet "github.com/Soontao/cachet-monitor"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
// Version string, in release version
// This variable will be overwrite by complier
var Version = "SNAPSHOT"
// AppName of this application
var AppName = "Cachet Monitor"
// AppUsage of this application
var AppUsage = "A Command Line Tool for Cachet Monitor"
func main() {
app := cli.NewApp()
app.Version = Version
app.Name = AppName
app.Usage = AppUsage
app.EnableBashCompletion = true
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config,c",
EnvVar: "CONFIG_FILE",
Value: "./config.json",
Usage: "Path to configuration file",
},
cli.StringFlag{
Name: "server,s",
EnvVar: "CACHET_SERVER_API",
Usage: "The server api uri, with schema & path",
},
cli.StringFlag{
Name: "token,t",
EnvVar: "CACHET_USER_TOKEN",
Usage: "The server user token",
},
cli.StringFlag{
Name: "auto,a",
EnvVar: "AUTO_CONFIG",
Usage: "Auto configuration",
},
cli.StringFlag{
Name: "log,l",
EnvVar: "LOG_FILE",
Usage: "Path to log file",
},
cli.StringFlag{
Name: "name,n",
EnvVar: "SYSTEM_NAME",
Usage: "System name",
},
}
app.Action = appAction
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func appAction(c *cli.Context) (err error) {
autoConfig := c.GlobalBool("auto")
configPath := c.GlobalString("config")
systemName := c.GlobalString("name")
logFile := c.GlobalString("log")
var cfg *cachet.CachetMonitor
if autoConfig {
logrus.Println("Start auto configuration")
api := cachet.CachetAPI{
URL: c.GlobalString("server"),
Token: c.GlobalString("token"),
}
if cfg, err = api.GetConfigurationFromRemote(); err != nil {
logrus.Panicf("Unable to start (fetch config): %v", err)
}
} else {
logrus.Println("Start file based configuration")
if cfg, err = getConfiguration(configPath); err != nil {
logrus.Panicf("Unable to start (reading config): %v", err)
}
}
if cfg, err = setupMonitors(cfg); err != nil {
logrus.Panicf("Unable to setup config: %v", err)
}
cfg.Immediate = true
if systemName != "" {
cfg.SystemName = systemName
}
logrus.SetOutput(getLogger(logFile))
if valid := cfg.Validate(); !valid {
logrus.Errorf("Invalid configuration")
os.Exit(1)
}
logrus.Debug("Configuration valid")
logrus.Infof("System: %s", cfg.SystemName)
logrus.Infof("API: %s", cfg.API.URL)
logrus.Infof("Monitors: %d\n", len(cfg.Monitors))
logrus.Infof("Pinging cachet")
if err := cfg.API.Ping(); err != nil {
logrus.Errorf("Cannot ping cachet!\n%v", err)
os.Exit(1)
}
logrus.Infof("Ping OK")
wg := &sync.WaitGroup{}
for index, monitor := range cfg.Monitors {
logrus.Infof("Starting Monitor #%d: ", index)
logrus.Infof("Features: \n - %v", strings.Join(monitor.Describe(), "\n - "))
go monitor.ClockStart(cfg, monitor, wg)
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
<-signals
logrus.Warnf("Abort: Waiting monitors to finish")
for _, mon := range cfg.Monitors {
mon.GetMonitor().ClockStop()
}
wg.Wait()
return err
}
func getLogger(logPath interface{}) *os.File {
if logPath == nil || len(logPath.(string)) == 0 {
return os.Stdout
}
file, err := os.Create(logPath.(string))
if err != nil {
logrus.Errorf("Unable to open file '%v' for logging: \n%v", logPath, err)
os.Exit(1)
}
return file
}
func setupMonitors(cfg *cachet.CachetMonitor) (*cachet.CachetMonitor, error) {
cfg.Monitors = make([]cachet.MonitorInterface, len(cfg.RawMonitors))
for index, rawMonitor := range cfg.RawMonitors {
var t cachet.MonitorInterface
var err error
monType := "unknown"
if t, ok := rawMonitor["type"].(string); ok {
monType = cachet.GetMonitorType(t)
}
switch monType {
case "http", "https":
var s cachet.HTTPMonitor
err = mapstructure.Decode(rawMonitor, &s)
t = &s
case "dns":
var s cachet.DNSMonitor
err = mapstructure.Decode(rawMonitor, &s)
t = &s
case "tcp":
var s cachet.TCPMonitor
err = mapstructure.Decode(rawMonitor, &s)
t = &s
default:
logrus.Errorf("Invalid monitor type (index: %d) %v", index, monType)
continue
}
// >> config default incident
defaultIncident := &cachet.Incident{
Name: t.GetMonitor().Name,
ComponentID: t.GetMonitor().ComponentID,
Notify: true,
}
componentStatus, err := cfg.API.GetComponentStatus(defaultIncident.ComponentID)
defaultIncident.ComponentStatus = componentStatus
// not operational
if defaultIncident.ComponentStatus > 1 {
logrus.Infof("%v start with incident.", t.GetMonitor().Name)
t.GetMonitor().SetDefaultIncident(defaultIncident)
}
t.GetMonitor().Type = monType
// << config default incident
if err != nil {
logrus.Errorf("Unable to unmarshal monitor to type (index: %d): %v", index, err)
continue
}
cfg.Monitors[index] = t
}
return cfg, nil
}
func getConfiguration(path string) (*cachet.CachetMonitor, error) {
cfg := &cachet.CachetMonitor{}
var data []byte
// test if its a url
url, err := url.ParseRequestURI(path)
if err == nil && len(url.Scheme) > 0 {
// download config
response, err := http.Get(path)
if err != nil {
logrus.Warn("Unable to download network configuration")
return nil, err
}
defer response.Body.Close()
data, _ = ioutil.ReadAll(response.Body)
logrus.Info("Downloaded network configuration.")
} else {
data, err = ioutil.ReadFile(path)
if err != nil {
return nil, errors.New("Unable to open file: '" + path + "'")
}
}
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
err = yaml.Unmarshal(data, cfg)
} else {
err = json.Unmarshal(data, cfg)
}
if err != nil {
logrus.Warnf("Unable to parse configuration file")
}
return cfg, err
}