Added basic structure for checks

This commit is contained in:
Christopher Ramey 2020-08-20 08:44:56 -08:00
parent 87c53831a2
commit ac45a8625b
5 changed files with 87 additions and 18 deletions

14
check.go Normal file
View File

@ -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)
}
}

13
check_ping.go Normal file
View File

@ -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
}

View File

@ -25,6 +25,19 @@ func (ag *AlrmGroup) NewHost() *AlrmHost {
type AlrmHost struct { type AlrmHost struct {
Name string Name string
Address 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) { func ReadConfig(fn string) (*AlrmConfig, error) {

View File

@ -14,6 +14,7 @@ const (
TK_MONITOR TK_MONITOR
TK_GROUP TK_GROUP
TK_HOST TK_HOST
TK_CHECK
) )
type Parser struct { type Parser struct {
@ -31,6 +32,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
config := &AlrmConfig{} config := &AlrmConfig{}
var group *AlrmGroup var group *AlrmGroup
var host *AlrmHost var host *AlrmHost
var check AlrmCheck
scan := bufio.NewScanner(file) scan := bufio.NewScanner(file)
scan.Split(p.Split) scan.Split(p.Split)
@ -51,23 +53,26 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
case TK_SET: case TK_SET:
key := strings.ToLower(tk) key := strings.ToLower(tk)
if scan.Scan() { if !scan.Scan() {
return nil, fmt.Errorf("empty value name for set in %s, line %d",
fn, p.Line+1)
}
value := scan.Text() value := scan.Text()
switch key { switch key {
case "interval": case "interval":
config.Interval, err = strconv.Atoi(value) config.Interval, err = strconv.Atoi(value)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"invalid number for interval, line %d: \"%s\"", "invalid number for interval in %s, line %d: \"%s\"",
p.Line+1, value, fn, p.Line+1, value,
) )
} }
default: default:
return nil, fmt.Errorf("invalid key for set, line %d: \"%s\"", return nil, fmt.Errorf("unknown key for set in %s, line %d: \"%s\"",
p.Line+1, tk, fn, p.Line+1, tk,
) )
} }
}
p.prevState() p.prevState()
case TK_MONITOR: case TK_MONITOR:
@ -87,7 +92,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
case TK_GROUP: case TK_GROUP:
if group == nil { if group == nil {
return nil, fmt.Errorf("Group token without initialization") return nil, fmt.Errorf("group without initialization")
} }
switch strings.ToLower(tk) { switch strings.ToLower(tk) {
@ -108,7 +113,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
case TK_HOST: case TK_HOST:
if host == nil { if host == nil {
return nil, fmt.Errorf("Host token without initialization") return nil, fmt.Errorf("host token without initialization")
} }
if host.Name == "" { if host.Name == "" {
@ -118,15 +123,36 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
switch strings.ToLower(tk) { switch strings.ToLower(tk) {
case "address": case "address":
if scan.Scan() { if !scan.Scan() {
host.Address = scan.Text() 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: default:
p.prevState() p.prevState()
goto stateswitch 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: default:
return nil, fmt.Errorf("unknown parser state: %d", p.state()) return nil, fmt.Errorf("unknown parser state: %d", p.state())
} }
@ -169,6 +195,8 @@ func (p *Parser) stateName() string {
return "TK_GROUP" return "TK_GROUP"
case TK_HOST: case TK_HOST:
return "TK_HOST" return "TK_HOST"
case TK_CHECK:
return "TK_CHECK"
default: default:
return "UNKNOWN" return "UNKNOWN"
} }

View File

@ -8,5 +8,6 @@ monitor host stupid.com
monitor group webservers monitor group webservers
# Monitor host one # Monitor host one
host one.com address 10.12.121.1 host one.com address 10.12.121.1
check ping
# Monitor host two # Monitor host two
host two.com host two.com