This commit is contained in:
laurentu 2023-08-17 16:14:13 +02:00
parent ceabd1d76b
commit b10c025860
1 changed files with 114 additions and 116 deletions

View File

@ -1,17 +1,17 @@
/* /*
Copyright 2023 Laurent Ulrich (laurentu@gmail.com) Copyright 2023 Laurent Ulrich (laurentu@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package main package main
@ -47,7 +47,6 @@ type Group struct {
Check Check `yaml:"check"` Check Check `yaml:"check"`
} }
func main() { func main() {
log.Println("Starting") log.Println("Starting")
@ -112,7 +111,7 @@ func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel
break break
case status := <-channel: case status := <-channel:
log.Println("Status for ", host, "is", status, "in group", name) log.Println("Status for ", host, "is", status, "in group", name)
updateTables( host, status, group.Tables ) updateTables(host, status, group.Tables)
default: default:
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
@ -121,7 +120,7 @@ func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel
} }
} }
func updateTables( host string, status int, tables []string ) { func updateTables(host string, status int, tables []string) {
for _, table := range tables { for _, table := range tables {
op := "add" op := "add"
if status != 0 { if status != 0 {
@ -165,14 +164,13 @@ func checkHost(status chan<- int, group string, host string, check Check, waitGr
status <- 0 status <- 0
} }
} }
time.Sleep(300*time.Millisecond) time.Sleep(300 * time.Millisecond)
} }
} }
} }
func CheckHTTP(host string, check Check) error { func CheckHTTP(host string, check Check) error {
client := &http.Client{ client := &http.Client{
Timeout: time.Duration(check.Timeout) * time.Second, Timeout: time.Duration(check.Timeout) * time.Second,
} }
@ -194,7 +192,7 @@ func CheckHTTP(host string, check Check) error {
} }
func CheckTCP(host string, check Check) error { func CheckTCP(host string, check Check) error {
cnx, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, check.Port), time.Duration(check.Timeout) * time.Second) cnx, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, check.Port), time.Duration(check.Timeout)*time.Second)
if err != nil { if err != nil {
return err return err
} }
@ -249,7 +247,7 @@ func validateConfiguration(conf map[string]Group) error {
} }
default: default:
return errors.New(fmt.Sprintf("Check type should be http, smtp or tcp in group %s",name)) return errors.New(fmt.Sprintf("Check type should be http, smtp or tcp in group %s", name))
} }
if group.Check.Interval == 0 { if group.Check.Interval == 0 {
group.Check.Interval = 5 group.Check.Interval = 5