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