2020-07-16 17:05:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/rosti-cz/node-api/apps"
|
|
|
|
"github.com/rosti-cz/node-api/docker"
|
|
|
|
)
|
|
|
|
|
2020-07-23 10:09:06 +00:00
|
|
|
func updateContainerStats(app *apps.App) error {
|
|
|
|
container := docker.Container{
|
|
|
|
App: app,
|
|
|
|
}
|
|
|
|
state, err := container.GetState()
|
2020-07-16 17:05:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:09:06 +00:00
|
|
|
err = apps.UpdateState(
|
|
|
|
app.Name,
|
|
|
|
state.State,
|
|
|
|
state.CPUUsage,
|
|
|
|
state.MemoryUsage,
|
|
|
|
state.DiskUsageBytes,
|
|
|
|
state.DiskUsageInodes,
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-16 17:05:38 +00:00
|
|
|
|
2020-07-23 10:09:06 +00:00
|
|
|
// gatherContainersStats gathers information about containers and saves it into the database
|
|
|
|
func gatherContainersStats() error {
|
|
|
|
appList, err := apps.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-16 17:05:38 +00:00
|
|
|
|
2020-07-23 10:09:06 +00:00
|
|
|
for _, app := range *appList {
|
|
|
|
err := updateContainerStats(&app)
|
2020-07-16 17:05:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("STATS ERROR:", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|