This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatspout.go
100 lines (81 loc) · 2.13 KB
/
statspout.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package statspout
import (
"os"
"os/signal"
"runtime"
"time"
"github.com/mijara/statspout/backend"
"github.com/mijara/statspout/log"
"github.com/mijara/statspout/opts"
)
func loop(client *backend.Client, containers map[string]backend.Container) {
ticker := time.NewTicker(time.Duration(opts.GetOpts().Interval) * time.Second)
closeC := make(chan os.Signal, 1)
signal.Notify(closeC, os.Interrupt, os.Kill)
// initial loop.
for name := range containers {
if !contains(opts.GetOpts().Ignore, name) {
client.Query(containers[name])
}
}
for {
select {
case <-closeC:
log.Info.Printf("Stopping: closing Goroutines and Clients. Please wait...")
ticker.Stop()
return
case <-ticker.C:
// query containers.
for name := range containers {
if !contains(opts.GetOpts().Ignore, name) {
client.Query(containers[name])
}
}
}
}
}
func inspect() {
ticker := time.NewTicker(10 * time.Second)
for range ticker.C {
n := runtime.NumGoroutine()
log.Debug.Printf("Goroutines: %d", n)
}
}
func Start(cfg *opts.Config) {
opts.GetOpts().Parse()
if opts.GetOpts().Interval < 1 {
log.Error.Fatal("Interval cannot be less than 1.")
}
// start the Repo.
repository, err := opts.CreateRepositoryFromFlags(cfg)
if err != nil {
log.Error.Fatal(err)
}
defer repository.Close()
// start the Docker Endpoint.
client, err := opts.CreateClientFromFlags(repository)
if err != nil {
log.Error.Fatal(err)
}
// get containers.
containers, err := client.GetContainers()
if err != nil {
log.Error.Fatal(err)
}
// small goroutine inspector.
go inspect()
log.Info.Printf("Statspout started: %d daemons, %d interval, %s mode, %s repo",
opts.GetOpts().Daemons,
opts.GetOpts().Interval,
opts.GetOpts().Mode.Name,
opts.GetOpts().Repository)
client.StartMonitor(containers)
// loop indefinitely until interrupt is received.
loop(client, containers)
// close all connections and goroutines.
client.Close()
// force exit
// TODO: this is needed because EventsMonitor is not able to exit gracefully when reading the HTTP
// TODO: stream Docker API, I don't know how to fix it at this time.
os.Exit(0)
}