forked from Cepave/open-falcon-backend
/
owlController.go
68 lines (60 loc) · 1.59 KB
/
owlController.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
package owl
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
log "github.com/sirupsen/logrus"
"github.com/Cepave/open-falcon-backend/modules/query/g"
)
type EndpointCounters struct {
Endpoint string `json:"endpoint"`
Counter string `json:"counter"`
}
type PostData struct {
Start int64 `json:"start"`
End int64 `json:"end"`
CF string `json:"cf"`
EndpointCounters []EndpointCounters `json:"endpoint_counters"`
}
func gethost() (endpoints []string) {
dat, _ := ioutil.ReadFile("./test/owl_endpoints")
endpoints = strings.Split(string(dat), ",")
return
}
func generatePostData(endpoints []string, counter string, sts int64, ets int64) PostData {
pd := PostData{
Start: sts,
End: ets,
CF: "AVERAGE",
}
var ec []EndpointCounters
for _, enp := range endpoints {
ec = append(ec, EndpointCounters{enp, counter})
}
pd.EndpointCounters = ec
return pd
}
func DoPost() {
conf := g.Config()
url := fmt.Sprintf("%s%s", conf.Http.Listen, "/graph/history")
// host := []string{"endpointA", "endpointB"}
host := gethost()
dd := generatePostData(host, "cpu.idle", 1464761471, 1464847858)
data, _ := json.Marshal(&dd)
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
if err != nil {
log.Error(err.Error())
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
}