forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamspeak.go
100 lines (82 loc) · 2.08 KB
/
teamspeak.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
package teamspeak
import (
"github.com/multiplay/go-ts3"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"strconv"
)
type Teamspeak struct {
Server string
Username string
Password string
VirtualServers []int `toml:"virtual_servers"`
client *ts3.Client
connected bool
}
func (ts *Teamspeak) Description() string {
return "Reads metrics from a Teamspeak 3 Server via ServerQuery"
}
const sampleConfig = `
## Server address for Teamspeak 3 ServerQuery
# server = "127.0.0.1:10011"
## Username for ServerQuery
username = "serverqueryuser"
## Password for ServerQuery
password = "secret"
## Array of virtual servers
# virtual_servers = [1]
`
func (ts *Teamspeak) SampleConfig() string {
return sampleConfig
}
func (ts *Teamspeak) Gather(acc telegraf.Accumulator) error {
var err error
if !ts.connected {
ts.client, err = ts3.NewClient(ts.Server)
if err != nil {
return err
}
err = ts.client.Login(ts.Username, ts.Password)
if err != nil {
return err
}
ts.connected = true
}
for _, vserver := range ts.VirtualServers {
ts.client.Use(vserver)
sm, err := ts.client.Server.Info()
if err != nil {
ts.connected = false
return err
}
sc, err := ts.client.Server.ServerConnectionInfo()
if err != nil {
ts.connected = false
return err
}
tags := map[string]string{
"virtual_server": strconv.Itoa(sm.ID),
"name": sm.Name,
}
fields := map[string]interface{}{
"uptime": sm.Uptime,
"clients_online": sm.ClientsOnline,
"total_ping": sm.TotalPing,
"total_packet_loss": sm.TotalPacketLossTotal,
"packets_sent_total": sc.PacketsSentTotal,
"packets_received_total": sc.PacketsReceivedTotal,
"bytes_sent_total": sc.BytesSentTotal,
"bytes_received_total": sc.BytesReceivedTotal,
}
acc.AddFields("teamspeak", fields, tags)
}
return nil
}
func init() {
inputs.Add("teamspeak", func() telegraf.Input {
return &Teamspeak{
Server: "127.0.0.1:10011",
VirtualServers: []int{1},
}
})
}