2022-02-05 23:01:05 +00:00
|
|
|
package glue
|
2020-07-16 17:05:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2024-01-26 20:43:45 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-02-07 16:00:16 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-07-16 17:05:38 +00:00
|
|
|
"github.com/rosti-cz/node-api/apps"
|
2022-02-05 01:22:53 +00:00
|
|
|
docker "github.com/rosti-cz/node-api/containers"
|
2020-07-16 17:05:38 +00:00
|
|
|
)
|
|
|
|
|
2022-02-07 16:00:16 +00:00
|
|
|
// StatsProcessor covers all methods that are needed
|
|
|
|
// to gather information about application containers.
|
|
|
|
type StatsProcessor struct {
|
2022-02-07 16:12:23 +00:00
|
|
|
DB *gorm.DB
|
|
|
|
DockerSock string
|
2022-02-07 16:18:44 +00:00
|
|
|
BindIPHTTP string
|
|
|
|
BindIPSSH string
|
|
|
|
AppsPath string
|
2022-02-07 16:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns instance of getAppProcessor
|
|
|
|
func (s *StatsProcessor) getAppProcessor() apps.AppsProcessor {
|
2021-10-02 18:00:35 +00:00
|
|
|
processor := apps.AppsProcessor{
|
2022-02-07 16:00:16 +00:00
|
|
|
DB: s.DB,
|
2021-10-02 18:00:35 +00:00
|
|
|
}
|
2022-02-07 16:00:16 +00:00
|
|
|
processor.Init()
|
|
|
|
return processor
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateUsage updates various resource usage of the container/app in the database
|
|
|
|
func (s *StatsProcessor) UpdateUsage(name string) error {
|
|
|
|
processor := s.getAppProcessor()
|
2021-10-02 18:00:35 +00:00
|
|
|
|
|
|
|
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{
|
2022-02-07 16:12:23 +00:00
|
|
|
App: &app,
|
|
|
|
DockerSock: s.DockerSock,
|
2022-02-07 16:18:44 +00:00
|
|
|
BindIPHTTP: s.BindIPHTTP,
|
|
|
|
BindIPSSH: s.BindIPSSH,
|
|
|
|
AppsPath: s.AppsPath,
|
2020-07-23 21:50:20 +00:00
|
|
|
}
|
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,
|
2022-10-04 17:54:26 +00:00
|
|
|
state.OOMKilled,
|
2020-07-25 22:34:16 +00:00
|
|
|
state.CPUUsage,
|
|
|
|
state.MemoryUsage,
|
|
|
|
state.DiskUsageBytes,
|
|
|
|
state.DiskUsageInodes,
|
2022-02-03 00:31:47 +00:00
|
|
|
state.Flags,
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-20 03:53:22 +00:00
|
|
|
// Updates only container's state. Check current status of the container and saves it into the database.
|
2022-02-07 16:00:16 +00:00
|
|
|
func (s *StatsProcessor) UpdateState(name string) error {
|
|
|
|
processor := s.getAppProcessor()
|
2021-10-02 18:00:35 +00:00
|
|
|
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{
|
2022-02-07 16:12:23 +00:00
|
|
|
App: &app,
|
|
|
|
DockerSock: s.DockerSock,
|
2022-02-07 16:18:44 +00:00
|
|
|
BindIPHTTP: s.BindIPHTTP,
|
|
|
|
BindIPSSH: s.BindIPSSH,
|
|
|
|
AppsPath: s.AppsPath,
|
2020-07-23 10:09:06 +00:00
|
|
|
}
|
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,
|
2022-10-04 17:54:26 +00:00
|
|
|
state.Status,
|
|
|
|
state.OOMKilled,
|
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.
|
2022-02-07 16:00:16 +00:00
|
|
|
func (s *StatsProcessor) GatherStats() error {
|
|
|
|
processor := s.getAppProcessor()
|
2021-10-02 18:00:35 +00:00
|
|
|
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
|
|
|
|
2022-02-05 23:01:05 +00:00
|
|
|
for _, app := range appList {
|
2022-02-07 16:00:16 +00:00
|
|
|
err := s.UpdateUsage(app.Name)
|
2020-07-16 17:05:38 +00:00
|
|
|
if err != nil {
|
2024-01-26 20:43:45 +00:00
|
|
|
sentry.CaptureException(err)
|
2020-07-16 17:05:38 +00:00
|
|
|
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
|
2022-02-07 16:00:16 +00:00
|
|
|
func (s *StatsProcessor) GatherStates() error {
|
|
|
|
processor := s.getAppProcessor()
|
2021-10-02 18:00:35 +00:00
|
|
|
appList, err := processor.List()
|
2020-07-23 21:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:01:05 +00:00
|
|
|
for _, app := range appList {
|
2022-02-07 16:00:16 +00:00
|
|
|
err := s.UpdateState(app.Name)
|
2020-07-23 21:50:20 +00:00
|
|
|
if err != nil {
|
2024-01-26 20:43:45 +00:00
|
|
|
sentry.CaptureException(err)
|
2020-07-23 21:50:20 +00:00
|
|
|
log.Println("STATE ERROR:", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|