2020-08-15 17:16:52 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-08-16 04:19:06 -08:00
|
|
|
"strconv"
|
2020-08-15 17:16:52 -08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AlrmConfig struct {
|
2020-08-16 04:19:06 -08:00
|
|
|
Groups []*AlrmGroup
|
|
|
|
Interval int
|
2020-08-15 17:16:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AlrmConfig) NewGroup() *AlrmGroup {
|
|
|
|
group := &AlrmGroup{}
|
|
|
|
ac.Groups = append(ac.Groups, group)
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AlrmConfig) LastGroup() *AlrmGroup {
|
|
|
|
return ac.Groups[len(ac.Groups)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
type AlrmGroup struct {
|
|
|
|
Name string
|
|
|
|
Hosts []*AlrmHost
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ag *AlrmGroup) NewHost() *AlrmHost {
|
|
|
|
host := &AlrmHost{}
|
|
|
|
ag.Hosts = append(ag.Hosts, host)
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ag *AlrmGroup) LastHost() *AlrmHost {
|
|
|
|
return ag.Hosts[len(ag.Hosts)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
type AlrmHost struct {
|
|
|
|
Name string
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
TK_NONE = iota
|
2020-08-16 04:19:06 -08:00
|
|
|
TK_SET
|
2020-08-15 17:16:52 -08:00
|
|
|
TK_MONITOR
|
|
|
|
TK_GROUP
|
2020-08-15 17:53:54 -08:00
|
|
|
TK_HOST
|
2020-08-15 17:16:52 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func stateName(s int) string {
|
|
|
|
switch s {
|
|
|
|
case TK_NONE:
|
|
|
|
return "TK_NONE"
|
2020-08-16 04:19:06 -08:00
|
|
|
case TK_SET:
|
|
|
|
return "TK_SET"
|
2020-08-15 17:16:52 -08:00
|
|
|
case TK_MONITOR:
|
|
|
|
return "TK_MONTIOR"
|
|
|
|
case TK_GROUP:
|
|
|
|
return "TK_GROUP"
|
2020-08-15 17:53:54 -08:00
|
|
|
case TK_HOST:
|
|
|
|
return "TK_HOST"
|
2020-08-15 17:16:52 -08:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig(fn string) (*AlrmConfig, error) {
|
|
|
|
file, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot open config \"%s\": %s", fn, err.Error())
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
config := &AlrmConfig{}
|
|
|
|
parser := &Parser{}
|
|
|
|
|
|
|
|
scan := bufio.NewScanner(file)
|
|
|
|
scan.Split(parser.Split)
|
|
|
|
for scan.Scan() {
|
|
|
|
tk := scan.Text()
|
|
|
|
|
|
|
|
stateswitch:
|
2020-08-15 17:53:54 -08:00
|
|
|
switch parser.GetState() {
|
2020-08-15 17:16:52 -08:00
|
|
|
case TK_NONE:
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "monitor":
|
|
|
|
parser.SetState(TK_MONITOR)
|
2020-08-16 04:19:06 -08:00
|
|
|
case "set":
|
|
|
|
parser.SetState(TK_SET)
|
2020-08-15 17:16:52 -08:00
|
|
|
default:
|
2020-08-16 04:19:06 -08:00
|
|
|
return nil, fmt.Errorf("invalid token in %s, line %d: \"%s\"",
|
2020-08-15 18:32:45 -08:00
|
|
|
fn, parser.Line+1, tk)
|
2020-08-15 17:16:52 -08:00
|
|
|
}
|
|
|
|
|
2020-08-16 04:19:06 -08:00
|
|
|
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()
|
|
|
|
|
2020-08-15 17:16:52 -08:00
|
|
|
case TK_MONITOR:
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "host":
|
|
|
|
config.NewGroup().NewHost()
|
2020-08-15 17:53:54 -08:00
|
|
|
parser.SetState(TK_HOST)
|
2020-08-15 17:16:52 -08:00
|
|
|
|
|
|
|
case "group":
|
|
|
|
config.NewGroup()
|
|
|
|
parser.SetState(TK_GROUP)
|
|
|
|
|
|
|
|
default:
|
2020-08-15 17:53:54 -08:00
|
|
|
parser.PrevState()
|
2020-08-15 17:16:52 -08:00
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
|
|
|
case TK_GROUP:
|
|
|
|
group := config.LastGroup()
|
|
|
|
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "host":
|
|
|
|
group.NewHost()
|
2020-08-15 17:53:54 -08:00
|
|
|
parser.SetState(TK_HOST)
|
2020-08-15 17:16:52 -08:00
|
|
|
continue
|
|
|
|
|
|
|
|
default:
|
|
|
|
if group.Name == "" {
|
|
|
|
group.Name = tk
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:53:54 -08:00
|
|
|
parser.PrevState()
|
2020-08-15 17:16:52 -08:00
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:53:54 -08:00
|
|
|
case TK_HOST:
|
2020-08-15 17:16:52 -08:00
|
|
|
host := config.LastGroup().LastHost()
|
|
|
|
if host.Name == "" {
|
|
|
|
host.Name = tk
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(tk) {
|
|
|
|
case "address":
|
|
|
|
if scan.Scan() {
|
|
|
|
host.Address = scan.Text()
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2020-08-15 17:53:54 -08:00
|
|
|
parser.PrevState()
|
2020-08-15 17:16:52 -08:00
|
|
|
goto stateswitch
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2020-08-16 04:19:06 -08:00
|
|
|
return nil, fmt.Errorf("unknown parser state: %d", parser.GetState())
|
2020-08-15 17:16:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scan.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Parser struct {
|
2020-08-16 04:19:06 -08:00
|
|
|
Line int
|
2020-08-15 17:53:54 -08:00
|
|
|
states []int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *Parser) GetState() int {
|
|
|
|
if len(pr.states) < 1 {
|
|
|
|
return TK_NONE
|
|
|
|
}
|
2020-08-16 04:19:06 -08:00
|
|
|
return pr.states[len(pr.states)-1]
|
2020-08-15 17:16:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *Parser) SetState(state int) {
|
2020-08-15 17:53:54 -08:00
|
|
|
//fmt.Printf("%s -> %s\n", stateName(pr.GetState()), stateName(state))
|
|
|
|
pr.states = append(pr.states, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *Parser) PrevState() int {
|
|
|
|
if len(pr.states) > 0 {
|
2020-08-16 04:19:06 -08:00
|
|
|
pr.states = pr.states[:len(pr.states)-1]
|
2020-08-15 17:53:54 -08:00
|
|
|
}
|
|
|
|
return pr.GetState()
|
2020-08-15 17:16:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *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]
|
|
|
|
switch c {
|
|
|
|
case '\f', '\n', '\r':
|
|
|
|
pr.Line++
|
|
|
|
if ignoreline {
|
|
|
|
ignoreline = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case ' ', '\t', '\v':
|
|
|
|
if started && quote == 0 {
|
|
|
|
return i + 1, data[startidx:i], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case '\'', '"', '`':
|
|
|
|
if started && quote == c {
|
|
|
|
return i + 1, data[startidx:i], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if quote == 0 {
|
|
|
|
quote = c
|
|
|
|
}
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
if !started {
|
|
|
|
ignoreline = true
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if !ignoreline && !started {
|
|
|
|
started = true
|
|
|
|
startidx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if atEOF {
|
|
|
|
if ignoreline {
|
|
|
|
return len(data), nil, nil
|
|
|
|
}
|
|
|
|
if started {
|
|
|
|
return len(data), data[startidx:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|