- Possibilité de rediriger le log vers syslog avec le flag -syslog (#1)

- Possibilité de régler le niveau de log (0,1,2) avec le flag -verbose (#2)
This commit is contained in:
2023-08-18 08:08:40 +02:00
parent 25126b2cdf
commit 6183d41d92

View File

@@ -29,6 +29,7 @@ import "errors"
import "net"
import "os/exec"
import "flag"
import "log/syslog"
type Check struct {
Type string `yaml:"type"`
@@ -47,12 +48,23 @@ type Group struct {
Check Check `yaml:"check"`
}
var verbose int
func main() {
if verbose > 0 {
log.Println("Starting")
}
confFileName := flag.String("f", "/usr/local/etc/hostchecker.yaml", "YAML configuration file")
flag.IntVar(&verbose, "verbose", 0, "Set logs verbosity")
useSyslog := flag.Bool("syslog", false, "Send logs to syslog")
flag.Parse()
if *useSyslog == true {
syslogWriter, err := syslog.New(syslog.LOG_ERR, "hostchecker")
if err != nil {
log.Fatal("Error opening syslog #", err)
}
log.SetOutput(syslogWriter)
}
var waitGroup sync.WaitGroup
var conf map[string]Group
@@ -71,7 +83,9 @@ func main() {
}
stopChannel := make(chan bool)
for name, group := range conf {
log.Println("Checking group", name, group)
if verbose > 0 {
log.Println("Checking group", name, group)
}
waitGroup.Add(1)
go checkGroup(name, group, &waitGroup, stopChannel)
}
@@ -81,8 +95,12 @@ func main() {
signal.Notify(exit, os.Interrupt)
s := <-exit
if verbose > 1 {
log.Println("Received signal", s)
}
if verbose > 0 {
log.Println("main closing stopChannel")
}
close(stopChannel)
waitGroup.Wait()
}
@@ -99,7 +117,9 @@ func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel
for {
select {
case <-stopChannel:
if verbose > 0 {
log.Println("checkGroup", name, "stopChannel")
}
waitGroup.Done()
return
break
@@ -107,10 +127,14 @@ func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel
for host, channel := range channels {
select {
case stop := <-stopChannel:
if verbose > 0 {
log.Println("checkGroup", name, "stopChannel", stop)
}
break
case status := <-channel:
if verbose > 1 {
log.Println("Status for ", host, "is", status, "in group", name)
}
updateTables(host, status, group.Tables)
default:
time.Sleep(100 * time.Millisecond)
@@ -141,7 +165,9 @@ func checkHost(status chan<- int, group string, host string, check Check, waitGr
for {
select {
case <-stopChannel:
if verbose > 0 {
log.Println("checkHost", host, "group", group, "stopChannel")
}
waitGroup.Done()
return
default:
@@ -159,7 +185,9 @@ func checkHost(status chan<- int, group string, host string, check Check, waitGr
}
if err != nil {
status <- 1
if verbose > 1 {
log.Println("checkHost", host, "group", group, "error", err)
}
} else {
status <- 0
}
@@ -206,11 +234,15 @@ func CheckSMTP(host string, check Check) error {
func validateConfiguration(conf map[string]Group) error {
for name, group := range conf {
if verbose > 1 {
log.Println("Validating configuration", name)
}
if len(group.Tables) == 0 {
return errors.New(fmt.Sprintf("No tables in group %s", name))
}
if verbose > 1 {
log.Println("Hosts", group.Hosts)
}
if len(group.Hosts) == 0 {
return errors.New(fmt.Sprintf("No hosts in group %s", name))
}