Presque ok

This commit is contained in:
laurentu 2023-08-16 16:16:41 +02:00
parent 06d9396d91
commit 5219deb79f
1 changed files with 72 additions and 22 deletions

94
main.go
View File

@ -8,6 +8,8 @@ import "log"
import "gopkg.in/yaml.v3" import "gopkg.in/yaml.v3"
import "time" import "time"
import "sync" import "sync"
import "os/signal"
import "os"
type Check struct { type Check struct {
Type string `yaml:"type"` Type string `yaml:"type"`
@ -47,29 +49,32 @@ func main() {
go checkGroup(name, group, &waitGroup, stopChannel) 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") log.Println("main closing stopChannel")
close(stopChannel) close(stopChannel)
waitGroup.Wait() waitGroup.Wait()
} }
func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel chan bool) { func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel chan bool) {
channels := make(map[string]chan int) channels := make(map[string]chan int)
var checkHostWaitGroup sync.WaitGroup
for _, host := range group.Hosts { for _, host := range group.Hosts {
channel := make(chan int, 1) channel := make(chan int, 1)
channels[host] = channel channels[host] = channel
checkHostWaitGroup.Add(1) waitGroup.Add(1)
go checkHost(channels[host], name, host, group.Check, &checkHostWaitGroup, stopChannel) go checkHost(channels[host], name, host, group.Check, waitGroup, stopChannel)
} }
for { for {
select { select {
case <-stopChannel: case <-stopChannel:
log.Println("checkGroup", name, "stopChannel") log.Println("checkGroup", name, "stopChannel")
checkHostWaitGroup.Wait() waitGroup.Done()
waitGroup.Done() return
return
break break
default: default:
for host, channel := range channels { for host, channel := range channels {
@ -87,28 +92,73 @@ 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) { 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 { for {
select { select {
case <-stopChannel: case <-stopChannel:
log.Println("checkHost", host, "group", group, "stopChannel") log.Println("checkHost", host, "group", group, "stopChannel")
waitGroup.Done() waitGroup.Done()
return return
default: default:
resp, err := client.Head(fmt.Sprintf("http://%s", host)) var err error
if err != nil {
log.Println(host, "Failed") err = nil
status <- 1 switch check.Type {
} else { case "http":
status <- 0 err = CheckHTTP(host, check)
_, _ = io.ReadAll(resp.Body) case "tcp":
resp.Body.Close() err = CheckTCP(host, check)
log.Println(host, "Status", resp.StatusCode) case "smtp":
err = CheckSMTP(host, check)
default:
err = CheckHTTP(host, check)
} }
if err != nil {
status <- 1
log.Println("checkHost", host, "group", group, "error", err)
} else {
status <- 0
}
time.Sleep(time.Duration(check.Interval) * time.Second) 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
}