1 /**
2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2009 Florian octo Forster
5 * Copyright (C) 2009 Marco Chiappero
6 * Copyright (C) 2009 Fabian Schuh
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * Authors:
22 * Doug MacEachern <dougm at hyperic.com>
23 * Florian octo Forster <octo at verplant.org>
24 * Marco Chiappero <marco at absence.it>
25 * Fabian Schuh <mail at xeroc.org>
26 **/
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #define V1STRING "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
33 #define V2STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
34 #define V3STRING "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)\n"
35 #define VSSTRING "OpenVPN STATISTICS\n"
38 struct vpn_status_s
39 {
40 char *file;
41 enum
42 {
43 MULTI1 = 1, /* status-version 1 */
44 MULTI2, /* status-version 2 */
45 MULTI3, /* status-version 3 */
46 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
47 } version;
48 char *name;
49 };
50 typedef struct vpn_status_s vpn_status_t;
52 static vpn_status_t **vpn_list = NULL;
53 static int vpn_num = 0;
55 static _Bool new_naming_schema = 0;
56 static _Bool collect_compression = 1;
57 static _Bool collect_user_count = 0;
58 static _Bool collect_individual_users = 1;
60 static const char *config_keys[] =
61 {
62 "StatusFile",
63 "Compression", /* old, deprecated name */
64 "ImprovedNamingSchema",
65 "CollectCompression",
66 "CollectUserCount",
67 "CollectIndividualUsers"
68 };
69 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
72 /* Helper function
73 * copy-n-pasted from common.c - changed delim to "," */
74 static int openvpn_strsplit (char *string, char **fields, size_t size)
75 {
76 size_t i;
77 char *ptr;
78 char *saveptr;
80 i = 0;
81 ptr = string;
82 saveptr = NULL;
83 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
84 {
85 ptr = NULL;
86 i++;
88 if (i >= size)
89 break;
90 }
92 return (i);
93 } /* int openvpn_strsplit */
95 /* dispatches number of users */
96 static void numusers_submit (char *pinst, char *tinst, gauge_t value)
97 {
98 value_t values[1];
99 value_list_t vl = VALUE_LIST_INIT;
101 values[0].gauge = value;
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 sstrncpy (vl.type, "users", sizeof (vl.type));
108 if (pinst != NULL)
109 sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
110 if (tinst != NULL)
111 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
113 plugin_dispatch_values (&vl);
114 } /* void numusers_submit */
116 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
117 static void iostats_submit (char *pinst, char *tinst, counter_t rx, counter_t tx)
118 {
119 value_t values[2];
120 value_list_t vl = VALUE_LIST_INIT;
122 values[0].counter = rx;
123 values[1].counter = tx;
125 /* NOTE ON THE NEW NAMING SCHEMA:
126 * using plugin_instance to identify each vpn config (and
127 * status) file; using type_instance to identify the endpoint
128 * host when in multimode, traffic or overhead when in single.
129 */
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, "if_octets", sizeof (vl.type));
139 if (tinst != NULL)
140 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
142 plugin_dispatch_values (&vl);
143 } /* void traffic_submit */
145 /* dispatches stats about data compression shown when in single mode */
146 static void compression_submit (char *pinst, char *tinst,
147 counter_t uncompressed, counter_t compressed)
148 {
149 value_t values[2];
150 value_list_t vl = VALUE_LIST_INIT;
152 values[0].counter = uncompressed;
153 values[1].counter = compressed;
155 vl.values = values;
156 vl.values_len = STATIC_ARRAY_SIZE (values);
157 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
158 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
159 if (pinst != NULL)
160 sstrncpy (vl.plugin_instance, pinst,
161 sizeof (vl.plugin_instance));
162 sstrncpy (vl.type, "compression", sizeof (vl.type));
163 if (tinst != NULL)
164 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
166 plugin_dispatch_values (&vl);
167 } /* void compression_submit */
169 static int single_read (char *name, FILE *fh)
170 {
171 char buffer[1024];
172 char *fields[4];
173 const int max_fields = STATIC_ARRAY_SIZE (fields);
174 int fields_num, read = 0;
176 counter_t link_rx, link_tx;
177 counter_t tun_rx, tun_tx;
178 counter_t pre_compress, post_compress;
179 counter_t pre_decompress, post_decompress;
180 counter_t overhead_rx, overhead_tx;
182 link_rx = 0;
183 link_tx = 0;
184 tun_rx = 0;
185 tun_tx = 0;
186 pre_compress = 0;
187 post_compress = 0;
188 pre_decompress = 0;
189 post_decompress = 0;
190 overhead_rx = 0;
191 overhead_tx = 0;
193 while (fgets (buffer, sizeof (buffer), fh) != NULL)
194 {
195 fields_num = openvpn_strsplit (buffer, fields, max_fields);
197 /* status file is generated by openvpn/sig.c:print_status()
198 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
199 *
200 * The line we're expecting has 2 fields. We ignore all lines
201 * with more or less fields.
202 */
203 if (fields_num != 2)
204 {
205 continue;
206 }
208 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
209 {
210 /* read from the system and sent over the tunnel */
211 tun_tx = atoll (fields[1]);
212 }
213 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
214 {
215 /* read from the tunnel and written in the system */
216 tun_rx = atoll (fields[1]);
217 }
218 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
219 {
220 link_rx = atoll (fields[1]);
221 }
222 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
223 {
224 link_tx = atoll (fields[1]);
225 }
226 else if (strcmp (fields[0], "pre-compress bytes") == 0)
227 {
228 pre_compress = atoll (fields[1]);
229 }
230 else if (strcmp (fields[0], "post-compress bytes") == 0)
231 {
232 post_compress = atoll (fields[1]);
233 }
234 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
235 {
236 pre_decompress = atoll (fields[1]);
237 }
238 else if (strcmp (fields[0], "post-decompress bytes") == 0)
239 {
240 post_decompress = atoll (fields[1]);
241 }
242 }
244 iostats_submit (name, "traffic", link_rx, link_tx);
246 /* we need to force this order to avoid negative values with these unsigned */
247 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
248 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
250 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
252 if (collect_compression)
253 {
254 compression_submit (name, "data_in", post_decompress, pre_decompress);
255 compression_submit (name, "data_out", pre_compress, post_compress);
256 }
258 read = 1;
260 return (read);
261 } /* int single_read */
263 /* for reading status version 1 */
264 static int multi1_read (char *name, FILE *fh)
265 {
266 char buffer[1024];
267 char *fields[10];
268 int fields_num, read = 0, found_header = 0;
269 long long sum_users = 0;
271 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
272 while (fgets (buffer, sizeof (buffer), fh) != NULL)
273 {
274 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
275 break;
277 if (strcmp (buffer, V1STRING) == 0)
278 {
279 found_header = 1;
280 continue;
281 }
283 /* skip the first lines until the client list section is found */
284 if (found_header == 0)
285 /* we can't start reading data until this string is found */
286 continue;
288 fields_num = openvpn_strsplit (buffer,
289 fields, STATIC_ARRAY_SIZE (fields));
290 if (fields_num < 4)
291 continue;
293 if (collect_user_count)
294 /* If so, sum all users, ignore the individuals*/
295 {
296 sum_users += 1;
297 }
298 if (collect_individual_users)
299 {
300 if (new_naming_schema)
301 {
302 iostats_submit (name, /* vpn instance */
303 fields[0], /* "Common Name" */
304 atoll (fields[2]), /* "Bytes Received" */
305 atoll (fields[3])); /* "Bytes Sent" */
306 }
307 else
308 {
309 iostats_submit (fields[0], /* "Common Name" */
310 NULL, /* unused when in multimode */
311 atoll (fields[2]), /* "Bytes Received" */
312 atoll (fields[3])); /* "Bytes Sent" */
313 }
314 }
316 read = 1;
317 }
319 if (collect_user_count)
320 {
321 numusers_submit(name, name, sum_users);
322 read = 1;
323 }
325 return (read);
326 } /* int multi1_read */
328 /* for reading status version 2 */
329 static int multi2_read (char *name, FILE *fh)
330 {
331 char buffer[1024];
332 char *fields[10];
333 const int max_fields = STATIC_ARRAY_SIZE (fields);
334 int fields_num, read = 0;
335 long long sum_users = 0;
337 while (fgets (buffer, sizeof (buffer), fh) != NULL)
338 {
339 fields_num = openvpn_strsplit (buffer, fields, max_fields);
341 /* status file is generated by openvpn/multi.c:multi_print_status()
342 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
343 *
344 * The line we're expecting has 8 fields. We ignore all lines
345 * with more or less fields.
346 */
347 if (fields_num != 8)
348 continue;
350 if (strcmp (fields[0], "CLIENT_LIST") != 0)
351 continue;
353 if (collect_user_count)
354 /* If so, sum all users, ignore the individuals*/
355 {
356 sum_users += 1;
357 }
358 if (collect_individual_users)
359 {
360 if (new_naming_schema)
361 {
362 /* plugin inst = file name, type inst = fields[1] */
363 iostats_submit (name, /* vpn instance */
364 fields[1], /* "Common Name" */
365 atoll (fields[4]), /* "Bytes Received" */
366 atoll (fields[5])); /* "Bytes Sent" */
367 }
368 else
369 {
370 /* plugin inst = fields[1], type inst = "" */
371 iostats_submit (fields[1], /* "Common Name" */
372 NULL, /* unused when in multimode */
373 atoll (fields[4]), /* "Bytes Received" */
374 atoll (fields[5])); /* "Bytes Sent" */
375 }
376 }
378 read = 1;
379 }
381 if (collect_user_count)
382 {
383 numusers_submit(name, name, sum_users);
384 read = 1;
385 }
387 return (read);
388 } /* int multi2_read */
390 /* for reading status version 3 */
391 static int multi3_read (char *name, FILE *fh)
392 {
393 char buffer[1024];
394 char *fields[15];
395 const int max_fields = STATIC_ARRAY_SIZE (fields);
396 int fields_num, read = 0;
397 long long sum_users = 0;
399 while (fgets (buffer, sizeof (buffer), fh) != NULL)
400 {
401 fields_num = strsplit (buffer, fields, max_fields);
403 /* status file is generated by openvpn/multi.c:multi_print_status()
404 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
405 *
406 * The line we're expecting has 12 fields. We ignore all lines
407 * with more or less fields.
408 */
409 if (fields_num != 12)
410 {
411 continue;
412 }
413 else
414 {
415 if (strcmp (fields[0], "CLIENT_LIST") != 0)
416 continue;
418 if (collect_user_count)
419 /* If so, sum all users, ignore the individuals*/
420 {
421 sum_users += 1;
422 }
424 if (collect_individual_users)
425 {
426 if (new_naming_schema)
427 {
428 iostats_submit (name, /* vpn instance */
429 fields[1], /* "Common Name" */
430 atoll (fields[4]), /* "Bytes Received" */
431 atoll (fields[5])); /* "Bytes Sent" */
432 }
433 else
434 {
435 iostats_submit (fields[1], /* "Common Name" */
436 NULL, /* unused when in multimode */
437 atoll (fields[4]), /* "Bytes Received" */
438 atoll (fields[5])); /* "Bytes Sent" */
439 }
440 }
442 read = 1;
443 }
444 }
446 if (collect_user_count)
447 {
448 numusers_submit(name, name, sum_users);
449 read = 1;
450 }
452 return (read);
453 } /* int multi3_read */
455 /* read callback */
456 static int openvpn_read (void)
457 {
458 FILE *fh;
459 int i, read;
461 read = 0;
463 /* call the right read function for every status entry in the list */
464 for (i = 0; i < vpn_num; i++)
465 {
466 fh = fopen (vpn_list[i]->file, "r");
467 if (fh == NULL)
468 {
469 char errbuf[1024];
470 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
471 sstrerror (errno, errbuf, sizeof (errbuf)));
473 continue;
474 }
476 switch (vpn_list[i]->version)
477 {
478 case SINGLE:
479 read = single_read(vpn_list[i]->name, fh);
480 break;
482 case MULTI1:
483 read = multi1_read(vpn_list[i]->name, fh);
484 break;
486 case MULTI2:
487 read = multi2_read(vpn_list[i]->name, fh);
488 break;
490 case MULTI3:
491 read = multi3_read(vpn_list[i]->name, fh);
492 break;
493 }
495 fclose (fh);
496 }
498 return (read ? 0 : -1);
499 } /* int openvpn_read */
501 static int version_detect (const char *filename)
502 {
503 FILE *fh;
504 char buffer[1024];
505 int version = 0;
507 /* Sanity checking. We're called from the config handling routine, so
508 * better play it save. */
509 if ((filename == NULL) || (*filename == 0))
510 return (0);
512 fh = fopen (filename, "r");
513 if (fh == NULL)
514 {
515 char errbuf[1024];
516 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
517 sstrerror (errno, errbuf, sizeof (errbuf)));
518 return (0);
519 }
521 /* now search for the specific multimode data format */
522 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
523 {
524 /* we look at the first line searching for SINGLE mode configuration */
525 if (strcmp (buffer, VSSTRING) == 0)
526 {
527 DEBUG ("openvpn plugin: found status file version SINGLE");
528 version = SINGLE;
529 break;
530 }
531 /* searching for multi version 1 */
532 else if (strcmp (buffer, V1STRING) == 0)
533 {
534 DEBUG ("openvpn plugin: found status file version MULTI1");
535 version = MULTI1;
536 break;
537 }
538 /* searching for multi version 2 */
539 else if (strcmp (buffer, V2STRING) == 0)
540 {
541 DEBUG ("openvpn plugin: found status file version MULTI2");
542 version = MULTI2;
543 break;
544 }
545 /* searching for multi version 3 */
546 else if (strcmp (buffer, V3STRING) == 0)
547 {
548 DEBUG ("openvpn plugin: found status file version MULTI3");
549 version = MULTI3;
550 break;
551 }
552 }
554 if (version == 0)
555 {
556 /* This is only reached during configuration, so complaining to
557 * the user is in order. */
558 NOTICE ("openvpn plugin: %s: Unknown file format, please "
559 "report this as bug. Make sure to include "
560 "your status file, so the plugin can "
561 "be adapted.", filename);
562 }
564 fclose (fh);
566 return version;
567 } /* int version_detect */
569 static int openvpn_config (const char *key, const char *value)
570 {
571 if (strcasecmp ("StatusFile", key) == 0)
572 {
573 char *status_file, *status_name, *filename;
574 int status_version, i;
575 vpn_status_t *temp;
577 /* try to detect the status file format */
578 status_version = version_detect (value);
580 if (status_version == 0)
581 {
582 WARNING ("openvpn plugin: unable to detect status version, \
583 discarding status file \"%s\".", value);
584 return (1);
585 }
587 status_file = sstrdup (value);
588 if (status_file == NULL)
589 {
590 char errbuf[1024];
591 WARNING ("openvpn plugin: sstrdup failed: %s",
592 sstrerror (errno, errbuf, sizeof (errbuf)));
593 return (1);
594 }
596 /* it determines the file name as string starting at location filename + 1 */
597 filename = strrchr (status_file, (int) '/');
598 if (filename == NULL)
599 {
600 /* status_file is already the file name only */
601 status_name = status_file;
602 }
603 else
604 {
605 /* doesn't waste memory, uses status_file starting at filename + 1 */
606 status_name = filename + 1;
607 }
609 /* scan the list looking for a clone */
610 for (i = 0; i < vpn_num; i++)
611 {
612 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
613 {
614 WARNING ("openvpn plugin: status filename \"%s\" "
615 "already used, please choose a "
616 "different one.", status_name);
617 sfree (status_file);
618 return (1);
619 }
620 }
622 /* create a new vpn element since file, version and name are ok */
623 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
624 temp->file = status_file;
625 temp->version = status_version;
626 temp->name = status_name;
628 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
629 if (vpn_list == NULL)
630 {
631 char errbuf[1024];
632 ERROR ("openvpn plugin: malloc failed: %s",
633 sstrerror (errno, errbuf, sizeof (errbuf)));
635 sfree (temp->file);
636 sfree (temp);
637 return (1);
638 }
640 vpn_list[vpn_num] = temp;
641 vpn_num++;
643 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
645 } /* if (strcasecmp ("StatusFile", key) == 0) */
646 else if ((strcasecmp ("CollectCompression", key) == 0)
647 || (strcasecmp ("Compression", key) == 0)) /* old, deprecated name */
648 {
649 if (IS_FALSE (value))
650 collect_compression = 0;
651 else
652 collect_compression = 1;
653 } /* if (strcasecmp ("CollectCompression", key) == 0) */
654 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
655 {
656 if (IS_TRUE (value))
657 {
658 DEBUG ("openvpn plugin: using the new naming schema");
659 new_naming_schema = 1;
660 }
661 else
662 {
663 new_naming_schema = 0;
664 }
665 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
666 else if (strcasecmp("CollectUserCount", key) == 0)
667 {
668 if (IS_TRUE(value))
669 collect_user_count = 1;
670 else
671 collect_user_count = 0;
672 } /* if (strcasecmp("CollectUserCount", key) == 0) */
673 else if (strcasecmp("CollectIndividualUsers", key) == 0)
674 {
675 if (IS_FALSE (value))
676 collect_individual_users = 0;
677 else
678 collect_individual_users = 1;
679 } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
680 else
681 {
682 return (-1);
683 }
685 return (0);
686 } /* int openvpn_config */
688 /* shutdown callback */
689 static int openvpn_shutdown (void)
690 {
691 int i;
693 for (i = 0; i < vpn_num; i++)
694 {
695 sfree (vpn_list[i]->file);
696 sfree (vpn_list[i]);
697 }
699 sfree (vpn_list);
701 return (0);
702 } /* int openvpn_shutdown */
704 static int openvpn_init (void)
705 {
706 if (!collect_individual_users
707 && !collect_compression
708 && !collect_user_count)
709 {
710 WARNING ("OpenVPN plugin: Neither `CollectIndividualUsers', "
711 "`CollectCompression', nor `CollectUserCount' is true. There's no "
712 "data left to collect.");
713 return (-1);
714 }
716 plugin_register_read ("openvpn", openvpn_read);
717 plugin_register_shutdown ("openvpn", openvpn_shutdown);
719 return (0);
720 } /* int openvpn_init */
722 void module_register (void)
723 {
724 plugin_register_config ("openvpn", openvpn_config,
725 config_keys, config_keys_num);
726 plugin_register_init ("openvpn", openvpn_init);
727 } /* void module_register */
729 /* vim: set sw=2 ts=2 : */