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>
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 V4STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t),Username\n"
36 #define VSSTRING "OpenVPN STATISTICS\n"
44 MULTI1 = 1, /* status-version 1 */
45 MULTI2, /* status-version 2 */
46 MULTI3, /* status-version 3 */
47 MULTI4, /* status-version 4 */
48 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
52 typedef struct vpn_status_s vpn_status_t;
54 static vpn_status_t **vpn_list = NULL;
55 static int vpn_num = 0;
57 static _Bool new_naming_schema = 0;
58 static _Bool collect_compression = 1;
59 static _Bool collect_user_count = 0;
60 static _Bool collect_individual_users = 1;
62 static const char *config_keys[] =
65 "Compression", /* old, deprecated name */
66 "ImprovedNamingSchema",
69 "CollectIndividualUsers"
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
75 * copy-n-pasted from common.c - changed delim to "," */
76 static int openvpn_strsplit (char *string, char **fields, size_t size)
85 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
95 } /* int openvpn_strsplit */
97 /* dispatches number of users */
98 static void numusers_submit (const char *pinst, const char *tinst,
102 value_list_t vl = VALUE_LIST_INIT;
104 values[0].gauge = value;
107 vl.values_len = STATIC_ARRAY_SIZE (values);
108 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
109 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
110 sstrncpy (vl.type, "users", sizeof (vl.type));
112 sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
114 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
116 plugin_dispatch_values (&vl);
117 } /* void numusers_submit */
119 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel
120 * per single endpoint */
121 static void iostats_submit (const char *pinst, const char *tinst,
122 derive_t rx, derive_t tx)
125 value_list_t vl = VALUE_LIST_INIT;
127 values[0].derive = rx;
128 values[1].derive = tx;
130 /* NOTE ON THE NEW NAMING SCHEMA:
131 * using plugin_instance to identify each vpn config (and
132 * status) file; using type_instance to identify the endpoint
133 * host when in multimode, traffic or overhead when in single.
137 vl.values_len = STATIC_ARRAY_SIZE (values);
138 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
139 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
141 sstrncpy (vl.plugin_instance, pinst,
142 sizeof (vl.plugin_instance));
143 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
145 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
147 plugin_dispatch_values (&vl);
148 } /* void traffic_submit */
150 /* dispatches stats about data compression shown when in single mode */
151 static void compression_submit (const char *pinst, const char *tinst,
152 derive_t uncompressed, derive_t compressed)
155 value_list_t vl = VALUE_LIST_INIT;
157 values[0].derive = uncompressed;
158 values[1].derive = compressed;
161 vl.values_len = STATIC_ARRAY_SIZE (values);
162 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
163 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
165 sstrncpy (vl.plugin_instance, pinst,
166 sizeof (vl.plugin_instance));
167 sstrncpy (vl.type, "compression", sizeof (vl.type));
169 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
171 plugin_dispatch_values (&vl);
172 } /* void compression_submit */
174 static int single_read (const char *name, FILE *fh)
178 const int max_fields = STATIC_ARRAY_SIZE (fields);
179 int fields_num, read = 0;
181 derive_t link_rx, link_tx;
182 derive_t tun_rx, tun_tx;
183 derive_t pre_compress, post_compress;
184 derive_t pre_decompress, post_decompress;
185 derive_t overhead_rx, overhead_tx;
196 while (fgets (buffer, sizeof (buffer), fh) != NULL)
198 fields_num = openvpn_strsplit (buffer, fields, max_fields);
200 /* status file is generated by openvpn/sig.c:print_status()
201 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
203 * The line we're expecting has 2 fields. We ignore all lines
204 * with more or less fields.
211 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
213 /* read from the system and sent over the tunnel */
214 tun_tx = atoll (fields[1]);
216 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
218 /* read from the tunnel and written in the system */
219 tun_rx = atoll (fields[1]);
221 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
223 link_rx = atoll (fields[1]);
225 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
227 link_tx = atoll (fields[1]);
229 else if (strcmp (fields[0], "pre-compress bytes") == 0)
231 pre_compress = atoll (fields[1]);
233 else if (strcmp (fields[0], "post-compress bytes") == 0)
235 post_compress = atoll (fields[1]);
237 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
239 pre_decompress = atoll (fields[1]);
241 else if (strcmp (fields[0], "post-decompress bytes") == 0)
243 post_decompress = atoll (fields[1]);
247 iostats_submit (name, "traffic", link_rx, link_tx);
249 /* we need to force this order to avoid negative values with these unsigned */
250 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
251 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
253 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
255 if (collect_compression)
257 compression_submit (name, "data_in", post_decompress, pre_decompress);
258 compression_submit (name, "data_out", pre_compress, post_compress);
264 } /* int single_read */
266 /* for reading status version 1 */
267 static int multi1_read (const char *name, FILE *fh)
271 int fields_num, found_header = 0;
272 long long sum_users = 0;
274 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
275 while (fgets (buffer, sizeof (buffer), fh) != NULL)
277 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
280 if (strcmp (buffer, V1STRING) == 0)
286 /* skip the first lines until the client list section is found */
287 if (found_header == 0)
288 /* we can't start reading data until this string is found */
291 fields_num = openvpn_strsplit (buffer,
292 fields, STATIC_ARRAY_SIZE (fields));
296 if (collect_user_count)
297 /* If so, sum all users, ignore the individuals*/
301 if (collect_individual_users)
303 if (new_naming_schema)
305 iostats_submit (name, /* vpn instance */
306 fields[0], /* "Common Name" */
307 atoll (fields[2]), /* "Bytes Received" */
308 atoll (fields[3])); /* "Bytes Sent" */
312 iostats_submit (fields[0], /* "Common Name" */
313 NULL, /* unused when in multimode */
314 atoll (fields[2]), /* "Bytes Received" */
315 atoll (fields[3])); /* "Bytes Sent" */
323 if (collect_user_count)
324 numusers_submit(name, name, sum_users);
327 } /* int multi1_read */
329 /* for reading status version 2 */
330 static int multi2_read (const char *name, FILE *fh)
334 const int max_fields = STATIC_ARRAY_SIZE (fields);
335 int fields_num, read = 0;
336 long long sum_users = 0;
338 while (fgets (buffer, sizeof (buffer), fh) != NULL)
340 fields_num = openvpn_strsplit (buffer, fields, max_fields);
342 /* status file is generated by openvpn/multi.c:multi_print_status()
343 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
345 * The line we're expecting has 8 fields. We ignore all lines
346 * with more or less fields.
351 if (strcmp (fields[0], "CLIENT_LIST") != 0)
354 if (collect_user_count)
355 /* If so, sum all users, ignore the individuals*/
359 if (collect_individual_users)
361 if (new_naming_schema)
363 /* plugin inst = file name, type inst = fields[1] */
364 iostats_submit (name, /* vpn instance */
365 fields[1], /* "Common Name" */
366 atoll (fields[4]), /* "Bytes Received" */
367 atoll (fields[5])); /* "Bytes Sent" */
371 /* plugin inst = fields[1], type inst = "" */
372 iostats_submit (fields[1], /* "Common Name" */
373 NULL, /* unused when in multimode */
374 atoll (fields[4]), /* "Bytes Received" */
375 atoll (fields[5])); /* "Bytes Sent" */
382 if (collect_user_count)
384 numusers_submit(name, name, sum_users);
389 } /* int multi2_read */
391 /* for reading status version 3 */
392 static int multi3_read (const char *name, FILE *fh)
396 const int max_fields = STATIC_ARRAY_SIZE (fields);
397 int fields_num, read = 0;
398 long long sum_users = 0;
400 while (fgets (buffer, sizeof (buffer), fh) != NULL)
402 fields_num = strsplit (buffer, fields, max_fields);
404 /* status file is generated by openvpn/multi.c:multi_print_status()
405 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
407 * The line we're expecting has 12 fields. We ignore all lines
408 * with more or less fields.
410 if (fields_num != 12)
416 if (strcmp (fields[0], "CLIENT_LIST") != 0)
419 if (collect_user_count)
420 /* If so, sum all users, ignore the individuals*/
425 if (collect_individual_users)
427 if (new_naming_schema)
429 iostats_submit (name, /* vpn instance */
430 fields[1], /* "Common Name" */
431 atoll (fields[4]), /* "Bytes Received" */
432 atoll (fields[5])); /* "Bytes Sent" */
436 iostats_submit (fields[1], /* "Common Name" */
437 NULL, /* unused when in multimode */
438 atoll (fields[4]), /* "Bytes Received" */
439 atoll (fields[5])); /* "Bytes Sent" */
447 if (collect_user_count)
449 numusers_submit(name, name, sum_users);
454 } /* int multi3_read */
456 /* for reading status version 4 */
457 static int multi4_read (const char *name, FILE *fh)
461 const int max_fields = STATIC_ARRAY_SIZE (fields);
462 int fields_num, read = 0;
463 long long sum_users = 0;
465 while (fgets (buffer, sizeof (buffer), fh) != NULL)
467 fields_num = openvpn_strsplit (buffer, fields, max_fields);
469 /* status file is generated by openvpn/multi.c:multi_print_status()
470 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
472 * The line we're expecting has 9 fields. We ignore all lines
473 * with more or less fields.
479 if (strcmp (fields[0], "CLIENT_LIST") != 0)
483 if (collect_user_count)
484 /* If so, sum all users, ignore the individuals*/
488 if (collect_individual_users)
490 if (new_naming_schema)
492 /* plugin inst = file name, type inst = fields[1] */
493 iostats_submit (name, /* vpn instance */
494 fields[1], /* "Common Name" */
495 atoll (fields[4]), /* "Bytes Received" */
496 atoll (fields[5])); /* "Bytes Sent" */
500 /* plugin inst = fields[1], type inst = "" */
501 iostats_submit (fields[1], /* "Common Name" */
502 NULL, /* unused when in multimode */
503 atoll (fields[4]), /* "Bytes Received" */
504 atoll (fields[5])); /* "Bytes Sent" */
511 if (collect_user_count)
513 numusers_submit(name, name, sum_users);
518 } /* int multi4_read */
521 static int openvpn_read (void)
528 /* call the right read function for every status entry in the list */
529 for (i = 0; i < vpn_num; i++)
533 fh = fopen (vpn_list[i]->file, "r");
537 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
538 sstrerror (errno, errbuf, sizeof (errbuf)));
543 switch (vpn_list[i]->version)
546 vpn_read = single_read(vpn_list[i]->name, fh);
550 vpn_read = multi1_read(vpn_list[i]->name, fh);
554 vpn_read = multi2_read(vpn_list[i]->name, fh);
558 vpn_read = multi3_read(vpn_list[i]->name, fh);
562 vpn_read = multi4_read(vpn_list[i]->name, fh);
570 return (read ? 0 : -1);
571 } /* int openvpn_read */
573 static int version_detect (const char *filename)
579 /* Sanity checking. We're called from the config handling routine, so
580 * better play it save. */
581 if ((filename == NULL) || (*filename == 0))
584 fh = fopen (filename, "r");
588 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
589 sstrerror (errno, errbuf, sizeof (errbuf)));
593 /* now search for the specific multimode data format */
594 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
596 /* we look at the first line searching for SINGLE mode configuration */
597 if (strcmp (buffer, VSSTRING) == 0)
599 DEBUG ("openvpn plugin: found status file version SINGLE");
603 /* searching for multi version 1 */
604 else if (strcmp (buffer, V1STRING) == 0)
606 DEBUG ("openvpn plugin: found status file version MULTI1");
610 /* searching for multi version 2 */
611 else if (strcmp (buffer, V2STRING) == 0)
613 DEBUG ("openvpn plugin: found status file version MULTI2");
617 /* searching for multi version 3 */
618 else if (strcmp (buffer, V3STRING) == 0)
620 DEBUG ("openvpn plugin: found status file version MULTI3");
624 /* searching for multi version 4 */
625 else if (strcmp (buffer, V4STRING) == 0)
627 DEBUG ("openvpn plugin: found status file version MULTI4");
635 /* This is only reached during configuration, so complaining to
636 * the user is in order. */
637 NOTICE ("openvpn plugin: %s: Unknown file format, please "
638 "report this as bug. Make sure to include "
639 "your status file, so the plugin can "
640 "be adapted.", filename);
646 } /* int version_detect */
648 static int openvpn_config (const char *key, const char *value)
650 if (strcasecmp ("StatusFile", key) == 0)
652 char *status_file, *status_name, *filename;
653 int status_version, i;
656 /* try to detect the status file format */
657 status_version = version_detect (value);
659 if (status_version == 0)
661 WARNING ("openvpn plugin: unable to detect status version, \
662 discarding status file \"%s\".", value);
666 status_file = sstrdup (value);
667 if (status_file == NULL)
670 WARNING ("openvpn plugin: sstrdup failed: %s",
671 sstrerror (errno, errbuf, sizeof (errbuf)));
675 /* it determines the file name as string starting at location filename + 1 */
676 filename = strrchr (status_file, (int) '/');
677 if (filename == NULL)
679 /* status_file is already the file name only */
680 status_name = status_file;
684 /* doesn't waste memory, uses status_file starting at filename + 1 */
685 status_name = filename + 1;
688 /* scan the list looking for a clone */
689 for (i = 0; i < vpn_num; i++)
691 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
693 WARNING ("openvpn plugin: status filename \"%s\" "
694 "already used, please choose a "
695 "different one.", status_name);
701 /* create a new vpn element since file, version and name are ok */
702 temp = malloc (sizeof (*temp));
706 ERROR ("openvpn plugin: malloc failed: %s",
707 sstrerror (errno, errbuf, sizeof (errbuf)));
711 temp->file = status_file;
712 temp->version = status_version;
713 temp->name = status_name;
715 vpn_status_t **tmp_list = realloc (vpn_list, (vpn_num + 1) * sizeof (*vpn_list));
716 if (tmp_list == NULL)
719 ERROR ("openvpn plugin: realloc failed: %s",
720 sstrerror (errno, errbuf, sizeof (errbuf)));
729 vpn_list[vpn_num] = temp;
732 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
734 } /* if (strcasecmp ("StatusFile", key) == 0) */
735 else if ((strcasecmp ("CollectCompression", key) == 0)
736 || (strcasecmp ("Compression", key) == 0)) /* old, deprecated name */
738 if (IS_FALSE (value))
739 collect_compression = 0;
741 collect_compression = 1;
742 } /* if (strcasecmp ("CollectCompression", key) == 0) */
743 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
747 DEBUG ("openvpn plugin: using the new naming schema");
748 new_naming_schema = 1;
752 new_naming_schema = 0;
754 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
755 else if (strcasecmp("CollectUserCount", key) == 0)
758 collect_user_count = 1;
760 collect_user_count = 0;
761 } /* if (strcasecmp("CollectUserCount", key) == 0) */
762 else if (strcasecmp("CollectIndividualUsers", key) == 0)
764 if (IS_FALSE (value))
765 collect_individual_users = 0;
767 collect_individual_users = 1;
768 } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
775 } /* int openvpn_config */
777 /* shutdown callback */
778 static int openvpn_shutdown (void)
782 for (i = 0; i < vpn_num; i++)
784 sfree (vpn_list[i]->file);
791 } /* int openvpn_shutdown */
793 static int openvpn_init (void)
795 if (!collect_individual_users
796 && !collect_compression
797 && !collect_user_count)
799 WARNING ("OpenVPN plugin: Neither `CollectIndividualUsers', "
800 "`CollectCompression', nor `CollectUserCount' is true. There's no "
801 "data left to collect.");
805 plugin_register_read ("openvpn", openvpn_read);
806 plugin_register_shutdown ("openvpn", openvpn_shutdown);
809 } /* int openvpn_init */
811 void module_register (void)
813 plugin_register_config ("openvpn", openvpn_config,
814 config_keys, config_keys_num);
815 plugin_register_init ("openvpn", openvpn_init);
816 } /* void module_register */
818 /* vim: set sw=2 ts=2 : */