-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk.go
48 lines (40 loc) · 1.06 KB
/
sdk.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
package sdk
import (
"bytes"
"fmt"
"net"
"net/http"
)
// monitor agent socket file
const MonitorAgentSocket = "/var/run/monitor-agent.sock"
// all your client.Get or client.Post calls has to be a valid url (http://xxxx.xxx/path not unix://...),
// the domain name doesn't matter since it won't be used in connecting.
const UnixSocketURL = "http://loda:1234/post?ns=%s"
var client *http.Client
func init() {
tr := &http.Transport{
Dial: unixsocketDial,
}
client = &http.Client{Transport: tr}
}
func unixsocketDial(proto, addr string) (conn net.Conn, err error) {
return net.Dial("unix", MonitorAgentSocket)
}
func Post(ns string, data []byte) error {
body := bytes.NewBuffer(data)
URL := fmt.Sprintf(UnixSocketURL, ns)
req, err := http.NewRequest("POST", URL, body)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json;charset=UTF-8")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
return nil
}
return fmt.Errorf("Post data failed StatusCode:%d", resp.StatusCode)
}