-
Notifications
You must be signed in to change notification settings - Fork 1
/
phonehome.go
85 lines (75 loc) · 1.63 KB
/
phonehome.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
)
const (
heartBeatURL = "https://analytics.artell.ai/"
heartBeatPeriod = time.Second * 30
waitTime = time.Second * 30
debug = true
// Prevent the execution of multiple agents. Also useful for occasional debugging.
httpServerPort = 9999
)
func heartBeat() {
s := newServerInfo()
resp, err := http.PostForm(heartBeatURL, s.Values())
if err != nil {
log.Printf("Request to %v err %v", heartBeatURL, err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Response from %v: err %v", heartBeatURL, err)
return
}
if resp.StatusCode != 200 {
log.Printf("Response from %v: %v", heartBeatURL, resp.Status)
if debug {
log.Println(string(body))
}
return
}
log.Printf("Response from %v: %v", heartBeatURL, resp.Status)
}
type serverInfo struct {
hostname string
sshPort int
}
func (s *serverInfo) Values() url.Values {
param := make(url.Values)
param.Set("hostname", s.hostname)
param.Set("sshPort", strconv.Itoa(s.sshPort))
return param
}
func newServerInfo() *serverInfo {
h, _ := os.Hostname()
if h == "" {
h = "unknown"
}
return &serverInfo{
hostname: h,
sshPort: 22,
}
}
// phoneHome sends stats to http://mothership.pw once a while.
func contactMothership() {
go func() {
err := http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", httpServerPort), nil)
if err != nil {
log.Fatal("could not start http server, check that no other agent is running: ", err)
}
}()
tick := time.Tick(heartBeatPeriod)
for {
heartBeat()
<-tick
}
}