Code

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