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 {
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) {

View File

@ -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,23 +53,26 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
case TK_SET:
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()
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,
"invalid number for interval in %s, line %d: \"%s\"",
fn, p.Line+1, value,
)
}
default:
return nil, fmt.Errorf("invalid key for set, line %d: \"%s\"",
p.Line+1, tk,
return nil, fmt.Errorf("unknown key for set in %s, line %d: \"%s\"",
fn, p.Line+1, tk,
)
}
}
p.prevState()
case TK_MONITOR:
@ -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"
}

View File

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