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