-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
291 lines (234 loc) · 7.01 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/GoMetric/opcache-dashboard/configuration"
"github.com/GoMetric/opcache-dashboard/metrics"
"github.com/GoMetric/opcache-dashboard/observer"
"github.com/GoMetric/opcache-dashboard/ui"
"github.com/NYTimes/gziphandler"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
GoMetricStatsdClient "github.com/GoMetric/go-statsd-client"
)
// Version is a current git commit hash and tag
// Injected by compilation flag
var Version = "Unknown"
// BuildNumber is a current commit hash
// Injected by compilation flag
var BuildNumber = "Unknown"
// BuildDate is a date of build
// Injected by compilation flag
var BuildDate = "Unknown"
func main() {
// command line options
var configPath = flag.String("config", "", "Path to configuration")
var httpHost = flag.String("http-host", configuration.DefaultHTTPHost, "HTTP Host for GUI and API")
var httpPort = flag.Int("http-port", configuration.DefaultHTTPPort, "HTTP Port for GUI and API")
var pullIntervalSeconds = flag.Int64("pull-interval", configuration.DefaultRefreshIntervalSeconds, "Pull interval in seconds")
var statsdHost = flag.String("statsd-host", "", "StatsD Host. If empty, metric tracking will be disabled")
var statsdPort = flag.Int("statsd-port", 0, "StatsD Port")
var statsdMetricPrefix = flag.String("statsd-metric-prefix", "", "Prefix of metric name")
var verbose = flag.Bool("verbose", false, "Verbose")
var version = flag.Bool("version", false, "Show version")
flag.Parse()
// show version and exit
if *version == true {
fmt.Printf(
"Opcache Dashboard v.%s, build %s from %s\n",
Version,
BuildNumber,
BuildDate,
)
os.Exit(0)
}
// configure logging
var logOutput io.Writer
if *verbose == true {
logOutput = os.Stderr
} else {
logOutput = ioutil.Discard
}
log.SetOutput(logOutput)
// read PHP cluster configuration
var applicationConfig configuration.ApplicationConfig
if *configPath != "" {
var absoluteConfigFilePath, _ = filepath.Abs(*configPath)
var configFileExt = filepath.Ext(absoluteConfigFilePath)[1:]
var configReader, configReadError = configuration.NewConfigReader(configFileExt)
if configReadError != nil {
log.Fatalln(configReadError)
return
}
applicationConfig = configReader.ReadConfig(absoluteConfigFilePath)
} else {
log.Fatal("Config not defined")
}
// apply cli flags to app config
applicationConfig.ApplyCliFlags(
configuration.CliFlags{
HttpHost: httpHost,
HttpPort: httpPort,
PullIntervalSeconds: pullIntervalSeconds,
StatsdHost: statsdHost,
StatsdPort: statsdPort,
StatsdMetricPrefix: statsdMetricPrefix,
},
)
// Start PHP OPCache observing ticker
log.Println(
fmt.Sprintf(
"Starting observer with refresh interval %d seconds",
applicationConfig.PullIntervalSeconds,
),
)
// Web request handler
router := mux.NewRouter()
// Build observer
var o = observer.Observer{
Clusters: applicationConfig.Clusters,
}
// Add StatsD sender if configured
if applicationConfig.Metrics.Statsd != nil {
var statsdClient = GoMetricStatsdClient.NewClient(
applicationConfig.Metrics.Statsd.Host,
applicationConfig.Metrics.Statsd.Port,
)
statsdClient.SetPrefix(applicationConfig.Metrics.Statsd.Prefix)
statsdClient.Open()
var statsdMetricSender = &metrics.StatsdMetricSender{
StatsdClient: statsdClient,
}
o.AddMetricSender(statsdMetricSender)
}
// // Add prometheus sender if configured
if applicationConfig.Metrics.Prometheus != nil {
prometheusRegistry := prometheus.NewRegistry()
o.AddMetricSender(metrics.NewPrometheusMetricSender(prometheusRegistry))
router.Handle(
"/api/nodes/statistics/prometheus",
promhttp.HandlerFor(prometheusRegistry, promhttp.HandlerOpts{}),
)
}
// opcache statistics common request handler
router.Handle(
"/api/nodes/statistics/opcache",
gziphandler.GzipHandler(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var jsonBody []byte
if r.URL.Query().Get("pretty") == "1" {
jsonBody, _ = json.MarshalIndent(o.GetOpcacheStatistics(), "", " ")
} else {
jsonBody, _ = json.Marshal(o.GetOpcacheStatistics())
}
w.Write(jsonBody)
},
),
),
)
// APCu statistics request handler
router.Handle(
"/api/nodes/statistics/apcu",
gziphandler.GzipHandler(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var jsonBody []byte
if r.URL.Query().Get("pretty") == "1" {
jsonBody, _ = json.MarshalIndent(o.GetApcuStatistics(), "", " ")
} else {
jsonBody, _ = json.Marshal(o.GetApcuStatistics())
}
w.Write(jsonBody)
},
),
),
)
// re-read opcache stat from agents
router.HandleFunc(
"/api/nodes/statistics/refresh",
func(w http.ResponseWriter, r *http.Request) {
go o.PullAgents()
w.Write([]byte("OK"))
},
)
// reset opcache on php node
router.HandleFunc(
"/api/nodes/{clusterName}/{groupName}/{hostName}/resetOpcache",
func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
go o.ResetOpcache(
vars["clusterName"],
vars["groupName"],
vars["hostName"],
)
w.Write([]byte("OK"))
},
)
// api status
router.HandleFunc(
"/api/status",
func(w http.ResponseWriter, r *http.Request) {
heartbeat := map[string]interface{}{
"version": Version,
"buildDate": BuildDate,
"buildNumber": BuildNumber,
"lastStatusUpdate": o.LastStatusUpdate,
}
heartbeatJson, _ := json.Marshal(heartbeat)
w.Write(heartbeatJson)
},
)
// user interface assets
router.PathPrefix("/assets/").Handler(
http.StripPrefix("/assets/", http.FileServer(ui.AssetFile())),
)
// frontend routes handler
router.PathPrefix("/").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var indexBody, _ = ui.Asset("index.html")
w.Write(indexBody)
},
)
// start pulling data from agents
o.StartPulling(applicationConfig.PullIntervalSeconds * int64(time.Second))
// HTTP server
var httpAddress = fmt.Sprintf("%s:%d", applicationConfig.UI.Host, applicationConfig.UI.Port)
httpServer := &http.Server{
Addr: httpAddress,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Printf("Starting HTTP server at %s", httpAddress)
// start listening server
go func() {
log.Fatal(httpServer.ListenAndServe())
}()
// gracefull shutdown
gracefullStopSignalHandler := make(chan os.Signal, 1)
signal.Notify(gracefullStopSignalHandler, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Block until we receive our signal.
<-gracefullStopSignalHandler
log.Printf("Stopping server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() {
cancel()
}()
httpServer.Shutdown(ctx)
log.Printf("Server stopped successfully")
os.Exit(0)
}