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