Name changes - removed transfer statistics (doesn't make sense here)
[collectd.git] / src / openvpn.c
1 /**
2  * collectd - src/openvpn.c
3  * Copyright (C) 2008  Doug MacEachern
4  * Copyright (C) 2009  Florian octo Forster
5  * Copyright (C) 2009  Marco Chiappero
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Doug MacEachern <dougm at hyperic.com>
22  *   Florian octo Forster <octo at verplant.org>
23  *   Marco Chiappero <marco at absence.it>
24  **/
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29
30 #define V1STRING "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
31 #define V2STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
32 #define V3STRING "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)\n"
33 #define VSSTRING "OpenVPN STATISTICS\n"
34
35
36 struct vpn_status_s
37 {
38         char *file;
39         enum
40         {
41                 MULTI1 = 1, /* status-version 1 */
42                 MULTI2,     /* status-version 2 */
43                 MULTI3,     /* status-version 3 */
44                 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
45         } version;
46         char *name;
47 };
48 typedef struct vpn_status_s vpn_status_t;
49
50 static vpn_status_t **vpn_list = NULL;
51 static int vpn_num = 0;
52
53 static int store_compression = 1;
54 static int new_naming_schema = 0;
55 static int number_connectedusers  = 0;
56
57 static const char *config_keys[] =
58 {
59         "StatusFile",
60         "Compression",
61         "ImprovedNamingSchema",
62  "AggregateUsers"
63 };
64 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
65
66
67 /* Helper function
68  * copy-n-pasted from common.c - changed delim to ","  */
69 static int openvpn_strsplit (char *string, char **fields, size_t size)
70 {
71         size_t i;
72         char *ptr;
73         char *saveptr;
74
75         i = 0;
76         ptr = string;
77         saveptr = NULL;
78         while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
79         {
80                 ptr = NULL;
81                 i++;
82
83                 if (i >= size)
84                         break;
85         }
86
87         return (i);
88 } /* int openvpn_strsplit */
89
90 /* dispatches number of users */
91 static void numusers_submit (char *pinst, char *tinst, gauge_t value)
92 {
93         value_t values[1];
94         value_list_t vl = VALUE_LIST_INIT;
95
96         values[0].gauge = value;
97
98         vl.values = values;
99         vl.values_len = STATIC_ARRAY_SIZE (values);
100         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
101         sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
102         sstrncpy (vl.type, "users", sizeof (vl.type));
103         if (pinst != NULL)
104                 sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
105         if (tinst != NULL)
106                 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
107
108         plugin_dispatch_values (&vl);
109 } /* void numusers_submit */
110
111 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
112 static void iostats_submit (char *pinst, char *tinst, counter_t rx, counter_t tx)
113 {
114         value_t values[2];
115         value_list_t vl = VALUE_LIST_INIT;
116
117         values[0].counter = rx;
118         values[1].counter = tx;
119
120         /* NOTE ON THE NEW NAMING SCHEMA:
121          *       using plugin_instance to identify each vpn config (and
122          *       status) file; using type_instance to identify the endpoint
123          *       host when in multimode, traffic or overhead when in single.
124          */
125
126         vl.values = values;
127         vl.values_len = STATIC_ARRAY_SIZE (values);
128         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
129         sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
130         if (pinst != NULL)
131                 sstrncpy (vl.plugin_instance, pinst,
132                                 sizeof (vl.plugin_instance));
133         sstrncpy (vl.type, "if_octets", sizeof (vl.type));
134         if (tinst != NULL)
135                 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
136
137         plugin_dispatch_values (&vl);
138 } /* void traffic_submit */
139
140 /* dispatches stats about data compression shown when in single mode */
141 static void compression_submit (char *pinst, char *tinst,
142                 counter_t uncompressed, counter_t compressed)
143 {
144         value_t values[2];
145         value_list_t vl = VALUE_LIST_INIT;
146
147         values[0].counter = uncompressed;
148         values[1].counter = compressed;
149
150         vl.values = values;
151         vl.values_len = STATIC_ARRAY_SIZE (values);
152         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
153         sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
154         if (pinst != NULL)
155                 sstrncpy (vl.plugin_instance, pinst,
156                                 sizeof (vl.plugin_instance));
157         sstrncpy (vl.type, "compression", sizeof (vl.type));
158         if (tinst != NULL)
159                 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
160
161         plugin_dispatch_values (&vl);
162 } /* void compression_submit */
163
164 static int single_read (char *name, FILE *fh)
165 {
166         char buffer[1024];
167         char *fields[4];
168         const int max_fields = STATIC_ARRAY_SIZE (fields);
169         int  fields_num, read = 0;
170
171         counter_t link_rx, link_tx;
172         counter_t tun_rx, tun_tx;
173         counter_t pre_compress, post_compress;
174         counter_t pre_decompress, post_decompress;
175         counter_t overhead_rx, overhead_tx;
176
177         link_rx = 0;
178         link_tx = 0;
179         tun_rx = 0;
180         tun_tx = 0;
181         pre_compress = 0;
182         post_compress = 0;
183         pre_decompress = 0;
184         post_decompress = 0;
185         overhead_rx = 0;
186         overhead_tx = 0;
187
188         while (fgets (buffer, sizeof (buffer), fh) != NULL)
189         {
190                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
191
192                 /* status file is generated by openvpn/sig.c:print_status()
193                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
194                  *
195                  * The line we're expecting has 2 fields. We ignore all lines
196                  *  with more or less fields.
197                  */
198                 if (fields_num != 2)
199                 {
200                         continue;
201                 }
202
203                 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
204                 {
205                         /* read from the system and sent over the tunnel */
206                         tun_tx = atoll (fields[1]);
207                 }
208                 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
209                 {
210                         /* read from the tunnel and written in the system */
211                         tun_rx = atoll (fields[1]);
212                 }
213                 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
214                 {
215                         link_rx = atoll (fields[1]);
216                 }
217                 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
218                 {
219                         link_tx = atoll (fields[1]);
220                 }
221                 else if (strcmp (fields[0], "pre-compress bytes") == 0)
222                 {
223                         pre_compress = atoll (fields[1]);
224                 }
225                 else if (strcmp (fields[0], "post-compress bytes") == 0)
226                 {
227                         post_compress = atoll (fields[1]);
228                 }
229                 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
230                 {
231                         pre_decompress = atoll (fields[1]);
232                 }
233                 else if (strcmp (fields[0], "post-decompress bytes") == 0)
234                 {
235                         post_decompress = atoll (fields[1]);
236                 }
237         }
238
239         iostats_submit (name, "traffic", link_rx, link_tx);
240
241         /* we need to force this order to avoid negative values with these unsigned */
242         overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
243         overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
244
245         iostats_submit (name, "overhead", overhead_rx, overhead_tx);
246
247         if (store_compression)
248         {
249                 compression_submit (name, "data_in", post_decompress, pre_decompress);
250                 compression_submit (name, "data_out", pre_compress, post_compress);
251         }
252
253         read = 1;
254
255         return (read);
256 } /* int single_read */
257
258 /* for reading status version 1 */
259 static int multi1_read (char *name, FILE *fh)
260 {
261         char buffer[1024];
262         char *fields[10];
263         int  fields_num, read = 0, found_header = 0;
264         long long sum_users = 0;
265
266         /* read the file until the "ROUTING TABLE" line is found (no more info after) */
267         while (fgets (buffer, sizeof (buffer), fh) != NULL)
268         {
269                 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
270                         break;
271
272                 if (strcmp (buffer, V1STRING) == 0)
273                 {
274                         found_header = 1;
275                         continue;
276                 }
277
278                 /* skip the first lines until the client list section is found */
279                 if (found_header == 0)
280                         /* we can't start reading data until this string is found */
281                         continue;
282
283                 fields_num = openvpn_strsplit (buffer,
284                                 fields, STATIC_ARRAY_SIZE (fields));
285                 if (fields_num < 4)
286                         continue;
287
288                 if (number_connectedusers)
289                 /* If so, sum all users, ignore the individuals*/
290                 {
291                  sum_users += 1;
292                 }
293
294                 if (new_naming_schema)
295                 {
296                         iostats_submit (fields[0],          /* "Common Name" */
297                                         NULL,               /* unused when in multimode */
298                                         atoll (fields[2]),  /* "Bytes Received" */
299                                         atoll (fields[3])); /* "Bytes Sent" */
300                 }
301                 else
302                 {
303                         iostats_submit (name,               /* vpn instance */
304                                         fields[0],          /* "Common Name" */
305                                         atoll (fields[2]),  /* "Bytes Received" */
306                                         atoll (fields[3])); /* "Bytes Sent" */
307                 }
308
309                 read = 1;
310         }
311
312         if (number_connectedusers && read == 1)
313         {
314                         numusers_submit(name, name, sum_users);
315         }
316
317         return (read);
318 } /* int multi1_read */
319
320 /* for reading status version 2 */
321 static int multi2_read (char *name, FILE *fh)
322 {
323         char buffer[1024];
324         char *fields[10];
325         const int max_fields = STATIC_ARRAY_SIZE (fields);
326         int  fields_num, read = 0;
327         long long sum_users    = 0;
328
329         while (fgets (buffer, sizeof (buffer), fh) != NULL)
330         {
331                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
332
333                 /* status file is generated by openvpn/multi.c:multi_print_status()
334                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
335                  *
336                  * The line we're expecting has 8 fields. We ignore all lines
337                  *  with more or less fields.
338                  */
339                 if (fields_num != 8)
340                         continue;
341
342                 if (strcmp (fields[0], "CLIENT_LIST") != 0)
343                         continue;
344
345                 if (number_connectedusers)
346                 /* If so, sum all users, ignore the individuals*/
347                 {
348                  sum_users += 1;
349                 }
350
351                 if (new_naming_schema)
352                 {
353                         /* plugin inst = file name, type inst = fields[1] */
354                         iostats_submit (name,               /* vpn instance */
355                                         fields[1],          /* "Common Name" */
356                                         atoll (fields[4]),  /* "Bytes Received" */
357                                         atoll (fields[5])); /* "Bytes Sent" */
358                 }
359                 else
360                 {
361                         /* plugin inst = fields[1], type inst = "" */
362                         iostats_submit (fields[1],          /* "Common Name" */
363                                         NULL,               /* unused when in multimode */
364                                         atoll (fields[4]),  /* "Bytes Received" */
365                                         atoll (fields[5])); /* "Bytes Sent" */
366                 }
367
368                 read = 1;
369         }
370
371         if (number_connectedusers && read == 1)
372         {
373                         numusers_submit(name, name, sum_users);
374                 }
375
376         return (read);
377 } /* int multi2_read */
378
379 /* for reading status version 3 */
380 static int multi3_read (char *name, FILE *fh)
381 {
382         char buffer[1024];
383         char *fields[15];
384         const int max_fields = STATIC_ARRAY_SIZE (fields);
385         int  fields_num, read = 0;
386         long long sum_users    = 0;
387
388         while (fgets (buffer, sizeof (buffer), fh) != NULL)
389         {
390                 fields_num = strsplit (buffer, fields, max_fields);
391
392                 /* status file is generated by openvpn/multi.c:multi_print_status()
393                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
394                  *
395                  * The line we're expecting has 12 fields. We ignore all lines
396                  *  with more or less fields.
397                  */
398                 if (fields_num != 12)
399                 {
400                         continue;
401                 }
402                 else
403                 {
404                         if (strcmp (fields[0], "CLIENT_LIST") != 0)
405                                 continue;
406
407                         if (number_connectedusers)
408                  /* If so, sum all users, ignore the individuals*/
409                         {
410                   sum_users += 1;
411                         }
412
413                         if (new_naming_schema)
414                         {
415                                 iostats_submit (name,               /* vpn instance */
416                                                 fields[1],          /* "Common Name" */
417                                                 atoll (fields[4]),  /* "Bytes Received" */
418                                                 atoll (fields[5])); /* "Bytes Sent" */
419                         }
420                         else
421                         {
422                                 iostats_submit (fields[1],          /* "Common Name" */
423                                                 NULL,               /* unused when in multimode */
424                                                 atoll (fields[4]),  /* "Bytes Received" */
425                                                 atoll (fields[5])); /* "Bytes Sent" */
426                         }
427
428                  read = 1;
429                 }
430         }
431
432         if (number_connectedusers && read == 1)
433         {
434                         numusers_submit(name, name, sum_users);
435         }
436
437         return (read);
438 } /* int multi3_read */
439
440 /* read callback */
441 static int openvpn_read (void)
442 {
443         FILE *fh;
444         int  i, read;
445
446         read = 0;
447
448         /* call the right read function for every status entry in the list */
449         for (i = 0; i < vpn_num; i++)
450         {
451                 fh = fopen (vpn_list[i]->file, "r");
452                 if (fh == NULL)
453                 {
454                         char errbuf[1024];
455                         WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
456                                 sstrerror (errno, errbuf, sizeof (errbuf)));
457
458                         continue;
459                 }
460
461                 switch (vpn_list[i]->version)
462                 {
463                         case SINGLE:
464                                 read = single_read(vpn_list[i]->name, fh);
465                                 break;
466
467                         case MULTI1:
468                                 read = multi1_read(vpn_list[i]->name, fh);
469                                 break;
470
471                         case MULTI2:
472                                 read = multi2_read(vpn_list[i]->name, fh);
473                                 break;
474
475                         case MULTI3:
476                                 read = multi3_read(vpn_list[i]->name, fh);
477                                 break;
478                 }
479
480                 fclose (fh);
481         }
482
483         return (read ? 0 : -1);
484 } /* int openvpn_read */
485
486 static int version_detect (const char *filename)
487 {
488         FILE *fh;
489         char buffer[1024];
490         int version = 0;
491
492         /* Sanity checking. We're called from the config handling routine, so
493          * better play it save. */
494         if ((filename == NULL) || (*filename == 0))
495                 return (0);
496
497         fh = fopen (filename, "r");
498         if (fh == NULL)
499         {
500                 char errbuf[1024];
501                 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
502                                 sstrerror (errno, errbuf, sizeof (errbuf)));
503                 return (0);
504         }
505
506         /* now search for the specific multimode data format */
507         while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
508         {
509                 /* we look at the first line searching for SINGLE mode configuration */
510                 if (strcmp (buffer, VSSTRING) == 0)
511                 {
512                         DEBUG ("openvpn plugin: found status file version SINGLE");
513                         version = SINGLE;
514                         break;
515                 }
516                 /* searching for multi version 1 */
517                 else if (strcmp (buffer, V1STRING) == 0)
518                 {
519                         DEBUG ("openvpn plugin: found status file version MULTI1");
520                         version = MULTI1;
521                         break;
522                 }
523                 /* searching for multi version 2 */
524                 else if (strcmp (buffer, V2STRING) == 0)
525                 {
526                         DEBUG ("openvpn plugin: found status file version MULTI2");
527                         version = MULTI2;
528                         break;
529                 }
530                 /* searching for multi version 3 */
531                 else if (strcmp (buffer, V3STRING) == 0)
532                 {
533                         DEBUG ("openvpn plugin: found status file version MULTI3");
534                         version = MULTI3;
535                         break;
536                 }
537         }
538
539         if (version == 0)
540         {
541                 /* This is only reached during configuration, so complaining to
542                  * the user is in order. */
543                 NOTICE ("openvpn plugin: %s: Unknown file format, please "
544                                 "report this as bug. Make sure to include "
545                                 "your status file, so the plugin can "
546                                 "be adapted.", filename);
547         }
548
549         fclose (fh);
550
551         return version;
552 } /* int version_detect */
553
554 static int openvpn_config (const char *key, const char *value)
555 {
556         if (strcasecmp ("StatusFile", key) == 0)
557         {
558                 char    *status_file, *status_name, *filename;
559                 int     status_version, i;
560                 vpn_status_t *temp;
561
562                 /* try to detect the status file format */
563                 status_version = version_detect (value);
564
565                 if (status_version == 0)
566                 {
567                         WARNING ("openvpn plugin: unable to detect status version, \
568                                 discarding status file \"%s\".", value);
569                         return (1);
570                 }
571
572                 status_file = sstrdup (value);
573                 if (status_file == NULL)
574                 {
575                         char errbuf[1024];
576                         WARNING ("openvpn plugin: sstrdup failed: %s",
577                                 sstrerror (errno, errbuf, sizeof (errbuf)));
578                         return (1);
579                 }
580
581                 /* it determines the file name as string starting at location filename + 1 */
582                 filename = strrchr (status_file, (int) '/');
583                 if (filename == NULL)
584                 {
585                         /* status_file is already the file name only */
586                         status_name = status_file;
587                 }
588                 else
589                 {
590                         /* doesn't waste memory, uses status_file starting at filename + 1 */
591                         status_name = filename + 1;
592                 }
593
594                 /* scan the list looking for a clone */
595                 for (i = 0; i < vpn_num; i++)
596                 {
597                         if (strcasecmp (vpn_list[i]->name, status_name) == 0)
598                         {
599                                 WARNING ("openvpn plugin: status filename \"%s\" "
600                                                 "already used, please choose a "
601                                                 "different one.", status_name);
602                                 sfree (status_file);
603                                 return (1);
604                         }
605                 }
606
607                 /* create a new vpn element since file, version and name are ok */
608                 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
609                 temp->file = status_file;
610                 temp->version = status_version;
611                 temp->name = status_name;
612
613                 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
614                 if (vpn_list == NULL)
615                 {
616                         char errbuf[1024];
617                         ERROR ("openvpn plugin: malloc failed: %s",
618                                 sstrerror (errno, errbuf, sizeof (errbuf)));
619
620                         sfree (temp->file);
621                         sfree (temp);
622                         return (1);
623                 }
624
625                 vpn_list[vpn_num] = temp;
626                 vpn_num++;
627
628                 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
629
630         } /* if (strcasecmp ("StatusFile", key) == 0) */
631         else if (strcasecmp ("Compression", key) == 0)
632         {
633                 if (IS_TRUE (value))
634                         store_compression = 1;
635                 else
636                 {
637                         store_compression = 0;
638                         DEBUG ("openvpn plugin: no 'compression statistcs' collected");
639                 }
640         } /* if (strcasecmp ("Compression", key) == 0) */
641         else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
642         {
643                 if (IS_TRUE (value))
644                 {
645                         DEBUG ("openvpn plugin: using the new naming schema");
646                         new_naming_schema = 1;
647                 }
648                 else
649                 {
650                         new_naming_schema = 0;
651                 }
652         } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
653         else if (strcasecmp("AggregateUsers", key) == 0)
654         {
655                 if (IS_TRUE(value))
656                 {
657                         DEBUG ("openvpn plugin: Summing up all users");
658                  number_connectedusers = 1;
659                 }
660                 else
661                 {
662                  number_connectedusers = 0;
663                 }
664         } /* if (strcasecmp("AggregateUsers", key) == 0) */
665         else
666         {
667                 return (-1);
668         }
669
670         return (0);
671 } /* int openvpn_config */
672
673 /* shutdown callback */
674 static int openvpn_shutdown (void)
675 {
676         int i;
677
678         for (i = 0; i < vpn_num; i++)
679         {
680                 sfree (vpn_list[i]->file);
681                 sfree (vpn_list[i]);
682         }
683
684         sfree (vpn_list);
685
686         return (0);
687 } /* int openvpn_shutdown */
688
689 void module_register (void)
690 {
691         plugin_register_config ("openvpn", openvpn_config,
692                         config_keys, config_keys_num);
693         plugin_register_read ("openvpn", openvpn_read);
694         plugin_register_shutdown ("openvpn", openvpn_shutdown);
695 } /* void module_register */
696
697 /* vim: set sw=2 ts=2 : */