forked from jondot/groundcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (110 loc) · 3.15 KB
/
main.go
File metadata and controls
138 lines (110 loc) · 3.15 KB
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
package main
/*
Ground Control
The main outline should read the configuration file, set up
various reporters and a timer with a goroutine that handles grabbing
the health data and reporting under each kind of reporter.
It should also set up the various HTTP handlers on the proper
mountpoints.
*/
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
func main() {
configPath := flag.String("config", "/etc/groundcontrol.json", "Ground control config file.")
version := flag.Bool("version", false, "Output version and exit")
flag.Parse()
if *version {
fmt.Println("Ground Control", VERSION)
os.Exit(0)
}
config := loadConfiguration(*configPath)
erroroutAndUsage := false
if config.Interval < 10 {
println("Error: Interval cannot be smaller than 10")
erroroutAndUsage = true
}
if erroroutAndUsage {
println("") // intentionally blank
flag.Usage()
os.Exit(-1)
}
control := NewControl(config.Controls)
//
// set up reporters
//
webreporter := NewWebReporter(config.HistoryInterval, config.HistoryBacklog)
reporters := []Reporter{webreporter}
if config.TempoDB.User == "" || config.TempoDB.Key == "" {
log.Println("Reporters: No TempoDB credentials, skipping.")
} else {
reporters = append(reporters, NewTempoDBReporter(config.TempoDB))
log.Println("Reporters: TempoDB OK.")
}
if config.Librato.User == "" || config.Librato.Key == "" {
log.Println("Reporters: No Librato credentials, skipping.")
} else {
reporters = append(reporters, NewLibratoReporter(config.Librato))
log.Println("Reporters: Librato OK.")
}
if config.Graphite.LineRec == "" {
log.Println("Reporters: No Graphite config, skipping.")
} else {
reporters = append(reporters, NewGraphiteReporter(config.Graphite))
log.Println("Reporters: Graphite OK.")
}
if config.Stdout {
log.Println("Reporters: Showing you output (you said -stdout=true).")
reporters = append(reporters, NewStdoutReporter())
}
log.Println("Launching Health")
report(config, &reporters)
// set up a periodic report
ticker := time.NewTicker(time.Second * time.Duration(config.Interval))
go func() {
for _ = range ticker.C {
report(config, &reporters)
}
}()
log.Println("Launching Control")
//statics (UI)
http.Handle("/", http.FileServer(http.Dir("./web/")))
//control endpoint
http.HandleFunc(control.Mount, control.Handler)
http.HandleFunc(webreporter.Mount, webreporter.Handler)
http.ListenAndServe(fmt.Sprintf("%s:%d", config.Host, config.Port), nil)
}
func report(config *GroundControlConfig, reporters *[]Reporter) {
h, err := GetHealth(config.Temperature)
if err != nil {
log.Fatalln(err)
os.Exit(-1)
}
for _, r := range *reporters {
r.ReportHealth(h)
}
}
func loadConfiguration(configPath string) (c *GroundControlConfig) {
if configPath == "" {
println("Error: You should specify a config file.")
flag.Usage()
os.Exit(-1)
}
text, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalln("Cannot read config file: ", configPath)
}
config := &GroundControlConfig{}
err = json.Unmarshal(text, &config)
if err != nil {
log.Fatalln("Cannot parse config file: ", configPath)
}
return config
}