forked from lacchain/node-health-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testPorts.go
58 lines (50 loc) · 1.11 KB
/
testPorts.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
package main
import (
"flag"
"fmt"
"net/http"
"time"
"github.com/reiver/go-telnet"
)
var port *string
func testPorts() <-chan bool {
port = flag.String("port", clientPort, "port on localhost to check")
flag.Parse()
c := make(chan bool)
go func() {
for i := 0; ; i++ {
time.Sleep(delayTimeMinutes * time.Minute)
msg := test()
if msg {
c <- msg //true ==> restart
}
}
}()
return c
}
func test() bool {
msg1 := testClientURL(clientURL)
msg2 := testNodePort(nodeURL, nodePort)
return msg1 || msg2
}
func testClientURL(url string) bool {
resp, err := http.Get(url + ":" + *port + "/upcheck")
if err != nil || resp.StatusCode != 200 {
fmt.Println("dead in client")
return true
}
fmt.Println("Successful response from ClientPort:", clientPort)
defer resp.Body.Close()
return false
}
func testNodePort(ip, nodePort string) bool {
conn, err := telnet.DialTo(ip + ":" + nodePort)
if err != nil {
fmt.Println("NodeUrl not responding")
return true
}
fmt.Println("Successful response from Nodeport:", nodePort)
conn.Write([]byte("hello world"))
conn.Write([]byte("\n"))
return false
}