/
pagerduty.go
119 lines (105 loc) · 2.89 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
119
package main
// implements the Pager Duty Collector
import (
"fmt"
jq "github.com/antonholmquist/jason"
"net/http"
"os"
"time"
)
type PDIncident struct {
Incident *jq.Object
Logs []*jq.Object
}
//implements Collector interface
type PDCollector struct {
Token string
}
var pdCollector = &PDCollector{}
func init() {
COLLECTORS = append(COLLECTORS, pdCollector)
}
func (this *PDCollector) Name() string {
return `PagerDuty`
}
func (this *PDCollector) Enabled() bool {
if token := os.Getenv("PDTOKEN"); token != `` {
this.Token = token
return true
} else {
return false
}
}
func (this *PDCollector) Run(offset int) {
for _, incident := range pdGetIncidents(this.Token, offset) {
increment("pagerduty.incidents", 1)
for _, log := range incident.Logs {
pdProcessLog(log)
}
}
}
func since(offset int) string {
return time.Now().UTC().Add(time.Duration(offset) * -1 * time.Minute).Format("2006-01-02T15:04:05Z")
}
func pdQuery(url string, token string) *jq.Object {
client := &http.Client{}
authToken := fmt.Sprintf("Token token=%s", token)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
req.Header.Add("Authorization", authToken)
resp, err := client.Do(req)
if err != nil {
debug(fmt.Sprintf("error was %s", err))
}
defer resp.Body.Close()
body, _ := jq.NewObjectFromReader(resp.Body)
return body
}
func pdGetLogs(id string, token string) []*jq.Object {
ret := []*jq.Object{}
lURL := fmt.Sprintf("https://api.pagerduty.com/incidents/%s/log_entries?time_zone=UTC&is_overview=false", id)
raw := pdQuery(lURL, token)
logs, _ := raw.GetObjectArray("log_entries")
for _, log := range logs {
ret = append(ret, log)
}
return ret
}
func pdProcessLog(log *jq.Object) error {
entry_type, _ := log.GetString("type")
if entry_type == `notify_log_entry` {
user, _ := log.GetString("user", "summary")
key := fmt.Sprintf("pagerduty.%s.notified", user)
increment(key, 1)
} else if entry_type == `assign_log_entry` {
assignees, _ := log.GetObjectArray("assignees")
for _, assignee := range assignees {
user, _ := assignee.GetString("summary")
key := fmt.Sprintf("pagerduty.%s.assigned", user)
increment(key, 1)
}
} else if entry_type == `acknowledge_log_entry` {
user, _ := log.GetString("agent","summary")
key := fmt.Sprintf("pagerduty.%s.notified", user)
increment(key, 1)
}
//todo proper error checking
return nil
}
func pdGetIncidents(token string, offset int) []*PDIncident {
ret := []*PDIncident{}
iURL := fmt.Sprintf("https://api.pagerduty.com/incidents?time_zone=UTC&since=%s", since(offset))
debug(iURL)
raw := pdQuery(iURL, token)
debug(raw.String())
incidents, _ := raw.GetObjectArray("incidents")
for _, incident := range incidents {
id, _ := incident.GetString("id")
me := &PDIncident{
Incident: incident,
Logs: pdGetLogs(id, token),
}
ret = append(ret, me)
}
return ret
}