2017-06-23 23:54:12 +00:00
|
|
|
package minecraft
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2019-04-23 18:14:35 +00:00
|
|
|
## Address of the Minecraft server.
|
2017-06-23 23:54:12 +00:00
|
|
|
# server = "localhost"
|
2019-04-23 18:14:35 +00:00
|
|
|
|
|
|
|
## Server RCON Port.
|
2017-06-23 23:54:12 +00:00
|
|
|
# port = "25575"
|
|
|
|
|
2019-04-23 18:14:35 +00:00
|
|
|
## Server RCON Password.
|
|
|
|
password = ""
|
2017-06-23 23:54:12 +00:00
|
|
|
|
2019-04-23 18:14:35 +00:00
|
|
|
## Uncomment to remove deprecated metric components.
|
|
|
|
# tagdrop = ["server"]
|
|
|
|
`
|
|
|
|
|
|
|
|
// Client is a client for the Minecraft server.
|
2017-06-23 23:54:12 +00:00
|
|
|
type Client interface {
|
2019-04-23 18:14:35 +00:00
|
|
|
// 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)
|
2017-06-23 23:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 18:14:35 +00:00
|
|
|
// Minecraft is the plugin type.
|
2017-06-23 23:54:12 +00:00
|
|
|
type Minecraft struct {
|
2019-04-23 18:14:35 +00:00
|
|
|
Server string `toml:"server"`
|
|
|
|
Port string `toml:"port"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
|
|
|
|
client Client
|
2017-06-23 23:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Minecraft) Description() string {
|
2019-04-23 18:14:35 +00:00
|
|
|
return "Collects scores from a Minecraft server's scoreboard using the RCON protocol"
|
2017-06-23 23:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Minecraft) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Minecraft) Gather(acc telegraf.Accumulator) error {
|
2019-04-23 18:14:35 +00:00
|
|
|
if s.client == nil {
|
|
|
|
connector, err := NewConnector(s.Server, s.Port, s.Password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := NewClient(connector)
|
2017-06-23 23:54:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-23 18:14:35 +00:00
|
|
|
s.client = client
|
2017-06-27 20:14:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 18:14:35 +00:00
|
|
|
players, err := s.client.Players()
|
2017-06-23 23:54:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-23 18:14:35 +00:00
|
|
|
for _, player := range players {
|
|
|
|
scores, err := s.client.Scores(player)
|
2017-06-23 23:54:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-23 18:14:35 +00:00
|
|
|
|
2017-06-23 23:54:12 +00:00
|
|
|
tags := map[string]string{
|
|
|
|
"player": player,
|
|
|
|
"server": s.Server + ":" + s.Port,
|
2019-04-23 18:14:35 +00:00
|
|
|
"source": s.Server,
|
|
|
|
"port": s.Port,
|
2017-06-23 23:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 18:14:35 +00:00
|
|
|
var fields = make(map[string]interface{}, len(scores))
|
|
|
|
for _, score := range scores {
|
|
|
|
fields[score.Name] = score.Value
|
2017-06-23 23:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("minecraft", fields, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("minecraft", func() telegraf.Input {
|
|
|
|
return &Minecraft{
|
|
|
|
Server: "localhost",
|
|
|
|
Port: "25575",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|