-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpmonitor_client.go
95 lines (80 loc) · 2.55 KB
/
httpmonitor_client.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
// Copyright 2015 [email protected] All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"path/filepath"
// "html/template"
// "log"
"net/http"
"net/rpc"
"regexp"
"time"
)
type HttpMonitor struct {
ScriptMonitor string
RpcMap map[string]*rpc.Client
}
type home struct {
Title string
}
var mux map[string]func(http.ResponseWriter, *http.Request)
func (t *HttpMonitor) Index(w http.ResponseWriter, r *http.Request) {
var allInfo []byte
for ip, rpcMonitor := range t.RpcMap {
item := MonitorItem{Script: t.ScriptMonitor}
var remoteInfo []byte
rpcMonitor.Call("RPC.GetStateInfo", item, &remoteInfo)
//fmt.Println(remoteInfo)
allInfo = append(allInfo, []byte(ip)...)
allInfo = append(allInfo, []byte(":\t\n\t")...)
allInfo = append(allInfo, remoteInfo...)
}
fmt.Fprintf(w, string(allInfo))
//fmt.Println("HTTP Index Run")
}
func (*HttpMonitor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h, ok := mux[r.URL.String()]; ok {
//fmt.Println(mux)
h(w, r)
return
}
if ok, _ := regexp.MatchString("/css/", r.URL.String()); ok {
//fmt.Println("2")
http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))).ServeHTTP(w, r)
} else {
//fmt.Println("3")
http.StripPrefix("/", http.FileServer(http.Dir("."))).ServeHTTP(w, r)
}
}
func (t *HttpMonitor) Init(cfg Run) {
t.ScriptMonitor = cfg.PrjName + "/" + cfg.PrjName + "_" + filepath.Base(cfg.PrjPackage) + "/" + cfg.ScriptMonitor
t.RpcMap = make(map[string]*rpc.Client)
for _, Ip := range cfg.RemoteIP {
if Ip.TaskMonitorPort == "" {
Ip.TaskMonitorPort = "9093"
}
fmt.Println("== Monitor Try To Link: " + Ip.Value + ":" + Ip.TaskMonitorPort + " ==\n")
client, err := rpc.Dial("tcp", Ip.Value+":"+Ip.TaskMonitorPort)
if err != nil {
fmt.Println("Monitor connect error:" + err.Error())
fmt.Println("== Monitor Link ERROR: " + Ip.Value + ":" + Ip.TaskMonitorPort + " ==")
continue
}
t.RpcMap[Ip.Value+":"+Ip.TaskMonitorPort] = client
fmt.Println("== Monitor Link Success: " + Ip.Value + ":" + Ip.TaskMonitorPort + " ==\n")
}
}
func StartHttpMonitor(cfg Run) {
http_serv := new(HttpMonitor)
http_serv.Init(cfg)
server := http.Server{
Addr: ":" + cfg.HttpPort,
Handler: &HttpMonitor{},
ReadTimeout: 5 * time.Second,
}
mux = make(map[string]func(http.ResponseWriter, *http.Request))
mux["/"] = http_serv.Index
server.ListenAndServe()
}