[TASK] use golang-lib

This commit is contained in:
Martin Geno 2017-10-27 13:07:12 +02:00
parent 2851513cfb
commit 5699de00cb
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 36 additions and 81 deletions

View File

@ -9,6 +9,8 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/genofire/golang-lib/file"
"github.com/genofire/golang-lib/worker"
"github.com/genofire/logmania/bot"
"github.com/genofire/logmania/lib"
"github.com/genofire/logmania/notify"
@ -19,13 +21,14 @@ import (
)
var (
configPath string
config *lib.Config
notifyState *configNotify.NotifyState
notifier notify.Notifier
receiver receive.Receiver
logChannel chan *log.Entry
logmaniaBot *bot.Bot
configPath string
config *lib.Config
notifyState *configNotify.NotifyState
stateSaveWorker *worker.Worker
notifier notify.Notifier
receiver receive.Receiver
logChannel chan *log.Entry
logmaniaBot *bot.Bot
)
// serverCmd represents the serve command
@ -37,13 +40,14 @@ var serverCmd = &cobra.Command{
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
config, err := lib.ReadConfig(configPath)
config := &lib.Config{}
err := file.ReadTOML(configPath, config)
if config == nil || err != nil {
log.Panicf("Could not load '%s' for configuration.", configPath)
}
notifyState := configNotify.ReadStateFile(config.Notify.StateFile)
go notifyState.Saver(config.Notify.StateFile)
notifyState = configNotify.ReadStateFile(config.Notify.StateFile)
stateSaveWorker = file.NewSaveJSONWorker(time.Minute, config.Notify.StateFile, notifyState)
logmaniaBot = bot.NewBot(notifyState)
@ -87,6 +91,8 @@ var serverCmd = &cobra.Command{
}
func quit() {
stateSaveWorker.Close()
file.SaveJSON(config.Notify.StateFile, notifyState)
receiver.Close()
notifier.Close()
log.Info("quit of logmania")
@ -95,8 +101,9 @@ func quit() {
func reload() {
log.Info("reload config file")
config, err := lib.ReadConfig(configPath)
if config == nil || err != nil {
var config lib.Config
err := file.ReadTOML(configPath, &config)
if err != nil {
log.Errorf("reload: could not load '%s' for new configuration. Skip reload.", configPath)
return
}

View File

@ -1,13 +1,5 @@
package lib
import (
"io/ioutil"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
// Struct of the configuration
// e.g. under github.com/genofire/logmania/logmania_example.conf
type Config struct {
@ -44,15 +36,3 @@ type ReceiveConfig struct {
Address string `toml:"address"`
} `toml:"journald_json"`
}
// read configuration from a file (use toml as file-format)
func ReadConfig(path string) (*Config, error) {
log.Infof("load of configfile: %s", path)
var config Config
file, _ := ioutil.ReadFile(path)
err := toml.Unmarshal(file, &config)
if err != nil {
return nil, err
}
return &config, nil
}

View File

@ -1,11 +1,10 @@
package config
import (
"encoding/json"
"os"
"regexp"
"time"
"github.com/genofire/golang-lib/file"
log "github.com/sirupsen/logrus"
)
@ -72,28 +71,25 @@ func (state *NotifyState) AddRegex(to, expression string) error {
func ReadStateFile(path string) *NotifyState {
var state NotifyState
if f, err := os.Open(path); err == nil { // transform data to legacy meshviewer
if err = json.NewDecoder(f).Decode(&state); err == nil {
log.Infof("loaded %d hosts", len(state.HostTo))
if state.Lastseen == nil {
state.Lastseen = make(map[string]time.Time)
}
if state.LastseenNotify == nil {
state.LastseenNotify = make(map[string]time.Time)
}
if state.RegexIn == nil {
state.RegexIn = make(map[string]map[string]*regexp.Regexp)
} else {
for to, regexs := range state.RegexIn {
for exp, _ := range regexs {
state.AddRegex(to, exp)
}
if err := file.ReadJSON(path, &state); err == nil {
log.Infof("loaded %d hosts", len(state.HostTo))
if state.Lastseen == nil {
state.Lastseen = make(map[string]time.Time)
}
if state.LastseenNotify == nil {
state.LastseenNotify = make(map[string]time.Time)
}
if state.RegexIn == nil {
state.RegexIn = make(map[string]map[string]*regexp.Regexp)
} else {
for to, regexs := range state.RegexIn {
for exp, _ := range regexs {
state.AddRegex(to, exp)
}
}
return &state
} else {
log.Error("failed to unmarshal nodes:", err)
}
return &state
} else {
log.Error("failed to open state notify file: ", path, ":", err)
}
@ -107,14 +103,6 @@ func ReadStateFile(path string) *NotifyState {
}
}
func (state *NotifyState) Saver(path string) {
c := time.Tick(time.Minute)
for range c {
state.SaveJSON(path)
}
}
func (state *NotifyState) Alert(expired time.Duration, send func(e *log.Entry) error) {
c := time.Tick(time.Minute)
@ -134,23 +122,3 @@ func (state *NotifyState) Alert(expired time.Duration, send func(e *log.Entry) e
}
}
}
// SaveJSON to path
func (state *NotifyState) SaveJSON(outputFile string) {
tmpFile := outputFile + ".tmp"
f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Panic(err)
}
err = json.NewEncoder(f).Encode(state)
if err != nil {
log.Panic(err)
}
f.Close()
if err := os.Rename(tmpFile, outputFile); err != nil {
log.Panic(err)
}
}