2020-07-16 17:05:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/rosti-cz/node-api/apps"
|
2021-10-02 18:00:35 +00:00
|
|
|
"github.com/rosti-cz/node-api/common"
|
2020-07-16 17:05:38 +00:00
|
|
|
"github.com/rosti-cz/node-api/docker"
|
|
|
|
)
|
|
|
|
|
2020-07-25 22:34:16 +00:00
|
|
|
// updateUsage updates various resource usage of the container/app in the database
|
|
|
|
func updateUsage(name string) error {
|
2021-10-02 18:00:35 +00:00
|
|
|
processor := apps.AppsProcessor{
|
|
|
|
DB: common.GetDBConnection(),
|
|
|
|
}
|
|
|
|
|
|
|
|
app, err := processor.Get(name)
|
2020-07-25 22:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-23 21:50:20 +00:00
|
|
|
container := docker.Container{
|
|
|
|
App: app,
|
|
|
|
}
|
2020-07-25 22:34:16 +00:00
|
|
|
|
|
|
|
state, err := container.GetState()
|
2020-07-23 21:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err = processor.UpdateResources(
|
2020-07-25 22:34:16 +00:00
|
|
|
name,
|
|
|
|
state.State,
|
|
|
|
state.CPUUsage,
|
|
|
|
state.MemoryUsage,
|
|
|
|
state.DiskUsageBytes,
|
|
|
|
state.DiskUsageInodes,
|
2020-07-23 21:50:20 +00:00
|
|
|
)
|
2020-07-25 22:34:16 +00:00
|
|
|
|
2020-07-23 21:50:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-25 22:34:16 +00:00
|
|
|
// Updates only container's state
|
|
|
|
func updateState(name string) error {
|
2021-10-02 18:00:35 +00:00
|
|
|
processor := apps.AppsProcessor{
|
|
|
|
DB: common.GetDBConnection(),
|
|
|
|
}
|
|
|
|
app, err := processor.Get(name)
|
2020-07-25 22:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:09:06 +00:00
|
|
|
container := docker.Container{
|
|
|
|
App: app,
|
|
|
|
}
|
2020-07-25 22:34:16 +00:00
|
|
|
state, err := container.Status()
|
2020-07-16 17:05:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err = processor.UpdateState(
|
2020-07-23 10:09:06 +00:00
|
|
|
app.Name,
|
2020-07-25 22:34:16 +00:00
|
|
|
state,
|
2020-07-23 10:09:06 +00:00
|
|
|
)
|
2020-07-23 21:50:20 +00:00
|
|
|
return err
|
2020-07-23 10:09:06 +00:00
|
|
|
}
|
2020-07-16 17:05:38 +00:00
|
|
|
|
2020-07-25 22:34:16 +00:00
|
|
|
// gatherStats loops over all applications and calls updateUsage to write various metric into the database.
|
|
|
|
func gatherStats() error {
|
2021-10-02 18:00:35 +00:00
|
|
|
processor := apps.AppsProcessor{
|
|
|
|
DB: common.GetDBConnection(),
|
|
|
|
}
|
|
|
|
appList, err := processor.List()
|
2020-07-23 10:09:06 +00:00
|
|
|
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 {
|
2020-07-25 22:34:16 +00:00
|
|
|
err := updateUsage(app.Name)
|
2020-07-16 17:05:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("STATS ERROR:", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-23 21:50:20 +00:00
|
|
|
|
2020-07-25 22:34:16 +00:00
|
|
|
// gatherStates loops over all apps and updates their container state
|
|
|
|
func gatherStates() error {
|
2021-10-02 18:00:35 +00:00
|
|
|
processor := apps.AppsProcessor{
|
|
|
|
DB: common.GetDBConnection(),
|
|
|
|
}
|
|
|
|
appList, err := processor.List()
|
2020-07-23 21:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, app := range *appList {
|
2020-07-25 22:34:16 +00:00
|
|
|
err := updateState(app.Name)
|
2020-07-23 21:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("STATE ERROR:", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|