-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjobs.go
226 lines (186 loc) · 5.32 KB
/
jobs.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/fernandezvara/scheduler"
tb "gopkg.in/tucnak/telebot.v2"
)
func (u *urlTester) executeMonitor(args []string) {
var (
id int
method string
urlString string
statusCode int
resultStatusCode int
checkStatus bool
checkText bool
checkTimeout bool
expected bool
duration time.Duration
err error
sched schedule
diff int64
)
id, err = strconv.Atoi(args[0])
if err != nil {
log.Println("ERROR: Unexpected Status code. Monitor called with these args:\n", args)
return
}
method = args[1]
urlString = args[2]
statusCode, err = strconv.Atoi(args[3])
if err != nil {
log.Println("ERROR: Unexpected Status code. Monitor called with these args:\n", args)
return
}
err = u.db.One("ID", id, &sched)
if err != nil {
log.Println("ERROR: Unexpected Status code. Monitor called with these args:\n", args)
return
}
if len(sched.Subscriptors) == 0 {
log.Println("Monitor with no subscriptors, skipping... ", expected, err, args)
return
}
duration, _, _, resultStatusCode, checkStatus, checkText, checkTimeout, expected, err = u.sendRequest(method, urlString, statusCode, sched.ExpectedText, sched.ExpectedTimeout)
if expected {
if u.lastStatus[sched.ID].Status != statusUp {
diff, err = u.addTimelineEntry(sched.ID, statusUp)
if err != nil {
log.Println(err.Error())
}
for _, sub := range sched.Subscriptors {
u.bot.Send(telegramUser{id: sub}, fmt.Sprintf("*RESOLVED*: (id:%d) \n%s [%s] (%d):\n\n*Downtime*: %s\n", sched.ID, sched.Method, sched.URL, sched.ExpectedStatus, secondsToHuman(diff)), tb.NoPreview, tb.ModeMarkdown)
}
}
} else {
var message string
message = fmt.Sprintf("*PROBLEM*: (id:%d) \n%s [%s] (%d):\n\n", sched.ID, sched.Method, sched.URL, sched.ExpectedStatus)
if err != nil {
message = fmt.Sprintf("%s*ERROR*: %s\n", message, err.Error())
} else {
if !checkStatus {
message = fmt.Sprintf("%s*ERROR*: HTTP status received: %d\n", message, resultStatusCode)
}
if !checkText {
message = fmt.Sprintf("%s*ERROR*: Text not found\n", message)
}
if !checkTimeout {
message = fmt.Sprintf("%s*ERROR*: Timeout exceed: %s\n", message, duration.String())
}
}
if u.lastStatus[sched.ID].Status != statusDown {
_, _ = u.addTimelineEntry(sched.ID, statusDown)
} else {
message = fmt.Sprintf("%s\n*Downtime*: %s\n", message, secondsToHuman(time.Now().Unix()-u.lastStatus[sched.ID].Timestamp))
}
for _, sub := range sched.Subscriptors {
u.bot.Send(telegramUser{id: sub}, message, tb.NoPreview, tb.ModeMarkdown)
}
}
}
// addJob adds a job the the scheduler,
// TODO: for update a scheduled job, first delete and recreate, the ID will remain the same
func (u *urlTester) addJob(sched schedule) error {
var (
err error
amount int
unit string
job *scheduler.Job
)
u.Lock()
amount, unit, _ = evaluateTimeExp(sched.Every)
job, err = newScheduledJob(amount, unit).NotImmediately().RunWithArgs(u.executeMonitor, []string{strconv.Itoa(sched.ID), sched.Method, sched.URL, strconv.Itoa(sched.ExpectedStatus)})
if err != nil {
return err
}
u.schedules[sched.ID] = job
log.Println("job added", sched.ID, sched.Method, sched.URL, sched.ExpectedStatus, sched.Every)
u.Unlock()
return nil
}
func newScheduledJob(amount int, unit string) *scheduler.Job {
var this *scheduler.Job
switch unit {
case "s", "S":
this = scheduler.Every(amount).Seconds()
case "m", "M":
this = scheduler.Every(amount).Minutes()
case "h", "H":
this = scheduler.Every(amount).Hours()
}
return this
}
func (u *urlTester) sendRequest(method, url string, expectedStatus int, text, timeout string) (duration time.Duration, body string, headers map[string]string, httpStatus int, checkStatus, checkText, checkTimeout, expected bool, err error) {
var (
client *http.Client
req *http.Request
res *http.Response
start time.Time
)
// init headers
headers = make(map[string]string)
client = &http.Client{
Timeout: 2 * time.Minute,
}
start = time.Now()
req, err = http.NewRequest(strings.ToUpper(method), url, nil)
if err != nil {
return
}
res, err = client.Do(req)
if err != nil {
return
}
// body
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return
}
body = string(bodyBytes)
for k, v := range res.Header {
var vstring string
for _, vv := range v {
vstring = fmt.Sprintf("%s %s", vstring, vv)
}
headers[k] = vstring
}
httpStatus = res.StatusCode
duration = time.Since(start)
// expectedStatus ok
checkStatus = expectedStatus == res.StatusCode
if text == "" {
checkText = true
} else {
checkText = strings.Contains(body, text)
}
if timeout == "" {
checkTimeout = true
} else {
var (
amount int
units string
expectedTimeout time.Duration
)
amount, units, _ = evaluateTimeExp(timeout)
switch units {
case "s", "S":
expectedTimeout = time.Duration(amount) * time.Second
case "m", "M":
expectedTimeout = time.Duration(amount) * time.Minute
case "h", "H":
expectedTimeout = time.Duration(amount) * time.Hour
}
checkTimeout = duration < expectedTimeout
}
if checkStatus && checkText && checkTimeout {
expected = true
}
return
}