-
Notifications
You must be signed in to change notification settings - Fork 49
/
http.go
executable file
·208 lines (186 loc) · 4.51 KB
/
http.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package goappmonitor
import (
"encoding/json"
"fmt"
"log"
"net/http"
_ "net/http/pprof" // pprof collector
"strings"
)
// Start Http serever.
func startHttp(addr string, debug bool) {
configCommonRoutes()
configProcRoutes()
if len(addr) >= 9 {
s := &http.Server{
Addr: addr,
MaxHeaderBytes: 1 << 30,
}
go func() {
if debug {
log.Println("[goappmonitor] http server start, listening on", addr)
}
s.ListenAndServe()
if debug {
log.Println("[goappmonitor] http server stop,", addr)
}
}()
}
}
// Routers config.
func configProcRoutes() {
http.HandleFunc("/pfc/proc/metrics/json", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, rawMetrics())
})
http.HandleFunc("/pfc/proc/metrics/falcon", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, falconMetrics())
})
http.HandleFunc("/pfc/proc/metrics/influxdb", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
InfluxDBJson(w, influxDBMetrics())
})
// url=/pfc/proc/metric/{json,falcon}
http.HandleFunc("/pfc/proc/metrics/", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
urlParam := r.URL.Path[len("/pfc/proc/metrics/"):]
args := strings.Split(urlParam, "/")
argsLen := len(args)
if argsLen != 2 {
RenderJson(w, "")
return
}
types := []string{}
typeslice := strings.Split(args[0], ",")
for _, t := range typeslice {
nt := strings.TrimSpace(t)
if nt != "" {
types = append(types, nt)
}
}
if args[1] == "json" {
RenderJson(w, rawMetric(types))
return
}
if args[1] == "falcon" {
RenderJson(w, falconMetric(types))
return
}
if args[1] == "influxdb" {
InfluxDBJson(w, influxDBMetric(types))
return
}
})
http.HandleFunc("/pfc/proc/metrics/size", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, rawSizes())
})
}
// Common router config.
func configCommonRoutes() {
http.HandleFunc("/pfc/health", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
w.Write([]byte("ok"))
})
http.HandleFunc("/pfc/version", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
w.Write([]byte(fmt.Sprintf("%s\n", VERSION)))
})
http.HandleFunc("/pfc/config", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, config())
})
http.HandleFunc("/pfc/config/reload", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
loadConfig()
RenderJson(w, "ok")
})
}
func isLocalReq(raddr string) bool {
return strings.HasPrefix(raddr, "127.0.0.1")
}
// RenderJson json
func RenderJson(w http.ResponseWriter, data interface{}) {
renderJson(w, Response{Msg: "success", Data: data})
}
// RenderString string
func RenderString(w http.ResponseWriter, msg string) {
renderJson(w, map[string]string{"msg": msg})
}
func renderJson(w http.ResponseWriter, v interface{}) {
bs, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(bs)
}
// Response http struct
type Response struct {
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
// Render json
func InfluxDBJson(w http.ResponseWriter, data interface{}) {
influxDBJson(w, InfluxDBResponse{
"success",
endpoint,
[]string{
"metric",
"counterType",
"tags",
"value",
"step",
"timestamp",
},
data,
})
}
// Render string
func InfluxDBString(w http.ResponseWriter, msg string) {
influxDBJson(w, map[string]string{"msg": msg})
}
func influxDBJson(w http.ResponseWriter, v interface{}) {
bs, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(bs)
}
type InfluxDBResponse struct {
Msg string `json:"msg"`
Name string `json:"name"`
Columns []string `json:"columns"`
Points interface{} `json:"points"`
}