-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagent.go
74 lines (60 loc) · 1.7 KB
/
agent.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
package main
import "strings"
import "time"
import "os"
import "log"
import "fmt"
import "net/http"
import "encoding/json"
import "encoding/base64"
import "crypto/hmac"
import "crypto/sha512"
import "github.com/matishsiao/goInfo"
func generatePayload(info ServerExtendedInfo, api_key string) (string, string) {
body, _ := json.Marshal(info)
mac := hmac.New(sha512.New, []byte(api_key))
mac.Write(body)
sum := base64.URLEncoding.EncodeToString(mac.Sum(nil))
return string(body), sum
}
func postPayload(url string, body string, hmac_header string) error {
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(body))
req.Header.Add("X-INTEGRITY", hmac_header)
if err != nil {
return fmt.Errorf("Failed creating http client: %v", err)
}
var resp *http.Response
resp, err = client.Do(req)
if err != nil {
return fmt.Errorf("Failed performing post request: %v", err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("Got bad status code from api: %v", resp.StatusCode)
}
return nil
}
func Agent() {
hostname, _ := os.Hostname()
gi := goInfo.GetInfo()
myInfo := ServerExtendedInfo{
OS: strings.Title(gi.Kernel),
Kernel: gi.Core,
Name: hostname,
}
api_url := Configs.ApiURL + "/api/v0/status/" + hostname
sleep_time := 30
for {
myInfo.Filesystems, _ = GetFilesystems()
body, macstr := generatePayload(myInfo, Configs.ApiKey)
err := postPayload(api_url, body, macstr)
if err == nil {
if !Configs.HushLogging {
log.Printf("Successful post. Sleeping for %v seconds.", sleep_time)
}
} else {
log.Printf("%v. Sleeping for %v seconds.", err, sleep_time)
}
time.Sleep(time.Duration(sleep_time) * time.Second)
}
}