[TASK] use golang-lib
This commit is contained in:
parent
2851513cfb
commit
5699de00cb
|
@ -9,6 +9,8 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"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/bot"
|
||||||
"github.com/genofire/logmania/lib"
|
"github.com/genofire/logmania/lib"
|
||||||
"github.com/genofire/logmania/notify"
|
"github.com/genofire/logmania/notify"
|
||||||
|
@ -22,6 +24,7 @@ var (
|
||||||
configPath string
|
configPath string
|
||||||
config *lib.Config
|
config *lib.Config
|
||||||
notifyState *configNotify.NotifyState
|
notifyState *configNotify.NotifyState
|
||||||
|
stateSaveWorker *worker.Worker
|
||||||
notifier notify.Notifier
|
notifier notify.Notifier
|
||||||
receiver receive.Receiver
|
receiver receive.Receiver
|
||||||
logChannel chan *log.Entry
|
logChannel chan *log.Entry
|
||||||
|
@ -37,13 +40,14 @@ var serverCmd = &cobra.Command{
|
||||||
log.SetFormatter(&log.TextFormatter{
|
log.SetFormatter(&log.TextFormatter{
|
||||||
DisableTimestamp: true,
|
DisableTimestamp: true,
|
||||||
})
|
})
|
||||||
config, err := lib.ReadConfig(configPath)
|
config := &lib.Config{}
|
||||||
|
err := file.ReadTOML(configPath, config)
|
||||||
if config == nil || err != nil {
|
if config == nil || err != nil {
|
||||||
log.Panicf("Could not load '%s' for configuration.", configPath)
|
log.Panicf("Could not load '%s' for configuration.", configPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
notifyState := configNotify.ReadStateFile(config.Notify.StateFile)
|
notifyState = configNotify.ReadStateFile(config.Notify.StateFile)
|
||||||
go notifyState.Saver(config.Notify.StateFile)
|
stateSaveWorker = file.NewSaveJSONWorker(time.Minute, config.Notify.StateFile, notifyState)
|
||||||
|
|
||||||
logmaniaBot = bot.NewBot(notifyState)
|
logmaniaBot = bot.NewBot(notifyState)
|
||||||
|
|
||||||
|
@ -87,6 +91,8 @@ var serverCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func quit() {
|
func quit() {
|
||||||
|
stateSaveWorker.Close()
|
||||||
|
file.SaveJSON(config.Notify.StateFile, notifyState)
|
||||||
receiver.Close()
|
receiver.Close()
|
||||||
notifier.Close()
|
notifier.Close()
|
||||||
log.Info("quit of logmania")
|
log.Info("quit of logmania")
|
||||||
|
@ -95,8 +101,9 @@ func quit() {
|
||||||
|
|
||||||
func reload() {
|
func reload() {
|
||||||
log.Info("reload config file")
|
log.Info("reload config file")
|
||||||
config, err := lib.ReadConfig(configPath)
|
var config lib.Config
|
||||||
if config == nil || err != nil {
|
err := file.ReadTOML(configPath, &config)
|
||||||
|
if err != nil {
|
||||||
log.Errorf("reload: could not load '%s' for new configuration. Skip reload.", configPath)
|
log.Errorf("reload: could not load '%s' for new configuration. Skip reload.", configPath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,5 @@
|
||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Struct of the configuration
|
// Struct of the configuration
|
||||||
// e.g. under github.com/genofire/logmania/logmania_example.conf
|
// e.g. under github.com/genofire/logmania/logmania_example.conf
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -44,15 +36,3 @@ type ReceiveConfig struct {
|
||||||
Address string `toml:"address"`
|
Address string `toml:"address"`
|
||||||
} `toml:"journald_json"`
|
} `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
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/genofire/golang-lib/file"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -72,8 +71,8 @@ func (state *NotifyState) AddRegex(to, expression string) error {
|
||||||
|
|
||||||
func ReadStateFile(path string) *NotifyState {
|
func ReadStateFile(path string) *NotifyState {
|
||||||
var state 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 {
|
if err := file.ReadJSON(path, &state); err == nil {
|
||||||
log.Infof("loaded %d hosts", len(state.HostTo))
|
log.Infof("loaded %d hosts", len(state.HostTo))
|
||||||
if state.Lastseen == nil {
|
if state.Lastseen == nil {
|
||||||
state.Lastseen = make(map[string]time.Time)
|
state.Lastseen = make(map[string]time.Time)
|
||||||
|
@ -91,9 +90,6 @@ func ReadStateFile(path string) *NotifyState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &state
|
return &state
|
||||||
} else {
|
|
||||||
log.Error("failed to unmarshal nodes:", err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
log.Error("failed to open state notify file: ", path, ":", err)
|
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) {
|
func (state *NotifyState) Alert(expired time.Duration, send func(e *log.Entry) error) {
|
||||||
c := time.Tick(time.Minute)
|
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