github.com/google/cloudprober@v0.11.3/web/web.go (about) 1 // Copyright 2018 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package web provides web interface for cloudprober. 16 package web 17 18 import ( 19 "bytes" 20 "fmt" 21 "html/template" 22 "net/http" 23 "time" 24 25 "github.com/google/cloudprober" 26 "github.com/google/cloudprober/config/runconfig" 27 "github.com/google/cloudprober/probes" 28 "github.com/google/cloudprober/servers" 29 "github.com/google/cloudprober/surfacers" 30 "github.com/google/cloudprober/sysvars" 31 ) 32 33 func execTmpl(tmpl *template.Template, v interface{}) template.HTML { 34 var statusBuf bytes.Buffer 35 err := tmpl.Execute(&statusBuf, v) 36 if err != nil { 37 return template.HTML(template.HTMLEscapeString(err.Error())) 38 } 39 return template.HTML(statusBuf.String()) 40 } 41 42 // Status returns cloudprober status string. 43 func Status() string { 44 var statusBuf bytes.Buffer 45 46 probeInfo, surfacerInfo, serverInfo := cloudprober.GetInfo() 47 startTime := sysvars.StartTime() 48 uptime := time.Since(startTime) 49 50 tmpl, _ := template.New("statusTmpl").Parse(statusTmpl) 51 tmpl.Execute(&statusBuf, struct { 52 Version, StartTime, Uptime, ProbesStatus, ServersStatus, SurfacersStatus interface{} 53 }{ 54 Version: runconfig.Version(), 55 StartTime: startTime.Format(time.RFC1123), 56 Uptime: uptime.String(), 57 ProbesStatus: execTmpl(probes.StatusTmpl, probeInfo), 58 SurfacersStatus: execTmpl(surfacers.StatusTmpl, surfacerInfo), 59 ServersStatus: execTmpl(servers.StatusTmpl, serverInfo), 60 }) 61 62 return statusBuf.String() 63 } 64 65 func configHandler(w http.ResponseWriter, r *http.Request) { 66 fmt.Fprint(w, cloudprober.GetTextConfig()) 67 } 68 69 func statusHandler(w http.ResponseWriter, r *http.Request) { 70 fmt.Fprintf(w, Status()) 71 } 72 73 // Init initializes cloudprober web interface handler. 74 func Init() { 75 http.HandleFunc("/config", configHandler) 76 http.HandleFunc("/status", statusHandler) 77 }