/
pingo.go
76 lines (67 loc) · 1.52 KB
/
pingo.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
package pingo
import (
"log"
"net"
"strconv"
"time"
"github.com/crowdmob/goamz/aws"
"github.com/crowdmob/goamz/cloudwatch"
)
const (
CloudWatchOkValue = float64(2)
CloudWatchFailValue = float64(1)
CloudWatchNamespace = "pingo"
)
type Host struct {
Hostname string
Port int
}
type Handler func(host Host, status bool, t time.Time) error
func (host Host) Address() string {
return host.Hostname + ":" + strconv.Itoa(host.Port)
}
func LoggingHandler(host Host, status bool, t time.Time) error {
text := "FAIL"
if status == true {
text = "OK"
}
log.Printf("PING %s %s", host.Address(), text)
return nil
}
func NewCloudWatchHandler(region string) Handler {
auth, err := aws.EnvAuth()
if err != nil {
log.Fatalln(err)
}
c, err := cloudwatch.NewCloudWatch(auth, aws.Regions[region].CloudWatchServicepoint)
if err != nil {
log.Fatalln(err)
}
return func(host Host, status bool, t time.Time) error {
value := CloudWatchFailValue
if status == true {
value = CloudWatchOkValue
}
metric := cloudwatch.MetricDatum{
MetricName: host.Address(),
Value: value,
}
_, err := c.PutMetricDataNamespace([]cloudwatch.MetricDatum{metric}, CloudWatchNamespace)
return err
}
}
func Ping(host Host, timeout time.Duration, handlers []Handler) {
address := host.Address()
now := time.Now()
conn, err := net.DialTimeout("tcp", address, timeout)
ok := err == nil
if conn != nil {
conn.Close()
}
for _, handler := range handlers {
err := handler(host, ok, now)
if err != nil {
log.Println(err)
}
}
}