logmania/cmd/logmania/main.go

109 lines
2.4 KiB
Go
Raw Normal View History

2017-06-13 00:21:19 +02:00
// logmania Server
//
// reload config with SIGUSR1
//
// Usage of logmania:
// -config string
// config file (default "logmania.conf")
// -debug
// enable debuging
2017-06-11 03:34:11 +02:00
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"github.com/genofire/logmania/bot"
2017-06-11 03:34:11 +02:00
"github.com/genofire/logmania/lib"
2017-08-09 08:45:45 +02:00
log "github.com/genofire/logmania/log"
2017-06-16 10:33:35 +02:00
"github.com/genofire/logmania/notify"
2017-08-09 08:45:45 +02:00
allNotify "github.com/genofire/logmania/notify/all"
configNotify "github.com/genofire/logmania/notify/config"
2017-08-09 08:45:45 +02:00
"github.com/genofire/logmania/receive"
allReceiver "github.com/genofire/logmania/receive/all"
2017-06-11 03:34:11 +02:00
)
var (
configPath string
config *lib.Config
notifyState *configNotify.NotifyState
notifier notify.Notifier
receiver receive.Receiver
logChannel chan *log.Entry
logmaniaBot *bot.Bot
2017-06-11 03:34:11 +02:00
)
func main() {
flag.StringVar(&configPath, "config", "logmania.conf", "config file")
2017-06-12 22:32:27 +02:00
flag.Parse()
2017-06-11 03:34:11 +02:00
config, err := lib.ReadConfig(configPath)
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)
logmaniaBot = bot.NewBot(notifyState)
notifier = allNotify.Init(&config.Notify, notifyState, logmaniaBot)
2017-08-09 08:45:45 +02:00
log.Save = notifier.Send
logChannel = make(chan *log.Entry)
2017-08-09 08:45:45 +02:00
go func() {
for a := range logChannel {
log.Save(a)
2017-08-09 08:45:45 +02:00
}
}()
2017-06-12 22:32:27 +02:00
2017-08-13 12:12:46 +02:00
go notifyState.Alert(config.Notify.AlertCheck.Duration, log.Save)
log.Info("starting logmania")
2017-08-09 08:45:45 +02:00
receiver = allReceiver.Init(&config.Receive, logChannel)
2017-06-16 10:33:35 +02:00
2017-08-09 08:45:45 +02:00
go receiver.Listen()
2017-06-12 22:32:27 +02:00
2017-06-11 03:34:11 +02:00
// Wait for system signal
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGUSR1)
for sig := range sigchan {
switch sig {
case syscall.SIGTERM:
2017-08-09 08:45:45 +02:00
log.Panic("terminated of logmania")
2017-06-11 03:34:11 +02:00
os.Exit(0)
case syscall.SIGQUIT:
quit()
case syscall.SIGHUP:
quit()
case syscall.SIGUSR1:
reload()
}
}
}
func quit() {
2017-08-09 08:45:45 +02:00
receiver.Close()
2017-06-16 10:33:35 +02:00
notifier.Close()
2017-06-11 03:34:11 +02:00
log.Info("quit of logmania")
os.Exit(0)
}
func reload() {
log.Info("reload config file")
config, err := lib.ReadConfig(configPath)
if config == nil || err != nil {
2017-06-12 22:32:27 +02:00
log.Errorf("reload: could not load '%s' for new configuration. Skip reload.", configPath)
2017-06-11 03:34:11 +02:00
return
}
2017-08-09 08:45:45 +02:00
receiver.Close()
receiver = allReceiver.Init(&config.Receive, logChannel)
go receiver.Listen()
notifier.Close()
notifier = allNotify.Init(&config.Notify, notifyState, logmaniaBot)
2017-06-11 03:34:11 +02:00
}