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