-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (159 loc) · 4.42 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"sentinel/config"
"sentinel/helpers"
"sentinel/logger"
"sentinel/mail"
"sentinel/models"
_ "github.com/lib/pq"
"github.com/roylee0704/gron"
"github.com/xuri/excelize/v2"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// var dbConn *gorm.DB
var isConfigSuccess = false
var equals string = strings.Repeat("=", 50)
// Second: gron.Every(1*time.Second)
// Minute: gron.Every(1*time.Minute)
// Hour: gron.Every(1*time.Hour)
// Day: gron.Every(1 * xtime.Day)
// Week: gron.Every(1 * xtime.Week)
// gron.Every(30 * xtime.Day).At("00:00")
// gron.Every(1 * xtime.Week).At("23:59")
var repeatTime = gron.Every(5 * time.Minute)
// ignored error messages array
var ignoredErrorMessages = []string{
// ignored error messages here
}
// To Users
var toUsers = []string{
// team members here
}
// CC Users
var ccUsers = []string{
// team members here
}
func main() {
// Connect to the database
// dbConn = dbConnection()
// push the toUsers and ccUsers from config file
toUsers = append(toUsers, strings.Split(config.C.App.ToUsers, ",")...)
ccUsers = append(ccUsers, strings.Split(config.C.App.CcUsers, ",")...)
// Run the task repeat time and check the changes
c := gron.New()
c.AddFunc(repeatTime, func() {
// Query the TARGET table and retrieve changes
changes, err := getChanges()
if err != nil {
panic(err)
}
// Handle the changes
fmt.Println(equals)
if len(changes) > 0 {
for _, change := range changes {
logger.CLogger.Infof("INFO: %s:%d - %s", change.Domain, change.Port, change.Message)
}
// Filter the changes
filteredChanges := helpers.FilterChanges(changes, ignoredErrorMessages)
if len(filteredChanges) > 0 {
for _, v := range filteredChanges {
logger.CLogger.Tracef("TRACE: %s:%d - %s", v.Domain, v.Port, v.Message)
}
f := helpers.SetChangesToExcel(filteredChanges)
sendMailWithAttachment(filteredChanges, f)
}
} else {
logger.CLogger.Info("INFO: No changes in the last minute.")
}
fmt.Println(equals)
})
c.Start()
// Infinite loop to keep the program running
select {}
}
// Initialize Application
func init() {
isConfigSuccess = configureApplication()
if !isConfigSuccess {
logger.CLogger.Error("INIT: Application configuration failed. Please check your config file.")
os.Exit(1)
}
}
// Configure Application
func configureApplication() bool {
// Clear the terminal screen
fmt.Println(equals)
dir, err := os.Getwd()
if err != nil {
logger.CLogger.Error("INIT: Cannot get current working directory os.Getwd()")
return false
} else {
config.ReadConfig(dir)
logger.CLogger.Info("INIT: Application configuration file read success.")
return true
}
}
// DB Connection
func dbConnection() *gorm.DB {
env := config.C.DB
// String to Int
port, err := strconv.Atoi(env.Port)
if err != nil {
logger.CLogger.Error("ERROR: ", err)
os.Exit(1)
}
// Connect to the "postgres" database
dbInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", env.Host, port, env.Username, env.Password, env.DBName, env.SSLMode)
db, err := gorm.Open(postgres.Open(dbInfo), &gorm.Config{})
if err != nil {
logger.CLogger.Info("ERROR: ", err)
os.Exit(1)
}
// Connection Success
logger.CLogger.Success("PostgreSQL Database Connection Success")
return db
}
func getChanges() ([]models.Log, error) {
var logs []models.Log
for _, domain := range helpers.DomainList {
const maxRetries = 3
for i := 0; i < maxRetries; i++ {
isOK, data := helpers.CheckDomainCertificate(domain, config.C.App.ExpireDay)
if isOK && data != nil {
// Successfully retrieved certificate, break out of the loop
logs = append(logs, *data)
break
} else {
if data != nil {
// Certificate will not expired in 30 days
logger.CLogger.Info("INFO: ", domain+" - "+data.Message)
break
} else {
// Connection Error
logger.CLogger.Error("ERROR: ", domain+" - Connection Error Attempt: "+strconv.Itoa(i+1)+"/"+strconv.Itoa(maxRetries))
}
}
// Wait for a brief period before retrying
time.Sleep(2 * time.Second)
}
}
return logs, nil
}
// Send Mail with Excel File
func sendMailWithAttachment(logs []models.Log, f *excelize.File) {
mailContent := &models.Mail{
Sender: config.C.Mail.FromMail,
To: toUsers,
Cc: ccUsers,
Bcc: []string{},
Subject: config.C.App.TargetApp + " Error Logs",
}
mail.SendMail(mailContent, logs, f)
}