Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
58
plugins/inputs/csgo/README.md
Normal file
58
plugins/inputs/csgo/README.md
Normal file
|
@ -0,0 +1,58 @@
|
|||
# Counter-Strike: Global Offensive (CSGO) Input Plugin
|
||||
|
||||
This plugin gather metrics from [Counter-Strike: Global Offensive][csgo]
|
||||
servers.
|
||||
|
||||
⭐ Telegraf v1.18.0
|
||||
🏷️ server
|
||||
💻 all
|
||||
|
||||
[csgo]: https://www.counter-strike.net/
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Fetch metrics from a CSGO SRCDS
|
||||
[[inputs.csgo]]
|
||||
## Specify servers using the following format:
|
||||
## servers = [
|
||||
## ["ip1:port1", "rcon_password1"],
|
||||
## ["ip2:port2", "rcon_password2"],
|
||||
## ]
|
||||
#
|
||||
## If no servers are specified, no data will be collected
|
||||
servers = []
|
||||
```
|
||||
|
||||
## Metrics
|
||||
|
||||
The plugin retrieves the output of the `stats` command that is executed via
|
||||
rcon.
|
||||
|
||||
If no servers are specified, no data will be collected
|
||||
|
||||
- csgo
|
||||
- tags:
|
||||
- host
|
||||
- fields:
|
||||
- cpu (float)
|
||||
- net_in (float)
|
||||
- net_out (float)
|
||||
- uptime_minutes (float)
|
||||
- maps (float)
|
||||
- fps (float)
|
||||
- players (float)
|
||||
- sv_ms (float)
|
||||
- variance_ms (float)
|
||||
- tick_ms (float)
|
||||
|
||||
## Example Output
|
151
plugins/inputs/csgo/csgo.go
Normal file
151
plugins/inputs/csgo/csgo.go
Normal file
|
@ -0,0 +1,151 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package csgo
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorcon/rcon"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
type CSGO struct {
|
||||
Servers [][]string `toml:"servers"`
|
||||
}
|
||||
|
||||
func (*CSGO) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (s *CSGO) Init() error {
|
||||
for _, server := range s.Servers {
|
||||
if len(server) != 2 {
|
||||
return errors.New("incorrect server config")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *CSGO) Gather(acc telegraf.Accumulator) error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Loop through each server and collect metrics
|
||||
for _, server := range s.Servers {
|
||||
wg.Add(1)
|
||||
go func(addr, passwd string) {
|
||||
defer wg.Done()
|
||||
|
||||
// Connect and send the request
|
||||
client, err := rcon.Dial(addr, passwd)
|
||||
if err != nil {
|
||||
acc.AddError(err)
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
t := time.Now()
|
||||
response, err := client.Execute("stats")
|
||||
if err != nil {
|
||||
acc.AddError(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate the metric and add it to the accumulator
|
||||
m, err := parseResponse(addr, response, t)
|
||||
if err != nil {
|
||||
acc.AddError(err)
|
||||
return
|
||||
}
|
||||
acc.AddMetric(m)
|
||||
}(server[0], server[1])
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseResponse(addr, response string, t time.Time) (telegraf.Metric, error) {
|
||||
rows := strings.Split(response, "\n")
|
||||
if len(rows) < 2 {
|
||||
return nil, errors.New("bad response")
|
||||
}
|
||||
|
||||
// Parse the columns
|
||||
columns := strings.Fields(rows[1])
|
||||
if len(columns) != 10 {
|
||||
return nil, errors.New("not enough columns")
|
||||
}
|
||||
|
||||
cpu, err := strconv.ParseFloat(columns[0], 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
netIn, err := strconv.ParseFloat(columns[1], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
netOut, err := strconv.ParseFloat(columns[2], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uptimeMinutes, err := strconv.ParseFloat(columns[3], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
maps, err := strconv.ParseFloat(columns[4], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fps, err := strconv.ParseFloat(columns[5], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
players, err := strconv.ParseFloat(columns[6], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
svms, err := strconv.ParseFloat(columns[7], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msVar, err := strconv.ParseFloat(columns[8], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tick, err := strconv.ParseFloat(columns[9], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Construct the metric
|
||||
tags := map[string]string{"host": addr}
|
||||
fields := map[string]interface{}{
|
||||
"cpu": cpu,
|
||||
"net_in": netIn,
|
||||
"net_out": netOut,
|
||||
"uptime_minutes": uptimeMinutes,
|
||||
"maps": maps,
|
||||
"fps": fps,
|
||||
"players": players,
|
||||
"sv_ms": svms,
|
||||
"variance_ms": msVar,
|
||||
"tick_ms": tick,
|
||||
}
|
||||
return metric.New("csgo", tags, fields, t, telegraf.Gauge), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("csgo", func() telegraf.Input {
|
||||
return &CSGO{}
|
||||
})
|
||||
}
|
82
plugins/inputs/csgo/csgo_test.go
Normal file
82
plugins/inputs/csgo/csgo_test.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package csgo
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gorcon/rcon"
|
||||
"github.com/gorcon/rcon/rcontest"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestCPUStats(t *testing.T) {
|
||||
// Define the input
|
||||
const input = `CPU NetIn NetOut Uptime Maps FPS Players Svms +-ms ~tick
|
||||
10.0 1.2 3.4 100 1 120.20 15 5.23 0.01 0.02`
|
||||
|
||||
// Start the mockup server
|
||||
server := rcontest.NewUnstartedServer()
|
||||
server.Settings.Password = "password"
|
||||
server.SetAuthHandler(func(c *rcontest.Context) {
|
||||
if c.Request().Body() == c.Server().Settings.Password {
|
||||
pkg := rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, c.Request().ID, "")
|
||||
_, err := pkg.WriteTo(c.Conn())
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
pkg := rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, -1, string([]byte{0x00}))
|
||||
_, err := pkg.WriteTo(c.Conn())
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
server.SetCommandHandler(func(c *rcontest.Context) {
|
||||
pkg := rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, input)
|
||||
_, err := pkg.WriteTo(c.Conn())
|
||||
require.NoError(t, err)
|
||||
})
|
||||
server.Start()
|
||||
defer server.Close()
|
||||
|
||||
// Setup the plugin
|
||||
plugin := &CSGO{
|
||||
Servers: [][]string{
|
||||
{server.Addr(), "password"},
|
||||
},
|
||||
}
|
||||
require.NoError(t, plugin.Init())
|
||||
|
||||
// Define expected result
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"csgo",
|
||||
map[string]string{
|
||||
"host": server.Addr(),
|
||||
},
|
||||
map[string]interface{}{
|
||||
"cpu": 10.0,
|
||||
"fps": 120.2,
|
||||
"maps": 1.0,
|
||||
"net_in": 1.2,
|
||||
"net_out": 3.4,
|
||||
"players": 15.0,
|
||||
"sv_ms": 5.23,
|
||||
"tick_ms": 0.02,
|
||||
"uptime_minutes": 100.0,
|
||||
"variance_ms": 0.01,
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
telegraf.Gauge,
|
||||
),
|
||||
}
|
||||
|
||||
// Gather data
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, acc.GatherError(plugin.Gather))
|
||||
|
||||
// Test the result
|
||||
actual := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
|
||||
}
|
10
plugins/inputs/csgo/sample.conf
Normal file
10
plugins/inputs/csgo/sample.conf
Normal file
|
@ -0,0 +1,10 @@
|
|||
# Fetch metrics from a CSGO SRCDS
|
||||
[[inputs.csgo]]
|
||||
## Specify servers using the following format:
|
||||
## servers = [
|
||||
## ["ip1:port1", "rcon_password1"],
|
||||
## ["ip2:port2", "rcon_password2"],
|
||||
## ]
|
||||
#
|
||||
## If no servers are specified, no data will be collected
|
||||
servers = []
|
Loading…
Add table
Add a link
Reference in a new issue