-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcontainer_stats.go
47 lines (38 loc) · 1 KB
/
container_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
44
45
46
47
package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
var ContainerNotFound = errors.New("No such Container")
var ContainerWrongState = errors.New("Container has wrong state")
// ContainerStats data takes a name or ID of a container returns stats data
func (c *API) ContainerStats(ctx context.Context, name string) (Stats, error) {
var stats Stats
res, err := c.Get(ctx, fmt.Sprintf("/v1.0.0/libpod/containers/%s/stats?stream=false", name))
if err != nil {
return stats, err
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
return stats, ContainerNotFound
}
if res.StatusCode == http.StatusConflict {
return stats, ContainerWrongState
}
if res.StatusCode != http.StatusOK {
return stats, fmt.Errorf("cannot get stats of container, status code: %d", res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return stats, err
}
err = json.Unmarshal(body, &stats)
if err != nil {
return stats, err
}
return stats, nil
}