[TASK] use golang-lib
This commit is contained in:
parent
2851513cfb
commit
5699de00cb
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue