-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathops.go
More file actions
139 lines (119 loc) · 3.2 KB
/
ops.go
File metadata and controls
139 lines (119 loc) · 3.2 KB
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
package admin
import (
"embed"
"encoding/json"
"html/template"
"log"
"net/http"
"runtime"
"github.com/fabricekabongo/loggerhead/clustering"
"github.com/fabricekabongo/loggerhead/config"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
//go:embed template
TemplateFS embed.FS
//go:embed static
StaticFS embed.FS
TMPL *template.Template
)
func init() {
tmpl, err := template.ParseFS(TemplateFS, "template/admin.html")
if err != nil {
panic(err)
}
TMPL = tmpl
}
type OpsServer struct {
cluster *clustering.Cluster
cfg config.Config
}
func NewOpsServer(cluster *clustering.Cluster, cfg config.Config) *OpsServer {
return &OpsServer{
cluster: cluster,
cfg: cfg,
}
}
func (o *OpsServer) Start() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(StaticFS))))
http.Handle("/metrics", promhttp.Handler())
http.Handle("/admin-data", o.AdminData())
http.Handle("/", o.AdminUI())
err := http.ListenAndServe(":20000", nil)
if err != nil {
log.Println("Failed to start the admin server: ", err)
return
}
}
type Data struct {
Name string
NodesAlive int
Health int
MemStats MemStats
CPUs int
GoRoutines int
Others []Data
State string
Address string
QueueCount int
}
type MemStats struct {
Alloc uint64
TotalAlloc uint64
Sys uint64
}
func (o *OpsServer) AdminData() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
data := Data{
Name: o.cluster.MemberList().LocalNode().Name,
Address: o.cluster.MemberList().LocalNode().Addr.String(),
NodesAlive: o.cluster.MemberList().NumMembers(),
MemStats: MemStats{
Alloc: (memStats.Alloc / 1024) / 1024,
TotalAlloc: (memStats.TotalAlloc / 1024) / 1024,
Sys: (memStats.Sys / 1024) / 1024,
},
CPUs: runtime.NumCPU(),
GoRoutines: runtime.NumGoroutine(),
Health: o.cluster.MemberList().GetHealthScore(),
State: clustering.StateToString(o.cluster.MemberList().LocalNode().State),
QueueCount: o.cluster.Broadcasts().NumQueued(),
}
getParams := r.URL.Query()
if getParams.Get("proxy") != "true" {
members := o.cluster.MemberList().Members()
membersAdminData := make([]Data, 0, len(members))
for _, member := range members {
if member.Name == o.cluster.MemberList().LocalNode().Name {
continue
}
httpResp, err := http.Get("http://" + member.Addr.String() + ":20000/admin-data?proxy=true")
if err != nil {
continue
}
var memberData Data
err = json.NewDecoder(httpResp.Body).Decode(&memberData)
if err != nil {
continue
}
membersAdminData = append(membersAdminData, memberData)
}
data.Others = membersAdminData
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}
func (*OpsServer) AdminUI() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := TMPL.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}