forked from apmckinlay/gsuneido
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpstatus.go
192 lines (178 loc) · 4.55 KB
/
httpstatus.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
// Copyright Suneido Software Corp. All rights reserved.
// Governed by the MIT license found in the LICENSE file.
package main
import (
"fmt"
"io"
"log"
"net/http"
_ "net/http/pprof"
"runtime"
"runtime/metrics"
"sort"
"strconv"
"strings"
"github.com/apmckinlay/gsuneido/builtin"
"github.com/apmckinlay/gsuneido/core"
"github.com/apmckinlay/gsuneido/dbms"
"github.com/apmckinlay/gsuneido/options"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
var httpServer *http.Server
func startHttpStatus() {
if httpServer != nil {
return // already started
}
http.HandleFunc("/", httpStatus)
http.HandleFunc("/metrics/", httpMetrics)
http.HandleFunc("/info/", httpInfo)
port := "3148"
if options.WebPort != "" {
port = options.WebPort
} else if options.Port != "" {
p, _ := strconv.Atoi(options.Port)
port = strconv.Itoa(p + 1)
}
addr := ":" + port
go func() {
httpServer = &http.Server{Addr: addr}
err := httpServer.ListenAndServe()
if err != http.ErrServerClosed {
log.Println("Server Monitor:", err)
}
}()
}
func httpStatus(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w,
`<html>
<head>
<title>Suneido Monitor</title>
<meta http-equiv="refresh" content="5" />
</head>
<body>
<h1>Suneido Server Monitor</h1>
`+body()+`
</body>
</html>`)
}
func body() string {
extra := ""
switch options.DbStatus.Load() {
case "starting":
extra = "<h2 style=\"color: blue;\">Starting ...</h2>"
case "corrupted":
extra = "<h2 style=\"color: red;\">Database damage detected - " +
"operating in read-only mode</h2>"
case "checking":
return `<h2 style="color: red;">Checking database ...<h2>`
case "repairing":
return `<h2 style="color: red;">Repairing database ...<h2>`
}
s := extra + `
<p>Built: ` + options.BuiltStr() + `</p>
<p>Heap: ` + heap() + `</p>` +
threads()
if dbmsLocal != nil {
s += `<p>Database: ` + mb(dbmsLocal.Size()) + `
` + trans() + `
` + dbms.Conns()
}
return s + `<p><a href="info/">Suneido Info</a>
<a href="metrics/">Go metrics</a>
<a href="debug/pprof/">Go pprof</a> </p>`
}
func mb(n uint64) string {
return strconv.FormatUint(((n+512*1024)/(1024*1024)), 10) + "mb"
}
func heap() string {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
return mb(ms.HeapSys) + " (" + mb(ms.HeapInuse) + " in use)"
}
func threads() string {
list := builtin.ThreadList()
sort.Strings(list)
var sb strings.Builder
fmt.Fprintf(&sb, "<p>Threads: (%d) ", len(list))
sep := ""
for _, s := range list {
sb.WriteString(sep)
sb.WriteString(s)
sep = ", "
}
sb.WriteString("<p>\n")
return sb.String()
}
func trans() string {
list := dbmsLocal.Transactions()
n := list.Size()
var sb strings.Builder
fmt.Fprintf(&sb, "<p>Transactions: (%d) ", n)
sep := ""
for i := 0; i < n; i++ {
sb.WriteString(sep)
sb.WriteString(list.ListGet(i).String())
sep = ", "
}
sb.WriteString("<p>\n")
return sb.String()
}
func httpMetrics(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if req.URL.Path == "/metrics" || req.URL.Path == "/metrics/" {
io.WriteString(w,
`<html>
<head><title>Go metrics</title></head>
<body>
<h1>Go metrics</h1>
`)
for _, d := range metrics.All() {
if d.Kind == metrics.KindUint64 || d.Kind == metrics.KindFloat64 {
fmt.Fprintf(w, `<a href="%s">%s</a><br />`+"\n",
strings.TrimPrefix(d.Name, "/"), d.Name)
fmt.Fprintln(w, "<p>"+d.Description+"</p>")
}
}
io.WriteString(w, `</body></html>`)
} else {
sample := make([]metrics.Sample, 1)
sample[0].Name = req.URL.Path[8:] // remove /metrics
metrics.Read(sample)
var x any
switch sample[0].Value.Kind() {
case metrics.KindUint64:
x = printer.Sprintf("%d", sample[0].Value.Uint64())
case metrics.KindFloat64:
x = sample[0].Value.Float64()
default:
x = "unsupported"
}
fmt.Fprint(w, req.URL.Path, " = ", x)
}
}
var printer = message.NewPrinter(language.English)
func httpInfo(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if req.URL.Path == "/info" || req.URL.Path == "/info/" {
io.WriteString(w,
`<html>
<head><title>Suneido Info</title></head>
<body>
<h1>Suneido Info</h1>
<pre>`)
for _, name := range core.InfoList() {
fmt.Fprintf(w, `<a href="%s">%s</a>`+"\n\n", name, name)
}
io.WriteString(w, `</pre></body></html>`)
} else {
s := core.InfoStr(req.URL.Path[6:]) // skip /info/
fmt.Fprint(w, req.URL.Path, " = ", s)
}
}