-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
193 lines (159 loc) · 3.91 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"time"
"github.com/gorilla/websocket"
)
type ServerStatusMsg struct {
ConnID int
Status string
}
type ServerConnection struct {
ConnID int
IP net.IP
updateChan chan *ServerUpdate
statusMsgChan chan *ServerStatusMsg
quit chan bool
sleep chan int
configRevision int
}
type ServerUpdate struct {
ConnID int `json:"connection_id"`
Hostname string `json:"h"`
ID string `json:"id"`
Version string `json:"v"`
Groups []string `json:"groups"`
IP string `json:"ip"`
Uptime int64 `json:"up"`
Queries int64 `json:"qs"`
Qps float64 `json:"qps"`
Qps1 float64 `json:"qps1m"`
Started int `json:"started"`
UUID string `json:"uuid"`
}
func NewServerConnection(ip net.IP, updates chan *ServerUpdate, sm chan *ServerStatusMsg) *ServerConnection {
sc := new(ServerConnection)
sc.IP = ip
sc.updateChan = updates
sc.statusMsgChan = sm
sc.quit = make(chan bool, 1)
sc.sleep = make(chan int, 1)
return sc
}
func (sc *ServerConnection) Start(id int) {
sc.ConnID = id
sc.statusMsg("Starting")
su := new(ServerUpdate)
su.ConnID = id
su.IP = sc.IP.String()
sc.updateChan <- su
go sc.start()
}
func (sc *ServerConnection) Stop() {
sc.quit <- true
}
func (sc *ServerConnection) statusErrorMsg(str string) {
sc.statusMsg(str)
su := new(ServerUpdate)
su.ConnID = sc.ConnID
su.IP = sc.IP.String()
sc.updateChan <- su
}
func (sc *ServerConnection) statusMsg(str string) {
msg := &ServerStatusMsg{sc.ConnID, str}
sc.statusMsgChan <- msg
}
func (sc *ServerConnection) start() {
log.Println("Fetch for", sc.IP)
retries := 0
for {
select {
case <-sc.quit:
log.Println("sc got quit!")
sc.statusErrorMsg("stopped")
return
case retries := <-sc.sleep:
delay := retries * retries / 2
if delay > 60 {
delay = 30
}
time.Sleep(time.Duration(delay) * time.Second)
default:
retries++
conn, err := net.Dial("tcp", net.JoinHostPort(sc.IP.String(), "8053"))
if err != nil {
status := fmt.Sprintf("%s", err)
sc.statusErrorMsg(status)
log.Println(status)
sc.sleep <- retries
continue
}
url, err := url.Parse("/monitor")
if err != nil {
log.Println("Could not parse url", err)
}
header := http.Header{}
header.Add("Origin", "http://monitor.pgeodns")
header.Add("Host", sc.IP.String())
header.Add("Set-WebSocket-Protocol", "chat")
ws, _, err := websocket.NewClient(conn, url, header, 1024, 1024)
if err != nil {
status := fmt.Sprintf("Could not upgrade WS on '%s': %s", sc.IP, err)
sc.statusErrorMsg(status)
log.Println(status)
sc.sleep <- retries
continue
}
sc.read(ws)
log.Println("server reader stopped")
err = conn.Close()
if err != nil {
log.Printf("Error closing connection to %s: %s", sc.IP, err)
}
sc.sleep <- retries
continue
}
}
}
func (sc *ServerConnection) read(ws *websocket.Conn) {
// log.Println("Response", resp)
status := new(ServerUpdate)
status.ConnID = sc.ConnID
for {
select {
case <-sc.quit:
log.Println("server reader got quit message")
sc.quit <- true
return
default:
ws.SetReadDeadline(time.Now().Add(time.Second * 3))
sc.statusMsg("Ok")
op, r, err := ws.NextReader()
if err != nil {
status := fmt.Sprintf("Error reading from server: %s", err)
sc.statusErrorMsg(status)
log.Println(status)
return
}
msg, err := ioutil.ReadAll(r)
// log.Println("op", op, "msg", string(msg), "err", err)
if op == websocket.TextMessage {
err = json.Unmarshal(msg, &status)
if err != nil {
log.Printf("Unmarshall err from '%s': '%s', data: '%s'\n", sc.IP.String(), err, msg)
}
// log.Printf("Got status: %#v\n", status)
sc.updateChan <- status
} else {
log.Println("op", op, "msg", string(msg), "err", err)
}
// os.Exit(0)
}
}
}