This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (93 loc) · 2.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"github.com/alexvanin/nezabx/db"
"github.com/alexvanin/nezabx/notifications/email"
"github.com/alexvanin/nezabx/runners"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
configFile := flag.String("c", "", "config file")
debugFlag := flag.Bool("debug", false, "debug mode")
versionFlag := flag.Bool("version", false, "show version")
flag.Parse()
if *versionFlag {
fmt.Printf("Nezabx %s\n", Version)
os.Exit(0)
}
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
log := Logger(*debugFlag)
cfg, err := ReadConfig(*configFile)
if err != nil {
log.Error("invalid configuration", zap.Error(err))
os.Exit(1)
}
state, err := db.NewBolt(cfg.State.Bolt)
if err != nil {
log.Error("invalid configuration", zap.Error(err))
os.Exit(1)
}
var mailNotificator *email.Notificator
if cfg.Notifications.Email != nil {
mailNotificator = email.NewNotificator(
cfg.Notifications.Email.SMTP,
cfg.Notifications.Email.Login,
cfg.Notifications.Email.Password,
)
for _, group := range cfg.Notifications.Email.Groups {
mailNotificator.AddGroup(group.Name, group.Addresses)
}
}
for _, command := range cfg.Commands {
cmd, err := runners.NewCommand(command.Exec)
if err != nil {
log.Warn("invalid command configuration", zap.String("command", command.Name), zap.Error(err))
os.Exit(1)
}
var cronSchedule cron.Schedule
if command.Interval == 0 {
cronSchedule, err = cron.ParseStandard(command.Cron)
if err != nil {
log.Warn("invalid cron configuration", zap.String("command", command.Name), zap.Error(err))
os.Exit(1)
}
}
commandRunner := runners.CommandRunner{
Log: log,
DB: state,
MailNotificator: mailNotificator,
Command: cmd,
Name: command.Name,
Threshold: command.Threshold,
ThresholdSleep: command.ThresholdSleep,
Timeout: command.Timeout,
Interval: command.Interval,
CronSchedule: cronSchedule,
Notifications: command.Notifications,
}
commandRunner.Run(ctx)
}
log.Info("application started")
<-ctx.Done()
log.Info("application received termination signal")
}
func Logger(debug bool) *zap.Logger {
logCfg := zap.NewProductionConfig()
logCfg.Level.SetLevel(zap.InfoLevel)
logCfg.DisableCaller = true
logCfg.Encoding = "console"
logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logCfg.DisableStacktrace = true
if debug {
logCfg.Level.SetLevel(zap.DebugLevel)
logCfg.DisableCaller = false
}
logger, _ := logCfg.Build()
return logger
}