Skip to content

Commit

Permalink
Add /containers/usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulou committed Jan 12, 2016
1 parent 0175ccc commit dd4bef8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v0.3.1

* NEW /containers/usage to get cpu + mem for all containers

## v0.3.0

* /containers/:id/cpu returns an object with `.usage_in_percents`
Expand Down
6 changes: 6 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ type Usage struct {
Net *NetUsage `json:"net,omitempty"`
}

type ContainersUsage map[string]Usage

func NewContainersUsage() ContainersUsage {
return ContainersUsage(make(map[string]Usage))
}

func NewClient(endpoint string) (*Client, error) {
_, err := url.Parse(endpoint)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
type Usage client.CpuUsage

var (
numCPU = runtime.NumCPU()
currentSystemUsage = make(map[string]int64)
previousSystemUsage = make(map[string]int64)
previousCpuUsages = make(map[string]int64)
Expand Down Expand Up @@ -104,7 +105,7 @@ func GetUsage(id string) (Usage, error) {
deltaCpuUsage := float64(cpuUsages[id] - previousCpuUsages[id])
deltaSystemCpuUsage := float64(currentSystemUsage[id] - previousSystemUsage[id])

percents := int(deltaCpuUsage / deltaSystemCpuUsage * 100 * float64(runtime.NumCPU()))
percents := int(deltaCpuUsage / deltaSystemCpuUsage * 100 * float64(numCPU))
return Usage{
UsageInPercents: percents,
}, nil
Expand Down
1 change: 1 addition & 0 deletions docker/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func ListRunningContainers(ids chan string) error {
for _, container := range containers {
ids <- container.ID
}
close(ids)

return nil
}
25 changes: 25 additions & 0 deletions server/acadock_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/Scalingo/acadock-monitoring/client"
"github.com/Scalingo/acadock-monitoring/cpu"
"github.com/Scalingo/acadock-monitoring/docker"
"github.com/Scalingo/acadock-monitoring/mem"
"github.com/Scalingo/acadock-monitoring/net"
"github.com/codegangsta/martini"
Expand Down Expand Up @@ -99,6 +100,29 @@ func containerNetUsageHandler(params martini.Params, res http.ResponseWriter) {
json.NewEncoder(res).Encode(&containerNet)
}

func containersUsageHandler(res http.ResponseWriter) {
usage := client.NewContainersUsage()
containerIdChan := make(chan string)
go docker.ListRunningContainers(containerIdChan)
for id := range containerIdChan {
cpuUsage, err := cpu.GetUsage(id)
if err != nil {
log.Println("Error getting cpu usage of ", id, ":", err)
continue
}
memUsage, err := mem.GetUsage(id)
if err != nil {
log.Println("Error getting mem usage of ", id, ":", err)
continue
}
usage[id] = client.Usage{
Cpu: (*client.CpuUsage)(&cpuUsage),
Memory: &memUsage.MemoryUsage,
}
}
json.NewEncoder(res).Encode(&usage)
}

func main() {
doProfile := flag.Bool("profile", false, "profile app")
flag.Parse()
Expand All @@ -111,6 +135,7 @@ func main() {
r.Get("/containers/:id/cpu", containerCpuUsageHandler)
r.Get("/containers/:id/net", containerNetUsageHandler)
r.Get("/containers/:id/usage", containerUsageHandler)
r.Get("/containers/usage", containersUsageHandler)

if *doProfile {
log.Println("Enable profiling")
Expand Down

0 comments on commit dd4bef8

Please sign in to comment.