2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-02-08 13:49:55 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2013-02-08 14:38:50 +00:00
|
|
|
"flag"
|
2013-04-05 11:41:52 +00:00
|
|
|
"fmt"
|
2013-03-27 16:40:01 +00:00
|
|
|
"html/template"
|
2014-07-29 16:28:48 +00:00
|
|
|
"io/ioutil"
|
2013-08-09 16:09:44 +00:00
|
|
|
"net"
|
2013-02-08 13:49:55 +00:00
|
|
|
"net/http"
|
2013-08-09 16:09:44 +00:00
|
|
|
"os"
|
2015-03-24 21:04:38 +00:00
|
|
|
"strings"
|
2013-08-29 13:15:22 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
pprof_runtime "runtime/pprof"
|
2013-06-28 08:19:16 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-20 16:10:29 +00:00
|
|
|
"github.com/prometheus/log"
|
2015-06-03 06:38:50 +00:00
|
|
|
"github.com/prometheus/prometheus/util/route"
|
2013-06-28 08:19:16 +00:00
|
|
|
|
2015-05-18 10:16:25 +00:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-06-28 08:19:16 +00:00
|
|
|
"github.com/prometheus/prometheus/web/api"
|
|
|
|
"github.com/prometheus/prometheus/web/blob"
|
2013-02-08 13:49:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-18 10:16:25 +00:00
|
|
|
var localhostRepresentations = []string{"127.0.0.1", "localhost"}
|
|
|
|
|
2013-02-08 14:38:50 +00:00
|
|
|
// Commandline flags.
|
|
|
|
var (
|
2015-02-08 22:29:57 +00:00
|
|
|
listenAddress = flag.String("web.listen-address", ":9090", "Address to listen on for the web interface, API, and telemetry.")
|
2015-05-22 10:29:33 +00:00
|
|
|
hostname = flag.String("web.hostname", "", "Hostname on which the server is available.")
|
2015-02-08 22:29:57 +00:00
|
|
|
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
|
2014-11-20 20:03:51 +00:00
|
|
|
useLocalAssets = flag.Bool("web.use-local-assets", false, "Read assets/templates from file instead of binary.")
|
|
|
|
userAssetsPath = flag.String("web.user-assets", "", "Path to static asset directory, available at /user.")
|
|
|
|
enableQuit = flag.Bool("web.enable-remote-shutdown", false, "Enable remote service shutdown.")
|
2013-02-08 14:38:50 +00:00
|
|
|
)
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// WebService handles the HTTP endpoints with the exception of /api.
|
2013-05-05 17:32:04 +00:00
|
|
|
type WebService struct {
|
2015-06-02 11:07:46 +00:00
|
|
|
QuitChan chan struct{}
|
2015-06-03 06:38:50 +00:00
|
|
|
router *route.Router
|
2015-06-02 11:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebServiceOptions struct {
|
|
|
|
PathPrefix string
|
2014-06-06 09:55:53 +00:00
|
|
|
StatusHandler *PrometheusStatusHandler
|
|
|
|
MetricsHandler *api.MetricsService
|
|
|
|
AlertsHandler *AlertsHandler
|
|
|
|
ConsolesHandler *ConsolesHandler
|
2015-03-24 21:04:38 +00:00
|
|
|
GraphsHandler *GraphsHandler
|
2013-05-05 17:32:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 11:07:46 +00:00
|
|
|
// NewWebService returns a new WebService.
|
|
|
|
func NewWebService(o *WebServiceOptions) *WebService {
|
2015-06-03 06:38:50 +00:00
|
|
|
router := route.New()
|
2015-03-24 21:04:38 +00:00
|
|
|
|
2015-06-02 11:07:46 +00:00
|
|
|
ws := &WebService{
|
2015-06-03 06:38:50 +00:00
|
|
|
router: router,
|
2015-06-02 11:07:46 +00:00
|
|
|
QuitChan: make(chan struct{}),
|
|
|
|
}
|
2013-05-06 07:56:32 +00:00
|
|
|
|
2015-06-03 06:38:50 +00:00
|
|
|
if o.PathPrefix != "" {
|
|
|
|
// If the prefix is missing for the root path, append it.
|
|
|
|
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, o.PathPrefix, 301)
|
|
|
|
})
|
|
|
|
router = router.WithPrefix(o.PathPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
instr := prometheus.InstrumentHandler
|
|
|
|
|
|
|
|
router.Get("/", instr("status", o.StatusHandler))
|
|
|
|
router.Get("/alerts", instr("alerts", o.AlertsHandler))
|
|
|
|
router.Get("/graph", instr("graph", o.GraphsHandler))
|
|
|
|
router.Get("/heap", instr("heap", http.HandlerFunc(dumpHeap)))
|
|
|
|
|
|
|
|
router.Get(*metricsPath, prometheus.Handler().ServeHTTP)
|
|
|
|
|
|
|
|
o.MetricsHandler.RegisterHandler(router.WithPrefix("/api"))
|
|
|
|
|
|
|
|
router.Get("/consoles/*filepath", instr("consoles", o.ConsolesHandler))
|
|
|
|
|
2013-03-21 14:55:48 +00:00
|
|
|
if *useLocalAssets {
|
2015-06-03 06:38:50 +00:00
|
|
|
router.Get("/static/*filepath", instr("static", route.FileServe("web/static")))
|
2013-03-21 12:55:59 +00:00
|
|
|
} else {
|
2015-06-03 06:38:50 +00:00
|
|
|
router.Get("/static/*filepath", instr("static", blob.Handler{}))
|
2013-03-21 12:55:59 +00:00
|
|
|
}
|
2013-02-08 13:49:55 +00:00
|
|
|
|
2013-05-23 13:47:00 +00:00
|
|
|
if *userAssetsPath != "" {
|
2015-06-03 06:38:50 +00:00
|
|
|
router.Get("/user/*filepath", instr("user", route.FileServe(*userAssetsPath)))
|
2013-05-23 13:47:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 23:02:15 +00:00
|
|
|
if *enableQuit {
|
2015-06-03 06:38:50 +00:00
|
|
|
router.Post("/-/quit", ws.quitHandler)
|
2015-03-24 21:04:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 11:07:46 +00:00
|
|
|
return ws
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run serves the HTTP endpoints.
|
|
|
|
func (ws *WebService) Run() {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Infof("Listening on %s", *listenAddress)
|
2013-05-05 17:32:04 +00:00
|
|
|
|
2015-05-19 12:38:50 +00:00
|
|
|
// If we cannot bind to a port, retry after 30 seconds.
|
|
|
|
for {
|
2015-06-03 06:38:50 +00:00
|
|
|
err := http.ListenAndServe(*listenAddress, ws.router)
|
2015-05-19 12:38:50 +00:00
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Errorf("Could not listen on %s: %s", *listenAddress, err)
|
2015-05-19 12:38:50 +00:00
|
|
|
}
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
}
|
2013-02-08 13:49:55 +00:00
|
|
|
}
|
2013-03-27 16:40:01 +00:00
|
|
|
|
2015-06-02 11:07:46 +00:00
|
|
|
func (ws *WebService) quitHandler(w http.ResponseWriter, r *http.Request) {
|
2014-04-14 23:02:15 +00:00
|
|
|
fmt.Fprintf(w, "Requesting termination... Goodbye!")
|
|
|
|
|
2015-03-15 02:36:15 +00:00
|
|
|
close(ws.QuitChan)
|
2014-04-14 23:02:15 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:28:48 +00:00
|
|
|
func getTemplateFile(name string) (string, error) {
|
|
|
|
if *useLocalAssets {
|
|
|
|
file, err := ioutil.ReadFile(fmt.Sprintf("web/templates/%s.html", name))
|
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Errorf("Could not read %s template: %s", name, err)
|
2014-07-29 16:28:48 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(file), nil
|
|
|
|
}
|
2014-12-10 15:16:49 +00:00
|
|
|
file, err := blob.GetFile(blob.TemplateFiles, name+".html")
|
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Errorf("Could not read %s template: %s", name, err)
|
2014-12-10 15:16:49 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(file), nil
|
2013-06-07 09:17:17 +00:00
|
|
|
}
|
2013-03-27 16:40:01 +00:00
|
|
|
|
2015-03-24 21:04:38 +00:00
|
|
|
func getConsoles(pathPrefix string) string {
|
2015-04-06 15:30:13 +00:00
|
|
|
if _, err := os.Stat(*consoleTemplatesPath + "/index.html"); !os.IsNotExist(err) {
|
2015-06-01 10:23:13 +00:00
|
|
|
return pathPrefix + "/consoles/index.html"
|
2014-10-02 14:44:47 +00:00
|
|
|
}
|
|
|
|
if *userAssetsPath != "" {
|
2015-04-06 15:30:13 +00:00
|
|
|
if _, err := os.Stat(*userAssetsPath + "/index.html"); !os.IsNotExist(err) {
|
2015-06-01 10:23:13 +00:00
|
|
|
return pathPrefix + "/user/index.html"
|
2014-10-02 14:44:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-05-18 10:16:25 +00:00
|
|
|
func getTemplate(name string, pathPrefix string) (*template.Template, error) {
|
|
|
|
t := template.New("_base")
|
|
|
|
var err error
|
2015-03-24 21:04:38 +00:00
|
|
|
|
2014-07-29 16:28:48 +00:00
|
|
|
t.Funcs(template.FuncMap{
|
2014-10-02 14:44:47 +00:00
|
|
|
"since": time.Since,
|
2015-03-24 21:04:38 +00:00
|
|
|
"getConsoles": func() string { return getConsoles(pathPrefix) },
|
|
|
|
"pathPrefix": func() string { return pathPrefix },
|
2015-05-18 10:16:25 +00:00
|
|
|
"stripLabels": func(lset clientmodel.LabelSet, labels ...clientmodel.LabelName) clientmodel.LabelSet {
|
|
|
|
for _, ln := range labels {
|
|
|
|
delete(lset, ln)
|
|
|
|
}
|
|
|
|
return lset
|
|
|
|
},
|
|
|
|
"globalURL": func(url string) string {
|
2015-05-22 10:29:33 +00:00
|
|
|
hostname, err := getHostname()
|
2015-05-18 10:16:25 +00:00
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Warnf("Couldn't get hostname: %s, returning target.URL()", err)
|
2015-05-18 10:16:25 +00:00
|
|
|
return url
|
|
|
|
}
|
|
|
|
for _, localhostRepresentation := range localhostRepresentations {
|
|
|
|
url = strings.Replace(url, "//"+localhostRepresentation, "//"+hostname, 1)
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
},
|
2014-07-29 16:28:48 +00:00
|
|
|
})
|
2015-05-18 10:16:25 +00:00
|
|
|
|
2014-07-29 16:28:48 +00:00
|
|
|
file, err := getTemplateFile("_base")
|
2013-03-27 16:40:01 +00:00
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Errorln("Could not read base template:", err)
|
2013-03-27 16:40:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-18 10:16:25 +00:00
|
|
|
t, err = t.Parse(file)
|
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Errorln("Could not parse base template:", err)
|
2015-05-18 10:16:25 +00:00
|
|
|
}
|
2013-03-27 16:40:01 +00:00
|
|
|
|
2014-07-29 16:28:48 +00:00
|
|
|
file, err = getTemplateFile(name)
|
2013-03-27 16:40:01 +00:00
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Error("Could not read template %s: %s", name, err)
|
2013-03-27 16:40:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-18 10:16:25 +00:00
|
|
|
t, err = t.Parse(file)
|
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Errorf("Could not parse template %s: %s", name, err)
|
2015-05-18 10:16:25 +00:00
|
|
|
}
|
|
|
|
return t, err
|
2013-03-27 16:40:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:04:38 +00:00
|
|
|
func executeTemplate(w http.ResponseWriter, name string, data interface{}, pathPrefix string) {
|
|
|
|
tpl, err := getTemplate(name, pathPrefix)
|
2013-03-27 16:40:01 +00:00
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Error("Error preparing layout template: ", err)
|
2013-03-27 16:40:01 +00:00
|
|
|
return
|
|
|
|
}
|
2013-04-28 17:01:56 +00:00
|
|
|
err = tpl.Execute(w, data)
|
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Error("Error executing template: ", err)
|
2013-04-28 17:01:56 +00:00
|
|
|
}
|
2013-03-27 16:40:01 +00:00
|
|
|
}
|
2013-08-09 16:09:44 +00:00
|
|
|
|
2013-08-29 13:15:22 +00:00
|
|
|
func dumpHeap(w http.ResponseWriter, r *http.Request) {
|
|
|
|
target := fmt.Sprintf("/tmp/%d.heap", time.Now().Unix())
|
|
|
|
f, err := os.Create(target)
|
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Error("Could not dump heap: ", err)
|
2013-08-29 13:15:22 +00:00
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "Writing to %s...", target)
|
|
|
|
defer f.Close()
|
|
|
|
pprof_runtime.WriteHeapProfile(f)
|
|
|
|
fmt.Fprintf(w, "Done")
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// MustBuildServerURL returns the server URL and panics in case an error occurs.
|
2015-03-24 21:04:38 +00:00
|
|
|
func MustBuildServerURL(pathPrefix string) string {
|
2013-08-09 16:09:44 +00:00
|
|
|
_, port, err := net.SplitHostPort(*listenAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-05-22 10:29:33 +00:00
|
|
|
hostname, err := getHostname()
|
2013-08-09 16:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-06-01 10:54:19 +00:00
|
|
|
return fmt.Sprintf("http://%s:%s%s/", hostname, port, pathPrefix)
|
2013-08-09 16:09:44 +00:00
|
|
|
}
|
2015-05-22 10:29:33 +00:00
|
|
|
|
|
|
|
func getHostname() (string, error) {
|
|
|
|
if *hostname != "" {
|
|
|
|
return *hostname, nil
|
|
|
|
}
|
|
|
|
return os.Hostname()
|
|
|
|
}
|