Added set support to config

This commit is contained in:
Christopher Ramey 2020-08-16 04:19:06 -08:00
parent 60d83288df
commit 90325e9dd5
2 changed files with 37 additions and 8 deletions

View File

@ -4,11 +4,13 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
) )
type AlrmConfig struct { type AlrmConfig struct {
Groups []*AlrmGroup Groups []*AlrmGroup
Interval int
} }
func (ac *AlrmConfig) NewGroup() *AlrmGroup { func (ac *AlrmConfig) NewGroup() *AlrmGroup {
@ -43,6 +45,7 @@ type AlrmHost struct {
const ( const (
TK_NONE = iota TK_NONE = iota
TK_SET
TK_MONITOR TK_MONITOR
TK_GROUP TK_GROUP
TK_HOST TK_HOST
@ -52,6 +55,8 @@ func stateName(s int) string {
switch s { switch s {
case TK_NONE: case TK_NONE:
return "TK_NONE" return "TK_NONE"
case TK_SET:
return "TK_SET"
case TK_MONITOR: case TK_MONITOR:
return "TK_MONTIOR" return "TK_MONTIOR"
case TK_GROUP: case TK_GROUP:
@ -84,11 +89,34 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
switch strings.ToLower(tk) { switch strings.ToLower(tk) {
case "monitor": case "monitor":
parser.SetState(TK_MONITOR) parser.SetState(TK_MONITOR)
case "set":
parser.SetState(TK_SET)
default: default:
return nil, fmt.Errorf("Invalid token in %s, line %d: \"%s\"", return nil, fmt.Errorf("invalid token in %s, line %d: \"%s\"",
fn, parser.Line+1, tk) fn, parser.Line+1, tk)
} }
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\"",
parser.Line+1, value,
)
}
default:
return nil, fmt.Errorf("invalid key for set, line %d: \"%s\"",
parser.Line+1, tk,
)
}
}
parser.PrevState()
case TK_MONITOR: case TK_MONITOR:
switch strings.ToLower(tk) { switch strings.ToLower(tk) {
case "host": case "host":
@ -135,7 +163,6 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
if scan.Scan() { if scan.Scan() {
host.Address = scan.Text() host.Address = scan.Text()
} }
continue
default: default:
parser.PrevState() parser.PrevState()
@ -143,7 +170,7 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
} }
default: default:
return nil, fmt.Errorf("Unknown parser state: %d", parser.GetState()) return nil, fmt.Errorf("unknown parser state: %d", parser.GetState())
} }
} }
if err := scan.Err(); err != nil { if err := scan.Err(); err != nil {
@ -161,7 +188,7 @@ func (pr *Parser) GetState() int {
if len(pr.states) < 1 { if len(pr.states) < 1 {
return TK_NONE return TK_NONE
} }
return pr.states[len(pr.states) - 1] return pr.states[len(pr.states)-1]
} }
func (pr *Parser) SetState(state int) { func (pr *Parser) SetState(state int) {
@ -171,7 +198,7 @@ func (pr *Parser) SetState(state int) {
func (pr *Parser) PrevState() int { func (pr *Parser) PrevState() int {
if len(pr.states) > 0 { if len(pr.states) > 0 {
pr.states = pr.states[:len(pr.states) - 1] pr.states = pr.states[:len(pr.states)-1]
} }
return pr.GetState() return pr.GetState()
} }

View File

@ -1,3 +1,5 @@
set interval 30
# Address is optional, derived from host # Address is optional, derived from host
monitor host monkey.com monitor host monkey.com
@ -7,4 +9,4 @@ 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
# Monitor host two # Monitor host two
host two.com address host two.com