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