-
Notifications
You must be signed in to change notification settings - Fork 78
/
http.go
89 lines (83 loc) · 2.42 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
package icmpping
import (
"net/http"
"io/ioutil"
"log"
"fmt"
"encoding/json"
"github.com/WeBankPartners/open-monitor/monitor-agent/ping_exporter/funcs"
)
func StartHttpServer() {
if funcs.Config().Prometheus.Enabled || funcs.Config().Source.Listen.Enabled {
port := funcs.Config().Source.Listen.Port
if funcs.Config().Prometheus.Enabled {
port = funcs.Config().Prometheus.Port
http.Handle(funcs.Config().Prometheus.Path, http.HandlerFunc(handlePrometheus))
}
if funcs.Config().Source.Listen.Enabled {
loadHttpConfigData()
http.Handle(funcs.Config().Source.Listen.Path, http.HandlerFunc(handleIpSource))
}
http.ListenAndServe(":"+port, nil)
}
}
func handlePrometheus(w http.ResponseWriter,r *http.Request) {
w.Write(funcs.GetExportMetric())
}
func handleIpSource(w http.ResponseWriter,r *http.Request) {
var respMessage string
var param funcs.RemoteResponse
b,err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
respMessage = fmt.Sprintf("handle ip source read body error : %v \n", err)
log.Printf(respMessage)
w.Write([]byte(respMessage))
return
}
err = json.Unmarshal(b, ¶m)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
respMessage = fmt.Sprintf("handle ip source unmarshal json error : %v \n", err)
log.Printf(respMessage)
w.Write([]byte(respMessage))
return
}
var ips []string
for _,v := range param.Config {
ips = append(ips, v.Ip)
}
funcs.UpdateIpList(ips, funcs.Config().Source.Listen.Weight)
funcs.UpdateSourceRemoteData(param.Config)
saveHttpConfigData(b)
w.WriteHeader(http.StatusOK)
w.Write([]byte("success"))
}
func saveHttpConfigData(bodyData []byte) {
err := ioutil.WriteFile("http_config_data.json", bodyData, 0644)
if err != nil {
log.Printf("Save http config data fail,error: %s \n", err.Error())
}else{
log.Println("Save http config data success")
}
}
func loadHttpConfigData() {
b,err := ioutil.ReadFile("http_config_data.json")
if err != nil {
log.Printf("Load http config data fail,error: %s \n", err.Error())
return
}
var param funcs.RemoteResponse
err = json.Unmarshal(b, ¶m)
if err != nil {
log.Printf("Load http config data fail,json unmarshal error: %s \n", err.Error())
return
}
var ips []string
for _,v := range param.Config {
ips = append(ips, v.Ip)
}
funcs.UpdateIpList(ips, funcs.Config().Source.Listen.Weight)
funcs.UpdateSourceRemoteData(param.Config)
}