-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtelegraf_cmd.go
106 lines (87 loc) · 2.66 KB
/
telegraf_cmd.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
101
102
103
104
105
106
package main
import (
"context"
"flag"
"fmt"
"github.com/google/subcommands"
"github.com/itzg/go-flagsfiller"
lpsender "github.com/itzg/line-protocol-sender"
"go.uber.org/zap"
"log"
"os"
"time"
)
type gatherTelegrafCmd struct {
Interval time.Duration `default:"1m" usage:"gathers and sends metrics at this interval"`
Servers []string `usage:"one or more [host:port] addresses of servers to monitor"`
TelegrafAddress string `default:"localhost:8094" usage:"[host:port] of telegraf accepting Influx line protocol"`
logger *zap.Logger
}
func (c *gatherTelegrafCmd) Name() string {
return "gather-for-telegraf"
}
func (c *gatherTelegrafCmd) Synopsis() string {
return "Periodically gathers to status of one or more Minecraft servers and sends metrics to telegraf over TCP using Influx line protocol"
}
func (c *gatherTelegrafCmd) Usage() string {
return ""
}
func (c *gatherTelegrafCmd) SetFlags(f *flag.FlagSet) {
filler := flagsfiller.New(flagsfiller.WithEnv("Gather"))
err := filler.Fill(f, c)
if err != nil {
log.Fatal(err)
}
}
func (c *gatherTelegrafCmd) Execute(ctx context.Context, _ *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
if len(c.Servers) == 0 {
_, _ = fmt.Fprintln(os.Stderr, "requires at least one server")
return subcommands.ExitUsageError
}
if c.TelegrafAddress == "" {
_, _ = fmt.Fprintln(os.Stderr, "requires TelegrafAddress")
return subcommands.ExitUsageError
}
c.logger = args[0].(*zap.Logger).Named("gather")
c.logger.Info("starting monitoring",
zap.Strings("servers", c.Servers),
zap.Duration("interval", c.Interval),
zap.String("telegrafAddress", c.TelegrafAddress))
ticker := time.NewTicker(c.Interval)
gatherers, err := c.createGatherers()
if err != nil {
c.logger.Error("failed to setup gatherers", zap.Error(err))
return subcommands.ExitFailure
}
for {
select {
case <-ctx.Done():
return subcommands.ExitSuccess
case <-ticker.C:
for _, gatherer := range gatherers {
gatherer.Gather()
}
}
}
}
func (c *gatherTelegrafCmd) createGatherers() ([]*TelegrafGatherer, error) {
gatherers := make([]*TelegrafGatherer, 0, len(c.Servers))
lpClient, err := lpsender.NewClient(context.Background(), lpsender.Config{
Endpoint: c.TelegrafAddress,
BatchSize: len(c.Servers),
ErrorListener: func(err error) {
c.logger.Error("failed to send metrics", zap.Error(err))
},
})
if err != nil {
return nil, err
}
for _, addr := range c.Servers {
host, port, err := SplitHostPort(addr, DefaultJavaPort)
if err != nil {
return nil, err
}
gatherers = append(gatherers, NewTelegrafGatherer(host, port, lpClient, c.logger))
}
return gatherers, nil
}