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