added hash calculation during tokenization, removed threads variable

This commit is contained in:
Christopher Ramey 2021-02-09 04:05:16 -09:00
parent 534baa3ccb
commit d5389caadb
4 changed files with 21 additions and 26 deletions

View File

@ -3,8 +3,6 @@ package config
import ( import (
"fmt" "fmt"
"git.binarythought.com/cdramey/alrm/alarm" "git.binarythought.com/cdramey/alrm/alarm"
"runtime"
"strconv"
"time" "time"
) )
@ -12,15 +10,13 @@ type Config struct {
Groups map[string]*Group Groups map[string]*Group
Alarms map[string]alarm.Alarm Alarms map[string]alarm.Alarm
Interval time.Duration Interval time.Duration
Threads int Hash string
} }
func NewConfig() *Config { func NewConfig() *Config {
return &Config{ return &Config{
// Default check interval, 30 seconds // Default check interval, 30 seconds
Interval: time.Second * 30, Interval: time.Second * 30,
// Default number of threads, use local CPU count
Threads: runtime.NumCPU(),
} }
} }
@ -66,16 +62,6 @@ func (c *Config) SetInterval(val string) error {
return nil return nil
} }
func (c *Config) SetThreads(val string) error {
threads, err := strconv.Atoi(val)
if err != nil {
return err
}
c.Threads = threads
return nil
}
func ReadConfig(fn string, debuglvl int) (*Config, error) { func ReadConfig(fn string, debuglvl int) (*Config, error) {
parser := &Parser{DebugLevel: debuglvl} parser := &Parser{DebugLevel: debuglvl}
config, err := parser.Parse(fn) config, err := parser.Parse(fn)

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"encoding/hex"
"fmt" "fmt"
"git.binarythought.com/cdramey/alrm/alarm" "git.binarythought.com/cdramey/alrm/alarm"
"git.binarythought.com/cdramey/alrm/check" "git.binarythought.com/cdramey/alrm/check"
@ -69,14 +70,6 @@ func (p *Parser) Parse(fn string) (*Config, error) {
fn, tok.Line(), value, fn, tok.Line(), value,
) )
} }
case "threads":
err := config.SetThreads(value)
if err != nil {
return nil, fmt.Errorf(
"invalid number for interval in %s, line %d: \"%s\"",
fn, tok.Line(), value,
)
}
default: default:
return nil, fmt.Errorf("unknown key for set in %s, line %d: \"%s\"", return nil, fmt.Errorf("unknown key for set in %s, line %d: \"%s\"",
fn, tok.Line(), tk, fn, tok.Line(), tk,
@ -207,6 +200,7 @@ func (p *Parser) Parse(fn string) (*Config, error) {
if err := tok.Err(); err != nil { if err := tok.Err(); err != nil {
return nil, err return nil, err
} }
config.Hash = hex.EncodeToString(tok.Hash.Sum(nil))
return config, nil return config, nil
} }

View File

@ -2,6 +2,9 @@ package config
import ( import (
"bufio" "bufio"
"crypto/sha256"
"fmt"
"hash"
"io" "io"
"os" "os"
"strings" "strings"
@ -16,6 +19,7 @@ const (
) )
type Tokenizer struct { type Tokenizer struct {
Hash hash.Hash
curline int curline int
repline int repline int
file *os.File file *os.File
@ -32,6 +36,7 @@ func NewTokenizer(fn string) (*Tokenizer, error) {
return nil, err return nil, err
} }
tk.Hash = sha256.New()
tk.reader = bufio.NewReader(tk.file) tk.reader = bufio.NewReader(tk.file)
return tk, nil return tk, nil
} }
@ -53,6 +58,11 @@ func (t *Tokenizer) Scan() bool {
if t.err != nil { if t.err != nil {
break break
} }
if r == unicode.ReplacementChar {
t.err = fmt.Errorf("invalid utf-8 encoding on line %s", t.repline)
break
}
t.Hash.Write([]byte(string(r)))
switch state { switch state {
case TK_NONE: case TK_NONE:

View File

@ -3,13 +3,12 @@ package main
import ( import (
"fmt" "fmt"
"git.binarythought.com/cdramey/alrm/config" "git.binarythought.com/cdramey/alrm/config"
"time"
"sync" "sync"
"time"
) )
func startServer(cfg *config.Config, debuglvl int) error { func startServer(cfg *config.Config, debuglvl int) error {
m := sync.Mutex{} c := sync.NewCond(&sync.Mutex{})
c := sync.NewCond(&m)
for _, g := range cfg.Groups { for _, g := range cfg.Groups {
go worker(g, c, debuglvl) go worker(g, c, debuglvl)
} }
@ -30,9 +29,15 @@ func startServer(cfg *config.Config, debuglvl int) error {
func worker(g *config.Group, c *sync.Cond, debuglvl int) { func worker(g *config.Group, c *sync.Cond, debuglvl int) {
for { for {
if debuglvl > 2 {
fmt.Printf("%s goroutine waiting.. \n", g.Name)
}
c.L.Lock() c.L.Lock()
c.Wait() c.Wait()
c.L.Unlock() c.L.Unlock()
if debuglvl > 2 {
fmt.Printf("%s goroutine wake.. \n", g.Name)
}
for _, h := range g.Hosts { for _, h := range g.Hosts {
for _, c := range h.Checks { for _, c := range h.Checks {