-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
441 lines (402 loc) · 10.1 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package main
import (
"code.google.com/p/go.net/websocket"
"config"
"encoding/json"
"errors"
"fmt"
"goMonitor/mconn"
"html/template"
"log"
"net/http"
"net/smtp"
"os"
"path"
"runtime"
"time"
)
//Protocol contain three forms of message that client and Monitor server can transfer
//information,they are 'start' 'stop' 'restart' 'stop all' 'restart all'
type Protocol struct {
Operation string
Servers []interface{}
}
type SendInfo struct {
ServerIp string
ProInfo []map[string]*mconn.Process
}
type ProInfo map[string][]map[string]*mconn.Process
type DialServer struct {
ServerIp string
Conns []*mconn.Mconn
}
type WSConn struct {
Conn *websocket.Conn
Servers []string
bSend bool
}
const VERSION = "0.2.0"
var (
MonServers map[string]*DialServer
CONFIG_PATH string
APP_PATH string
VIEW_PATH string
user2psd map[string]string = make(map[string]string)
Operations []string
CmdChan chan bool
DialServersChan chan []*DialServer
QuitWSChan chan bool
EndMonChan chan bool
DataFlowState bool
SEND_INFO_DELAY time.Duration
ConnNum int
WSConnChan chan *WSConn
SendInfoChan chan *SendInfo
)
func initData() {
runtime.GOMAXPROCS(2)
APP_PATH, _ = os.Getwd()
CONFIG_PATH = path.Join(APP_PATH, "conf")
VIEW_PATH = path.Join(APP_PATH, "view")
MonServers = make(map[string]*DialServer)
CmdChan = make(chan bool)
DialServersChan = make(chan []*DialServer)
QuitWSChan = make(chan bool)
EndMonChan = make(chan bool)
WSConnChan = make(chan *WSConn)
SendInfoChan = make(chan *SendInfo)
DataFlowState = false
//init user/password data
user2psd["u1"] = "p1"
user2psd["u2"] = "p2"
user2psd["u3"] = "p3"
SEND_INFO_DELAY = 100 * time.Millisecond
//init processes which will be monitored
//MonitorProInfo = &ProcessInfo{ColPname: "pname", ColPid: "pid", ColCpu: "%cpu", ColMem: "%mem",
// Pros: make(map[string]*Process)}
InitConfig()
go dataFlow()
}
//func filter([]string, func ())
func SendMail() {
// Set up authentication information.
auth := smtp.PlainAuth(
"", // identity
"test", // user name
"123456", // password
"smtp.163.com", // host
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
"smtp.163.com:25", // addr eg: mail.example.com:25
auth, // auth
"[email protected]", // from
[]string{"[email protected]"}, // to
[]byte("This is the email body."), // msg
)
if err != nil {
log.Fatal(err)
}
}
func viewFilePath(file string) string {
return path.Join(VIEW_PATH, file)
}
func confFilePath(file string) string {
return path.Join(CONFIG_PATH, file)
}
func loginCheck(username string, password string) (err error) {
bInvalidUsername := false
bInvalidPassword := false
err = nil
for k, v := range user2psd {
if username == k {
bInvalidUsername = true
if password == v {
bInvalidPassword = true
break
} else {
bInvalidPassword = false
}
}
}
if bInvalidUsername == false {
fmt.Println(username, " doesn't exists.")
err = errors.New(username + " doesn't exists.")
//return UNAME_NO_EXIST
} else if bInvalidPassword == false {
fmt.Println("wrong password")
err = errors.New("wrong password")
//return PASSWORD_WRONG
}
return err
}
func send(ws *websocket.Conn, msg interface{}) error {
err := websocket.JSON.Send(ws, msg)
return err
}
func recv(ws *websocket.Conn) (*Protocol, error) {
var p Protocol
fmt.Println("receiving message...")
err := websocket.JSON.Receive(ws, &p)
if err != nil {
return &p, err
}
return &p, nil
}
func WS_Handler(ws *websocket.Conn) {
ConnNum++
defer func() {
// if DataFlowState == true {
// QuitWSChan <- true
// }
ws.Close()
fmt.Println("websocket closed")
}()
for {
clientData, err := recv(ws)
if err != nil {
fmt.Println("recv error:" + err.Error())
break
}
//fmt.Println(clientData)
// fmt.Println(reflect.TypeOf(clientData.Servers))
// for k, v := range clientData.Servers {
// fmt.Println(k, v)
// }
if DealClientMsg(ws, clientData) == false {
break
}
}
}
func DialAllServer(wsConn *WSConn, servers []interface{}) {
curServer := make([]*DialServer, 0)
for i := 0; i < len(servers); i++ {
server := servers[i].(map[string]interface{})
for _, v := range server {
ip := v.(string)
//fmt.Println(ip)
dialServer, ok := MonServers[ip]
if !ok {
continue
}
wsConn.Servers = append(wsConn.Servers, ip)
s := DialServer{}
s.ServerIp = ip
s.Conns = make([]*mconn.Mconn, 0)
for _, conn := range dialServer.Conns {
if conn.IsDialed() {
continue
}
//fmt.Println("dial server:" + conn.DialServer)
err := conn.Dial()
if err != nil {
fmt.Println("dialAllServer error: " + err.Error())
continue
}
s.Conns = append(s.Conns, conn)
}
curServer = append(curServer, &s)
}
}
// fmt.Println("len of s: ", len(curServer))
// for _, item := range curServer {
// fmt.Println(item.ServerIp)
// for _, v := range item.Conns {
// fmt.Println(v.DialServer)
// fmt.Println(v.State)
// }
// }
WSConnChan <- wsConn
DialServersChan <- curServer
fmt.Println("dial servers done")
}
func DealClientMsg(ws *websocket.Conn, clientData *Protocol) bool {
switch clientData.Operation {
case "start monitor":
DialAllServer(&WSConn{ws, make([]string, 0), true}, clientData.Servers)
return true
//EndMonChan <- true
case "end monitor":
WSConnChan <- &WSConn{ws, make([]string, 0), false}
// if len(clientData.Servers) == 0 {
// return
// }
//EndMonChan <- false
return true
case "stop":
return true
// var pid int
// client := FindRpcClient(pid)
// if client == nil {
// fmt.Println("no client conn")
// break
// }
// pro := &Process{}
// args := &Args{pid}
// err := client.Call("ProcessInfo.KillProcess", args, pro)
// if err != nil {
// panic(err)
// }
// fmt.Println(pro.Pid)
}
return false
}
func PrintInfo(info []*SendInfo) {
type SendInfo struct {
ServerIp string
ProInfo []map[string]*mconn.Process
}
for _, v := range info {
for _, p := range v.ProInfo {
for _, c := range p {
fmt.Println(c.Cpu)
fmt.Println(c.Mem)
fmt.Println(c.Name)
fmt.Println(c.Pid)
}
}
}
}
func dataFlow() {
//fmt.Println("conn num:", ConnNum)
DataFlowState = true
dialServers := make([]*DialServer, 0)
conns := make(map[*websocket.Conn]*WSConn)
for {
select {
case server := <-DialServersChan:
dialServers = append(dialServers, server...)
case conn := <-WSConnChan:
conns[conn.Conn] = conn
default:
sendInfo := getProFromServers(dialServers)
// for ip, v := range *sendInfo {
// fmt.Println("ip: " + ip)
// for _, item := range v {
// for _, va := range item {
// fmt.Println(va.Pid)
// }
// }
// }
for ws, WSConnection := range conns {
if !WSConnection.bSend {
continue
}
info := make([]*SendInfo, 0)
for _, serverIp := range WSConnection.Servers {
for _, v := range sendInfo {
if v.ServerIp == serverIp {
info = append(info, v)
}
}
}
//PrintInfo(info)
msg, err := json.Marshal(info)
if err != nil {
panic(err)
}
err = send(WSConnection.Conn, string(msg))
if err != nil {
fmt.Println("send error: " + err.Error())
// if connIndex+1 < len(conns) {
// copy(conns[connIndex:], conns[connIndex+1:])
// }
// conns = conns[:len(conns)-1]
delete(conns, ws)
}
}
time.Sleep(SEND_INFO_DELAY)
}
}
}
func getProFromServers(dialServers []*DialServer) []*SendInfo {
//fmt.Println("len of conns: ", len(dialServers))
result := make([]*SendInfo, 0)
for i := 0; i < len(dialServers); i++ {
s := dialServers[i]
Si := &SendInfo{}
Si.ProInfo = make([]map[string]*mconn.Process, 0)
Si.ServerIp = s.ServerIp
pro := make(map[string]*mconn.Process)
for _, conn := range s.Conns {
conn.GetProInfo(&pro)
Si.ProInfo = append(Si.ProInfo, pro)
}
result = append(result, Si)
}
return result
}
func InitConfig() {
serverListConf, err := config.OpenIniFile(confFilePath("serverlist.conf"))
if err != nil {
panic(err)
}
if err := serverListConf.Parse(); err != nil {
panic(err)
}
for ip, servers := range serverListConf.Sections {
conns := make([]*mconn.Mconn, 0)
MonServer := &DialServer{ip, conns}
for _, node := range servers.Nodes {
conn := &mconn.Mconn{}
conn.DialServer = node.Value
MonServer.Conns = append(MonServer.Conns, conn)
}
MonServers[servers.Name] = MonServer
}
// for _, v := range MonServers {
// fmt.Println(v.ServerIp)
// for _, c := range v.Conns {
// fmt.Println(c.DialServer)
// }
// }
operationConf, err := config.OpenIniFile(confFilePath("operations.conf"))
if err != nil {
panic(err)
}
if err := operationConf.Parse(); err != nil {
panic(err)
}
sec := operationConf.GetSection("Operations")
for _, operation := range sec.Nodes {
Operations = append(Operations, operation.Value)
}
}
func monitor(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles(viewFilePath("login_fail.html"))
t.Execute(w, "You Need to sign in!")
} else {
r.ParseForm()
username := template.HTMLEscapeString(r.Form.Get("username"))
password := template.HTMLEscapeString(r.Form.Get("password"))
fmt.Println(username, password)
if err := loginCheck(username, password); err != nil {
t, _ := template.ParseFiles(viewFilePath("login_fail.html"))
t.Execute(w, err.Error())
} else {
fmt.Println("login successful!")
//t, err := template.ParseFiles(viewFilePath("mon.html"))
http.ServeFile(w, r, viewFilePath("mon.html"))
// template.Must(t, err)
// getProcessInfo(MonitorProInfo)
// t.Execute(w, nil)
}
}
}
func login(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles(viewFilePath("login.html"))
t.Execute(w, nil)
} else {
}
}
func main() {
initData()
fmt.Println("GoMonitor starting up...")
http.HandleFunc("/login", login)
http.HandleFunc("/monitor", monitor)
http.Handle("/processInfo", websocket.Handler(WS_Handler))
http.ListenAndServe(":9091", nil)
}