Presque ok
This commit is contained in:
parent
06d9396d91
commit
5219deb79f
74
main.go
74
main.go
|
@ -8,6 +8,8 @@ import "log"
|
|||
import "gopkg.in/yaml.v3"
|
||||
import "time"
|
||||
import "sync"
|
||||
import "os/signal"
|
||||
import "os"
|
||||
|
||||
type Check struct {
|
||||
Type string `yaml:"type"`
|
||||
|
@ -47,7 +49,12 @@ func main() {
|
|||
go checkGroup(name, group, &waitGroup, stopChannel)
|
||||
}
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
exit := make(chan os.Signal, 1)
|
||||
|
||||
signal.Notify(exit, os.Interrupt)
|
||||
|
||||
s := <-exit
|
||||
log.Println("Received signal", s)
|
||||
log.Println("main closing stopChannel")
|
||||
close(stopChannel)
|
||||
waitGroup.Wait()
|
||||
|
@ -55,19 +62,17 @@ func main() {
|
|||
|
||||
func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel chan bool) {
|
||||
channels := make(map[string]chan int)
|
||||
var checkHostWaitGroup sync.WaitGroup
|
||||
for _, host := range group.Hosts {
|
||||
channel := make(chan int, 1)
|
||||
channels[host] = channel
|
||||
checkHostWaitGroup.Add(1)
|
||||
go checkHost(channels[host], name, host, group.Check, &checkHostWaitGroup, stopChannel)
|
||||
waitGroup.Add(1)
|
||||
go checkHost(channels[host], name, host, group.Check, waitGroup, stopChannel)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-stopChannel:
|
||||
log.Println("checkGroup", name, "stopChannel")
|
||||
checkHostWaitGroup.Wait()
|
||||
waitGroup.Done()
|
||||
return
|
||||
break
|
||||
|
@ -88,8 +93,21 @@ func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel
|
|||
}
|
||||
|
||||
func checkHost(status chan<- int, group string, host string, check Check, waitGroup *sync.WaitGroup, stopChannel chan bool) {
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * time.Duration(check.Timeout),
|
||||
|
||||
if check.Timeout == 0 {
|
||||
check.Timeout = 5
|
||||
}
|
||||
if check.Interval == 0 {
|
||||
check.Interval = 60
|
||||
}
|
||||
if len(check.Method) == 0 {
|
||||
check.Method = "HEAD"
|
||||
}
|
||||
if len(check.Uri) == 0 {
|
||||
check.Uri = "/"
|
||||
}
|
||||
if check.Port == 0 {
|
||||
check.Port = 80
|
||||
}
|
||||
for {
|
||||
select {
|
||||
|
@ -98,17 +116,49 @@ func checkHost(status chan<- int,group string, host string, check Check, waitGro
|
|||
waitGroup.Done()
|
||||
return
|
||||
default:
|
||||
resp, err := client.Head(fmt.Sprintf("http://%s", host))
|
||||
var err error
|
||||
|
||||
err = nil
|
||||
switch check.Type {
|
||||
case "http":
|
||||
err = CheckHTTP(host, check)
|
||||
case "tcp":
|
||||
err = CheckTCP(host, check)
|
||||
case "smtp":
|
||||
err = CheckSMTP(host, check)
|
||||
default:
|
||||
err = CheckHTTP(host, check)
|
||||
}
|
||||
if err != nil {
|
||||
log.Println(host, "Failed")
|
||||
status <- 1
|
||||
log.Println("checkHost", host, "group", group, "error", err)
|
||||
} else {
|
||||
status <- 0
|
||||
_, _ = io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
log.Println(host, "Status", resp.StatusCode)
|
||||
}
|
||||
time.Sleep(time.Duration(check.Interval) * time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CheckHTTP(host string, check Check) error {
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * time.Duration(check.Timeout),
|
||||
}
|
||||
resp, err := client.Head(fmt.Sprintf("http://%s", host))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckTCP(host string, check Check) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckSMTP(host string, check Check) error {
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue