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,
102 value_list_t vl = VALUE_LIST_INIT;
104 vl.values = &(value_t) { .gauge = value };
106 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
107 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
108 sstrncpy (vl.type, "users", sizeof (vl.type));
110 sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
112 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
114 plugin_dispatch_values (&vl);
115 } /* void numusers_submit */
117 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel
118 * per single endpoint */
119 static void iostats_submit (const char *pinst, const char *tinst,
120 derive_t rx, derive_t tx)
122 value_list_t vl = VALUE_LIST_INIT;
128 /* NOTE ON THE NEW NAMING SCHEMA:
129 * using plugin_instance to identify each vpn config (and
130 * status) file; using type_instance to identify the endpoint
131 * host when in multimode, traffic or overhead when in single.
135 vl.values_len = STATIC_ARRAY_SIZE (values);
136 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
137 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
139 sstrncpy (vl.plugin_instance, pinst,
140 sizeof (vl.plugin_instance));
141 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
143 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
145 plugin_dispatch_values (&vl);
146 } /* void traffic_submit */
148 /* dispatches stats about data compression shown when in single mode */
149 static void compression_submit (const char *pinst, const char *tinst,
150 derive_t uncompressed, derive_t compressed)
152 value_list_t vl = VALUE_LIST_INIT;
154 { .derive = uncompressed },
155 { .derive = compressed },
159 vl.values_len = STATIC_ARRAY_SIZE (values);
160 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
161 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
163 sstrncpy (vl.plugin_instance, pinst,
164 sizeof (vl.plugin_instance));
165 sstrncpy (vl.type, "compression", sizeof (vl.type));
167 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
169 plugin_dispatch_values (&vl);
170 } /* void compression_submit */
172 static int single_read (const char *name, FILE *fh)
176 const int max_fields = STATIC_ARRAY_SIZE (fields);
177 int fields_num, read = 0;
179 derive_t link_rx, link_tx;
180 derive_t tun_rx, tun_tx;
181 derive_t pre_compress, post_compress;
182 derive_t pre_decompress, post_decompress;
183 derive_t overhead_rx, overhead_tx;
194 while (fgets (buffer, sizeof (buffer), fh) != NULL)
196 fields_num = openvpn_strsplit (buffer, fields, max_fields);
198 /* status file is generated by openvpn/sig.c:print_status()
199 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
201 * The line we're expecting has 2 fields. We ignore all lines
202 * with more or less fields.
209 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
211 /* read from the system and sent over the tunnel */
212 tun_tx = atoll (fields[1]);
214 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
216 /* read from the tunnel and written in the system */
217 tun_rx = atoll (fields[1]);
219 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
221 link_rx = atoll (fields[1]);
223 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
225 link_tx = atoll (fields[1]);
227 else if (strcmp (fields[0], "pre-compress bytes") == 0)
229 pre_compress = atoll (fields[1]);
231 else if (strcmp (fields[0], "post-compress bytes") == 0)
233 post_compress = atoll (fields[1]);
235 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
237 pre_decompress = atoll (fields[1]);
239 else if (strcmp (fields[0], "post-decompress bytes") == 0)
241 post_decompress = atoll (fields[1]);
245 iostats_submit (name, "traffic", link_rx, link_tx);
247 /* we need to force this order to avoid negative values with these unsigned */
248 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
249 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
251 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
253 if (collect_compression)
255 compression_submit (name, "data_in", post_decompress, pre_decompress);
256 compression_submit (name, "data_out", pre_compress, post_compress);
262 } /* int single_read */
264 /* for reading status version 1 */
265 static int multi1_read (const char *name, FILE *fh)
269 int fields_num, found_header = 0;
270 long long sum_users = 0;
272 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
273 while (fgets (buffer, sizeof (buffer), fh) != NULL)
275 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
278 if (strcmp (buffer, V1STRING) == 0)
284 /* skip the first lines until the client list section is found */
285 if (found_header == 0)
286 /* we can't start reading data until this string is found */
289 fields_num = openvpn_strsplit (buffer,
290 fields, STATIC_ARRAY_SIZE (fields));
294 if (collect_user_count)
295 /* If so, sum all users, ignore the individuals*/
299 if (collect_individual_users)
301 if (new_naming_schema)
303 iostats_submit (name, /* vpn instance */
304 fields[0], /* "Common Name" */
305 atoll (fields[2]), /* "Bytes Received" */
306 atoll (fields[3])); /* "Bytes Sent" */
310 iostats_submit (fields[0], /* "Common Name" */
311 NULL, /* unused when in multimode */
312 atoll (fields[2]), /* "Bytes Received" */
313 atoll (fields[3])); /* "Bytes Sent" */
321 if (collect_user_count)
322 numusers_submit(name, name, sum_users);
325 } /* int multi1_read */
327 /* for reading status version 2 */
328 static int multi2_read (const char *name, FILE *fh)
332 const int max_fields = STATIC_ARRAY_SIZE (fields);
333 int fields_num, read = 0;
334 long long sum_users = 0;
336 while (fgets (buffer, sizeof (buffer), fh) != NULL)
338 fields_num = openvpn_strsplit (buffer, fields, max_fields);
340 /* status file is generated by openvpn/multi.c:multi_print_status()
341 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
343 * The line we're expecting has 8 fields. We ignore all lines
344 * with more or less fields.
349 if (strcmp (fields[0], "CLIENT_LIST") != 0)
352 if (collect_user_count)
353 /* If so, sum all users, ignore the individuals*/
357 if (collect_individual_users)
359 if (new_naming_schema)
361 /* plugin inst = file name, type inst = fields[1] */
362 iostats_submit (name, /* vpn instance */
363 fields[1], /* "Common Name" */
364 atoll (fields[4]), /* "Bytes Received" */
365 atoll (fields[5])); /* "Bytes Sent" */
369 /* plugin inst = fields[1], type inst = "" */
370 iostats_submit (fields[1], /* "Common Name" */
371 NULL, /* unused when in multimode */
372 atoll (fields[4]), /* "Bytes Received" */
373 atoll (fields[5])); /* "Bytes Sent" */
380 if (collect_user_count)
382 numusers_submit(name, name, sum_users);
387 } /* int multi2_read */
389 /* for reading status version 3 */
390 static int multi3_read (const char *name, FILE *fh)
394 const int max_fields = STATIC_ARRAY_SIZE (fields);
395 int fields_num, read = 0;
396 long long sum_users = 0;
398 while (fgets (buffer, sizeof (buffer), fh) != NULL)
400 fields_num = strsplit (buffer, fields, max_fields);
402 /* status file is generated by openvpn/multi.c:multi_print_status()
403 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
405 * The line we're expecting has 12 fields. We ignore all lines
406 * with more or less fields.
408 if (fields_num != 12)
414 if (strcmp (fields[0], "CLIENT_LIST") != 0)
417 if (collect_user_count)
418 /* If so, sum all users, ignore the individuals*/
423 if (collect_individual_users)
425 if (new_naming_schema)
427 iostats_submit (name, /* vpn instance */
428 fields[1], /* "Common Name" */
429 atoll (fields[4]), /* "Bytes Received" */
430 atoll (fields[5])); /* "Bytes Sent" */
434 iostats_submit (fields[1], /* "Common Name" */
435 NULL, /* unused when in multimode */
436 atoll (fields[4]), /* "Bytes Received" */
437 atoll (fields[5])); /* "Bytes Sent" */
445 if (collect_user_count)
447 numusers_submit(name, name, sum_users);
452 } /* int multi3_read */
454 /* for reading status version 4 */
455 static int multi4_read (const char *name, FILE *fh)
459 const int max_fields = STATIC_ARRAY_SIZE (fields);
460 int fields_num, read = 0;
461 long long sum_users = 0;
463 while (fgets (buffer, sizeof (buffer), fh) != NULL)
465 fields_num = openvpn_strsplit (buffer, fields, max_fields);
467 /* status file is generated by openvpn/multi.c:multi_print_status()
468 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
470 * The line we're expecting has 9 fields. We ignore all lines
471 * with more or less fields.
477 if (strcmp (fields[0], "CLIENT_LIST") != 0)
481 if (collect_user_count)
482 /* If so, sum all users, ignore the individuals*/
486 if (collect_individual_users)
488 if (new_naming_schema)
490 /* plugin inst = file name, type inst = fields[1] */
491 iostats_submit (name, /* vpn instance */
492 fields[1], /* "Common Name" */
493 atoll (fields[4]), /* "Bytes Received" */
494 atoll (fields[5])); /* "Bytes Sent" */
498 /* plugin inst = fields[1], type inst = "" */
499 iostats_submit (fields[1], /* "Common Name" */
500 NULL, /* unused when in multimode */
501 atoll (fields[4]), /* "Bytes Received" */
502 atoll (fields[5])); /* "Bytes Sent" */
509 if (collect_user_count)
511 numusers_submit(name, name, sum_users);
516 } /* int multi4_read */
519 static int openvpn_read (void)
526 /* call the right read function for every status entry in the list */
527 for (int i = 0; i < vpn_num; i++)
531 fh = fopen (vpn_list[i]->file, "r");
535 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
536 sstrerror (errno, errbuf, sizeof (errbuf)));
541 switch (vpn_list[i]->version)
544 vpn_read = single_read(vpn_list[i]->name, fh);
548 vpn_read = multi1_read(vpn_list[i]->name, fh);
552 vpn_read = multi2_read(vpn_list[i]->name, fh);
556 vpn_read = multi3_read(vpn_list[i]->name, fh);
560 vpn_read = multi4_read(vpn_list[i]->name, fh);
568 return (read ? 0 : -1);
569 } /* int openvpn_read */
571 static int version_detect (const char *filename)
577 /* Sanity checking. We're called from the config handling routine, so
578 * better play it save. */
579 if ((filename == NULL) || (*filename == 0))
582 fh = fopen (filename, "r");
586 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
587 sstrerror (errno, errbuf, sizeof (errbuf)));
591 /* now search for the specific multimode data format */
592 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
594 /* we look at the first line searching for SINGLE mode configuration */
595 if (strcmp (buffer, VSSTRING) == 0)
597 DEBUG ("openvpn plugin: found status file version SINGLE");
601 /* searching for multi version 1 */
602 else if (strcmp (buffer, V1STRING) == 0)
604 DEBUG ("openvpn plugin: found status file version MULTI1");
608 /* searching for multi version 2 */
609 else if (strcmp (buffer, V2STRING) == 0)
611 DEBUG ("openvpn plugin: found status file version MULTI2");
615 /* searching for multi version 3 */
616 else if (strcmp (buffer, V3STRING) == 0)
618 DEBUG ("openvpn plugin: found status file version MULTI3");
622 /* searching for multi version 4 */
623 else if (strcmp (buffer, V4STRING) == 0)
625 DEBUG ("openvpn plugin: found status file version MULTI4");
633 /* This is only reached during configuration, so complaining to
634 * the user is in order. */
635 NOTICE ("openvpn plugin: %s: Unknown file format, please "
636 "report this as bug. Make sure to include "
637 "your status file, so the plugin can "
638 "be adapted.", filename);
644 } /* int version_detect */
646 static int openvpn_config (const char *key, const char *value)
648 if (strcasecmp ("StatusFile", key) == 0)
650 char *status_file, *status_name, *filename;
654 /* try to detect the status file format */
655 status_version = version_detect (value);
657 if (status_version == 0)
659 WARNING ("openvpn plugin: unable to detect status version, \
660 discarding status file \"%s\".", value);
664 status_file = sstrdup (value);
665 if (status_file == NULL)
668 WARNING ("openvpn plugin: sstrdup failed: %s",
669 sstrerror (errno, errbuf, sizeof (errbuf)));
673 /* it determines the file name as string starting at location filename + 1 */
674 filename = strrchr (status_file, (int) '/');
675 if (filename == NULL)
677 /* status_file is already the file name only */
678 status_name = status_file;
682 /* doesn't waste memory, uses status_file starting at filename + 1 */
683 status_name = filename + 1;
686 /* scan the list looking for a clone */
687 for (int i = 0; i < vpn_num; i++)
689 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
691 WARNING ("openvpn plugin: status filename \"%s\" "
692 "already used, please choose a "
693 "different one.", status_name);
699 /* create a new vpn element since file, version and name are ok */
700 temp = malloc (sizeof (*temp));
704 ERROR ("openvpn plugin: malloc failed: %s",
705 sstrerror (errno, errbuf, sizeof (errbuf)));
709 temp->file = status_file;
710 temp->version = status_version;
711 temp->name = status_name;
713 vpn_status_t **tmp_list = realloc (vpn_list, (vpn_num + 1) * sizeof (*vpn_list));
714 if (tmp_list == NULL)
717 ERROR ("openvpn plugin: realloc failed: %s",
718 sstrerror (errno, errbuf, sizeof (errbuf)));
727 vpn_list[vpn_num] = temp;
730 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
732 } /* if (strcasecmp ("StatusFile", key) == 0) */
733 else if ((strcasecmp ("CollectCompression", key) == 0)
734 || (strcasecmp ("Compression", key) == 0)) /* old, deprecated name */
736 if (IS_FALSE (value))
737 collect_compression = 0;
739 collect_compression = 1;
740 } /* if (strcasecmp ("CollectCompression", key) == 0) */
741 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
745 DEBUG ("openvpn plugin: using the new naming schema");
746 new_naming_schema = 1;
750 new_naming_schema = 0;
752 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
753 else if (strcasecmp("CollectUserCount", key) == 0)
756 collect_user_count = 1;
758 collect_user_count = 0;
759 } /* if (strcasecmp("CollectUserCount", key) == 0) */
760 else if (strcasecmp("CollectIndividualUsers", key) == 0)
762 if (IS_FALSE (value))
763 collect_individual_users = 0;
765 collect_individual_users = 1;
766 } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
773 } /* int openvpn_config */
775 /* shutdown callback */
776 static int openvpn_shutdown (void)
778 for (int i = 0; i < vpn_num; i++)
780 sfree (vpn_list[i]->file);
787 } /* int openvpn_shutdown */
789 static int openvpn_init (void)
791 if (!collect_individual_users
792 && !collect_compression
793 && !collect_user_count)
795 WARNING ("OpenVPN plugin: Neither `CollectIndividualUsers', "
796 "`CollectCompression', nor `CollectUserCount' is true. There's no "
797 "data left to collect.");
801 plugin_register_read ("openvpn", openvpn_read);
802 plugin_register_shutdown ("openvpn", openvpn_shutdown);
805 } /* int openvpn_init */
807 void module_register (void)
809 plugin_register_config ("openvpn", openvpn_config,
810 config_keys, config_keys_num);
811 plugin_register_init ("openvpn", openvpn_init);
812 } /* void module_register */
814 /* vim: set sw=2 ts=2 : */