This repository has been archived by the owner on Dec 2, 2018. It is now read-only.
forked from canaryio/sensord
/
sensord.go
188 lines (148 loc) · 4.19 KB
/
sensord.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/andelf/go-curl"
"github.com/nu7hatch/gouuid"
)
type Config struct {
Location string
ChecksUrl string
MeasurerCount int
}
type Check struct {
Id string `json:"id"`
Url string `json:"url"`
}
type Measurement struct {
Check Check `json:"check"`
Id string `json:"id"`
Location string `json:"location"`
T int `json:"t"`
ExitStatus int `json:"exit_status"`
ConnectTime float64 `json:"connect_time,omitempty"`
StartTransferTime float64 `json:"starttransfer_time,omitempty"`
LocalIp string `json:"local_ip,omitempty"`
PrimaryIp string `json:"primary_ip,omitempty"`
TotalTime float64 `json:"total_time,omitempty"`
HttpStatus int `json:"http_status,omitempty"`
NameLookupTime float64 `json:"namelookup_time,omitempty"`
SizeDownload float64 `json:"size_download,omitempty"`
}
func (c *Check) Measure(config Config) Measurement {
var m Measurement
id, _ := uuid.NewV4()
m.Id = id.String()
m.Check = *c
m.Location = config.Location
easy := curl.EasyInit()
defer easy.Cleanup()
easy.Setopt(curl.OPT_URL, c.Url)
// dummy func for curl output
noOut := func(buf []byte, userdata interface{}) bool {
return true
}
easy.Setopt(curl.OPT_WRITEFUNCTION, noOut)
easy.Setopt(curl.OPT_CONNECTTIMEOUT, 10)
easy.Setopt(curl.OPT_TIMEOUT, 10)
now := time.Now()
m.T = int(now.Unix())
if err := easy.Perform(); err != nil {
if e, ok := err.(curl.CurlError); ok {
m.ExitStatus = (int(e))
return m
}
os.Exit(1)
}
m.ExitStatus = 0
http_status, _ := easy.Getinfo(curl.INFO_RESPONSE_CODE)
m.HttpStatus = http_status.(int)
connect_time, _ := easy.Getinfo(curl.INFO_CONNECT_TIME)
m.ConnectTime = connect_time.(float64)
namelookup_time, _ := easy.Getinfo(curl.INFO_NAMELOOKUP_TIME)
m.NameLookupTime = namelookup_time.(float64)
starttransfer_time, _ := easy.Getinfo(curl.INFO_STARTTRANSFER_TIME)
m.StartTransferTime = starttransfer_time.(float64)
total_time, _ := easy.Getinfo(curl.INFO_TOTAL_TIME)
m.TotalTime = total_time.(float64)
local_ip, _ := easy.Getinfo(curl.INFO_LOCAL_IP)
m.LocalIp = local_ip.(string)
primary_ip, _ := easy.Getinfo(curl.INFO_PRIMARY_IP)
m.PrimaryIp = primary_ip.(string)
size_download, _ := easy.Getinfo(curl.INFO_SIZE_DOWNLOAD)
m.SizeDownload = size_download.(float64)
return m
}
func measurer(config Config, toMeasurer chan Check, toStreamer chan Measurement) {
for {
c := <-toMeasurer
m := c.Measure(config)
toStreamer <- m
}
}
func streamer(config Config, toStreamer chan Measurement) {
h := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
for {
err := enc.Encode(<-toStreamer)
if err != nil {
return
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
}
}
http.HandleFunc("/measurements", h)
http.ListenAndServe(":5000", nil)
}
func getChecks(config Config) []Check {
url := config.ChecksUrl
res, err := http.Get(url)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
var checks []Check
err = json.Unmarshal(body, &checks)
if err != nil {
panic(err)
}
return checks
}
func scheduler(check Check, toMeasurer chan Check) {
for {
toMeasurer <- check
time.Sleep(1000 * time.Millisecond)
}
}
func main() {
config := Config{}
flag.StringVar(&config.Location, "location", "undefined", "location of this sensor")
flag.StringVar(&config.ChecksUrl, "checks_url", "https://s3.amazonaws.com/canary-public-data/checks.json", "URL for check data")
flag.IntVar(&config.MeasurerCount, "measurer_count", 1, "number of measurers to run")
flag.Parse()
check_list := getChecks(config)
toMeasurer := make(chan Check)
toStreamer := make(chan Measurement)
// spawn one scheduler per check
for _, c := range check_list {
go scheduler(c, toMeasurer)
}
// spawn N measurers
for i := 0; i < config.MeasurerCount; i++ {
go measurer(config, toMeasurer, toStreamer)
}
// stream measurements to clients over HTTP
go streamer(config, toStreamer)
select {}
}