From 7d6e9c0ae62ab7f4a90d5a03394b0d9ef1d1c547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Thu, 23 Jul 2020 12:09:06 +0200 Subject: [PATCH] Fix status --- .gitignore | 1 + docker/docker.go | 12 +++--------- main.go | 12 +++++++++++- stats.go | 43 +++++++++++++++++++++++-------------------- ui/index.html | 24 ++++++++++++++++-------- 5 files changed, 54 insertions(+), 38 deletions(-) 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 @@ - Bare - Start Bootstrap Template + Node manager @@ -38,7 +38,12 @@

Node

- 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) )}

@@ -67,19 +72,19 @@ {( app.name )} - {( app.state.state )} + {( app.state )} {( app.image.replace("docker.io/", "") )} - {( app.cpu_usage )} % / {( app.cpu )} % - {( app.memory_usage )} MB / {( app.memory )} MB + {( (app.cpu_usage).toFixed(2) )} % / {( app.cpu )} % + {( (app.memory_usage/1000/1000).toFixed(2) )} MB / {( app.memory )} MB {( (app.disk_usage_bytes / 1000 / 1000 / 1000).toFixed(2) )} GB {( app.disk_usage_inodes )} {( label.value )} - - - + + + @@ -102,10 +107,13 @@ apps: [], api_status_code: 0, api_response: "", + node: {}, }, created() { fetch('/v1/apps').then(response => response.json()) .then(data => this.apps = data); + fetch('/v1/node').then(response => response.json()) + .then(data => this.node = data); }, methods: { isSuccess: () => {