forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alert_envelope.go
59 lines (46 loc) · 1.31 KB
/
alert_envelope.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
package jobsupervisor
import (
"regexp"
"strings"
"github.com/pivotal/go-smtpd/smtpd"
boshalert "github.com/cloudfoundry/bosh-agent/agent/alert"
)
type alertEnvelope struct {
*smtpd.BasicEnvelope
handler JobFailureHandler
alert *boshalert.MonitAlert
}
func (e *alertEnvelope) Write(lineBytes []byte) (err error) {
line := strings.TrimSpace(string(lineBytes))
idRegexp, err := regexp.Compile("^Message-id: <([^>]+)>$")
if err != nil {
return
}
idMatches := idRegexp.FindStringSubmatch(line)
switch {
case len(idMatches) == 2:
e.alert.ID = idMatches[1]
case strings.HasPrefix(line, "Service: "):
e.alert.Service = strings.Replace(line, "Service: ", "", 1)
case strings.HasPrefix(line, "Event: "):
e.alert.Event = strings.Replace(line, "Event: ", "", 1)
case strings.HasPrefix(line, "Action: "):
e.alert.Action = strings.Replace(line, "Action: ", "", 1)
case strings.HasPrefix(line, "Date: "):
e.alert.Date = strings.Replace(line, "Date: ", "", 1)
case strings.HasPrefix(line, "Description: "):
e.alert.Description = strings.Replace(line, "Description: ", "", 1)
}
return nil
}
func (e *alertEnvelope) Close() error {
alertToHandle := *e.alert
emptyAlert := boshalert.MonitAlert{}
if alertToHandle != emptyAlert {
err := e.handler(*e.alert)
if err != nil {
return err
}
}
return nil
}