2021-01-01 09:26:49 -09:00
|
|
|
package config
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-03 05:59:31 -09:00
|
|
|
"git.binarythought.com/cdramey/alrm/alarm"
|
|
|
|
"git.binarythought.com/cdramey/alrm/check"
|
2020-08-20 04:34:22 -08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-18 07:29:38 -09:00
|
|
|
PR_NONE = iota
|
|
|
|
PR_SET
|
|
|
|
PR_MONITOR
|
|
|
|
PR_GROUP
|
|
|
|
PR_HOST
|
|
|
|
PR_CHECK
|
|
|
|
PR_ALARM
|
2020-08-20 04:34:22 -08:00
|
|
|
)
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
type parser struct {
|
|
|
|
config *Config
|
2021-01-17 09:04:08 -09:00
|
|
|
states []int
|
|
|
|
lastHost *Host
|
|
|
|
lastGroup *Group
|
|
|
|
lastCheck check.Check
|
|
|
|
lastAlarm alarm.Alarm
|
|
|
|
lastAlarmName string
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
func (p *parser) parse() error {
|
|
|
|
tok, err := NewTokenizer(p.config.Path)
|
2020-08-20 04:34:22 -08:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return err
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
2021-01-17 16:41:28 -09:00
|
|
|
defer tok.Close()
|
2020-08-20 04:34:22 -08:00
|
|
|
|
2021-01-17 16:41:28 -09:00
|
|
|
for tok.Scan() {
|
|
|
|
tk := tok.Text()
|
2020-08-20 04:34:22 -08:00
|
|
|
stateswitch:
|
|
|
|
switch p.state() {
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_NONE:
|
2020-08-20 04:34:22 -08:00
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "monitor":
|
2021-01-18 07:29:38 -09:00
|
|
|
p.setState(PR_MONITOR)
|
2020-08-20 04:34:22 -08:00
|
|
|
case "set":
|
2021-01-18 07:29:38 -09:00
|
|
|
p.setState(PR_SET)
|
2021-01-17 09:04:08 -09:00
|
|
|
case "alarm":
|
2021-01-18 07:29:38 -09:00
|
|
|
p.setState(PR_ALARM)
|
2020-08-20 04:34:22 -08:00
|
|
|
default:
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("invalid token in %s, line %d: \"%s\"",
|
|
|
|
p.config.Path, tok.Line(), tk)
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_SET:
|
2020-08-20 04:34:22 -08:00
|
|
|
key := strings.ToLower(tk)
|
2021-01-17 16:41:28 -09:00
|
|
|
if !tok.Scan() {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("empty value name for set in %s, line %d",
|
|
|
|
p.config.Path, tok.Line())
|
2020-08-20 08:44:56 -08:00
|
|
|
}
|
|
|
|
|
2021-01-17 16:41:28 -09:00
|
|
|
value := tok.Text()
|
2020-08-20 08:44:56 -08:00
|
|
|
switch key {
|
|
|
|
case "interval":
|
2021-02-27 19:14:39 -09:00
|
|
|
err := p.config.SetInterval(value)
|
2021-01-28 20:14:16 -09:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf(
|
2021-01-28 20:14:16 -09:00
|
|
|
"invalid duration for interval in %s, line %d: \"%s\"",
|
2021-02-27 19:14:39 -09:00
|
|
|
p.config.Path, tok.Line(), value,
|
2021-01-28 20:14:16 -09:00
|
|
|
)
|
|
|
|
}
|
2021-02-14 09:26:56 -09:00
|
|
|
case "listen":
|
2021-02-27 19:14:39 -09:00
|
|
|
p.config.Listen = value
|
|
|
|
case "api.key":
|
2021-03-01 07:26:27 -09:00
|
|
|
p.config.APIKey = []byte(value)
|
2021-02-27 19:14:39 -09:00
|
|
|
case "api.keyfile":
|
|
|
|
p.config.APIKeyFile = value
|
2020-08-20 08:44:56 -08:00
|
|
|
default:
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("unknown key for set in %s, line %d: \"%s\"",
|
|
|
|
p.config.Path, tok.Line(), tk,
|
2020-08-20 08:44:56 -08:00
|
|
|
)
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
p.prevState()
|
|
|
|
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_MONITOR:
|
2020-08-20 04:34:22 -08:00
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "host":
|
2021-01-18 07:29:38 -09:00
|
|
|
p.setState(PR_HOST)
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
case "group":
|
2021-01-18 07:29:38 -09:00
|
|
|
p.setState(PR_GROUP)
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
default:
|
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_GROUP:
|
2021-01-17 09:04:08 -09:00
|
|
|
if p.lastGroup == nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
p.lastGroup, err = p.config.NewGroup(tk)
|
2021-01-01 09:26:49 -09:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), p.config.Path, tok.Line(),
|
2021-01-01 09:26:49 -09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
continue
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "host":
|
2021-01-18 07:29:38 -09:00
|
|
|
p.setState(PR_HOST)
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
default:
|
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_HOST:
|
2021-01-01 09:26:49 -09:00
|
|
|
// If a host has no group, inherit the host name
|
2021-01-17 09:04:08 -09:00
|
|
|
if p.lastGroup == nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
p.lastGroup, err = p.config.NewGroup(tk)
|
2021-01-01 09:26:49 -09:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), p.config.Path, tok.Line(),
|
2021-01-01 09:26:49 -09:00
|
|
|
)
|
|
|
|
}
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
if p.lastHost == nil {
|
|
|
|
p.lastHost, err = p.lastGroup.NewHost(tk)
|
2021-01-01 09:26:49 -09:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), p.config.Path, tok.Line(),
|
2021-01-01 09:26:49 -09:00
|
|
|
)
|
|
|
|
}
|
2020-08-20 04:34:22 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "address":
|
2021-01-17 16:41:28 -09:00
|
|
|
if !tok.Scan() {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("empty address for host in %s, line %d",
|
|
|
|
p.config.Path, tok.Line())
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
2021-01-17 16:41:28 -09:00
|
|
|
p.lastHost.Address = tok.Text()
|
2020-08-20 08:44:56 -08:00
|
|
|
|
|
|
|
case "check":
|
2021-01-18 07:29:38 -09:00
|
|
|
p.setState(PR_CHECK)
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
default:
|
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_CHECK:
|
2021-01-17 09:04:08 -09:00
|
|
|
if p.lastCheck == nil {
|
|
|
|
p.lastCheck, err = p.lastHost.NewCheck(tk)
|
2020-08-20 08:44:56 -08:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), p.config.Path, tok.Line())
|
2020-08-20 08:44:56 -08:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2021-01-17 09:04:08 -09:00
|
|
|
cont, err := p.lastCheck.Parse(tk)
|
2020-12-21 05:03:03 -09:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), p.config.Path, tok.Line())
|
2020-12-21 05:03:03 -09:00
|
|
|
}
|
|
|
|
if !cont {
|
2021-01-17 09:04:08 -09:00
|
|
|
p.lastCheck = nil
|
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_ALARM:
|
2021-01-17 09:04:08 -09:00
|
|
|
if p.lastAlarm == nil {
|
|
|
|
if p.lastAlarmName == "" {
|
|
|
|
p.lastAlarmName = tk
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
p.lastAlarm, err = p.config.NewAlarm(p.lastAlarmName, tk)
|
2021-01-17 09:04:08 -09:00
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), p.config.Path, tok.Line())
|
2021-01-17 09:04:08 -09:00
|
|
|
}
|
|
|
|
p.lastAlarmName = ""
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cont, err := p.lastAlarm.Parse(tk)
|
|
|
|
if err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), p.config.Path, tok.Line())
|
2021-01-17 09:04:08 -09:00
|
|
|
}
|
|
|
|
if !cont {
|
|
|
|
p.lastAlarm = nil
|
2020-12-21 05:03:03 -09:00
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
2020-08-20 08:44:56 -08:00
|
|
|
|
2020-08-20 04:34:22 -08:00
|
|
|
default:
|
2021-02-27 19:14:39 -09:00
|
|
|
return fmt.Errorf("unknown parser state: %d", p.state())
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
}
|
2021-01-17 16:41:28 -09:00
|
|
|
if err := tok.Err(); err != nil {
|
2021-02-27 19:14:39 -09:00
|
|
|
return err
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
2021-02-27 19:14:39 -09:00
|
|
|
return nil
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
func (p *parser) state() int {
|
2020-08-20 04:34:22 -08:00
|
|
|
if len(p.states) < 1 {
|
2021-01-18 07:29:38 -09:00
|
|
|
return PR_NONE
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
return p.states[len(p.states)-1]
|
|
|
|
}
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
func (p *parser) setState(state int) {
|
2021-01-01 09:26:49 -09:00
|
|
|
switch state {
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_SET, PR_MONITOR:
|
2021-01-01 09:26:49 -09:00
|
|
|
fallthrough
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_GROUP:
|
2021-01-17 09:04:08 -09:00
|
|
|
p.lastGroup = nil
|
2021-01-01 09:26:49 -09:00
|
|
|
fallthrough
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_HOST:
|
2021-01-17 09:04:08 -09:00
|
|
|
p.lastHost = nil
|
|
|
|
p.lastCheck = nil
|
2021-01-01 09:26:49 -09:00
|
|
|
}
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
if p.config.DebugLevel > 1 {
|
2021-01-01 09:26:49 -09:00
|
|
|
fmt.Printf("Parser state: %s", p.stateName())
|
|
|
|
}
|
2020-08-20 04:34:22 -08:00
|
|
|
p.states = append(p.states, state)
|
2021-02-27 19:14:39 -09:00
|
|
|
if p.config.DebugLevel > 1 {
|
2021-01-01 09:26:49 -09:00
|
|
|
fmt.Printf(" -> %s\n", p.stateName())
|
|
|
|
}
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
func (p *parser) prevState() int {
|
2020-08-20 04:34:22 -08:00
|
|
|
if len(p.states) > 0 {
|
|
|
|
p.states = p.states[:len(p.states)-1]
|
|
|
|
}
|
|
|
|
return p.state()
|
|
|
|
}
|
|
|
|
|
2021-02-27 19:14:39 -09:00
|
|
|
func (p *parser) stateName() string {
|
2020-08-20 04:34:22 -08:00
|
|
|
switch p.state() {
|
2021-01-18 07:29:38 -09:00
|
|
|
case PR_NONE:
|
|
|
|
return "PR_NONE"
|
|
|
|
case PR_SET:
|
|
|
|
return "PR_SET"
|
|
|
|
case PR_MONITOR:
|
|
|
|
return "PR_MONITOR"
|
|
|
|
case PR_GROUP:
|
|
|
|
return "PR_GROUP"
|
|
|
|
case PR_HOST:
|
|
|
|
return "PR_HOST"
|
|
|
|
case PR_CHECK:
|
|
|
|
return "PR_CHECK"
|
|
|
|
case PR_ALARM:
|
|
|
|
return "PR_ALARM"
|
2020-08-20 04:34:22 -08:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|