From acba43fdae4c4f3ffc5b9b16bb45369681178b35 Mon Sep 17 00:00:00 2001 From: laurentu Date: Thu, 17 Aug 2023 14:26:40 +0200 Subject: [PATCH] added command for pfctl and flag for lauching hostchecker --- hostchecker.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/hostchecker.go b/hostchecker.go index 978b347..3231ee0 100644 --- a/hostchecker.go +++ b/hostchecker.go @@ -12,6 +12,8 @@ import "os/signal" import "os" import "errors" import "net" +import "os/exec" +import "flag" type Check struct { Type string `yaml:"type"` @@ -34,10 +36,13 @@ type Group struct { func main() { log.Println("Starting") + confFileName := flag.String("f", "/usr/local/etc/hostchecker.yaml", "YAML configuration file") + flag.Parse() + var waitGroup sync.WaitGroup var conf map[string]Group - yamlFile, err := ioutil.ReadFile("conf.yaml") + yamlFile, err := ioutil.ReadFile(*confFileName) if err != nil { log.Fatalf("Configuration open error #%v ", err) } @@ -92,11 +97,7 @@ func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel break case status := <-channel: log.Println("Status for ", host, "is", status, "in group", name) - if status == 0 { - addHostToTables( host, group.Tables ) - } else { - removeHostFromTables( host, group.Tables) - } + updateTables( host, status, group.Tables ) default: time.Sleep(100 * time.Millisecond) } @@ -105,10 +106,20 @@ func checkGroup(name string, group Group, waitGroup *sync.WaitGroup, stopChannel } } -func addHostToTables( host string, tables []string ) { -} -func removeHostFromTables( host string, tables []string ) { +func updateTables( host string, status int, tables []string ) { + for _, table := range tables { + op := "add" + if status != 0 { + op = "del" + } + cmd := exec.Command("pfctl", "-t", table, "-T", op, host) + err := cmd.Run() + if err != nil { + log.Println("Unable to run command #", cmd) + } + } } + func checkHost(status chan<- int, group string, host string, check Check, waitGroup *sync.WaitGroup, stopChannel chan bool) { var lastCheck time.Time