-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (58 loc) · 1.72 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
package sauron3
import (
"fmt"
"net"
"time"
"io/ioutil"
"encoding/json"
"gopkg.in/yaml.v2"
)
const retryCount = 5
type Probe struct {
Title string `yaml:"title"`
Proto string `yaml:"proto"`
Port int32 `yaml:"port"`
Alive bool `yaml:"alive"`
}
type Host struct {
Title string `yaml:"title"`
IP string `yaml:"ip"`
Probes []*Probe `yaml:"probes"`
}
var Database []*Host
var DatabaseJson string
func LoadConfig(path string){
data, err := ioutil.ReadFile(path)
if err != nil { panic(fmt.Sprintf("%v",err)) }
err = yaml.Unmarshal(data, &Database)
if err != nil { panic(fmt.Sprintf("%v",err)) }
jsonObj, err := json.Marshal(Database)
if err != nil { panic(fmt.Sprintf("%v",err)) }
DatabaseJson = string(jsonObj)
}
func Livecheck(ip string, probe Probe) bool{
if probe.Proto=="tcp" {
cs := ip+":"+fmt.Sprintf("%v", probe.Port)
_, err := net.DialTimeout(probe.Proto, cs, TCPOpenTimeout*time.Millisecond)
return err==nil
} else if probe.Proto=="udp"{
return false //non trivial, TODO: implement me
} else { // assume ICMP PING as everything other for simple fallback
state, _ := Ping(ip)
return state
}
}
func CheckAll() string{
LiveStatus := Database
for _,host := range LiveStatus {
for _,probe := range host.Probes {
for i:=0; i<retryCount; i++ {
probe.Alive=Livecheck(host.IP, *probe)
if probe.Alive { break; }
}
}
}
jsonObj, err := json.Marshal(LiveStatus)
if err != nil { return "{\"error!\"}" }
return string(jsonObj)
}