-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (135 loc) · 4.21 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
tls "crypto/tls"
"fmt"
"math/rand"
"net/http"
"net/http/cookiejar"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/ryancurrah/unifi-notifications/infrastructure"
"github.com/sirupsen/logrus"
"github.com/ryancurrah/unifi-notifications/domain/model"
)
var (
alarmsLastChecked = time.Now()
eventsLastChecked = time.Now()
alarmsQuitSignal chan bool
eventsQuitSignal chan bool
mainQuitSignal chan os.Signal
wg sync.WaitGroup
)
func main() {
defer wg.Wait()
alarmsQuitSignal = make(chan bool)
eventsQuitSignal = make(chan bool)
mainQuitSignal = make(chan os.Signal, 1)
signal.Notify(mainQuitSignal, syscall.SIGINT, syscall.SIGTERM)
appConfig, loggerConfig, unifiConfig, slackConfig, err := model.NewConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
logger, err := infrastructure.NewLogHandler(loggerConfig)
if err != nil {
logger.Fatalf("logger handler setup failed, error=%s", err)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
jar, err := cookiejar.New(nil)
if err != nil {
logger.Fatalf("cookie jar setup failed, error=%s", err)
}
httpClient := http.Client{Jar: jar, Transport: tr}
unifiHandler := infrastructure.NewUnifiHandler(unifiConfig, httpClient, logger)
slackHandler := infrastructure.NewSlackHandler(slackConfig, logger)
go checkAlarms(appConfig.CheckInterval, logger, unifiHandler, slackHandler)
go checkEvents(appConfig.CheckInterval, logger, unifiHandler, slackHandler, unifiConfig.Username)
logger.Info("started successfully")
for {
select {
case <-mainQuitSignal:
logger.Warn("received quit signal")
go func() {
alarmsQuitSignal <- true
logger.Info("alarms checker quit succesfully")
}()
go func() {
eventsQuitSignal <- true
logger.Info("events checker quit succesfully")
}()
return
}
}
}
func checkAlarms(checkInterval int, logger *logrus.Logger, unifiHandler infrastructure.UnifiHandler, slackHandler infrastructure.SlackHandler) {
wg.Add(1)
defer wg.Done()
for {
select {
case <-time.After(time.Duration(checkInterval)*time.Minute + time.Duration(rand.Intn(30-1)+1)*time.Second):
logger.Infof("checking for new alarms since %s", alarmsLastChecked.String())
siteAlarms, err := unifiHandler.GetAlarms(alarmsLastChecked)
if err != nil {
logger.Error(err)
}
for _, unifiAlarms := range siteAlarms {
for _, unifiAlarm := range unifiAlarms.Alarms {
logger.WithField("type", "alarm").Infof("%s %s", unifiAlarm.Msg, unifiAlarm.Datetime.String())
}
}
err = slackHandler.NotifyAlarms(siteAlarms)
if err != nil {
logger.Error(err)
}
alarmsLastChecked = time.Now()
case <-alarmsQuitSignal:
return
}
}
}
func checkEvents(checkInterval int, logger *logrus.Logger, unifiHandler infrastructure.UnifiHandler, slackHandler infrastructure.SlackHandler, username string) {
wg.Add(1)
defer wg.Done()
for {
select {
case <-time.After(time.Duration(checkInterval)*time.Minute + time.Duration(rand.Intn(30-1)+1)*time.Second):
logger.Infof("checking for new events since %s", eventsLastChecked.String())
siteEvents, err := unifiHandler.GetEvents(eventsLastChecked)
if err != nil {
logger.Error(err)
}
siteEvents = filterAdminLoginEvents(username, siteEvents)
for _, unifiEvents := range siteEvents {
for _, unifiEvent := range unifiEvents.Events {
logger.WithField("type", "event").Infof("%s %s", unifiEvent.Msg, unifiEvent.Datetime.String())
}
}
err = slackHandler.NotifyEvents(siteEvents)
if err != nil {
logger.Error(err)
}
eventsLastChecked = time.Now()
case <-eventsQuitSignal:
return
}
}
}
func filterAdminLoginEvents(adminName string, unifiSiteEvents model.UnifiSiteEvents) model.UnifiSiteEvents {
filteredUnifiSiteEvents := model.UnifiSiteEvents{}
for site, unifiEvents := range unifiSiteEvents {
filteredUnifiEvents := model.UnifiEvents{}
for _, unifiEvent := range unifiEvents.Events {
if !strings.HasPrefix(unifiEvent.Msg, fmt.Sprintf("Admin[%s] log in from", adminName)) {
filteredUnifiEvents.Events = append(filteredUnifiEvents.Events, unifiEvent)
}
}
filteredUnifiSiteEvents[site] = filteredUnifiEvents
}
return filteredUnifiSiteEvents
}