-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (70 loc) · 1.63 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
package main
import (
"flag"
"fmt"
"github.com/jmshin92/alerter/pkgs/alerter"
"github.com/jmshin92/alerter/pkgs/config"
"github.com/jmshin92/alerter/pkgs/mail"
"github.com/jmshin92/alerter/pkgs/mail/factory"
"github.com/sirupsen/logrus"
"os"
"time"
)
var (
ConfPath string
Vendors bool
)
func init() {
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
logrus.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
}
func main() {
flag.BoolVar(&Vendors, "v", false, "list of supported vendors")
flag.StringVar(&ConfPath, "c", "", "config path")
flag.Parse()
if Vendors {
fmt.Println(mail.Vendors())
os.Exit(0)
}
if len(ConfPath) == 0 {
logrus.Error("config path is mandatory")
flag.Usage()
os.Exit(-1)
}
c, err := config.GetConfig(ConfPath)
if err != nil {
logrus.Error("failed to get config. error: ", err)
os.Exit(-1)
}
alert := func(msg string) error {
mail, err := mail_factory.NewMail(&c.Mail.MailConf)
if err != nil {
logrus.Error(err)
return err
}
mail.SetSubject("Server is not OK")
mail.SetBody(fmt.Sprintf("[%v] Server is not OK!\n%s", time.Now(), msg))
return mail.Send()
}
recover := func() error {
mail, err := mail_factory.NewMail(&c.Mail.MailConf)
if err != nil {
logrus.Error(err)
return err
}
mail.SetSubject("Server recovered")
mail.SetBody(fmt.Sprintf("[%v] Server recovered!", time.Now()))
return mail.Send()
}
err = alerter.NewAlerter(c.Alert.AlerterConfig).
SetAlert(alert).
SetRecover(recover).
SetTarget(c.TargetUri).
Run()
if err != nil {
logrus.Error(err)
os.Exit(-1)
}
}