This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pagerduty.go
118 lines (110 loc) · 2.85 KB
/
pagerduty.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
package checkup
import (
"encoding/json"
"fmt"
"strings"
"time"
"log"
"github.com/PagerDuty/go-pagerduty"
)
// PagerDuty consist of all the sub components required to use PagerDuty API
type PagerDuty struct {
Service string `json:"service_key"`
}
// NetDetails for PagerDuty Net event
type NetDetails struct {
Endpoint string
Timestamp string
Threshold string
Max string
Min string
Median string
Mean string
All string
Assessment string
Notice string
}
// TLSDetails for PagerDuty TLS event
type TLSDetails struct {
CommonName string
Serial string
NotBefore string
NotAfter string
DNSNames string
Issuer string
ExpiresIn string
Assessment string
Notice string
}
//Notify implements notifier interface
func (p PagerDuty) Notify(results []Result) error {
for _, result := range results {
if !result.Healthy {
p.Send(result)
}
}
return nil
}
//Send request via Pagerduty API to create incident
func (p PagerDuty) Send(result Result) error {
var status string
var details []byte
if result.Type == "tls" {
switch result.Status() {
case "down":
status = "EXPIRED"
case "degraded":
status = "EXPIRING"
}
property := result.Context.(CertProperties)
d := TLSDetails{
CommonName: property.CommonName,
Serial: fmt.Sprint(property.Serial),
NotBefore: fmt.Sprint(property.NotBefore),
NotAfter: fmt.Sprint(property.NotAfter),
DNSNames: fmt.Sprint(property.DNSNames),
Issuer: property.Issuer,
ExpiresIn: fmt.Sprintf("%d day(s)", -1*int(time.Since(property.NotAfter).Hours()/24)),
Assessment: fmt.Sprintf("%s", status),
Notice: result.Notice,
}
details, _ = json.Marshal(d)
} else {
status = strings.ToUpper(fmt.Sprint(result.Status()))
stats := result.ComputeStats()
d := NetDetails{
Endpoint: result.Endpoint,
Timestamp: fmt.Sprint(time.Unix(0, result.Timestamp).UTC()),
Threshold: fmt.Sprint(result.ThresholdRTT),
Max: fmt.Sprint(stats.Max),
Min: fmt.Sprint(stats.Min),
Median: fmt.Sprint(stats.Median),
Mean: fmt.Sprint(stats.Mean),
All: fmt.Sprintf("%v", result.Times),
Assessment: fmt.Sprintf("%s", status),
Notice: result.Notice,
}
details, _ = json.Marshal(d)
}
var jDetails map[string]string
json.Unmarshal(details, &jDetails)
for k, v := range result.Tags {
jDetails[k] = v
}
event := pagerduty.Event{
ServiceKey: p.Service,
Type: "trigger",
IncidentKey: result.Endpoint,
Description: result.Title + " (" + result.Endpoint + ") is " + status,
Client: result.Title,
ClientURL: result.Endpoint,
Details: jDetails,
}
resp, err := pagerduty.CreateEvent(event)
if err != nil {
log.Print("ERROR: ", err)
return err
}
log.Print(resp.Message, " for incident key '", resp.IncidentKey, "' with status ", resp.Status)
return nil
}