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