2 * collectd - src/teamspeak2.c
3 * Copyright (C) 2008 Stefan Hacker
4 * Copyright (C) 2008 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Stefan Hacker <d0t at dbclan dot de>
21 * Florian Forster <octo at verplant.org>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
37 /* Default host and port */
38 #define DEFAULT_HOST "127.0.0.1"
39 #define DEFAULT_PORT "51234"
44 /* Server linked list structure */
45 typedef struct vserver_list_s
48 struct vserver_list_s *next;
50 static vserver_list_t *server_list = NULL;
53 static char *config_host = NULL;
54 static char *config_port = NULL;
56 static FILE *global_read_fh = NULL;
57 static FILE *global_write_fh = NULL;
60 static const char *config_keys[] =
66 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
71 static int tss2_add_vserver (int vserver_port)
73 vserver_list_t *entry;
75 if ((vserver_port <= 0) || (vserver_port > 65535))
77 ERROR ("teamspeak2 plugin: VServer port is invalid: %i",
82 entry = (vserver_list_t *) malloc (sizeof (vserver_list_t));
85 ERROR ("teamspeak2 plugin: malloc failed.");
88 memset (entry, 0, sizeof (vserver_list_t));
90 entry->port = vserver_port;
92 if(server_list == NULL) {
93 /* Add the server as the first element */
99 /* Add the server to the end of the list */
101 while (prev->next != NULL)
106 INFO ("teamspeak2 plugin: Registered new vserver: %i", vserver_port);
109 } /* int tss2_add_vserver */
111 static void tss2_submit_gauge (const char *plugin_instance, const char *type,
115 value_list_t vl = VALUE_LIST_INIT;
117 values[0].gauge = value;
121 vl.time = time (NULL);
122 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
123 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
125 if (plugin_instance != NULL)
126 sstrncpy (vl.plugin_instance, plugin_instance,
127 sizeof (vl.plugin_instance));
129 plugin_dispatch_values (type, &vl);
130 } /* void tss2_submit_gauge */
132 static void tss2_submit_io (const char *plugin_instance, const char *type,
133 counter_t rx, counter_t tx)
136 value_list_t vl = VALUE_LIST_INIT;
138 values[0].counter = rx;
139 values[1].counter = tx;
143 vl.time = time (NULL);
144 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
145 sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
147 if (plugin_instance != NULL)
148 sstrncpy (vl.plugin_instance, plugin_instance,
149 sizeof (vl.plugin_instance));
151 plugin_dispatch_values (type, &vl);
152 } /* void tss2_submit_gauge */
154 static void tss2_close_socket (void)
156 if (global_write_fh != NULL)
158 fputs ("quit\r\n", global_write_fh);
161 if (global_read_fh != NULL)
163 fclose (global_read_fh);
164 global_read_fh = NULL;
167 if (global_write_fh != NULL)
169 fclose (global_write_fh);
170 global_write_fh = NULL;
172 } /* void tss2_close_socket */
174 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
176 struct addrinfo ai_hints;
177 struct addrinfo *ai_head;
178 struct addrinfo *ai_ptr;
182 if ((global_read_fh != NULL) && (global_write_fh != NULL))
184 if (ret_read_fh != NULL)
185 *ret_read_fh = global_read_fh;
186 if (ret_write_fh != NULL)
187 *ret_write_fh = global_write_fh;
191 memset (&ai_hints, 0, sizeof (ai_hints));
193 ai_hints.ai_flags |= AI_ADDRCONFIG;
195 ai_hints.ai_family = AF_UNSPEC;
196 ai_hints.ai_socktype = SOCK_STREAM;
198 status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
199 (config_port != NULL) ? config_port : DEFAULT_PORT,
204 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
205 gai_strerror (status));
209 for (ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
211 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
212 ai_ptr->ai_protocol);
216 WARNING ("teamspeak2 plugin: socket failed: %s",
217 sstrerror (errno, errbuf, sizeof (errbuf)));
221 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
225 WARNING ("teamspeak2 plugin: connect failed: %s",
226 sstrerror (errno, errbuf, sizeof (errbuf)));
234 freeaddrinfo (ai_head);
239 global_read_fh = fdopen (sd, "r");
240 if (global_read_fh == NULL)
243 ERROR ("teamspeak2 plugin: fdopen failed: %s",
244 sstrerror (errno, errbuf, sizeof (errbuf)));
249 global_write_fh = fdopen (sd, "w");
250 if (global_write_fh == NULL)
253 ERROR ("teamspeak2 plugin: fdopen failed: %s",
254 sstrerror (errno, errbuf, sizeof (errbuf)));
255 tss2_close_socket ();
259 { /* Check that the server correctly identifies itself. */
263 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
264 buffer[sizeof (buffer) - 1] = 0;
266 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
268 ERROR ("teamspeak2 plugin: Unexpected response when connecting "
269 "to server. Expected ``[TS]'', got ``%s''.",
271 tss2_close_socket ();
276 if (ret_read_fh != NULL)
277 *ret_read_fh = global_read_fh;
278 if (ret_write_fh != NULL)
279 *ret_write_fh = global_write_fh;
281 } /* int tss2_get_socket */
283 static int tss2_send_request (FILE *fh, const char *request)
287 status = fputs (request, fh);
290 ERROR ("teamspeak2 plugin: fputs failed.");
291 tss2_close_socket ();
296 } /* int tss2_send_request */
298 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
303 * fgets is blocking but much easier then doing anything else
304 * TODO: Non-blocking Version would be safer
306 temp = fgets (buffer, buffer_size, fh);
310 ERROR ("teamspeak2 plugin: fgets failed: %s",
311 sstrerror (errno, errbuf, sizeof(errbuf)));
312 tss2_close_socket ();
316 buffer[buffer_size - 1] = 0;
318 } /* int tss2_receive_line */
320 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
326 snprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
327 command[sizeof (command) - 1] = 0;
329 status = tss2_send_request (write_fh, command);
332 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
336 status = tss2_receive_line (read_fh, response, sizeof (response));
339 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
342 response[sizeof (response)] = 0;
344 if ((strncmp ("OK", response, 2) == 0)
345 && ((response[2] == 0)
346 || (response[2] == '\n')
347 || (response[2] == '\r')))
350 ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
351 "Response received from server was: ``%s''.",
354 } /* int tss2_select_vserver */
356 static int tss2_read_vserver (vserver_list_t *vserver)
361 counter_t rx_octets = 0;
362 counter_t tx_octets = 0;
363 counter_t rx_packets = 0;
364 counter_t tx_packets = 0;
367 char plugin_instance[DATA_MAX_NAME_LEN];
372 status = tss2_get_socket (&read_fh, &write_fh);
375 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
381 memset (plugin_instance, 0, sizeof (plugin_instance));
383 status = tss2_send_request (write_fh, "gi\r\n");
387 snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
389 plugin_instance[sizeof (plugin_instance) - 1] = 0;
391 status = tss2_select_vserver (read_fh, write_fh, vserver);
394 status = tss2_send_request (write_fh, "si\r\n");
399 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
410 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
413 /* Set to NULL just to make sure noone uses these FHs anymore. */
416 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
420 if (strncmp ("ERROR", buffer, 5) == 0)
422 ERROR ("teamspeak2 plugin: Server returned an error: %s",
426 else if (strncmp ("OK", buffer, 2) == 0)
431 /* Split line into key and value */
432 key = strchr (buffer, '_');
435 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
440 value = strchr (key, '=');
443 DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
449 if ((strcmp ("currentusers", key) == 0)
450 || (strcmp ("users_online", key) == 0))
452 users = strtod (value, &endptr);
456 else if (strcmp ("bytesreceived", key) == 0)
458 rx_octets = strtoll (value, &endptr, 0);
462 else if (strcmp ("bytessend", key) == 0)
464 tx_octets = strtoll (value, &endptr, 0);
468 else if (strcmp ("packetsreceived", key) == 0)
470 rx_packets = strtoll (value, &endptr, 0);
474 else if (strcmp ("packetssend", key) == 0)
476 tx_packets = strtoll (value, &endptr, 0);
482 DEBUG ("teamspeak2 plugin: Unknown key-value-pair: key = %s; value = %s;",
487 if ((valid & 0x01) == 0x01)
488 tss2_submit_gauge (plugin_instance, "users", users);
490 if ((valid & 0x06) == 0x06)
491 tss2_submit_io (plugin_instance, "if_octets", rx_octets, tx_octets);
493 if ((valid & 0x18) == 0x18)
494 tss2_submit_io (plugin_instance, "if_packets", rx_packets, tx_packets);
499 } /* int tss2_read_vserver */
501 static int tss2_config (const char *key, const char *value)
503 if (strcasecmp ("Host", key) == 0)
507 temp = strdup (value);
510 ERROR("teamspeak2 plugin: strdup failed.");
516 else if (strcasecmp ("Port", key) == 0)
520 temp = strdup (value);
523 ERROR("teamspeak2 plugin: strdup failed.");
529 else if (strcasecmp ("Server", key) == 0)
531 /* Server variable found */
534 status = tss2_add_vserver (atoi (value));
540 /* Unknow variable found */
545 } /* int tss2_config */
547 static int tss2_read (void)
549 vserver_list_t *vserver;
553 /* Handle global server variables */
554 status = tss2_read_vserver (NULL);
561 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
564 for (vserver = server_list; vserver != NULL; vserver = vserver->next)
566 status = tss2_read_vserver (vserver);
573 WARNING ("teamspeak2 plugin: Reading statistics "
574 "for vserver %i failed.", vserver->port);
582 } /* int tss2_read */
584 static int tss2_shutdown(void)
586 vserver_list_t *entry;
588 tss2_close_socket ();
592 while (entry != NULL)
594 vserver_list_t *next;
601 /* Get rid of the configuration */
606 } /* int tss2_shutdown */
608 void module_register(void)
610 plugin_register_config ("teamspeak2", tss2_config,
611 config_keys, config_keys_num);
612 plugin_register_read ("teamspeak2", tss2_read);
613 plugin_register_shutdown ("teamspeak2", tss2_shutdown);
614 } /* void module_register */
616 /* vim: set sw=4 ts=4 : */