2021-01-01 09:26:49 -09:00
|
|
|
package config
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
import (
|
2021-01-17 09:04:08 -09:00
|
|
|
"alrm/alarm"
|
2021-01-01 09:26:49 -09:00
|
|
|
"alrm/check"
|
2020-08-20 04:34:22 -08:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TK_NONE = iota
|
|
|
|
TK_SET
|
|
|
|
TK_MONITOR
|
|
|
|
TK_GROUP
|
|
|
|
TK_HOST
|
2020-08-20 08:44:56 -08:00
|
|
|
TK_CHECK
|
2021-01-17 09:04:08 -09:00
|
|
|
TK_ALARM
|
2020-08-20 04:34:22 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Parser struct {
|
2021-01-17 09:04:08 -09:00
|
|
|
DebugLevel int
|
|
|
|
Line int
|
|
|
|
states []int
|
|
|
|
lastHost *Host
|
|
|
|
lastGroup *Group
|
|
|
|
lastCheck check.Check
|
|
|
|
lastAlarm alarm.Alarm
|
|
|
|
lastAlarmName string
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:04:08 -09:00
|
|
|
func (p *Parser) Parse(fn string) (*Config, error) {
|
2020-08-20 04:34:22 -08:00
|
|
|
file, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot open config \"%s\": %s", fn, err.Error())
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2021-01-17 05:24:13 -09:00
|
|
|
config := NewConfig()
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
scan := bufio.NewScanner(file)
|
|
|
|
scan.Split(p.Split)
|
|
|
|
for scan.Scan() {
|
|
|
|
tk := scan.Text()
|
|
|
|
stateswitch:
|
|
|
|
switch p.state() {
|
|
|
|
case TK_NONE:
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "monitor":
|
|
|
|
p.setState(TK_MONITOR)
|
|
|
|
case "set":
|
|
|
|
p.setState(TK_SET)
|
2021-01-17 09:04:08 -09:00
|
|
|
case "alarm":
|
|
|
|
p.setState(TK_ALARM)
|
2020-08-20 04:34:22 -08:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid token in %s, line %d: \"%s\"",
|
2021-01-02 13:21:57 -09:00
|
|
|
fn, p.Line, tk)
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
case TK_SET:
|
|
|
|
key := strings.ToLower(tk)
|
2020-08-20 08:44:56 -08:00
|
|
|
if !scan.Scan() {
|
|
|
|
return nil, fmt.Errorf("empty value name for set in %s, line %d",
|
2021-01-02 13:21:57 -09:00
|
|
|
fn, p.Line)
|
2020-08-20 08:44:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
value := scan.Text()
|
|
|
|
switch key {
|
|
|
|
case "interval":
|
2021-01-01 09:26:49 -09:00
|
|
|
err := config.SetInterval(value)
|
2020-08-20 08:44:56 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"invalid number for interval in %s, line %d: \"%s\"",
|
2021-01-02 13:21:57 -09:00
|
|
|
fn, p.Line, value,
|
2020-08-20 04:34:22 -08:00
|
|
|
)
|
|
|
|
}
|
2020-08-20 08:44:56 -08:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown key for set in %s, line %d: \"%s\"",
|
2021-01-02 13:21:57 -09:00
|
|
|
fn, p.Line, tk,
|
2020-08-20 08:44:56 -08:00
|
|
|
)
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
p.prevState()
|
|
|
|
|
|
|
|
case TK_MONITOR:
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "host":
|
|
|
|
p.setState(TK_HOST)
|
|
|
|
|
|
|
|
case "group":
|
|
|
|
p.setState(TK_GROUP)
|
|
|
|
|
|
|
|
default:
|
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
|
|
|
case TK_GROUP:
|
2021-01-17 09:04:08 -09:00
|
|
|
if p.lastGroup == nil {
|
|
|
|
p.lastGroup, err = config.NewGroup(tk)
|
2021-01-01 09:26:49 -09:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s in %s, line %d",
|
2021-01-02 13:21:57 -09:00
|
|
|
err.Error(), fn, p.Line,
|
2021-01-01 09:26:49 -09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
continue
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "host":
|
|
|
|
p.setState(TK_HOST)
|
|
|
|
|
|
|
|
default:
|
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
|
|
|
case TK_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 {
|
|
|
|
p.lastGroup, err = config.NewGroup(tk)
|
2021-01-01 09:26:49 -09:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s in %s, line %d",
|
2021-01-02 13:21:57 -09:00
|
|
|
err.Error(), fn, p.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 {
|
|
|
|
return nil, fmt.Errorf("%s in %s, line %d",
|
2021-01-02 13:21:57 -09:00
|
|
|
err.Error(), fn, p.Line,
|
2021-01-01 09:26:49 -09:00
|
|
|
)
|
|
|
|
}
|
2020-08-20 04:34:22 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "address":
|
2020-08-20 08:44:56 -08:00
|
|
|
if !scan.Scan() {
|
|
|
|
return nil, fmt.Errorf("empty address for host in %s, line %d",
|
2021-01-02 13:21:57 -09:00
|
|
|
fn, p.Line)
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
2021-01-17 09:04:08 -09:00
|
|
|
p.lastHost.Address = scan.Text()
|
2020-08-20 08:44:56 -08:00
|
|
|
|
|
|
|
case "check":
|
|
|
|
p.setState(TK_CHECK)
|
2020-08-20 04:34:22 -08:00
|
|
|
|
|
|
|
default:
|
|
|
|
p.prevState()
|
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:44:56 -08:00
|
|
|
case TK_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 {
|
|
|
|
return nil, fmt.Errorf("%s in %s, line %d",
|
2021-01-02 13:21:57 -09:00
|
|
|
err.Error(), fn, p.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-01-02 13:21:57 -09:00
|
|
|
return nil, fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), fn, p.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
|
|
|
|
}
|
|
|
|
|
|
|
|
case TK_ALARM:
|
|
|
|
if p.lastAlarm == nil {
|
|
|
|
if p.lastAlarmName == "" {
|
|
|
|
p.lastAlarmName = tk
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p.lastAlarm, err = config.NewAlarm(p.lastAlarmName, tk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), fn, p.Line)
|
|
|
|
}
|
|
|
|
p.lastAlarmName = ""
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cont, err := p.lastAlarm.Parse(tk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s in %s, line %d",
|
|
|
|
err.Error(), fn, p.Line)
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
return nil, fmt.Errorf("unknown parser state: %d", p.state())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scan.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) state() int {
|
|
|
|
if len(p.states) < 1 {
|
|
|
|
return TK_NONE
|
|
|
|
}
|
|
|
|
return p.states[len(p.states)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) setState(state int) {
|
2021-01-01 09:26:49 -09:00
|
|
|
switch state {
|
|
|
|
case TK_SET, TK_MONITOR:
|
|
|
|
fallthrough
|
|
|
|
case TK_GROUP:
|
2021-01-17 09:04:08 -09:00
|
|
|
p.lastGroup = nil
|
2021-01-01 09:26:49 -09:00
|
|
|
fallthrough
|
|
|
|
case TK_HOST:
|
2021-01-17 09:04:08 -09:00
|
|
|
p.lastHost = nil
|
|
|
|
p.lastCheck = nil
|
2021-01-01 09:26:49 -09:00
|
|
|
}
|
|
|
|
|
2021-01-11 06:28:14 -09:00
|
|
|
if p.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-01-11 06:28:14 -09:00
|
|
|
if p.DebugLevel > 1 {
|
2021-01-01 09:26:49 -09:00
|
|
|
fmt.Printf(" -> %s\n", p.stateName())
|
|
|
|
}
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) prevState() int {
|
|
|
|
if len(p.states) > 0 {
|
|
|
|
p.states = p.states[:len(p.states)-1]
|
|
|
|
}
|
|
|
|
return p.state()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) stateName() string {
|
|
|
|
switch p.state() {
|
|
|
|
case TK_NONE:
|
|
|
|
return "TK_NONE"
|
|
|
|
case TK_SET:
|
|
|
|
return "TK_SET"
|
|
|
|
case TK_MONITOR:
|
|
|
|
return "TK_MONTIOR"
|
|
|
|
case TK_GROUP:
|
|
|
|
return "TK_GROUP"
|
|
|
|
case TK_HOST:
|
|
|
|
return "TK_HOST"
|
2020-08-20 08:44:56 -08:00
|
|
|
case TK_CHECK:
|
|
|
|
return "TK_CHECK"
|
2021-01-17 09:04:08 -09:00
|
|
|
case TK_ALARM:
|
|
|
|
return "TK_ALARM"
|
2020-08-20 04:34:22 -08:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) Split(data []byte, atEOF bool) (int, []byte, error) {
|
|
|
|
if atEOF && len(data) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ignoreline bool
|
|
|
|
var started bool
|
|
|
|
var startidx int
|
|
|
|
var quote byte
|
|
|
|
|
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
c := data[i]
|
2021-01-17 09:44:39 -09:00
|
|
|
//fmt.Printf("%c (%t) (%t)\n", c, started, ignoreline)
|
2020-08-20 04:34:22 -08:00
|
|
|
switch c {
|
|
|
|
case '\f', '\n', '\r':
|
|
|
|
p.Line++
|
|
|
|
if ignoreline {
|
|
|
|
ignoreline = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case ' ', '\t', '\v':
|
|
|
|
if started && quote == 0 {
|
|
|
|
return i + 1, data[startidx:i], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case '\'', '"', '`':
|
2021-01-17 09:44:39 -09:00
|
|
|
// When the quote ends
|
|
|
|
if quote == c {
|
|
|
|
// if we've gotten data, return it
|
|
|
|
if started {
|
|
|
|
return i + 1, data[startidx:i], nil
|
|
|
|
}
|
|
|
|
// if we haven't return nothing
|
|
|
|
return i + 1, []byte{}, nil
|
2020-08-20 04:34:22 -08:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:44:39 -09:00
|
|
|
// start a quoted string
|
2020-12-21 05:03:03 -09:00
|
|
|
if !ignoreline && quote == 0 {
|
2020-08-20 04:34:22 -08:00
|
|
|
quote = c
|
|
|
|
}
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
if !started {
|
|
|
|
ignoreline = true
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if !ignoreline && !started {
|
|
|
|
started = true
|
|
|
|
startidx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if atEOF {
|
2021-01-17 09:44:39 -09:00
|
|
|
if quote != 0 {
|
|
|
|
return 0, nil, fmt.Errorf("unterminated quote")
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:34:22 -08:00
|
|
|
if ignoreline {
|
|
|
|
return len(data), nil, nil
|
|
|
|
}
|
|
|
|
if started {
|
|
|
|
return len(data), data[startidx:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|