node-api/stats.go

81 lines
1.4 KiB
Go
Raw Normal View History

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"
)
// Updates only container's state
func updateContainerState(app *apps.App) error {
container := docker.Container{
App: app,
}
state, err := container.Status()
if err != nil {
return err
}
err = apps.UpdateContainerState(
app.Name,
state,
)
return err
}
// Updates info about all containers
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 err
2020-07-23 10:09:06 +00:00
}
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
}
// gatherContainersStates refreshes all container's state
func gatherContainersStates() error {
appList, err := apps.List()
if err != nil {
return err
}
for _, app := range *appList {
err := updateContainerState(&app)
if err != nil {
log.Println("STATE ERROR:", err.Error())
}
}
return nil
}