-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (49 loc) · 1.01 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
package main
import (
"fmt"
"net/http"
"os/exec"
"time"
. "github.com/ehsanhajian/k8s-pingpong/config"
"github.com/gin-gonic/gin"
)
type Server struct {
config AppConfig
}
func main() {
var c AppConfig
config := c.LoadConf()
ticker := time.NewTicker(time.Duration(config.PingInterval) * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
for _, server := range c.Servers {
pingOut := ping(server.IP)
fmt.Println(pingOut)
}
case <-quit:
ticker.Stop()
return
}
}
}()
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
serverPort := fmt.Sprintf(":%s", c.ServerPort)
r.Run(serverPort)
}
func ping(ip string) string {
out, _ := exec.Command("ping", ip, "-c 5", "-i 3", "-w 10").Output()
return string(out)
// if strings.Contains(string(out), "Destination Host Unreachable") {
// fmt.Println("TANGO DOWN")
// } else {
// fmt.Println("IT'S ALIVEEE")
// }
}