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 (
|
2015-11-11 15:31:09 +00:00
|
|
|
"bytes"
|
2015-06-16 12:57:30 +00:00
|
|
|
"encoding/json"
|
2013-04-05 11:41:52 +00:00
|
|
|
"fmt"
|
2015-06-15 10:50:53 +00:00
|
|
|
"io"
|
2014-07-29 16:28:48 +00:00
|
|
|
"io/ioutil"
|
2015-06-30 12:38:01 +00:00
|
|
|
"net"
|
2013-02-08 13:49:55 +00:00
|
|
|
"net/http"
|
2015-06-15 10:50:53 +00:00
|
|
|
"net/url"
|
2013-08-09 16:09:44 +00:00
|
|
|
"os"
|
2015-06-15 10:50:53 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2015-07-08 14:14:57 +00:00
|
|
|
"strings"
|
2015-06-15 10:50:53 +00:00
|
|
|
"sync"
|
2013-08-29 13:15:22 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
pprof_runtime "runtime/pprof"
|
2015-06-23 15:46:50 +00:00
|
|
|
template_text "text/template"
|
2013-06-28 08:19:16 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-03 08:21:43 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-24 15:07:11 +00:00
|
|
|
"github.com/prometheus/common/route"
|
2013-06-28 08:19:16 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/promql"
|
|
|
|
"github.com/prometheus/prometheus/retrieval"
|
|
|
|
"github.com/prometheus/prometheus/rules"
|
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
|
|
|
"github.com/prometheus/prometheus/template"
|
2015-09-17 12:49:50 +00:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2016-06-23 15:14:32 +00:00
|
|
|
api_v1 "github.com/prometheus/prometheus/web/api/v1"
|
2015-11-11 15:31:09 +00:00
|
|
|
"github.com/prometheus/prometheus/web/ui"
|
2013-02-08 13:49:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-18 10:16:25 +00:00
|
|
|
var localhostRepresentations = []string{"127.0.0.1", "localhost"}
|
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
// Handler serves various HTTP endpoints of the Prometheus server
|
|
|
|
type Handler struct {
|
2016-05-13 15:59:59 +00:00
|
|
|
targetManager *retrieval.TargetManager
|
|
|
|
ruleManager *rules.Manager
|
|
|
|
queryEngine *promql.Engine
|
|
|
|
storage local.Storage
|
2015-06-15 10:50:53 +00:00
|
|
|
|
2016-06-23 15:14:32 +00:00
|
|
|
apiV1 *api_v1.API
|
2015-06-15 10:50:53 +00:00
|
|
|
|
2016-05-13 15:59:59 +00:00
|
|
|
router *route.Router
|
|
|
|
listenErrCh chan error
|
|
|
|
quitCh chan struct{}
|
2016-07-11 14:24:54 +00:00
|
|
|
reloadCh chan chan error
|
2016-05-13 15:59:59 +00:00
|
|
|
options *Options
|
|
|
|
configString string
|
|
|
|
versionInfo *PrometheusVersion
|
|
|
|
birth time.Time
|
|
|
|
flagsMap map[string]string
|
2015-06-15 10:50:53 +00:00
|
|
|
|
2015-09-29 15:51:03 +00:00
|
|
|
externalLabels model.LabelSet
|
|
|
|
mtx sync.RWMutex
|
2015-09-01 16:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyConfig updates the status state as the new config requires.
|
2016-07-11 14:24:54 +00:00
|
|
|
func (h *Handler) ApplyConfig(conf *config.Config) error {
|
2015-09-01 16:47:48 +00:00
|
|
|
h.mtx.Lock()
|
|
|
|
defer h.mtx.Unlock()
|
|
|
|
|
2015-09-29 15:51:03 +00:00
|
|
|
h.externalLabels = conf.GlobalConfig.ExternalLabels
|
2016-05-13 15:59:59 +00:00
|
|
|
h.configString = conf.String()
|
2015-09-01 16:47:48 +00:00
|
|
|
|
2016-07-11 14:24:54 +00:00
|
|
|
return nil
|
2015-06-15 10:50:53 +00:00
|
|
|
}
|
2013-02-08 14:38:50 +00:00
|
|
|
|
2016-05-13 15:59:59 +00:00
|
|
|
// PrometheusVersion contains build information about Prometheus.
|
2016-05-05 11:46:51 +00:00
|
|
|
type PrometheusVersion struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Revision string `json:"revision"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
BuildUser string `json:"buildUser"`
|
|
|
|
BuildDate string `json:"buildDate"`
|
|
|
|
GoVersion string `json:"goVersion"`
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
// Options for the web Handler.
|
|
|
|
type Options struct {
|
|
|
|
ListenAddress string
|
2015-06-30 12:38:01 +00:00
|
|
|
ExternalURL *url.URL
|
2016-07-05 13:05:43 +00:00
|
|
|
RoutePrefix string
|
2015-06-15 10:50:53 +00:00
|
|
|
MetricsPath string
|
|
|
|
UseLocalAssets bool
|
|
|
|
UserAssetsPath string
|
|
|
|
ConsoleTemplatesPath string
|
|
|
|
ConsoleLibrariesPath string
|
|
|
|
EnableQuit bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a new web Handler.
|
2016-05-13 15:59:59 +00:00
|
|
|
func New(
|
|
|
|
st local.Storage,
|
|
|
|
qe *promql.Engine,
|
|
|
|
tm *retrieval.TargetManager,
|
|
|
|
rm *rules.Manager,
|
|
|
|
version *PrometheusVersion,
|
|
|
|
flags map[string]string,
|
|
|
|
o *Options,
|
|
|
|
) *Handler {
|
2015-06-03 06:38:50 +00:00
|
|
|
router := route.New()
|
2015-03-24 21:04:38 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
h := &Handler{
|
2015-08-20 16:23:57 +00:00
|
|
|
router: router,
|
|
|
|
listenErrCh: make(chan error),
|
|
|
|
quitCh: make(chan struct{}),
|
2016-07-11 14:24:54 +00:00
|
|
|
reloadCh: make(chan chan error),
|
2015-08-20 16:23:57 +00:00
|
|
|
options: o,
|
2016-05-05 11:46:51 +00:00
|
|
|
versionInfo: version,
|
2016-05-13 15:59:59 +00:00
|
|
|
birth: time.Now(),
|
|
|
|
flagsMap: flags,
|
2015-06-15 10:50:53 +00:00
|
|
|
|
2016-05-13 15:59:59 +00:00
|
|
|
targetManager: tm,
|
|
|
|
ruleManager: rm,
|
|
|
|
queryEngine: qe,
|
|
|
|
storage: st,
|
2015-06-15 10:50:53 +00:00
|
|
|
|
2016-06-23 15:14:32 +00:00
|
|
|
apiV1: api_v1.NewAPI(qe, st),
|
2015-06-02 11:07:46 +00:00
|
|
|
}
|
2013-05-06 07:56:32 +00:00
|
|
|
|
2016-07-05 13:05:43 +00:00
|
|
|
if o.RoutePrefix != "/" {
|
2015-06-15 10:50:53 +00:00
|
|
|
// If the prefix is missing for the root path, prepend it.
|
2015-06-03 06:38:50 +00:00
|
|
|
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2016-07-05 13:05:43 +00:00
|
|
|
http.Redirect(w, r, o.RoutePrefix, http.StatusFound)
|
2015-06-03 06:38:50 +00:00
|
|
|
})
|
2016-07-05 13:05:43 +00:00
|
|
|
router = router.WithPrefix(o.RoutePrefix)
|
2015-06-03 06:38:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 14:33:14 +00:00
|
|
|
instrh := prometheus.InstrumentHandler
|
|
|
|
instrf := prometheus.InstrumentHandlerFunc
|
2015-06-03 06:38:50 +00:00
|
|
|
|
2015-08-28 08:53:13 +00:00
|
|
|
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2015-09-22 13:32:21 +00:00
|
|
|
router.Redirect(w, r, "/graph", http.StatusFound)
|
2015-08-28 08:53:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.Get("/alerts", instrf("alerts", h.alerts))
|
2016-05-13 15:59:59 +00:00
|
|
|
router.Get("/graph", instrf("graph", h.graph))
|
|
|
|
router.Get("/status", instrf("status", h.status))
|
|
|
|
router.Get("/flags", instrf("flags", h.flags))
|
|
|
|
router.Get("/config", instrf("config", h.config))
|
|
|
|
router.Get("/rules", instrf("rules", h.rules))
|
|
|
|
router.Get("/targets", instrf("targets", h.targets))
|
2015-06-16 12:57:30 +00:00
|
|
|
router.Get("/version", instrf("version", h.version))
|
2015-06-03 06:38:50 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
router.Get("/heap", instrf("heap", dumpHeap))
|
2015-06-03 06:38:50 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
router.Get(o.MetricsPath, prometheus.Handler().ServeHTTP)
|
2015-06-04 16:24:04 +00:00
|
|
|
|
2015-09-17 14:33:14 +00:00
|
|
|
router.Get("/federate", instrh("federate", httputil.CompressionHandler{
|
2015-09-18 14:51:53 +00:00
|
|
|
Handler: http.HandlerFunc(h.federation),
|
2015-09-17 14:33:14 +00:00
|
|
|
}))
|
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
h.apiV1.Register(router.WithPrefix("/api/v1"))
|
2015-06-03 06:38:50 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
router.Get("/consoles/*filepath", instrf("consoles", h.consoles))
|
2015-06-03 06:38:50 +00:00
|
|
|
|
2015-11-11 15:31:09 +00:00
|
|
|
router.Get("/static/*filepath", instrf("static", serveStaticAsset))
|
2013-02-08 13:49:55 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
if o.UserAssetsPath != "" {
|
|
|
|
router.Get("/user/*filepath", instrf("user", route.FileServe(o.UserAssetsPath)))
|
2013-05-23 13:47:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
if o.EnableQuit {
|
|
|
|
router.Post("/-/quit", h.quit)
|
2015-03-24 21:04:38 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 10:26:41 +00:00
|
|
|
router.Post("/-/reload", h.reload)
|
2015-08-26 15:42:25 +00:00
|
|
|
|
2015-07-21 22:27:55 +00:00
|
|
|
router.Get("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
|
2015-08-26 15:42:25 +00:00
|
|
|
router.Post("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
|
2015-07-21 22:27:55 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2015-11-11 15:31:09 +00:00
|
|
|
func serveStaticAsset(w http.ResponseWriter, req *http.Request) {
|
|
|
|
fp := route.Param(route.Context(req), "filepath")
|
|
|
|
fp = filepath.Join("web/ui/static", fp)
|
|
|
|
|
|
|
|
info, err := ui.AssetInfo(fp)
|
|
|
|
if err != nil {
|
|
|
|
log.With("file", fp).Warn("Could not get file info: ", err)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file, err := ui.Asset(fp)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.With("file", fp).Warn("Could not get file: ", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(w, req, info.Name(), info.ModTime(), bytes.NewReader(file))
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:23:57 +00:00
|
|
|
// ListenError returns the receive-only channel that signals errors while starting the web server.
|
|
|
|
func (h *Handler) ListenError() <-chan error {
|
|
|
|
return h.listenErrCh
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
// Quit returns the receive-only quit channel.
|
|
|
|
func (h *Handler) Quit() <-chan struct{} {
|
|
|
|
return h.quitCh
|
2015-06-02 11:07:46 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:23:57 +00:00
|
|
|
// Reload returns the receive-only channel that signals configuration reload requests.
|
2016-07-11 14:24:54 +00:00
|
|
|
func (h *Handler) Reload() <-chan chan error {
|
2015-08-11 07:08:17 +00:00
|
|
|
return h.reloadCh
|
|
|
|
}
|
|
|
|
|
2015-06-02 11:07:46 +00:00
|
|
|
// Run serves the HTTP endpoints.
|
2015-06-15 10:50:53 +00:00
|
|
|
func (h *Handler) Run() {
|
|
|
|
log.Infof("Listening on %s", h.options.ListenAddress)
|
2016-05-25 11:13:22 +00:00
|
|
|
server := &http.Server{
|
|
|
|
Addr: h.options.ListenAddress,
|
|
|
|
Handler: h.router,
|
|
|
|
ErrorLog: log.NewErrorLogger(),
|
|
|
|
}
|
|
|
|
h.listenErrCh <- server.ListenAndServe()
|
2013-02-08 13:49:55 +00:00
|
|
|
}
|
2013-03-27 16:40:01 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
alerts := h.ruleManager.AlertingRules()
|
|
|
|
alertsSorter := byAlertStateSorter{alerts: alerts}
|
|
|
|
sort.Sort(alertsSorter)
|
|
|
|
|
|
|
|
alertStatus := AlertStatus{
|
|
|
|
AlertingRules: alertsSorter.alerts,
|
|
|
|
AlertStateToRowClass: map[rules.AlertState]string{
|
|
|
|
rules.StateInactive: "success",
|
|
|
|
rules.StatePending: "warning",
|
|
|
|
rules.StateFiring: "danger",
|
|
|
|
},
|
|
|
|
}
|
2015-11-11 15:31:09 +00:00
|
|
|
h.executeTemplate(w, "alerts.html", alertStatus)
|
2015-06-15 10:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) consoles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := route.Context(r)
|
|
|
|
name := route.Param(ctx, "filepath")
|
|
|
|
|
|
|
|
file, err := http.Dir(h.options.ConsoleTemplatesPath).Open(name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
text, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide URL parameters as a map for easy use. Advanced users may have need for
|
|
|
|
// parameters beyond the first, so provide RawParams.
|
|
|
|
rawParams, err := url.ParseQuery(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
params := map[string]string{}
|
|
|
|
for k, v := range rawParams {
|
|
|
|
params[k] = v[0]
|
|
|
|
}
|
|
|
|
data := struct {
|
|
|
|
RawParams url.Values
|
|
|
|
Params map[string]string
|
|
|
|
Path string
|
|
|
|
}{
|
|
|
|
RawParams: rawParams,
|
|
|
|
Params: params,
|
2015-07-08 14:14:57 +00:00
|
|
|
Path: strings.TrimLeft(name, "/"),
|
2015-06-15 10:50:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
tmpl := template.NewTemplateExpander(string(text), "__console_"+name, data, model.Now(), h.queryEngine, h.options.ExternalURL.Path)
|
2015-06-15 10:50:53 +00:00
|
|
|
filenames, err := filepath.Glob(h.options.ConsoleLibrariesPath + "/*.lib")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result, err := tmpl.ExpandHTML(filenames)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
io.WriteString(w, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) graph(w http.ResponseWriter, r *http.Request) {
|
2015-11-11 15:31:09 +00:00
|
|
|
h.executeTemplate(w, "graph.html", nil)
|
2015-06-15 10:50:53 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 12:57:30 +00:00
|
|
|
func (h *Handler) status(w http.ResponseWriter, r *http.Request) {
|
2015-11-11 15:31:09 +00:00
|
|
|
h.executeTemplate(w, "status.html", struct {
|
2016-05-13 15:59:59 +00:00
|
|
|
Birth time.Time
|
2016-05-05 11:46:51 +00:00
|
|
|
Version *PrometheusVersion
|
2015-06-15 10:23:02 +00:00
|
|
|
}{
|
2016-05-13 15:59:59 +00:00
|
|
|
Birth: h.birth,
|
2016-05-05 11:46:51 +00:00
|
|
|
Version: h.versionInfo,
|
2015-06-15 10:23:02 +00:00
|
|
|
})
|
2015-06-15 10:50:53 +00:00
|
|
|
}
|
2014-04-14 23:02:15 +00:00
|
|
|
|
2016-05-13 15:59:59 +00:00
|
|
|
func (h *Handler) flags(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.executeTemplate(w, "flags.html", h.flagsMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) config(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.mtx.RLock()
|
|
|
|
defer h.mtx.RUnlock()
|
|
|
|
|
|
|
|
h.executeTemplate(w, "config.html", h.configString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) rules(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.executeTemplate(w, "rules.html", h.ruleManager)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) targets(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.executeTemplate(w, "targets.html", h.targetManager)
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:57:30 +00:00
|
|
|
func (h *Handler) version(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dec := json.NewEncoder(w)
|
2016-05-05 11:46:51 +00:00
|
|
|
if err := dec.Encode(h.versionInfo); err != nil {
|
2015-06-16 12:57:30 +00:00
|
|
|
http.Error(w, fmt.Sprintf("error encoding JSON: %s", err), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
func (h *Handler) quit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "Requesting termination... Goodbye!")
|
|
|
|
close(h.quitCh)
|
2014-04-14 23:02:15 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 07:08:17 +00:00
|
|
|
func (h *Handler) reload(w http.ResponseWriter, r *http.Request) {
|
2016-07-11 14:24:54 +00:00
|
|
|
rc := make(chan error)
|
|
|
|
h.reloadCh <- rc
|
|
|
|
if err := <-rc; err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to reload config: %s", err), http.StatusInternalServerError)
|
|
|
|
}
|
2015-08-11 07:08:17 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 12:38:01 +00:00
|
|
|
func (h *Handler) consolesPath() string {
|
2015-06-15 10:50:53 +00:00
|
|
|
if _, err := os.Stat(h.options.ConsoleTemplatesPath + "/index.html"); !os.IsNotExist(err) {
|
2015-06-30 12:38:01 +00:00
|
|
|
return h.options.ExternalURL.Path + "/consoles/index.html"
|
2014-10-02 14:44:47 +00:00
|
|
|
}
|
2015-06-15 10:50:53 +00:00
|
|
|
if h.options.UserAssetsPath != "" {
|
|
|
|
if _, err := os.Stat(h.options.UserAssetsPath + "/index.html"); !os.IsNotExist(err) {
|
2015-06-30 12:38:01 +00:00
|
|
|
return h.options.ExternalURL.Path + "/user/index.html"
|
2014-10-02 14:44:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:38:01 +00:00
|
|
|
func tmplFuncs(consolesPath string, opts *Options) template_text.FuncMap {
|
|
|
|
return template_text.FuncMap{
|
2016-06-08 18:23:54 +00:00
|
|
|
"since": func(t time.Time) time.Duration {
|
|
|
|
return time.Since(t) / time.Millisecond * time.Millisecond
|
|
|
|
},
|
2015-06-30 12:38:01 +00:00
|
|
|
"consolesPath": func() string { return consolesPath },
|
|
|
|
"pathPrefix": func() string { return opts.ExternalURL.Path },
|
2015-08-20 15:18:46 +00:00
|
|
|
"stripLabels": func(lset model.LabelSet, labels ...model.LabelName) model.LabelSet {
|
2015-05-18 10:16:25 +00:00
|
|
|
for _, ln := range labels {
|
|
|
|
delete(lset, ln)
|
|
|
|
}
|
|
|
|
return lset
|
|
|
|
},
|
2015-06-22 20:57:32 +00:00
|
|
|
"globalURL": func(u *url.URL) *url.URL {
|
2015-06-30 12:38:01 +00:00
|
|
|
host, port, err := net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
return u
|
|
|
|
}
|
2015-06-22 20:57:32 +00:00
|
|
|
for _, lhr := range localhostRepresentations {
|
2015-06-30 12:38:01 +00:00
|
|
|
if host == lhr {
|
|
|
|
_, ownPort, err := net.SplitHostPort(opts.ListenAddress)
|
|
|
|
if err != nil {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
if port == ownPort {
|
|
|
|
// Only in the case where the target is on localhost and its port is
|
|
|
|
// the same as the one we're listening on, we know for sure that
|
|
|
|
// we're monitoring our own process and that we need to change the
|
|
|
|
// scheme, hostname, and port to the externally reachable ones as
|
|
|
|
// well. We shouldn't need to touch the path at all, since if a
|
|
|
|
// path prefix is defined, the path under which we scrape ourselves
|
|
|
|
// should already contain the prefix.
|
|
|
|
u.Scheme = opts.ExternalURL.Scheme
|
|
|
|
u.Host = opts.ExternalURL.Host
|
|
|
|
} else {
|
|
|
|
// Otherwise, we only know that localhost is not reachable
|
|
|
|
// externally, so we replace only the hostname by the one in the
|
|
|
|
// external URL. It could be the wrong hostname for the service on
|
|
|
|
// this port, but it's still the best possible guess.
|
|
|
|
host, _, err := net.SplitHostPort(opts.ExternalURL.Host)
|
|
|
|
if err != nil {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
u.Host = host + ":" + port
|
|
|
|
}
|
|
|
|
break
|
2015-06-22 20:57:32 +00:00
|
|
|
}
|
2015-05-18 10:16:25 +00:00
|
|
|
}
|
2015-06-22 20:57:32 +00:00
|
|
|
return u
|
2015-05-18 10:16:25 +00:00
|
|
|
},
|
2015-06-15 10:50:53 +00:00
|
|
|
"healthToClass": func(th retrieval.TargetHealth) string {
|
|
|
|
switch th {
|
|
|
|
case retrieval.HealthUnknown:
|
|
|
|
return "warning"
|
|
|
|
case retrieval.HealthGood:
|
|
|
|
return "success"
|
|
|
|
default:
|
|
|
|
return "danger"
|
|
|
|
}
|
|
|
|
},
|
2015-06-23 15:46:50 +00:00
|
|
|
"alertStateToClass": func(as rules.AlertState) string {
|
|
|
|
switch as {
|
|
|
|
case rules.StateInactive:
|
|
|
|
return "success"
|
|
|
|
case rules.StatePending:
|
|
|
|
return "warning"
|
|
|
|
case rules.StateFiring:
|
|
|
|
return "danger"
|
|
|
|
default:
|
|
|
|
panic("unknown alert state")
|
|
|
|
}
|
|
|
|
},
|
2015-06-30 12:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 15:31:09 +00:00
|
|
|
func (h *Handler) getTemplate(name string) (string, error) {
|
|
|
|
baseTmpl, err := ui.Asset("web/ui/templates/_base.html")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error reading base template: %s", err)
|
|
|
|
}
|
|
|
|
pageTmpl, err := ui.Asset(filepath.Join("web/ui/templates", name))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error reading page template %s: %s", name, err)
|
|
|
|
}
|
|
|
|
return string(baseTmpl) + string(pageTmpl), nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:38:01 +00:00
|
|
|
func (h *Handler) executeTemplate(w http.ResponseWriter, name string, data interface{}) {
|
|
|
|
text, err := h.getTemplate(name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
tmpl := template.NewTemplateExpander(text, name, data, model.Now(), h.queryEngine, h.options.ExternalURL.Path)
|
2015-06-30 12:38:01 +00:00
|
|
|
tmpl.Funcs(tmplFuncs(h.consolesPath(), h.options))
|
2015-05-18 10:16:25 +00:00
|
|
|
|
2015-06-23 15:46:50 +00:00
|
|
|
result, err := tmpl.ExpandHTML(nil)
|
2013-03-27 16:40:01 +00:00
|
|
|
if err != nil {
|
2015-06-23 15:46:50 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-03-27 16:40:01 +00:00
|
|
|
return
|
|
|
|
}
|
2015-06-23 15:46:50 +00:00
|
|
|
io.WriteString(w, result)
|
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")
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
// AlertStatus bundles alerting rules and the mapping of alert states to row classes.
|
|
|
|
type AlertStatus struct {
|
|
|
|
AlertingRules []*rules.AlertingRule
|
|
|
|
AlertStateToRowClass map[rules.AlertState]string
|
2013-08-09 16:09:44 +00:00
|
|
|
}
|
2015-05-22 10:29:33 +00:00
|
|
|
|
2015-06-15 10:50:53 +00:00
|
|
|
type byAlertStateSorter struct {
|
|
|
|
alerts []*rules.AlertingRule
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s byAlertStateSorter) Len() int {
|
|
|
|
return len(s.alerts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s byAlertStateSorter) Less(i, j int) bool {
|
|
|
|
return s.alerts[i].State() > s.alerts[j].State()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s byAlertStateSorter) Swap(i, j int) {
|
|
|
|
s.alerts[i], s.alerts[j] = s.alerts[j], s.alerts[i]
|
2015-05-22 10:29:33 +00:00
|
|
|
}
|