teamspeak2 plugin: Flush the sending filehandle after writing to it.
[collectd.git] / src / teamspeak2.c
1 /**
2  * collectd - src/teamspeak2.c
3  * Copyright (C) 2008  Stefan Hacker
4  * Copyright (C) 2008  Florian Forster
5  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Stefan Hacker <d0t at dbclan dot de>
21  *   Florian Forster <octo at verplant.org>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netdb.h>
33
34 /*
35  * Defines
36  */
37 /* Default host and port */
38 #define DEFAULT_HOST    "127.0.0.1"
39 #define DEFAULT_PORT    "51234"
40
41 /*
42  * Variables
43  */
44 /* Server linked list structure */
45 typedef struct vserver_list_s
46 {
47         int port;
48         struct vserver_list_s *next;
49 } vserver_list_t;
50 static vserver_list_t *server_list = NULL;
51
52 /* Host data */
53 static char *config_host = NULL;
54 static char *config_port = NULL;
55
56 static FILE *global_read_fh = NULL;
57 static FILE *global_write_fh = NULL;
58
59 /* Config data */
60 static const char *config_keys[] =
61 {
62         "Host",
63         "Port",
64         "Server"
65 };
66 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
67
68 /*
69  * Functions
70  */
71 static int tss2_add_vserver (int vserver_port)
72 {
73         vserver_list_t *entry;
74
75         if ((vserver_port <= 0) || (vserver_port > 65535))
76         {
77                 ERROR ("teamspeak2 plugin: VServer port is invalid: %i",
78                                 vserver_port);
79                 return (-1);
80         }
81
82         entry = (vserver_list_t *) malloc (sizeof (vserver_list_t));
83         if (entry == NULL)
84         {
85                 ERROR ("teamspeak2 plugin: malloc failed.");
86                 return (-1);
87         }
88         memset (entry, 0, sizeof (vserver_list_t));
89
90         entry->port = vserver_port;
91
92         if(server_list == NULL) {
93                 /* Add the server as the first element */
94                 server_list = entry;
95         }
96         else {
97                 vserver_list_t *prev;
98
99                 /* Add the server to the end of the list */
100                 prev = server_list;
101                 while (prev->next != NULL)
102                         prev = prev->next;
103                 prev->next = entry;
104         }
105
106         INFO ("teamspeak2 plugin: Registered new vserver: %i", vserver_port);
107
108         return (0);
109 } /* int tss2_add_vserver */
110
111 static void tss2_submit_gauge (const char *plugin_instance, const char *type,
112                 gauge_t value)
113 {
114         value_t values[1];
115         value_list_t vl = VALUE_LIST_INIT;
116
117         values[0].gauge = value;
118
119         vl.values     = values;
120         vl.values_len = 1;
121         vl.time       = time (NULL);
122         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
123         sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
124
125         if (plugin_instance != NULL)
126                 sstrncpy (vl.plugin_instance, plugin_instance,
127                                 sizeof (vl.plugin_instance));
128         
129         plugin_dispatch_values (type, &vl);
130 } /* void tss2_submit_gauge */
131
132 static void tss2_submit_io (const char *plugin_instance, const char *type,
133                 counter_t rx, counter_t tx)
134 {
135         value_t values[2];
136         value_list_t vl = VALUE_LIST_INIT;
137
138         values[0].counter = rx;
139         values[1].counter = tx;
140
141         vl.values     = values;
142         vl.values_len = 2;
143         vl.time       = time (NULL);
144         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
145         sstrncpy (vl.plugin, "teamspeak2", sizeof (vl.plugin));
146
147         if (plugin_instance != NULL)
148                 sstrncpy (vl.plugin_instance, plugin_instance,
149                                 sizeof (vl.plugin_instance));
150         
151         plugin_dispatch_values (type, &vl);
152 } /* void tss2_submit_gauge */
153
154 static void tss2_close_socket (void)
155 {
156         if (global_write_fh != NULL)
157         {
158                 fputs ("quit\r\n", global_write_fh);
159         }
160
161         if (global_read_fh != NULL)
162         {
163                 fclose (global_read_fh);
164                 global_read_fh = NULL;
165         }
166
167         if (global_write_fh != NULL)
168         {
169                 fclose (global_write_fh);
170                 global_write_fh = NULL;
171         }
172 } /* void tss2_close_socket */
173
174 static int tss2_get_socket (FILE **ret_read_fh, FILE **ret_write_fh)
175 {
176         struct addrinfo ai_hints;
177         struct addrinfo *ai_head;
178         struct addrinfo *ai_ptr;
179         int sd = -1;
180         int status;
181
182         if ((global_read_fh != NULL) && (global_write_fh != NULL))
183         {
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;
188                 return (0);
189         }
190
191         memset (&ai_hints, 0, sizeof (ai_hints));
192 #ifdef AI_ADDRCONFIG
193         ai_hints.ai_flags |= AI_ADDRCONFIG;
194 #endif
195         ai_hints.ai_family = AF_UNSPEC;
196         ai_hints.ai_socktype = SOCK_STREAM;
197
198         status = getaddrinfo ((config_host != NULL) ? config_host : DEFAULT_HOST,
199                         (config_port != NULL) ? config_port : DEFAULT_PORT,
200                         &ai_hints,
201                         &ai_head);
202         if (status != 0)
203         {
204                 ERROR ("teamspeak2 plugin: getaddrinfo failed: %s",
205                                 gai_strerror (status));
206                 return (-1);
207         }
208
209         for (ai_ptr = ai_head; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
210         {
211                 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
212                                 ai_ptr->ai_protocol);
213                 if (sd < 0)
214                 {
215                         char errbuf[1024];
216                         WARNING ("teamspeak2 plugin: socket failed: %s",
217                                         sstrerror (errno, errbuf, sizeof (errbuf)));
218                         continue;
219                 }
220
221                 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
222                 if (status != 0)
223                 {
224                         char errbuf[1024];
225                         WARNING ("teamspeak2 plugin: connect failed: %s",
226                                         sstrerror (errno, errbuf, sizeof (errbuf)));
227                         close (sd);
228                         continue;
229                 }
230
231                 break;
232         } /* for (ai_ptr) */
233
234         freeaddrinfo (ai_head);
235
236         if (sd < 0)
237                 return (-1);
238
239         global_read_fh = fdopen (sd, "r");
240         if (global_read_fh == NULL)
241         {
242                 char errbuf[1024];
243                 ERROR ("teamspeak2 plugin: fdopen failed: %s",
244                                 sstrerror (errno, errbuf, sizeof (errbuf)));
245                 close (sd);
246                 return (-1);
247         }
248
249         global_write_fh = fdopen (sd, "w");
250         if (global_write_fh == NULL)
251         {
252                 char errbuf[1024];
253                 ERROR ("teamspeak2 plugin: fdopen failed: %s",
254                                 sstrerror (errno, errbuf, sizeof (errbuf)));
255                 tss2_close_socket ();
256                 return (-1);
257         }
258
259         { /* Check that the server correctly identifies itself. */
260                 char buffer[4096];
261                 char *buffer_ptr;
262
263                 buffer_ptr = fgets (buffer, sizeof (buffer), global_read_fh);
264                 buffer[sizeof (buffer) - 1] = 0;
265
266                 if (memcmp ("[TS]\r\n", buffer, 6) != 0)
267                 {
268                         ERROR ("teamspeak2 plugin: Unexpected response when connecting "
269                                         "to server. Expected ``[TS]'', got ``%s''.",
270                                         buffer);
271                         tss2_close_socket ();
272                         return (-1);
273                 }
274         }
275
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;
280         return (0);
281 } /* int tss2_get_socket */
282
283 static int tss2_send_request (FILE *fh, const char *request)
284 {
285         int status;
286
287         status = fputs (request, fh);
288         if (status < 0)
289         {
290                 ERROR ("teamspeak2 plugin: fputs failed.");
291                 tss2_close_socket ();
292                 return (-1);
293         }
294         fflush (fh);
295
296         return (0);
297 } /* int tss2_send_request */
298
299 static int tss2_receive_line (FILE *fh, char *buffer, int buffer_size)
300 {
301         char *temp;
302          
303         /*
304          * fgets is blocking but much easier then doing anything else
305          * TODO: Non-blocking Version would be safer
306          */
307         temp = fgets (buffer, buffer_size, fh);
308         if (temp == NULL)
309         {
310                 char errbuf[1024];
311                 ERROR ("teamspeak2 plugin: fgets failed: %s",
312                                 sstrerror (errno, errbuf, sizeof(errbuf)));
313                 tss2_close_socket ();
314                 return (-1);
315         }
316
317         buffer[buffer_size - 1] = 0;
318         return (0);
319 } /* int tss2_receive_line */
320
321 static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *vserver)
322 {
323         char command[128];
324         char response[128];
325         int status;
326
327         snprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
328         command[sizeof (command) - 1] = 0;
329
330         status = tss2_send_request (write_fh, command);
331         if (status != 0)
332         {
333                 ERROR ("teamspeak2 plugin: tss2_send_request (%s) failed.", command);
334                 return (-1);
335         }
336
337         status = tss2_receive_line (read_fh, response, sizeof (response));
338         if (status != 0)
339         {
340                 ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
341                 return (-1);
342         }
343         response[sizeof (response)] = 0;
344
345         if ((strncmp ("OK", response, 2) == 0)
346                         && ((response[2] == 0)
347                                 || (response[2] == '\n')
348                                 || (response[2] == '\r')))
349                 return (0);
350
351         ERROR ("teamspeak2 plugin: Command ``%s'' failed. "
352                         "Response received from server was: ``%s''.",
353                         command, response);
354         return (-1);
355 } /* int tss2_select_vserver */
356
357 static int tss2_read_vserver (vserver_list_t *vserver)
358 {
359         int status;
360
361         gauge_t users = NAN;
362         counter_t rx_octets = 0;
363         counter_t tx_octets = 0;
364         counter_t rx_packets = 0;
365         counter_t tx_packets = 0;
366         int valid = 0;
367
368         char plugin_instance[DATA_MAX_NAME_LEN];
369
370         FILE *read_fh;
371         FILE *write_fh;
372
373         status = tss2_get_socket (&read_fh, &write_fh);
374         if (status != 0)
375         {
376                 ERROR ("teamspeak2 plugin: tss2_get_socket failed.");
377                 return (-1);
378         }
379
380         if (vserver == NULL)
381         {
382                 memset (plugin_instance, 0, sizeof (plugin_instance));
383
384                 status = tss2_send_request (write_fh, "gi\r\n");
385         }
386         else
387         {
388                 snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
389                                 vserver->port);
390                 plugin_instance[sizeof (plugin_instance) - 1] = 0;
391
392                 status = tss2_select_vserver (read_fh, write_fh, vserver);
393                 if (status != 0)
394                         return (status);
395                 status = tss2_send_request (write_fh, "si\r\n");
396         }
397
398         if (status != 0)
399         {
400                 ERROR ("teamspeak2 plugin: tss2_send_request failed.");
401                 return (-1);
402         }
403
404         while (42)
405         {
406                 char buffer[4096];
407                 char *key;
408                 char *value;
409                 char *endptr = NULL;
410
411                 status = tss2_receive_line (read_fh, buffer, sizeof (buffer));
412                 if (status != 0)
413                 {
414                         /* Set to NULL just to make sure noone uses these FHs anymore. */
415                         read_fh = NULL;
416                         write_fh = NULL;
417                         ERROR ("teamspeak2 plugin: tss2_receive_line failed.");
418                         break;
419                 }
420
421                 if (strncmp ("ERROR", buffer, 5) == 0)
422                 {
423                         ERROR ("teamspeak2 plugin: Server returned an error: %s",
424                                         buffer);
425                         break;
426                 }
427                 else if (strncmp ("OK", buffer, 2) == 0)
428                 {
429                         break;
430                 }
431
432                 /* Split line into key and value */
433                 key = strchr (buffer, '_');
434                 if (key == NULL)
435                 {
436                         DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
437                         continue;
438                 }
439                 key++;
440
441                 value = strchr (key, '=');
442                 if (value == NULL)
443                 {
444                         DEBUG ("teamspeak2 plugin: Cannot parse line: %s", buffer);
445                         continue;
446                 }
447                 *value = 0;
448                 value++;
449
450                 if ((strcmp ("currentusers", key) == 0)
451                                 || (strcmp ("users_online", key) == 0))
452                 {
453                         users = strtod (value, &endptr);
454                         if (value != endptr)
455                                 valid |= 0x01;
456                 }
457                 else if (strcmp ("bytesreceived", key) == 0)
458                 {
459                         rx_octets = strtoll (value, &endptr, 0);
460                         if (value != endptr)
461                                 valid |= 0x02;
462                 }
463                 else if (strcmp ("bytessend", key) == 0)
464                 {
465                         tx_octets = strtoll (value, &endptr, 0);
466                         if (value != endptr)
467                                 valid |= 0x04;
468                 }
469                 else if (strcmp ("packetsreceived", key) == 0)
470                 {
471                         rx_packets = strtoll (value, &endptr, 0);
472                         if (value != endptr)
473                                 valid |= 0x08;
474                 }
475                 else if (strcmp ("packetssend", key) == 0)
476                 {
477                         tx_packets = strtoll (value, &endptr, 0);
478                         if (value != endptr)
479                                 valid |= 0x10;
480                 }
481                 else
482                 {
483                         DEBUG ("teamspeak2 plugin: Unknown key-value-pair: key = %s; value = %s;",
484                                         key, value);
485                 }
486         } /* while (42) */
487
488         if ((valid & 0x01) == 0x01)
489                 tss2_submit_gauge (plugin_instance, "users", users);
490
491         if ((valid & 0x06) == 0x06)
492                 tss2_submit_io (plugin_instance, "io_octets", rx_octets, tx_octets);
493
494         if ((valid & 0x18) == 0x18)
495                 tss2_submit_io (plugin_instance, "io_packets", rx_packets, tx_packets);
496
497         if (valid == 0)
498                 return (-1);
499         return (0);
500 } /* int tss2_read_vserver */
501
502 static int tss2_config (const char *key, const char *value)
503 {
504     if (strcasecmp ("Host", key) == 0)
505         {
506                 char *temp;
507
508                 temp = strdup (value);
509                 if (temp == NULL)
510                 {
511                         ERROR("teamspeak2 plugin: strdup failed.");
512                         return (1);
513                 }
514                 sfree (config_host);
515                 config_host = temp;
516         }
517         else if (strcasecmp ("Port", key) == 0)
518         {
519                 char *temp;
520
521                 temp = strdup (value);
522                 if (temp == NULL)
523                 {
524                         ERROR("teamspeak2 plugin: strdup failed.");
525                         return (1);
526                 }
527                 sfree (config_port);
528                 config_port = temp;
529         }
530         else if (strcasecmp ("Server", key) == 0)
531         {
532                 /* Server variable found */
533                 int status;
534                 
535                 status = tss2_add_vserver (atoi (value));
536                 if (status != 0)
537                         return (1);
538         }
539         else
540         {
541                 /* Unknow variable found */
542                 return (-1);
543         }
544
545         return 0;
546 } /* int tss2_config */
547
548 static int tss2_read (void)
549 {
550         vserver_list_t *vserver;
551         int success = 0;
552         int status;
553
554         /* Handle global server variables */
555         status = tss2_read_vserver (NULL);
556         if (status == 0)
557         {
558                 success++;
559         }
560         else
561         {
562                 WARNING ("teamspeak2 plugin: Reading global server variables failed.");
563         }
564
565         for (vserver = server_list; vserver != NULL; vserver = vserver->next)
566         {
567                 status = tss2_read_vserver (vserver);
568                 if (status == 0)
569                 {
570                         success++;
571                 }
572                 else
573                 {
574                         WARNING ("teamspeak2 plugin: Reading statistics "
575                                         "for vserver %i failed.", vserver->port);
576                         continue;
577                 }
578         }
579         
580         if (success == 0)
581                 return (-1);
582     return (0);
583 } /* int tss2_read */
584
585 static int tss2_shutdown(void)
586 {
587         vserver_list_t *entry;
588
589         tss2_close_socket ();
590
591         entry = server_list;
592         server_list = NULL;
593         while (entry != NULL)
594         {
595                 vserver_list_t *next;
596
597                 next = entry->next;
598                 sfree (entry);
599                 entry = next;
600         }
601
602         /* Get rid of the configuration */
603         sfree (config_host);
604         sfree (config_port);
605         
606     return (0);
607 } /* int tss2_shutdown */
608
609 void module_register(void)
610 {
611         plugin_register_config ("teamspeak2", tss2_config,
612                         config_keys, config_keys_num);
613         plugin_register_read ("teamspeak2", tss2_read);
614         plugin_register_shutdown ("teamspeak2", tss2_shutdown);
615 } /* void module_register */
616
617 /* vim: set sw=4 ts=4 : */