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
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.
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.
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
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>
33 #define V1STRING "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
34 #define V2STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
35 #define V3STRING "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)\n"
36 #define V4STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t),Username\n"
37 #define VSSTRING "OpenVPN STATISTICS\n"
45 MULTI1 = 1, /* status-version 1 */
46 MULTI2, /* status-version 2 */
47 MULTI3, /* status-version 3 */
48 MULTI4, /* status-version 4 */
49 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
53 typedef struct vpn_status_s vpn_status_t;
55 static vpn_status_t **vpn_list = NULL;
56 static int vpn_num = 0;
58 static _Bool new_naming_schema = 0;
59 static _Bool collect_compression = 1;
60 static _Bool collect_user_count = 0;
61 static _Bool collect_individual_users = 1;
63 static const char *config_keys[] =
66 "Compression", /* old, deprecated name */
67 "ImprovedNamingSchema",
70 "CollectIndividualUsers"
72 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
76 * copy-n-pasted from common.c - changed delim to "," */
77 static int openvpn_strsplit (char *string, char **fields, size_t size)
86 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
96 } /* int openvpn_strsplit */
98 /* dispatches number of users */
99 static void numusers_submit (const char *pinst, const char *tinst,
103 value_list_t vl = VALUE_LIST_INIT;
105 values[0].gauge = value;
108 vl.values_len = STATIC_ARRAY_SIZE (values);
109 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
110 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
111 sstrncpy (vl.type, "users", sizeof (vl.type));
113 sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
115 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
117 plugin_dispatch_values (&vl);
118 } /* void numusers_submit */
120 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel
121 * per single endpoint */
122 static void iostats_submit (const char *pinst, const char *tinst,
123 derive_t rx, derive_t tx)
126 value_list_t vl = VALUE_LIST_INIT;
128 values[0].derive = rx;
129 values[1].derive = tx;
131 /* NOTE ON THE NEW NAMING SCHEMA:
132 * using plugin_instance to identify each vpn config (and
133 * status) file; using type_instance to identify the endpoint
134 * host when in multimode, traffic or overhead when in single.
138 vl.values_len = STATIC_ARRAY_SIZE (values);
139 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
140 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
142 sstrncpy (vl.plugin_instance, pinst,
143 sizeof (vl.plugin_instance));
144 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
146 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
148 plugin_dispatch_values (&vl);
149 } /* void traffic_submit */
151 /* dispatches stats about data compression shown when in single mode */
152 static void compression_submit (const char *pinst, const char *tinst,
153 derive_t uncompressed, derive_t compressed)
156 value_list_t vl = VALUE_LIST_INIT;
158 values[0].derive = uncompressed;
159 values[1].derive = compressed;
162 vl.values_len = STATIC_ARRAY_SIZE (values);
163 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
166 sstrncpy (vl.plugin_instance, pinst,
167 sizeof (vl.plugin_instance));
168 sstrncpy (vl.type, "compression", sizeof (vl.type));
170 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
172 plugin_dispatch_values (&vl);
173 } /* void compression_submit */
175 static int single_read (const char *name, FILE *fh)
179 const int max_fields = STATIC_ARRAY_SIZE (fields);
180 int fields_num, read = 0;
182 derive_t link_rx, link_tx;
183 derive_t tun_rx, tun_tx;
184 derive_t pre_compress, post_compress;
185 derive_t pre_decompress, post_decompress;
186 derive_t overhead_rx, overhead_tx;
197 while (fgets (buffer, sizeof (buffer), fh) != NULL)
199 fields_num = openvpn_strsplit (buffer, fields, max_fields);
201 /* status file is generated by openvpn/sig.c:print_status()
202 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
204 * The line we're expecting has 2 fields. We ignore all lines
205 * with more or less fields.
212 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
214 /* read from the system and sent over the tunnel */
215 tun_tx = atoll (fields[1]);
217 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
219 /* read from the tunnel and written in the system */
220 tun_rx = atoll (fields[1]);
222 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
224 link_rx = atoll (fields[1]);
226 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
228 link_tx = atoll (fields[1]);
230 else if (strcmp (fields[0], "pre-compress bytes") == 0)
232 pre_compress = atoll (fields[1]);
234 else if (strcmp (fields[0], "post-compress bytes") == 0)
236 post_compress = atoll (fields[1]);
238 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
240 pre_decompress = atoll (fields[1]);
242 else if (strcmp (fields[0], "post-decompress bytes") == 0)
244 post_decompress = atoll (fields[1]);
248 iostats_submit (name, "traffic", link_rx, link_tx);
250 /* we need to force this order to avoid negative values with these unsigned */
251 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
252 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
254 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
256 if (collect_compression)
258 compression_submit (name, "data_in", post_decompress, pre_decompress);
259 compression_submit (name, "data_out", pre_compress, post_compress);
265 } /* int single_read */
267 /* for reading status version 1 */
268 static int multi1_read (const char *name, FILE *fh)
272 int fields_num, found_header = 0;
273 long long sum_users = 0;
275 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
276 while (fgets (buffer, sizeof (buffer), fh) != NULL)
278 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
281 if (strcmp (buffer, V1STRING) == 0)
287 /* skip the first lines until the client list section is found */
288 if (found_header == 0)
289 /* we can't start reading data until this string is found */
292 fields_num = openvpn_strsplit (buffer,
293 fields, STATIC_ARRAY_SIZE (fields));
297 if (collect_user_count)
298 /* If so, sum all users, ignore the individuals*/
302 if (collect_individual_users)
304 if (new_naming_schema)
306 iostats_submit (name, /* vpn instance */
307 fields[0], /* "Common Name" */
308 atoll (fields[2]), /* "Bytes Received" */
309 atoll (fields[3])); /* "Bytes Sent" */
313 iostats_submit (fields[0], /* "Common Name" */
314 NULL, /* unused when in multimode */
315 atoll (fields[2]), /* "Bytes Received" */
316 atoll (fields[3])); /* "Bytes Sent" */
324 if (collect_user_count)
325 numusers_submit(name, name, sum_users);
328 } /* int multi1_read */
330 /* for reading status version 2 */
331 static int multi2_read (const char *name, FILE *fh)
335 const int max_fields = STATIC_ARRAY_SIZE (fields);
336 int fields_num, read = 0;
337 long long sum_users = 0;
339 while (fgets (buffer, sizeof (buffer), fh) != NULL)
341 fields_num = openvpn_strsplit (buffer, fields, max_fields);
343 /* status file is generated by openvpn/multi.c:multi_print_status()
344 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
346 * The line we're expecting has 8 fields. We ignore all lines
347 * with more or less fields.
352 if (strcmp (fields[0], "CLIENT_LIST") != 0)
355 if (collect_user_count)
356 /* If so, sum all users, ignore the individuals*/
360 if (collect_individual_users)
362 if (new_naming_schema)
364 /* plugin inst = file name, type inst = fields[1] */
365 iostats_submit (name, /* vpn instance */
366 fields[1], /* "Common Name" */
367 atoll (fields[4]), /* "Bytes Received" */
368 atoll (fields[5])); /* "Bytes Sent" */
372 /* plugin inst = fields[1], type inst = "" */
373 iostats_submit (fields[1], /* "Common Name" */
374 NULL, /* unused when in multimode */
375 atoll (fields[4]), /* "Bytes Received" */
376 atoll (fields[5])); /* "Bytes Sent" */
383 if (collect_user_count)
385 numusers_submit(name, name, sum_users);
390 } /* int multi2_read */
392 /* for reading status version 3 */
393 static int multi3_read (const char *name, FILE *fh)
397 const int max_fields = STATIC_ARRAY_SIZE (fields);
398 int fields_num, read = 0;
399 long long sum_users = 0;
401 while (fgets (buffer, sizeof (buffer), fh) != NULL)
403 fields_num = strsplit (buffer, fields, max_fields);
405 /* status file is generated by openvpn/multi.c:multi_print_status()
406 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
408 * The line we're expecting has 12 fields. We ignore all lines
409 * with more or less fields.
411 if (fields_num != 12)
417 if (strcmp (fields[0], "CLIENT_LIST") != 0)
420 if (collect_user_count)
421 /* If so, sum all users, ignore the individuals*/
426 if (collect_individual_users)
428 if (new_naming_schema)
430 iostats_submit (name, /* vpn instance */
431 fields[1], /* "Common Name" */
432 atoll (fields[4]), /* "Bytes Received" */
433 atoll (fields[5])); /* "Bytes Sent" */
437 iostats_submit (fields[1], /* "Common Name" */
438 NULL, /* unused when in multimode */
439 atoll (fields[4]), /* "Bytes Received" */
440 atoll (fields[5])); /* "Bytes Sent" */
448 if (collect_user_count)
450 numusers_submit(name, name, sum_users);
455 } /* int multi3_read */
457 /* for reading status version 4 */
458 static int multi4_read (const char *name, FILE *fh)
462 const int max_fields = STATIC_ARRAY_SIZE (fields);
463 int fields_num, read = 0;
464 long long sum_users = 0;
466 while (fgets (buffer, sizeof (buffer), fh) != NULL)
468 fields_num = openvpn_strsplit (buffer, fields, max_fields);
470 /* status file is generated by openvpn/multi.c:multi_print_status()
471 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
473 * The line we're expecting has 9 fields. We ignore all lines
474 * with more or less fields.
480 if (strcmp (fields[0], "CLIENT_LIST") != 0)
484 if (collect_user_count)
485 /* If so, sum all users, ignore the individuals*/
489 if (collect_individual_users)
491 if (new_naming_schema)
493 /* plugin inst = file name, type inst = fields[1] */
494 iostats_submit (name, /* vpn instance */
495 fields[1], /* "Common Name" */
496 atoll (fields[4]), /* "Bytes Received" */
497 atoll (fields[5])); /* "Bytes Sent" */
501 /* plugin inst = fields[1], type inst = "" */
502 iostats_submit (fields[1], /* "Common Name" */
503 NULL, /* unused when in multimode */
504 atoll (fields[4]), /* "Bytes Received" */
505 atoll (fields[5])); /* "Bytes Sent" */
512 if (collect_user_count)
514 numusers_submit(name, name, sum_users);
519 } /* int multi4_read */
522 static int openvpn_read (void)
529 /* call the right read function for every status entry in the list */
530 for (int i = 0; i < vpn_num; i++)
534 fh = fopen (vpn_list[i]->file, "r");
538 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
539 sstrerror (errno, errbuf, sizeof (errbuf)));
544 switch (vpn_list[i]->version)
547 vpn_read = single_read(vpn_list[i]->name, fh);
551 vpn_read = multi1_read(vpn_list[i]->name, fh);
555 vpn_read = multi2_read(vpn_list[i]->name, fh);
559 vpn_read = multi3_read(vpn_list[i]->name, fh);
563 vpn_read = multi4_read(vpn_list[i]->name, fh);
571 return (read ? 0 : -1);
572 } /* int openvpn_read */
574 static int version_detect (const char *filename)
580 /* Sanity checking. We're called from the config handling routine, so
581 * better play it save. */
582 if ((filename == NULL) || (*filename == 0))
585 fh = fopen (filename, "r");
589 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
590 sstrerror (errno, errbuf, sizeof (errbuf)));
594 /* now search for the specific multimode data format */
595 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
597 /* we look at the first line searching for SINGLE mode configuration */
598 if (strcmp (buffer, VSSTRING) == 0)
600 DEBUG ("openvpn plugin: found status file version SINGLE");
604 /* searching for multi version 1 */
605 else if (strcmp (buffer, V1STRING) == 0)
607 DEBUG ("openvpn plugin: found status file version MULTI1");
611 /* searching for multi version 2 */
612 else if (strcmp (buffer, V2STRING) == 0)
614 DEBUG ("openvpn plugin: found status file version MULTI2");
618 /* searching for multi version 3 */
619 else if (strcmp (buffer, V3STRING) == 0)
621 DEBUG ("openvpn plugin: found status file version MULTI3");
625 /* searching for multi version 4 */
626 else if (strcmp (buffer, V4STRING) == 0)
628 DEBUG ("openvpn plugin: found status file version MULTI4");
636 /* This is only reached during configuration, so complaining to
637 * the user is in order. */
638 NOTICE ("openvpn plugin: %s: Unknown file format, please "
639 "report this as bug. Make sure to include "
640 "your status file, so the plugin can "
641 "be adapted.", filename);
647 } /* int version_detect */
649 static int openvpn_config (const char *key, const char *value)
651 if (strcasecmp ("StatusFile", key) == 0)
653 char *status_file, *status_name, *filename;
657 /* try to detect the status file format */
658 status_version = version_detect (value);
660 if (status_version == 0)
662 WARNING ("openvpn plugin: unable to detect status version, \
663 discarding status file \"%s\".", value);
667 status_file = sstrdup (value);
668 if (status_file == NULL)
671 WARNING ("openvpn plugin: sstrdup failed: %s",
672 sstrerror (errno, errbuf, sizeof (errbuf)));
676 /* it determines the file name as string starting at location filename + 1 */
677 filename = strrchr (status_file, (int) '/');
678 if (filename == NULL)
680 /* status_file is already the file name only */
681 status_name = status_file;
685 /* doesn't waste memory, uses status_file starting at filename + 1 */
686 status_name = filename + 1;
689 /* scan the list looking for a clone */
690 for (int i = 0; i < vpn_num; i++)
692 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
694 WARNING ("openvpn plugin: status filename \"%s\" "
695 "already used, please choose a "
696 "different one.", status_name);
702 /* create a new vpn element since file, version and name are ok */
703 temp = malloc (sizeof (*temp));
707 ERROR ("openvpn plugin: malloc failed: %s",
708 sstrerror (errno, errbuf, sizeof (errbuf)));
712 temp->file = status_file;
713 temp->version = status_version;
714 temp->name = status_name;
716 vpn_status_t **tmp_list = realloc (vpn_list, (vpn_num + 1) * sizeof (*vpn_list));
717 if (tmp_list == NULL)
720 ERROR ("openvpn plugin: realloc failed: %s",
721 sstrerror (errno, errbuf, sizeof (errbuf)));
730 vpn_list[vpn_num] = temp;
733 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
735 } /* if (strcasecmp ("StatusFile", key) == 0) */
736 else if ((strcasecmp ("CollectCompression", key) == 0)
737 || (strcasecmp ("Compression", key) == 0)) /* old, deprecated name */
739 if (IS_FALSE (value))
740 collect_compression = 0;
742 collect_compression = 1;
743 } /* if (strcasecmp ("CollectCompression", key) == 0) */
744 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
748 DEBUG ("openvpn plugin: using the new naming schema");
749 new_naming_schema = 1;
753 new_naming_schema = 0;
755 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
756 else if (strcasecmp("CollectUserCount", key) == 0)
759 collect_user_count = 1;
761 collect_user_count = 0;
762 } /* if (strcasecmp("CollectUserCount", key) == 0) */
763 else if (strcasecmp("CollectIndividualUsers", key) == 0)
765 if (IS_FALSE (value))
766 collect_individual_users = 0;
768 collect_individual_users = 1;
769 } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
776 } /* int openvpn_config */
778 /* shutdown callback */
779 static int openvpn_shutdown (void)
781 for (int i = 0; i < vpn_num; i++)
783 sfree (vpn_list[i]->file);
790 } /* int openvpn_shutdown */
792 static int openvpn_init (void)
794 if (!collect_individual_users
795 && !collect_compression
796 && !collect_user_count)
798 WARNING ("OpenVPN plugin: Neither `CollectIndividualUsers', "
799 "`CollectCompression', nor `CollectUserCount' is true. There's no "
800 "data left to collect.");
804 plugin_register_read ("openvpn", openvpn_read);
805 plugin_register_shutdown ("openvpn", openvpn_shutdown);
808 } /* int openvpn_init */
810 void module_register (void)
812 plugin_register_config ("openvpn", openvpn_config,
813 config_keys, config_keys_num);
814 plugin_register_init ("openvpn", openvpn_init);
815 } /* void module_register */
817 /* vim: set sw=2 ts=2 : */