2017-10-25 00:36:16 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2019-06-20 09:25:43 +02:00
|
|
|
"github.com/bdlm/log"
|
2017-10-25 00:36:16 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-04-13 16:44:23 +02:00
|
|
|
"dev.sum7.eu/genofire/golang-lib/file"
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/worker"
|
2019-08-08 15:51:55 +02:00
|
|
|
"dev.sum7.eu/sum7/logmania/bot"
|
|
|
|
"dev.sum7.eu/sum7/logmania/database"
|
|
|
|
"dev.sum7.eu/sum7/logmania/input"
|
|
|
|
allInput "dev.sum7.eu/sum7/logmania/input/all"
|
|
|
|
"dev.sum7.eu/sum7/logmania/lib"
|
|
|
|
"dev.sum7.eu/sum7/logmania/output"
|
|
|
|
allOutput "dev.sum7.eu/sum7/logmania/output/all"
|
2017-10-25 00:36:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-11-10 18:57:36 +01:00
|
|
|
configPath string
|
|
|
|
config *lib.Config
|
|
|
|
db *database.DB
|
|
|
|
dbSaveWorker *worker.Worker
|
2018-09-05 01:53:23 +02:00
|
|
|
out output.Output
|
|
|
|
in input.Input
|
2017-11-10 18:57:36 +01:00
|
|
|
logChannel chan *log.Entry
|
|
|
|
logmaniaBot *bot.Bot
|
2017-10-25 00:36:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// serverCmd represents the serve command
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
Use: "server",
|
|
|
|
Short: "Runs the logmania server",
|
|
|
|
Example: "logmania server --config /etc/yanic.toml",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
log.SetFormatter(&log.TextFormatter{
|
|
|
|
DisableTimestamp: true,
|
|
|
|
})
|
2017-10-27 13:07:12 +02:00
|
|
|
config := &lib.Config{}
|
|
|
|
err := file.ReadTOML(configPath, config)
|
2017-10-25 00:36:16 +02:00
|
|
|
if config == nil || err != nil {
|
|
|
|
log.Panicf("Could not load '%s' for configuration.", configPath)
|
|
|
|
}
|
2018-09-06 13:36:25 +02:00
|
|
|
if config.Debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
2017-10-25 00:36:16 +02:00
|
|
|
|
2017-11-10 18:57:36 +01:00
|
|
|
db = database.ReadDBFile(config.DB)
|
2018-04-18 14:32:28 +02:00
|
|
|
go func() { dbSaveWorker = file.NewSaveJSONWorker(time.Minute, config.DB, db) }()
|
2017-10-25 00:36:16 +02:00
|
|
|
|
2017-11-10 18:57:36 +01:00
|
|
|
logmaniaBot = bot.NewBot(db)
|
2017-10-25 00:36:16 +02:00
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
out = allOutput.Init(config.Output, db, logmaniaBot)
|
2017-10-25 00:36:16 +02:00
|
|
|
logChannel = make(chan *log.Entry)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for a := range logChannel {
|
2018-09-05 01:53:23 +02:00
|
|
|
out.Send(a, nil)
|
2017-10-25 00:36:16 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
if config.AlertCheck.Duration > time.Duration(time.Second) {
|
|
|
|
go db.Alert(config.AlertCheck.Duration, out.Send)
|
2017-10-25 00:36:16 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 21:00:02 +02:00
|
|
|
log.WithField("defaults", len(db.DefaultNotify)).Info("starting logmania")
|
2017-10-25 00:36:16 +02:00
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
in = allInput.Init(config.Input, logChannel)
|
2017-10-25 00:36:16 +02:00
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
go in.Listen()
|
2017-10-25 00:36:16 +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:
|
|
|
|
log.Panic("terminated of logmania")
|
|
|
|
os.Exit(0)
|
|
|
|
case syscall.SIGQUIT:
|
|
|
|
quit()
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
quit()
|
|
|
|
case syscall.SIGUSR1:
|
|
|
|
reload()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit() {
|
2017-11-10 18:57:36 +01:00
|
|
|
dbSaveWorker.Close()
|
|
|
|
file.SaveJSON(config.DB, db)
|
2018-09-05 01:53:23 +02:00
|
|
|
in.Close()
|
|
|
|
out.Close()
|
2017-10-25 00:36:16 +02:00
|
|
|
log.Info("quit of logmania")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reload() {
|
|
|
|
log.Info("reload config file")
|
2017-10-27 13:07:12 +02:00
|
|
|
var config lib.Config
|
|
|
|
err := file.ReadTOML(configPath, &config)
|
|
|
|
if err != nil {
|
2017-10-25 00:36:16 +02:00
|
|
|
log.Errorf("reload: could not load '%s' for new configuration. Skip reload.", configPath)
|
|
|
|
return
|
|
|
|
}
|
2018-09-06 13:36:25 +02:00
|
|
|
if config.Debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
}
|
2018-09-05 01:53:23 +02:00
|
|
|
in.Close()
|
|
|
|
in = allInput.Init(config.Input, logChannel)
|
|
|
|
go in.Listen()
|
2017-10-25 00:36:16 +02:00
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
out.Close()
|
|
|
|
out = allOutput.Init(config.Output, db, logmaniaBot)
|
2017-10-25 00:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(serverCmd)
|
|
|
|
serverCmd.Flags().StringVarP(&configPath, "config", "c", "logmania.conf", "Path to configuration file")
|
|
|
|
}
|