diff --git a/.gitignore b/.gitignore index ba4761f..0a57693 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ rosti.db node-api .history/ api-node-17.http +*-packr.go diff --git a/docker/docker.go b/docker/docker.go index d09348f..69a006f 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -100,13 +100,7 @@ func (d *Driver) Status(name string) (string, error) { return status, err } - if info.State.Running { - status = "running" - } else { - status = "stopped" - } - - return status, nil + return info.State.Status, nil } @@ -308,8 +302,8 @@ func (d *Driver) Create(name string, image string, volumePath string, HTTPPort i Resources: container.Resources{ CPUPeriod: 100000, CPUQuota: int64(CPU) * 1000, - Memory: int64(memory*110/100)*1024 ^ 3, // Allow 10 % more memory so we have space for MemoryReservation - MemoryReservation: int64(memory)*1024 ^ 3, // This should provide softer way how to limit the memory of our containers + Memory: int64(memory*110/100) * 1024 * 1024, // Allow 10 % more memory so we have space for MemoryReservation + MemoryReservation: int64(memory) * 1024 * 1024, // This should provide softer way how to limit the memory of our containers }, PortBindings: portmaps, AutoRemove: false, diff --git a/main.go b/main.go index 676c035..f9d4790 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { // Stats loop go func() { for { - err := gatherContainerStats() + err := gatherContainersStats() if err != nil { log.Println("LOOP ERROR:", err.Error()) } @@ -148,6 +148,8 @@ func main() { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } + go updateContainerStats(&app) + return c.JSON(http.StatusOK, Message{Message: "ok"}) }) @@ -169,6 +171,8 @@ func main() { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } + go updateContainerStats(app) + return c.JSON(http.StatusOK, Message{Message: "ok"}) }) @@ -190,6 +194,8 @@ func main() { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } + go updateContainerStats(app) + return c.JSON(http.StatusOK, Message{Message: "ok"}) }) @@ -211,6 +217,8 @@ func main() { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } + go updateContainerStats(app) + return c.JSON(http.StatusOK, Message{Message: "ok"}) }) @@ -263,6 +271,8 @@ func main() { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } + go updateContainerStats(app) + return c.JSON(http.StatusOK, Message{Message: "ok"}) }) diff --git a/stats.go b/stats.go index a095671..646afe3 100644 --- a/stats.go +++ b/stats.go @@ -7,32 +7,35 @@ import ( "github.com/rosti-cz/node-api/docker" ) -// gatherContainerStats gathers information about containers and saves it into the database -func gatherContainerStats() error { +func updateContainerStats(app *apps.App) error { + container := docker.Container{ + App: app, + } + state, err := container.GetState() + if err != nil { + return err + } + + err = apps.UpdateState( + app.Name, + state.State, + state.CPUUsage, + state.MemoryUsage, + state.DiskUsageBytes, + state.DiskUsageInodes, + ) + return nil +} + +// gatherContainersStats gathers information about containers and saves it into the database +func gatherContainersStats() error { appList, err := apps.List() if err != nil { return err } for _, app := range *appList { - container := docker.Container{ - App: &app, - } - state, err := container.GetState() - if err != nil { - log.Println("STATS ERROR:", err.Error()) - continue - } - - err = apps.UpdateState( - app.Name, - state.State, - state.CPUUsage, - state.MemoryUsage, - state.DiskUsageBytes, - state.DiskUsageInodes, - ) - + err := updateContainerStats(&app) if err != nil { log.Println("STATS ERROR:", err.Error()) } diff --git a/ui/index.html b/ui/index.html index 2e9c6e8..40650df 100644 --- a/ui/index.html +++ b/ui/index.html @@ -8,7 +8,7 @@ -
- Load index: 12
+ Overall index: {( node.index.toFixed(2) )}
+ Load 1 index: {( node.load1_index.toFixed(2) )}
+ Load 5 index: {( node.load5_index.toFixed(2) )}
+ Load 15 index: {( node.load15_index.toFixed(2) )}
+ Memory index: {( node.memory_index.toFixed(2) )}
+ Disk space index: {( node.disk_space_index.toFixed(2) )}