-
Notifications
You must be signed in to change notification settings - Fork 0
/
armada_stats.go
84 lines (68 loc) · 1.85 KB
/
armada_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"flag"
"fmt"
"github.com/sirupsen/logrus"
"github.com/krise3k/armada-stats/models"
"github.com/krise3k/armada-stats/models/armada"
"github.com/krise3k/armada-stats/utils"
"os"
"sync"
"time"
)
var (
configPath = flag.String("config", "/etc/armada-stats/armada-stats.yml", "config file location")
v = flag.Bool("v", false, "prints current version and exits")
logger *logrus.Logger
version string
)
func main() {
flag.Parse()
if *v {
fmt.Printf("Version %s \n", version)
os.Exit(0)
}
utils.InitConfig(*configPath)
utils.Config.Set("version", version)
logger = utils.GetLogger()
GatherStats()
}
func GatherStats() {
defer func() {
if r := recover(); r != nil {
var ok bool
var err error
err, ok = r.(error)
if !ok {
err = fmt.Errorf("Panic: %v", r)
}
logger.WithError(err).Fatal("Captured panic")
}
}()
suf, _ := utils.Config.Int("stats_update_frequency")
stats_update_frequency := time.Duration(suf) * time.Second
armadaHost, _ := utils.Config.String("armada_host")
armadaPort, _ := utils.Config.String("armada_port")
armadaClient := armada.NewArmadaClient(armadaHost, armadaPort)
armadaContainers := armadaClient.GetLocalContainerList()
containers := new(models.Containers)
containers.Add(armadaContainers)
for range time.Tick(stats_update_frequency) {
armadaContainers = armadaClient.GetLocalContainerList()
containers.MatchWithArmada(armadaContainers)
if len(containers.ContainerList) == 0 {
logger.Warn("ContainerList is empty, check is armada running")
continue
}
containers.Mu.Lock()
waitCollectAll := &sync.WaitGroup{}
waitCollectAll.Add(len(containers.ContainerList))
for _, cl := range containers.ContainerList {
go cl.Collect(waitCollectAll)
}
waitCollectAll.Wait()
go models.SendMetrics(*containers)
containers.Mu.Unlock()
}
return
}