-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minecraft Input Plugin using RCON #2960
Changes from 1 commit
48d7258
5cbb550
9d737b7
8fd5848
a4b87da
96994e3
54b1778
b1c970c
e775ba0
93c36f6
c3c6a8e
80ed9f3
c4fab38
db22951
6a8b6b5
e72948e
846b487
6fd5718
c48cf8d
f0f5f8b
bfbb286
a0ed0cf
f850a78
3e4559d
7d33813
2de9da1
66adba4
5818f5c
8ed6801
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Ayrdrie Palmer <ayrdriepalmer@gmail.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package minecraft | |
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
|
@@ -19,20 +20,25 @@ const sampleConfig = ` | |
password = "replace_me" | ||
` | ||
|
||
// Minecraft represents a connection to a minecraft server | ||
type Minecraft struct { | ||
Server string | ||
Port string | ||
Password string | ||
} | ||
|
||
// Description gives a brief description. | ||
func (s *Minecraft) Description() string { | ||
return "it collects stats from Minecraft servers" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make more better: "Collect scores from a Minecraft server's scoreboard" |
||
} | ||
|
||
// SampleConfig returns our sampleConfig. | ||
func (s *Minecraft) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
// Gather uses the RCON protocal to collect username and | ||
// scoreboard stats from a minecraft server. | ||
func (s *Minecraft) Gather(acc telegraf.Accumulator) error { | ||
if s.Port == " " { | ||
acc.AddFields("state", map[string]interface{}{"value": "pretty good"}, nil) | ||
|
@@ -43,6 +49,8 @@ func (s *Minecraft) Gather(acc telegraf.Accumulator) error { | |
return nil | ||
} | ||
|
||
// ParseUsername takes an input string from rcon, to parse | ||
// the username. | ||
func ParseUsername(input string) (string, error) { | ||
var re = regexp.MustCompile(`for\s(.*):-`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these regex be package level so they are only compiled once? I suggest something along the lines of |
||
|
||
|
@@ -53,6 +61,45 @@ func ParseUsername(input string) (string, error) { | |
return usernameMatches[0][1], nil | ||
} | ||
|
||
// Score is an individual tracked scoreboard stat. | ||
type Score struct { | ||
Name string | ||
Value int | ||
} | ||
|
||
// ParseScoreboard takes an input string from rcon, to parse | ||
// scoreboard stats. | ||
func ParseScoreboard(input string) ([]Score, error) { | ||
var re = regexp.MustCompile(`(?U):\s(\d+)\s\((.*)\)`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these regex be package level so they are only compiled once? |
||
scoreMatches := re.FindAllStringSubmatch(input, -1) | ||
if scoreMatches == nil { | ||
return nil, fmt.Errorf("No scores found") | ||
} | ||
|
||
var scores []Score | ||
|
||
for _, match := range scoreMatches { | ||
//fmt.Println(match) | ||
number := match[1] | ||
name := match[2] | ||
n, err := strconv.Atoi(number) | ||
//Not necessary in current state, because regex can only match integers, | ||
// maybe become necessary if regex is modified to match more types of | ||
//numbers | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to parse statistic") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use consistent terminology: "failed to parse score", also fix up the spacing on comment above :) |
||
} | ||
s := Score{ | ||
Name: name, | ||
Value: n, | ||
} | ||
// fmt.Println(s) | ||
scores = append(scores, s) | ||
} | ||
//fmt.Println(scores) | ||
return scores, nil | ||
} | ||
|
||
func init() { | ||
inputs.Add("minecraft", func() telegraf.Input { return &Minecraft{} }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to set config file defaults here. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done inconsistently in our current plugins, but comments should have two hashes, items with default values should have a single hash, and items that must be filled should be with no hashes.