forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft.go
102 lines (80 loc) · 1.95 KB
/
minecraft.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
package minecraft
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
const sampleConfig = `
## Address of the Minecraft server.
# server = "localhost"
## Server RCON Port.
# port = "25575"
## Server RCON Password.
password = ""
## Uncomment to remove deprecated metric components.
# tagdrop = ["server"]
`
// Client is a client for the Minecraft server.
type Client interface {
// Connect establishes a connection to the server.
Connect() error
// Players returns the players on the scoreboard.
Players() ([]string, error)
// Scores return the objective scores for a player.
Scores(player string) ([]Score, error)
}
// Minecraft is the plugin type.
type Minecraft struct {
Server string `toml:"server"`
Port string `toml:"port"`
Password string `toml:"password"`
client Client
}
func (s *Minecraft) Description() string {
return "Collects scores from a Minecraft server's scoreboard using the RCON protocol"
}
func (s *Minecraft) SampleConfig() string {
return sampleConfig
}
func (s *Minecraft) Gather(acc telegraf.Accumulator) error {
if s.client == nil {
connector, err := NewConnector(s.Server, s.Port, s.Password)
if err != nil {
return err
}
client, err := NewClient(connector)
if err != nil {
return err
}
s.client = client
}
players, err := s.client.Players()
if err != nil {
return err
}
for _, player := range players {
scores, err := s.client.Scores(player)
if err != nil {
return err
}
tags := map[string]string{
"player": player,
"server": s.Server + ":" + s.Port,
"source": s.Server,
"port": s.Port,
}
var fields = make(map[string]interface{}, len(scores))
for _, score := range scores {
fields[score.Name] = score.Value
}
acc.AddFields("minecraft", fields, tags)
}
return nil
}
func init() {
inputs.Add("minecraft", func() telegraf.Input {
return &Minecraft{
Server: "localhost",
Port: "25575",
}
})
}