added command for pfctl and flag for lauching hostchecker

This commit is contained in:
laurentu 2023-08-17 14:26:40 +02:00
parent 351e6fe67c
commit acba43fdae
1 changed files with 20 additions and 9 deletions

View File

@ -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 updateTables( host string, status int, tables []string ) {
for _, table := range tables {
op := "add"
if status != 0 {
op = "del"
}
func removeHostFromTables( host string, tables []string ) {
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