forked from rokett/Citrix-NetScaler-Exporter
/
get_stats.go
43 lines (33 loc) · 918 Bytes
/
get_stats.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
package netscaler
import (
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
// GetStats sends a request to the Nitro API and retrieves stats for the given type.
func (c *NitroClient) GetStats(statsType string, querystring string) ([]byte, error) {
url := c.url + "stat/" + statsType
if querystring != "" {
url = url + "?" + querystring
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "error creating HTTP request")
}
req.Header.Set("Accept", "application/json")
resp, err := c.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, errors.Wrap(err, "error sending request")
}
switch resp.StatusCode {
case 200:
body, _ := ioutil.ReadAll(resp.Body)
return body, nil
default:
body, _ := ioutil.ReadAll(resp.Body)
return body, errors.New("read failed: " + resp.Status + " (" + string(body) + ")")
}
}