1 /**
2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2009 Florian octo Forster
5 * Copyright (C) 2009 Marco Chiappero
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Doug MacEachern <dougm at hyperic.com>
22 * Florian octo Forster <octo at verplant.org>
23 * Marco Chiappero <marco at absence.it>
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
30 #define V1STRING "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
31 #define V2STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
32 #define V3STRING "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)\n"
33 #define VSSTRING "OpenVPN STATISTICS\n"
36 struct vpn_status_s
37 {
38 char *file;
39 enum
40 {
41 MULTI1 = 1, /* status-version 1 */
42 MULTI2, /* status-version 2 */
43 MULTI3, /* status-version 3 */
44 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
45 } version;
46 char *name;
47 };
48 typedef struct vpn_status_s vpn_status_t;
50 static vpn_status_t **vpn_list = NULL;
51 static int vpn_num = 0;
53 static int store_compression = 1;
54 static int new_naming_schema = 0;
55 static int sumover_allusers = 0;
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);
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;
75 i = 0;
76 ptr = string;
77 saveptr = NULL;
78 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
79 {
80 ptr = NULL;
81 i++;
83 if (i >= size)
84 break;
85 }
87 return (i);
88 } /* int openvpn_strsplit */
90 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
91 static void iostats_submit (char *pinst, char *tinst, counter_t rx, counter_t tx)
92 {
93 value_t values[2];
94 value_list_t vl = VALUE_LIST_INIT;
96 values[0].counter = rx;
97 values[1].counter = tx;
99 /* NOTE ON THE NEW NAMING SCHEMA:
100 * using plugin_instance to identify each vpn config (and
101 * status) file; using type_instance to identify the endpoint
102 * host when in multimode, traffic or overhead when in single.
103 */
105 vl.values = values;
106 vl.values_len = STATIC_ARRAY_SIZE (values);
107 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
108 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
109 if (pinst != NULL)
110 sstrncpy (vl.plugin_instance, pinst,
111 sizeof (vl.plugin_instance));
112 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
113 if (tinst != NULL)
114 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
116 plugin_dispatch_values (&vl);
117 } /* void traffic_submit */
119 /* dispatches stats about data compression shown when in single mode */
120 static void compression_submit (char *pinst, char *tinst,
121 counter_t uncompressed, counter_t compressed)
122 {
123 value_t values[2];
124 value_list_t vl = VALUE_LIST_INIT;
126 values[0].counter = uncompressed;
127 values[1].counter = compressed;
129 vl.values = values;
130 vl.values_len = STATIC_ARRAY_SIZE (values);
131 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
132 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
133 if (pinst != NULL)
134 sstrncpy (vl.plugin_instance, pinst,
135 sizeof (vl.plugin_instance));
136 sstrncpy (vl.type, "compression", sizeof (vl.type));
137 if (tinst != NULL)
138 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
140 plugin_dispatch_values (&vl);
141 } /* void compression_submit */
143 static int single_read (char *name, FILE *fh)
144 {
145 char buffer[1024];
146 char *fields[4];
147 const int max_fields = STATIC_ARRAY_SIZE (fields);
148 int fields_num, read = 0;
150 counter_t link_rx, link_tx;
151 counter_t tun_rx, tun_tx;
152 counter_t pre_compress, post_compress;
153 counter_t pre_decompress, post_decompress;
154 counter_t overhead_rx, overhead_tx;
156 link_rx = 0;
157 link_tx = 0;
158 tun_rx = 0;
159 tun_tx = 0;
160 pre_compress = 0;
161 post_compress = 0;
162 pre_decompress = 0;
163 post_decompress = 0;
164 overhead_rx = 0;
165 overhead_tx = 0;
167 while (fgets (buffer, sizeof (buffer), fh) != NULL)
168 {
169 fields_num = openvpn_strsplit (buffer, fields, max_fields);
171 /* status file is generated by openvpn/sig.c:print_status()
172 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
173 *
174 * The line we're expecting has 2 fields. We ignore all lines
175 * with more or less fields.
176 */
177 if (fields_num != 2)
178 {
179 continue;
180 }
182 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
183 {
184 /* read from the system and sent over the tunnel */
185 tun_tx = atoll (fields[1]);
186 }
187 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
188 {
189 /* read from the tunnel and written in the system */
190 tun_rx = atoll (fields[1]);
191 }
192 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
193 {
194 link_rx = atoll (fields[1]);
195 }
196 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
197 {
198 link_tx = atoll (fields[1]);
199 }
200 else if (strcmp (fields[0], "pre-compress bytes") == 0)
201 {
202 pre_compress = atoll (fields[1]);
203 }
204 else if (strcmp (fields[0], "post-compress bytes") == 0)
205 {
206 post_compress = atoll (fields[1]);
207 }
208 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
209 {
210 pre_decompress = atoll (fields[1]);
211 }
212 else if (strcmp (fields[0], "post-decompress bytes") == 0)
213 {
214 post_decompress = atoll (fields[1]);
215 }
216 }
218 iostats_submit (name, "traffic", link_rx, link_tx);
220 /* we need to force this order to avoid negative values with these unsigned */
221 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
222 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
224 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
226 if (store_compression)
227 {
228 compression_submit (name, "data_in", post_decompress, pre_decompress);
229 compression_submit (name, "data_out", pre_compress, post_compress);
230 }
232 read = 1;
234 return (read);
235 } /* int single_read */
237 /* for reading status version 1 */
238 static int multi1_read (char *name, FILE *fh)
239 {
240 char buffer[1024];
241 char *fields[10];
242 int fields_num, read = 0, found_header = 0;
243 long long sum_rx = 0;
244 long long sum_tx = 0;
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;
252 if (strcmp (buffer, V1STRING) == 0)
253 {
254 found_header = 1;
255 continue;
256 }
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;
263 fields_num = openvpn_strsplit (buffer,
264 fields, STATIC_ARRAY_SIZE (fields));
265 if (fields_num < 4)
266 continue;
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 }
292 read = 1;
293 }
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 }
303 return (read);
304 } /* int multi1_read */
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 long long sum_rx = 0;
314 long long sum_tx = 0;
316 while (fgets (buffer, sizeof (buffer), fh) != NULL)
317 {
318 fields_num = openvpn_strsplit (buffer, fields, max_fields);
320 /* status file is generated by openvpn/multi.c:multi_print_status()
321 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
322 *
323 * The line we're expecting has 8 fields. We ignore all lines
324 * with more or less fields.
325 */
326 if (fields_num != 8)
327 continue;
329 if (strcmp (fields[0], "CLIENT_LIST") != 0)
330 continue;
332 if (sumover_allusers)
333 /* If so, sum over all users, ignore the individuals*/
334 {
335 sum_rx += atoll(fields[2]);
336 sum_tx += atoll(fields[3]);
337 }
338 else
339 {
340 if (new_naming_schema)
341 {
342 /* plugin inst = file name, type inst = fields[1] */
343 iostats_submit (name, /* vpn instance */
344 fields[1], /* "Common Name" */
345 atoll (fields[4]), /* "Bytes Received" */
346 atoll (fields[5])); /* "Bytes Sent" */
347 }
348 else
349 {
350 /* plugin inst = fields[1], type inst = "" */
351 iostats_submit (fields[1], /* "Common Name" */
352 NULL, /* unused when in multimode */
353 atoll (fields[4]), /* "Bytes Received" */
354 atoll (fields[5])); /* "Bytes Sent" */
355 }
356 }
357 read = 1;
358 }
360 if (sumover_allusers)
361 {
362 iostats_submit (name, /* vpn instance */
363 "OverAllTraffic", /* "Common Name" */
364 sum_rx, /* "Bytes Received" */
365 sum_rx); /* "Bytes Sent" */
366 }
368 return (read);
369 } /* int multi2_read */
371 /* for reading status version 3 */
372 static int multi3_read (char *name, FILE *fh)
373 {
374 char buffer[1024];
375 char *fields[15];
376 const int max_fields = STATIC_ARRAY_SIZE (fields);
377 int fields_num, read = 0;
378 long long sum_rx = 0;
379 long long sum_tx = 0;
381 while (fgets (buffer, sizeof (buffer), fh) != NULL)
382 {
383 fields_num = strsplit (buffer, fields, max_fields);
385 /* status file is generated by openvpn/multi.c:multi_print_status()
386 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
387 *
388 * The line we're expecting has 12 fields. We ignore all lines
389 * with more or less fields.
390 */
391 if (fields_num != 12)
392 {
393 continue;
394 }
395 else
396 {
397 if (strcmp (fields[0], "CLIENT_LIST") != 0)
398 continue;
400 if (sumover_allusers)
401 /* If so, sum over all users, ignore the individuals*/
402 {
403 sum_rx += atoll(fields[2]);
404 sum_tx += atoll(fields[3]);
405 }
406 else
407 {
408 if (new_naming_schema)
409 {
410 iostats_submit (name, /* vpn instance */
411 fields[1], /* "Common Name" */
412 atoll (fields[4]), /* "Bytes Received" */
413 atoll (fields[5])); /* "Bytes Sent" */
414 }
415 else
416 {
417 iostats_submit (fields[1], /* "Common Name" */
418 NULL, /* unused when in multimode */
419 atoll (fields[4]), /* "Bytes Received" */
420 atoll (fields[5])); /* "Bytes Sent" */
421 }
422 }
424 read = 1;
425 }
426 }
428 if (sumover_allusers)
429 {
430 iostats_submit (name, /* vpn instance */
431 "OverAllTraffic", /* "Common Name" */
432 sum_rx, /* "Bytes Received" */
433 sum_rx); /* "Bytes Sent" */
434 }
436 return (read);
437 } /* int multi3_read */
439 /* read callback */
440 static int openvpn_read (void)
441 {
442 FILE *fh;
443 int i, read;
445 read = 0;
447 /* call the right read function for every status entry in the list */
448 for (i = 0; i < vpn_num; i++)
449 {
450 fh = fopen (vpn_list[i]->file, "r");
451 if (fh == NULL)
452 {
453 char errbuf[1024];
454 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
455 sstrerror (errno, errbuf, sizeof (errbuf)));
457 continue;
458 }
460 switch (vpn_list[i]->version)
461 {
462 case SINGLE:
463 read = single_read(vpn_list[i]->name, fh);
464 break;
466 case MULTI1:
467 read = multi1_read(vpn_list[i]->name, fh);
468 break;
470 case MULTI2:
471 read = multi2_read(vpn_list[i]->name, fh);
472 break;
474 case MULTI3:
475 read = multi3_read(vpn_list[i]->name, fh);
476 break;
477 }
479 fclose (fh);
480 }
482 return (read ? 0 : -1);
483 } /* int openvpn_read */
485 static int version_detect (const char *filename)
486 {
487 FILE *fh;
488 char buffer[1024];
489 int version = 0;
491 /* Sanity checking. We're called from the config handling routine, so
492 * better play it save. */
493 if ((filename == NULL) || (*filename == 0))
494 return (0);
496 fh = fopen (filename, "r");
497 if (fh == NULL)
498 {
499 char errbuf[1024];
500 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
501 sstrerror (errno, errbuf, sizeof (errbuf)));
502 return (0);
503 }
505 /* now search for the specific multimode data format */
506 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
507 {
508 /* we look at the first line searching for SINGLE mode configuration */
509 if (strcmp (buffer, VSSTRING) == 0)
510 {
511 DEBUG ("openvpn plugin: found status file version SINGLE");
512 version = SINGLE;
513 break;
514 }
515 /* searching for multi version 1 */
516 else if (strcmp (buffer, V1STRING) == 0)
517 {
518 DEBUG ("openvpn plugin: found status file version MULTI1");
519 version = MULTI1;
520 break;
521 }
522 /* searching for multi version 2 */
523 else if (strcmp (buffer, V2STRING) == 0)
524 {
525 DEBUG ("openvpn plugin: found status file version MULTI2");
526 version = MULTI2;
527 break;
528 }
529 /* searching for multi version 3 */
530 else if (strcmp (buffer, V3STRING) == 0)
531 {
532 DEBUG ("openvpn plugin: found status file version MULTI3");
533 version = MULTI3;
534 break;
535 }
536 }
538 if (version == 0)
539 {
540 /* This is only reached during configuration, so complaining to
541 * the user is in order. */
542 NOTICE ("openvpn plugin: %s: Unknown file format, please "
543 "report this as bug. Make sure to include "
544 "your status file, so the plugin can "
545 "be adapted.", filename);
546 }
548 fclose (fh);
550 return version;
551 } /* int version_detect */
553 static int openvpn_config (const char *key, const char *value)
554 {
555 if (strcasecmp ("StatusFile", key) == 0)
556 {
557 char *status_file, *status_name, *filename;
558 int status_version, i;
559 vpn_status_t *temp;
561 /* try to detect the status file format */
562 status_version = version_detect (value);
564 if (status_version == 0)
565 {
566 WARNING ("openvpn plugin: unable to detect status version, \
567 discarding status file \"%s\".", value);
568 return (1);
569 }
571 status_file = sstrdup (value);
572 if (status_file == NULL)
573 {
574 char errbuf[1024];
575 WARNING ("openvpn plugin: sstrdup failed: %s",
576 sstrerror (errno, errbuf, sizeof (errbuf)));
577 return (1);
578 }
580 /* it determines the file name as string starting at location filename + 1 */
581 filename = strrchr (status_file, (int) '/');
582 if (filename == NULL)
583 {
584 /* status_file is already the file name only */
585 status_name = status_file;
586 }
587 else
588 {
589 /* doesn't waste memory, uses status_file starting at filename + 1 */
590 status_name = filename + 1;
591 }
593 /* scan the list looking for a clone */
594 for (i = 0; i < vpn_num; i++)
595 {
596 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
597 {
598 WARNING ("openvpn plugin: status filename \"%s\" "
599 "already used, please choose a "
600 "different one.", status_name);
601 sfree (status_file);
602 return (1);
603 }
604 }
606 /* create a new vpn element since file, version and name are ok */
607 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
608 temp->file = status_file;
609 temp->version = status_version;
610 temp->name = status_name;
612 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
613 if (vpn_list == NULL)
614 {
615 char errbuf[1024];
616 ERROR ("openvpn plugin: malloc failed: %s",
617 sstrerror (errno, errbuf, sizeof (errbuf)));
619 sfree (temp->file);
620 sfree (temp);
621 return (1);
622 }
624 vpn_list[vpn_num] = temp;
625 vpn_num++;
627 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
629 } /* if (strcasecmp ("StatusFile", key) == 0) */
630 else if (strcasecmp ("Compression", key) == 0)
631 {
632 if (IS_TRUE (value))
633 store_compression = 1;
634 else
635 {
636 store_compression = 0;
637 DEBUG ("openvpn plugin: no 'compression statistcs' collected");
638 }
639 } /* if (strcasecmp ("Compression", key) == 0) */
640 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
641 {
642 if (IS_TRUE (value))
643 {
644 DEBUG ("openvpn plugin: using the new naming schema");
645 new_naming_schema = 1;
646 }
647 else
648 {
649 new_naming_schema = 0;
650 }
651 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
652 else if (strcasecmp("SumOverAllUsers", key) == 0)
653 {
654 if (IS_TRUE(value))
655 {
656 DEBUG ("openvpn plugin: Summing up over all users");
657 sumover_allusers = 1;
658 }
659 else
660 {
661 sumover_allusers = 1;
662 }
663 } /* if (strcasecmp("SumOverAllUsers", key) == 0) */
664 else
665 {
666 return (-1);
667 }
669 return (0);
670 } /* int openvpn_config */
672 /* shutdown callback */
673 static int openvpn_shutdown (void)
674 {
675 int i;
677 for (i = 0; i < vpn_num; i++)
678 {
679 sfree (vpn_list[i]->file);
680 sfree (vpn_list[i]);
681 }
683 sfree (vpn_list);
685 return (0);
686 } /* int openvpn_shutdown */
688 void module_register (void)
689 {
690 plugin_register_config ("openvpn", openvpn_config,
691 config_keys, config_keys_num);
692 plugin_register_read ("openvpn", openvpn_read);
693 plugin_register_shutdown ("openvpn", openvpn_shutdown);
694 } /* void module_register */
696 /* vim: set sw=2 ts=2 : */