Browse Source

Include uptime on /status page

Ask Bjørn Hansen 12 years ago
parent
commit
435da8687b
3 changed files with 112 additions and 3 deletions
  1. 94 0
      dayduration.go
  2. 9 1
      monitor.go
  3. 9 2
      status.html

+ 94 - 0
dayduration.go

@@ -0,0 +1,94 @@
+package main
+
+// Add a function similar to time.Duration.String() to
+// pretty print an "uptime duration".
+
+import (
+	"time"
+)
+
+type DayDuration struct {
+	time.Duration
+}
+
+func fmtInt(buf []byte, v uint64) int {
+	w := len(buf)
+	if v == 0 {
+		w--
+		buf[w] = '0'
+	} else {
+		for v > 0 {
+			w--
+			buf[w] = byte(v%10) + '0'
+			v /= 10
+		}
+	}
+	return w
+}
+
+// Copied from time/time.go
+func (d DayDuration) DayString() string {
+	var buf [32]byte
+	w := len(buf)
+
+	u := uint64(d.Nanoseconds())
+
+	neg := d.Nanoseconds() < 0
+	if neg {
+		u = -u
+	}
+
+	if u < uint64(time.Second) {
+		// Don't show times less than a second
+		w -= 2
+		buf[w] = '0'
+		buf[w+1] = 's'
+	} else {
+		w--
+		buf[w] = 's'
+
+		// Skip fractional seconds
+		u /= uint64(time.Second)
+
+		// u is now integer seconds
+		w = fmtInt(buf[:w], u%60)
+		u /= 60
+
+		// u is now integer minutes
+		if u > 0 {
+			w--
+			buf[w] = ' '
+			w--
+			buf[w] = 'm'
+			w = fmtInt(buf[:w], u%60)
+			u /= 60
+
+			// u is now integer hours
+			if u > 0 {
+				w--
+				buf[w] = ' '
+				w--
+				buf[w] = 'h'
+				w = fmtInt(buf[:w], u%24)
+				u /= 24
+			}
+
+			// u is now integer days
+			if u > 0 {
+				w--
+				buf[w] = ' '
+				w--
+				buf[w] = 'd'
+				w = fmtInt(buf[:w], u)
+			}
+
+		}
+	}
+
+	if neg {
+		w--
+		buf[w] = '-'
+	}
+
+	return string(buf[w:])
+}

+ 9 - 1
monitor.go

@@ -280,14 +280,22 @@ func StatusServer(zones Zones) func(http.ResponseWriter, *http.Request) {
 		type statusData struct {
 			Version string
 			Zones   Rates
+			Uptime  DayDuration
 		}
 
+		uptime := DayDuration{time.Since(timeStarted)}
+		log.Println("Uptime", uptime)
+
 		status := statusData{
 			Version: VERSION,
 			Zones:   rates,
+			Uptime:  uptime,
 		}
 
-		tmpl.Execute(w, status)
+		err = tmpl.Execute(w, status)
+		if err != nil {
+			log.Println("Status template error", err)
+		}
 	}
 }
 

+ 9 - 2
status.html

@@ -2,12 +2,19 @@
 <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
 <link href="//netdna.bootstrapcdn.com/bootswatch/2.3.1/cerulean/bootstrap.min.css" rel="stylesheet">
 <style>
-	td.zonename { font-weight: bold;  }
+	td.zonename,td.header { font-weight: bold; }
 </style>
 <body>
 
-    <div class="container">
+<div class="container">
 
+<h1>Global</h1>
+
+<table class="table table-bordered table-condensed">
+<tr>
+	<td class="info header">Uptime</td><td>{{.Uptime.DayString}}</td>
+</tr>
+</table>
 
 <h1>Zones</h1>
 <table class="table table-bordered table-condensed">