/
http_notifications_action.go
65 lines (56 loc) · 1.74 KB
/
http_notifications_action.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
package main
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
func PrepareEventAction(host string, rtt int64, checkTime time.Time, up bool, action string) string {
action = strings.ReplaceAll(action, "{HOST}", url.QueryEscape(host))
action = strings.ReplaceAll(action, "{TIME}", url.QueryEscape(checkTime.In(ChecksTZ).String()))
action = strings.ReplaceAll(action, "{TIMESTAMP}", strconv.FormatInt(checkTime.Unix(), 10))
action = strings.ReplaceAll(action, "{RTT}", strconv.FormatInt(rtt, 10))
var rttstr time.Duration = time.Duration(rtt) * time.Nanosecond
action = strings.ReplaceAll(action, "{RTTSTR}", url.QueryEscape(rttstr.String()))
if up {
action = strings.ReplaceAll(action, "{STATE}", "up")
action = strings.ReplaceAll(action, "{UP}", "true")
action = strings.ReplaceAll(action, "{DOWN}", "false")
} else {
action = strings.ReplaceAll(action, "{STATE}", "down")
action = strings.ReplaceAll(action, "{UP}", "false")
action = strings.ReplaceAll(action, "{DOWN}", "true")
}
return action
}
func EventHTTPNotify(host string, rtt int64, checkTime time.Time, up bool, action string) error {
action = PrepareEventAction(host, rtt, checkTime, up, action)
fmt.Println(action)
client := &http.Client{
Timeout: time.Duration(30) * time.Second,
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
var err error
var req *http.Request
req, err = http.NewRequest("GET", action, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
var resp *http.Response
resp, err = client.Do(req)
if resp != nil {
err = resp.Body.Close()
if err != nil {
return err
}
}
if !(resp.StatusCode >= 200 && resp.StatusCode <= 399) {
return fmt.Errorf("Response status: %v", resp.StatusCode)
}
return nil
}