1 /**
2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2009,2010 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 collectd.org>
24 * Marco Chiappero <marco at absence.it>
25 * Fabian Schuh <mail at xeroc.org>
26 **/
28 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
33 #define V1STRING \
34 "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
35 #define V2STRING \
36 "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes " \
37 "Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
38 #define V3STRING \
39 "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes " \
40 "Received Bytes Sent Connected Since Connected Since (time_t)\n"
41 #define V4STRING \
42 "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes " \
43 "Received,Bytes Sent,Connected Since,Connected Since (time_t),Username\n"
44 #define VSSTRING "OpenVPN STATISTICS\n"
46 struct vpn_status_s {
47 char *file;
48 enum {
49 MULTI1 = 1, /* status-version 1 */
50 MULTI2, /* status-version 2 */
51 MULTI3, /* status-version 3 */
52 MULTI4, /* status-version 4 */
53 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
54 } version;
55 char *name;
56 };
57 typedef struct vpn_status_s vpn_status_t;
59 static vpn_status_t **vpn_list = NULL;
60 static int vpn_num = 0;
62 static _Bool new_naming_schema = 0;
63 static _Bool collect_compression = 1;
64 static _Bool collect_user_count = 0;
65 static _Bool collect_individual_users = 1;
67 static const char *config_keys[] = {
68 "StatusFile", "Compression", /* old, deprecated name */
69 "ImprovedNamingSchema", "CollectCompression",
70 "CollectUserCount", "CollectIndividualUsers"};
71 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
73 /* Helper function
74 * copy-n-pasted from common.c - changed delim to "," */
75 static int openvpn_strsplit(char *string, char **fields, size_t size) {
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 ptr = NULL;
85 i++;
87 if (i >= size)
88 break;
89 }
91 return (i);
92 } /* int openvpn_strsplit */
94 /* dispatches number of users */
95 static void numusers_submit(const char *pinst, const char *tinst,
96 gauge_t value) {
97 value_t values[1];
98 value_list_t vl = VALUE_LIST_INIT;
100 values[0].gauge = value;
102 vl.values = values;
103 vl.values_len = STATIC_ARRAY_SIZE(values);
104 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
105 sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin));
106 sstrncpy(vl.type, "users", sizeof(vl.type));
107 if (pinst != NULL)
108 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
109 if (tinst != NULL)
110 sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance));
112 plugin_dispatch_values(&vl);
113 } /* void numusers_submit */
115 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel
116 * per single endpoint */
117 static void iostats_submit(const char *pinst, const char *tinst, derive_t rx,
118 derive_t tx) {
119 value_t values[2];
120 value_list_t vl = VALUE_LIST_INIT;
122 values[0].derive = rx;
123 values[1].derive = 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, sizeof(vl.plugin_instance));
137 sstrncpy(vl.type, "if_octets", sizeof(vl.type));
138 if (tinst != NULL)
139 sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance));
141 plugin_dispatch_values(&vl);
142 } /* void traffic_submit */
144 /* dispatches stats about data compression shown when in single mode */
145 static void compression_submit(const char *pinst, const char *tinst,
146 derive_t uncompressed, derive_t compressed) {
147 value_t values[2];
148 value_list_t vl = VALUE_LIST_INIT;
150 values[0].derive = uncompressed;
151 values[1].derive = compressed;
153 vl.values = values;
154 vl.values_len = STATIC_ARRAY_SIZE(values);
155 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
156 sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin));
157 if (pinst != NULL)
158 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
159 sstrncpy(vl.type, "compression", sizeof(vl.type));
160 if (tinst != NULL)
161 sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance));
163 plugin_dispatch_values(&vl);
164 } /* void compression_submit */
166 static int single_read(const char *name, FILE *fh) {
167 char buffer[1024];
168 char *fields[4];
169 const int max_fields = STATIC_ARRAY_SIZE(fields);
170 int fields_num, read = 0;
172 derive_t link_rx, link_tx;
173 derive_t tun_rx, tun_tx;
174 derive_t pre_compress, post_compress;
175 derive_t pre_decompress, post_decompress;
176 derive_t overhead_rx, overhead_tx;
178 link_rx = 0;
179 link_tx = 0;
180 tun_rx = 0;
181 tun_tx = 0;
182 pre_compress = 0;
183 post_compress = 0;
184 pre_decompress = 0;
185 post_decompress = 0;
187 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
188 fields_num = openvpn_strsplit(buffer, fields, max_fields);
190 /* status file is generated by openvpn/sig.c:print_status()
191 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
192 *
193 * The line we're expecting has 2 fields. We ignore all lines
194 * with more or less fields.
195 */
196 if (fields_num != 2) {
197 continue;
198 }
200 if (strcmp(fields[0], "TUN/TAP read bytes") == 0) {
201 /* read from the system and sent over the tunnel */
202 tun_tx = atoll(fields[1]);
203 } else if (strcmp(fields[0], "TUN/TAP write bytes") == 0) {
204 /* read from the tunnel and written in the system */
205 tun_rx = atoll(fields[1]);
206 } else if (strcmp(fields[0], "TCP/UDP read bytes") == 0) {
207 link_rx = atoll(fields[1]);
208 } else if (strcmp(fields[0], "TCP/UDP write bytes") == 0) {
209 link_tx = atoll(fields[1]);
210 } else if (strcmp(fields[0], "pre-compress bytes") == 0) {
211 pre_compress = atoll(fields[1]);
212 } else if (strcmp(fields[0], "post-compress bytes") == 0) {
213 post_compress = atoll(fields[1]);
214 } else if (strcmp(fields[0], "pre-decompress bytes") == 0) {
215 pre_decompress = atoll(fields[1]);
216 } else if (strcmp(fields[0], "post-decompress bytes") == 0) {
217 post_decompress = atoll(fields[1]);
218 }
219 }
221 iostats_submit(name, "traffic", link_rx, link_tx);
223 /* we need to force this order to avoid negative values with these unsigned */
224 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
225 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
227 iostats_submit(name, "overhead", overhead_rx, overhead_tx);
229 if (collect_compression) {
230 compression_submit(name, "data_in", post_decompress, pre_decompress);
231 compression_submit(name, "data_out", pre_compress, post_compress);
232 }
234 read = 1;
236 return (read);
237 } /* int single_read */
239 /* for reading status version 1 */
240 static int multi1_read(const char *name, FILE *fh) {
241 char buffer[1024];
242 char *fields[10];
243 int fields_num, found_header = 0;
244 long long sum_users = 0;
246 /* read the file until the "ROUTING TABLE" line is found (no more info after)
247 */
248 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
249 if (strcmp(buffer, "ROUTING TABLE\n") == 0)
250 break;
252 if (strcmp(buffer, V1STRING) == 0) {
253 found_header = 1;
254 continue;
255 }
257 /* skip the first lines until the client list section is found */
258 if (found_header == 0)
259 /* we can't start reading data until this string is found */
260 continue;
262 fields_num = openvpn_strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
263 if (fields_num < 4)
264 continue;
266 if (collect_user_count)
267 /* If so, sum all users, ignore the individuals*/
268 {
269 sum_users += 1;
270 }
271 if (collect_individual_users) {
272 if (new_naming_schema) {
273 iostats_submit(name, /* vpn instance */
274 fields[0], /* "Common Name" */
275 atoll(fields[2]), /* "Bytes Received" */
276 atoll(fields[3])); /* "Bytes Sent" */
277 } else {
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 }
284 }
286 if (ferror(fh))
287 return (0);
289 if (collect_user_count)
290 numusers_submit(name, name, sum_users);
292 return (1);
293 } /* int multi1_read */
295 /* for reading status version 2 */
296 static int multi2_read(const char *name, FILE *fh) {
297 char buffer[1024];
298 char *fields[10];
299 const int max_fields = STATIC_ARRAY_SIZE(fields);
300 int fields_num, read = 0;
301 long long sum_users = 0;
303 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
304 fields_num = openvpn_strsplit(buffer, fields, max_fields);
306 /* status file is generated by openvpn/multi.c:multi_print_status()
307 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
308 *
309 * The line we're expecting has 8 fields. We ignore all lines
310 * with more or less fields.
311 */
312 if (fields_num != 8)
313 continue;
315 if (strcmp(fields[0], "CLIENT_LIST") != 0)
316 continue;
318 if (collect_user_count)
319 /* If so, sum all users, ignore the individuals*/
320 {
321 sum_users += 1;
322 }
323 if (collect_individual_users) {
324 if (new_naming_schema) {
325 /* plugin inst = file name, type inst = fields[1] */
326 iostats_submit(name, /* vpn instance */
327 fields[1], /* "Common Name" */
328 atoll(fields[4]), /* "Bytes Received" */
329 atoll(fields[5])); /* "Bytes Sent" */
330 } else {
331 /* plugin inst = fields[1], type inst = "" */
332 iostats_submit(fields[1], /* "Common Name" */
333 NULL, /* unused when in multimode */
334 atoll(fields[4]), /* "Bytes Received" */
335 atoll(fields[5])); /* "Bytes Sent" */
336 }
337 }
339 read = 1;
340 }
342 if (collect_user_count) {
343 numusers_submit(name, name, sum_users);
344 read = 1;
345 }
347 return (read);
348 } /* int multi2_read */
350 /* for reading status version 3 */
351 static int multi3_read(const char *name, FILE *fh) {
352 char buffer[1024];
353 char *fields[15];
354 const int max_fields = STATIC_ARRAY_SIZE(fields);
355 int fields_num, read = 0;
356 long long sum_users = 0;
358 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
359 fields_num = strsplit(buffer, fields, max_fields);
361 /* status file is generated by openvpn/multi.c:multi_print_status()
362 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
363 *
364 * The line we're expecting has 12 fields. We ignore all lines
365 * with more or less fields.
366 */
367 if (fields_num != 12) {
368 continue;
369 } else {
370 if (strcmp(fields[0], "CLIENT_LIST") != 0)
371 continue;
373 if (collect_user_count)
374 /* If so, sum all users, ignore the individuals*/
375 {
376 sum_users += 1;
377 }
379 if (collect_individual_users) {
380 if (new_naming_schema) {
381 iostats_submit(name, /* vpn instance */
382 fields[1], /* "Common Name" */
383 atoll(fields[4]), /* "Bytes Received" */
384 atoll(fields[5])); /* "Bytes Sent" */
385 } else {
386 iostats_submit(fields[1], /* "Common Name" */
387 NULL, /* unused when in multimode */
388 atoll(fields[4]), /* "Bytes Received" */
389 atoll(fields[5])); /* "Bytes Sent" */
390 }
391 }
393 read = 1;
394 }
395 }
397 if (collect_user_count) {
398 numusers_submit(name, name, sum_users);
399 read = 1;
400 }
402 return (read);
403 } /* int multi3_read */
405 /* for reading status version 4 */
406 static int multi4_read(const char *name, FILE *fh) {
407 char buffer[1024];
408 char *fields[11];
409 const int max_fields = STATIC_ARRAY_SIZE(fields);
410 int fields_num, read = 0;
411 long long sum_users = 0;
413 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
414 fields_num = openvpn_strsplit(buffer, fields, max_fields);
416 /* status file is generated by openvpn/multi.c:multi_print_status()
417 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
418 *
419 * The line we're expecting has 9 fields. We ignore all lines
420 * with more or less fields.
421 */
422 if (fields_num != 9)
423 continue;
425 if (strcmp(fields[0], "CLIENT_LIST") != 0)
426 continue;
428 if (collect_user_count)
429 /* If so, sum all users, ignore the individuals*/
430 {
431 sum_users += 1;
432 }
433 if (collect_individual_users) {
434 if (new_naming_schema) {
435 /* plugin inst = file name, type inst = fields[1] */
436 iostats_submit(name, /* vpn instance */
437 fields[1], /* "Common Name" */
438 atoll(fields[4]), /* "Bytes Received" */
439 atoll(fields[5])); /* "Bytes Sent" */
440 } else {
441 /* plugin inst = fields[1], type inst = "" */
442 iostats_submit(fields[1], /* "Common Name" */
443 NULL, /* unused when in multimode */
444 atoll(fields[4]), /* "Bytes Received" */
445 atoll(fields[5])); /* "Bytes Sent" */
446 }
447 }
449 read = 1;
450 }
452 if (collect_user_count) {
453 numusers_submit(name, name, sum_users);
454 read = 1;
455 }
457 return (read);
458 } /* int multi4_read */
460 /* read callback */
461 static int openvpn_read(void) {
462 FILE *fh;
463 int read;
465 read = 0;
467 if (vpn_num == 0)
468 return (0);
470 /* call the right read function for every status entry in the list */
471 for (int i = 0; i < vpn_num; i++) {
472 int vpn_read = 0;
474 fh = fopen(vpn_list[i]->file, "r");
475 if (fh == NULL) {
476 char errbuf[1024];
477 WARNING("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
478 sstrerror(errno, errbuf, sizeof(errbuf)));
480 continue;
481 }
483 switch (vpn_list[i]->version) {
484 case SINGLE:
485 vpn_read = single_read(vpn_list[i]->name, fh);
486 break;
488 case MULTI1:
489 vpn_read = multi1_read(vpn_list[i]->name, fh);
490 break;
492 case MULTI2:
493 vpn_read = multi2_read(vpn_list[i]->name, fh);
494 break;
496 case MULTI3:
497 vpn_read = multi3_read(vpn_list[i]->name, fh);
498 break;
500 case MULTI4:
501 vpn_read = multi4_read(vpn_list[i]->name, fh);
502 break;
503 }
505 fclose(fh);
506 read += vpn_read;
507 }
509 return (read ? 0 : -1);
510 } /* int openvpn_read */
512 static int version_detect(const char *filename) {
513 FILE *fh;
514 char buffer[1024];
515 int version = 0;
517 /* Sanity checking. We're called from the config handling routine, so
518 * better play it save. */
519 if ((filename == NULL) || (*filename == 0))
520 return (0);
522 fh = fopen(filename, "r");
523 if (fh == NULL) {
524 char errbuf[1024];
525 WARNING("openvpn plugin: Unable to read \"%s\": %s", filename,
526 sstrerror(errno, errbuf, sizeof(errbuf)));
527 return (0);
528 }
530 /* now search for the specific multimode data format */
531 while ((fgets(buffer, sizeof(buffer), fh)) != NULL) {
532 /* we look at the first line searching for SINGLE mode configuration */
533 if (strcmp(buffer, VSSTRING) == 0) {
534 DEBUG("openvpn plugin: found status file version SINGLE");
535 version = SINGLE;
536 break;
537 }
538 /* searching for multi version 1 */
539 else if (strcmp(buffer, V1STRING) == 0) {
540 DEBUG("openvpn plugin: found status file version MULTI1");
541 version = MULTI1;
542 break;
543 }
544 /* searching for multi version 2 */
545 else if (strcmp(buffer, V2STRING) == 0) {
546 DEBUG("openvpn plugin: found status file version MULTI2");
547 version = MULTI2;
548 break;
549 }
550 /* searching for multi version 3 */
551 else if (strcmp(buffer, V3STRING) == 0) {
552 DEBUG("openvpn plugin: found status file version MULTI3");
553 version = MULTI3;
554 break;
555 }
556 /* searching for multi version 4 */
557 else if (strcmp(buffer, V4STRING) == 0) {
558 DEBUG("openvpn plugin: found status file version MULTI4");
559 version = MULTI4;
560 break;
561 }
562 }
564 if (version == 0) {
565 /* This is only reached during configuration, so complaining to
566 * the user is in order. */
567 NOTICE("openvpn plugin: %s: Unknown file format, please "
568 "report this as bug. Make sure to include "
569 "your status file, so the plugin can "
570 "be adapted.",
571 filename);
572 }
574 fclose(fh);
576 return version;
577 } /* int version_detect */
579 static int openvpn_config(const char *key, const char *value) {
580 if (strcasecmp("StatusFile", key) == 0) {
581 char *status_file, *status_name, *filename;
582 int status_version;
583 vpn_status_t *temp;
585 /* try to detect the status file format */
586 status_version = version_detect(value);
588 if (status_version == 0) {
589 WARNING("openvpn plugin: unable to detect status version, "
590 "discarding status file \"%s\".",
591 value);
592 return (1);
593 }
595 status_file = sstrdup(value);
596 if (status_file == NULL) {
597 char errbuf[1024];
598 WARNING("openvpn plugin: sstrdup failed: %s",
599 sstrerror(errno, errbuf, sizeof(errbuf)));
600 return (1);
601 }
603 /* it determines the file name as string starting at location filename + 1
604 */
605 filename = strrchr(status_file, (int)'/');
606 if (filename == NULL) {
607 /* status_file is already the file name only */
608 status_name = status_file;
609 } else {
610 /* doesn't waste memory, uses status_file starting at filename + 1 */
611 status_name = filename + 1;
612 }
614 /* scan the list looking for a clone */
615 for (int i = 0; i < vpn_num; i++) {
616 if (strcasecmp(vpn_list[i]->name, status_name) == 0) {
617 WARNING("openvpn plugin: status filename \"%s\" "
618 "already used, please choose a "
619 "different one.",
620 status_name);
621 sfree(status_file);
622 return (1);
623 }
624 }
626 /* create a new vpn element since file, version and name are ok */
627 temp = malloc(sizeof(*temp));
628 if (temp == NULL) {
629 char errbuf[1024];
630 ERROR("openvpn plugin: malloc failed: %s",
631 sstrerror(errno, errbuf, sizeof(errbuf)));
632 sfree(status_file);
633 return (1);
634 }
635 temp->file = status_file;
636 temp->version = status_version;
637 temp->name = status_name;
639 vpn_status_t **tmp_list =
640 realloc(vpn_list, (vpn_num + 1) * sizeof(*vpn_list));
641 if (tmp_list == NULL) {
642 char errbuf[1024];
643 ERROR("openvpn plugin: realloc failed: %s",
644 sstrerror(errno, errbuf, sizeof(errbuf)));
646 sfree(vpn_list);
647 sfree(temp->file);
648 sfree(temp);
649 return (1);
650 }
651 vpn_list = tmp_list;
653 vpn_list[vpn_num] = temp;
654 vpn_num++;
656 DEBUG("openvpn plugin: status file \"%s\" added", temp->file);
658 } /* if (strcasecmp ("StatusFile", key) == 0) */
659 else if ((strcasecmp("CollectCompression", key) == 0) ||
660 (strcasecmp("Compression", key) == 0)) /* old, deprecated name */
661 {
662 if (IS_FALSE(value))
663 collect_compression = 0;
664 else
665 collect_compression = 1;
666 } /* if (strcasecmp ("CollectCompression", key) == 0) */
667 else if (strcasecmp("ImprovedNamingSchema", key) == 0) {
668 if (IS_TRUE(value)) {
669 DEBUG("openvpn plugin: using the new naming schema");
670 new_naming_schema = 1;
671 } else {
672 new_naming_schema = 0;
673 }
674 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
675 else if (strcasecmp("CollectUserCount", key) == 0) {
676 if (IS_TRUE(value))
677 collect_user_count = 1;
678 else
679 collect_user_count = 0;
680 } /* if (strcasecmp("CollectUserCount", key) == 0) */
681 else if (strcasecmp("CollectIndividualUsers", key) == 0) {
682 if (IS_FALSE(value))
683 collect_individual_users = 0;
684 else
685 collect_individual_users = 1;
686 } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
687 else {
688 return (-1);
689 }
691 return (0);
692 } /* int openvpn_config */
694 /* shutdown callback */
695 static int openvpn_shutdown(void) {
696 for (int i = 0; i < vpn_num; i++) {
697 sfree(vpn_list[i]->file);
698 sfree(vpn_list[i]);
699 }
701 sfree(vpn_list);
703 return (0);
704 } /* int openvpn_shutdown */
706 static int openvpn_init(void) {
707 if (!collect_individual_users && !collect_compression &&
708 !collect_user_count) {
709 WARNING("OpenVPN plugin: Neither `CollectIndividualUsers', "
710 "`CollectCompression', nor `CollectUserCount' is true. There's no "
711 "data left to collect.");
712 return (-1);
713 }
715 plugin_register_read("openvpn", openvpn_read);
716 plugin_register_shutdown("openvpn", openvpn_shutdown);
718 return (0);
719 } /* int openvpn_init */
721 void module_register(void) {
722 plugin_register_config("openvpn", openvpn_config, config_keys,
723 config_keys_num);
724 plugin_register_init("openvpn", openvpn_init);
725 } /* void module_register */
727 /* vim: set sw=2 ts=2 : */