diff --git a/check.go b/check.go new file mode 100644 index 0000000..e50daf7 --- /dev/null +++ b/check.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" +) + +func NewCheck(name string, addr string) (AlrmCheck, error) { + switch name { + case "ping": + return &CheckPing{Address: addr}, nil + default: + return nil, fmt.Errorf("unknown check name \"%s\"", name) + } +} diff --git a/check_ping.go b/check_ping.go new file mode 100644 index 0000000..6c20ed4 --- /dev/null +++ b/check_ping.go @@ -0,0 +1,13 @@ +package main + +type CheckPing struct { + Address string +} + +func (c *CheckPing) Check() error { + return nil +} + +func (c *CheckPing) Parse(tk string) (bool, error) { + return false, nil +} diff --git a/config.go b/config.go index 27ac166..0c1301c 100644 --- a/config.go +++ b/config.go @@ -25,6 +25,19 @@ func (ag *AlrmGroup) NewHost() *AlrmHost { type AlrmHost struct { Name string Address string + Checks []AlrmCheck +} + +func (ah *AlrmHost) GetAddress() string { + if ah.Address != "" { + return ah.Address + } + return ah.Name +} + +type AlrmCheck interface { + Parse(string) (bool, error) + Check() error } func ReadConfig(fn string) (*AlrmConfig, error) { diff --git a/parser.go b/parser.go index 144cb4b..71c949a 100644 --- a/parser.go +++ b/parser.go @@ -14,6 +14,7 @@ const ( TK_MONITOR TK_GROUP TK_HOST + TK_CHECK ) type Parser struct { @@ -31,6 +32,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) { config := &AlrmConfig{} var group *AlrmGroup var host *AlrmHost + var check AlrmCheck scan := bufio.NewScanner(file) scan.Split(p.Split) @@ -51,22 +53,25 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) { case TK_SET: key := strings.ToLower(tk) - if scan.Scan() { - value := scan.Text() - switch key { - case "interval": - config.Interval, err = strconv.Atoi(value) - if err != nil { - return nil, fmt.Errorf( - "invalid number for interval, line %d: \"%s\"", - p.Line+1, value, - ) - } - default: - return nil, fmt.Errorf("invalid key for set, line %d: \"%s\"", - p.Line+1, tk, + if !scan.Scan() { + return nil, fmt.Errorf("empty value name for set in %s, line %d", + fn, p.Line+1) + } + + value := scan.Text() + switch key { + case "interval": + config.Interval, err = strconv.Atoi(value) + if err != nil { + return nil, fmt.Errorf( + "invalid number for interval in %s, line %d: \"%s\"", + fn, p.Line+1, value, ) } + default: + return nil, fmt.Errorf("unknown key for set in %s, line %d: \"%s\"", + fn, p.Line+1, tk, + ) } p.prevState() @@ -87,7 +92,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) { case TK_GROUP: if group == nil { - return nil, fmt.Errorf("Group token without initialization") + return nil, fmt.Errorf("group without initialization") } switch strings.ToLower(tk) { @@ -108,7 +113,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) { case TK_HOST: if host == nil { - return nil, fmt.Errorf("Host token without initialization") + return nil, fmt.Errorf("host token without initialization") } if host.Name == "" { @@ -118,15 +123,36 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) { switch strings.ToLower(tk) { case "address": - if scan.Scan() { - host.Address = scan.Text() + if !scan.Scan() { + return nil, fmt.Errorf("empty address for host in %s, line %d", + fn, p.Line+1) } + host.Address = scan.Text() + + case "check": + check = nil + p.setState(TK_CHECK) default: p.prevState() goto stateswitch } + case TK_CHECK: + if check == nil { + if host == nil { + return nil, fmt.Errorf("host token without initialization") + } + check, err = NewCheck(strings.ToLower(tk), host.GetAddress()) + if err != nil { + return nil, fmt.Errorf("%s in %s, line %d", + err.Error(), fn, p.Line+1) + } + host.Checks = append(host.Checks, check) + continue + } + check.Parse(tk) + default: return nil, fmt.Errorf("unknown parser state: %d", p.state()) } @@ -169,6 +195,8 @@ func (p *Parser) stateName() string { return "TK_GROUP" case TK_HOST: return "TK_HOST" + case TK_CHECK: + return "TK_CHECK" default: return "UNKNOWN" } diff --git a/small.config b/small.config index 7299407..37696eb 100644 --- a/small.config +++ b/small.config @@ -8,5 +8,6 @@ monitor host stupid.com monitor group webservers # Monitor host one host one.com address 10.12.121.1 + check ping # Monitor host two host two.com