aboutsummaryrefslogtreecommitdiffstats
path: root/teamspeak-multi.py
blob: bec9f378e2e0a507f23e93aa13cad76fbe3f81aa (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Plugin to monitor a TeamSpeak Server via telnet
# 	* general bandwidth
# 	* filetransfer bandwidth
# 	* uptime
#	* user count
#
# Parameters understood:
#     config   (required)
#     autoconf (optional - used by munin-config)
#
# Magic markers - optional - used by installation scripts and
# munin-config:
#
#  #%# family=manual
#  #%# capabilities=autoconf
import ts3
import sys
import os


class TeamspeakMulti:
	def config(self):
		config = {
			'bandwidth': [
				'multigraph teamspeak_transfer',
				'graph_order down up',
				'graph_title Teamspeak Bandwidth',
				'graph_args --base 1024',
				'graph_vlabel bits in (-) / out (+)',
				'graph_category voip',
				'graph_info This graph shows the Teamspeak3 Voice Bandwidth In and Out',

				'down.label received',
				'down.info Bandwidth received in the last 5 minutes',
				'down.type DERIVE',
				'down.graph no',
				'down.min 0',
				'up.label sent',
				'up.info Bandwidth sent in the last 5 minutes',
				'up.type DERIVE',
				'up.negative down',
				'up.min 0'
			],
			'filetransfer': [
				'multigraph teamspeak_fttransfer',
				'graph_order ftdown ftup',
				'graph_title Teamspeak File Bandwidth',
				'graph_args --base 1024',
				'graph_vlabel bits in (-) / out (+)',
				'graph_category voip',
				'graph_info This graph shows the Teamspeak3 File Bandwidth In and Out',

				'ftdown.label received',
				'ftdown.info Bandwidth received in the last 5 minutes',
				'ftdown.type DERIVE',
				'ftdown.graph no',
				'ftdown.min 0',
				'ftup.label sent',
				'ftup.info Bandwidth sent in the last 5 minutes',
				'ftup.type DERIVE',
				'ftup.negative down',
				'ftup.min 0'
			],
			'uptime': [
				'multigraph teamspeak_uptime',
				'graph_title Connected Teamspeak Users',
				'graph_args --base 1000 -l 0',
				'graph_scale no',
				'graph_vlabel uptime in days',
				'graph_category voip',
				'graph_info This graph shows the Teamspeak3 overall uptime',

				'uptime.label uptime in seconds',
				'uptime.info TeamSpeak Server Uptime',
				'uptime.min 0',
				'uptime.draw AREA'
			],
			'users': [
				'multigraph teamspeak_usercount',
				'graph_title TeamSpeak User Activity',
				'graph_args --base 1024 -l 0',
				'graph_printf %.0lf',
				'graph_vlabel connected users',
				'graph_category voip',
				'graph_info This graph shows the number of connected users on the Teamspeak3 server',

				'user.label last 5 minutes',
				'user.info users connected in the last 5 minutes',
				'user.min 0'
			]
		}

		return config

	def get_data(self, response):
		data = {
			'teamspeak_transfer': [],
			'teamspeak_fttransfer': [],
			'teamspeak_uptime': [],
			'teamspeak_usercount': []
		}

		# transfer
		data['teamspeak_transfer'].append('multigraph teamspeak_transfer')
		data['teamspeak_transfer'].append('down.value %s' % response["connection_bytes_received_total"])
		data['teamspeak_transfer'].append('up.value %s' % response["connection_bytes_sent_total"])

		# fttransfer
		data['teamspeak_fttransfer'].append('multigraph teamspeak_fttransfer')
		data['teamspeak_fttransfer'].append('ftdown.value %s' % response["connection_filetransfer_bytes_received_total"])
		data['teamspeak_fttransfer'].append('ftup.value %s' % response["connection_filetransfer_bytes_sent_total"])

		# uptime
		data['teamspeak_uptime'].append('multigraph teamspeak_uptime')
		uptime = int(response["instance_uptime"]) / 86400
		data['teamspeak_uptime'].append('uptime.value %s' % str(uptime))

		# user count
		data['teamspeak_usercount'].append('multigraph teamspeak_usercount')
		data['teamspeak_usercount'].append('user.value %s' % response['virtualservers_total_clients_online'])

		return data

	def run(self):
		# read the configuration from munin environment
		try:
			server = (os.environ['host'], os.environ['port'], os.environ['id'])
		except KeyError:
			# if connection variables are not set use default
			server = ('localhost', 10011, 1)

		auth = (os.environ['username'], os.environ['password'])

		with ts3.query.TS3Connection(server[0], server[1]) as ts3conn:
			# will raise a TS3QueryError if response code is not 0
			try:
				ts3conn.login(
						client_login_name=auth[0],
						client_login_password=auth[1],
				)
			except ts3.query.TS3QueryError as err:
				print("Login failed:", err.resp.error["msg"])
				exit(1)

			ts3conn.use(sid=server[2])
			hostinfo = ts3conn.hostinfo().parsed

		result = self.get_data(hostinfo[0])

		for key in result.keys():
			print('\n'.join(result[key]))

	def main(self):
		if (sys.argv.__len__() == 2) and (sys.argv[1] == "config"):
			for key in self.config().keys():
				print('\n'.join(self.config()[key]))
			try:
				if os.environ['MUNIN_CAP_DIRTYCONFIG'] == '1':
					self.run()
			except KeyError:
				pass

		elif (sys.argv.__len__() == 2) and (sys.argv[1] == 'autoconf'):
			# check host if env variables are set
			try:
				if None not in {os.environ['username'], os.environ['password']}:
					print('yes')
			except KeyError:
				print('no env configuration options are missing')
		else:
			self.run()


if __name__ == "__main__":
	TeamspeakMulti().main()
	quit(0)