Browse Source

added hash calculation during tokenization, removed threads variable

master
Christopher Ramey 3 years ago
parent
commit
d5389caadb
  1. 16
      config/config.go
  2. 10
      config/parser.go
  3. 10
      config/tokenizer.go
  4. 11
      server.go

16
config/config.go

@ -3,8 +3,6 @@ package config
import (
"fmt"
"git.binarythought.com/cdramey/alrm/alarm"
"runtime"
"strconv"
"time"
)
@ -12,15 +10,13 @@ type Config struct {
Groups map[string]*Group
Alarms map[string]alarm.Alarm
Interval time.Duration
Threads int
Hash string
}
func NewConfig() *Config {
return &Config{
// Default check interval, 30 seconds
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
}
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) {
parser := &Parser{DebugLevel: debuglvl}
config, err := parser.Parse(fn)

10
config/parser.go

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

10
config/tokenizer.go

@ -2,6 +2,9 @@ package config
import (
"bufio"
"crypto/sha256"
"fmt"
"hash"
"io"
"os"
"strings"
@ -16,6 +19,7 @@ const (
)
type Tokenizer struct {
Hash hash.Hash
curline int
repline int
file *os.File
@ -32,6 +36,7 @@ func NewTokenizer(fn string) (*Tokenizer, error) {
return nil, err
}
tk.Hash = sha256.New()
tk.reader = bufio.NewReader(tk.file)
return tk, nil
}
@ -53,6 +58,11 @@ func (t *Tokenizer) Scan() bool {
if t.err != nil {
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 {
case TK_NONE:

11
server.go

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

Loading…
Cancel
Save