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>
34 "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
36 "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes " \
37 "Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
39 "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes " \
40 "Received Bytes Sent Connected Since Connected Since (time_t)\n"
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"
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 */
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);
74 * copy-n-pasted from common.c - changed delim to "," */
75 static int openvpn_strsplit(char *string, char **fields, size_t size) {
83 while ((fields[i] = strtok_r(ptr, ",", &saveptr)) != NULL) {
92 } /* int openvpn_strsplit */
94 /* dispatches number of users */
95 static void numusers_submit(const char *pinst, const char *tinst,
98 value_list_t vl = VALUE_LIST_INIT;
100 values[0].gauge = value;
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));
108 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
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,
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.
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));
136 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
137 sstrncpy(vl.type, "if_octets", sizeof(vl.type));
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) {
148 value_list_t vl = VALUE_LIST_INIT;
150 values[0].derive = uncompressed;
151 values[1].derive = compressed;
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));
158 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
159 sstrncpy(vl.type, "compression", sizeof(vl.type));
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) {
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;
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
193 * The line we're expecting has 2 fields. We ignore all lines
194 * with more or less fields.
196 if (fields_num != 2) {
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]);
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);
237 } /* int single_read */
239 /* for reading status version 1 */
240 static int multi1_read(const char *name, FILE *fh) {
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)
248 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
249 if (strcmp(buffer, "ROUTING TABLE\n") == 0)
252 if (strcmp(buffer, V1STRING) == 0) {
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 */
262 fields_num = openvpn_strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
266 if (collect_user_count)
267 /* If so, sum all users, ignore the individuals*/
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" */
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" */
289 if (collect_user_count)
290 numusers_submit(name, name, sum_users);
293 } /* int multi1_read */
295 /* for reading status version 2 */
296 static int multi2_read(const char *name, FILE *fh) {
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
309 * The line we're expecting has 8 fields. We ignore all lines
310 * with more or less fields.
315 if (strcmp(fields[0], "CLIENT_LIST") != 0)
318 if (collect_user_count)
319 /* If so, sum all users, ignore the individuals*/
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" */
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" */
342 if (collect_user_count) {
343 numusers_submit(name, name, sum_users);
348 } /* int multi2_read */
350 /* for reading status version 3 */
351 static int multi3_read(const char *name, FILE *fh) {
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
364 * The line we're expecting has 12 fields. We ignore all lines
365 * with more or less fields.
367 if (fields_num != 12) {
370 if (strcmp(fields[0], "CLIENT_LIST") != 0)
373 if (collect_user_count)
374 /* If so, sum all users, ignore the individuals*/
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" */
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" */
397 if (collect_user_count) {
398 numusers_submit(name, name, sum_users);
403 } /* int multi3_read */
405 /* for reading status version 4 */
406 static int multi4_read(const char *name, FILE *fh) {
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
419 * The line we're expecting has 9 fields. We ignore all lines
420 * with more or less fields.
425 if (strcmp(fields[0], "CLIENT_LIST") != 0)
428 if (collect_user_count)
429 /* If so, sum all users, ignore the individuals*/
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" */
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" */
452 if (collect_user_count) {
453 numusers_submit(name, name, sum_users);
458 } /* int multi4_read */
461 static int openvpn_read(void) {
470 /* call the right read function for every status entry in the list */
471 for (int i = 0; i < vpn_num; i++) {
474 fh = fopen(vpn_list[i]->file, "r");
477 WARNING("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
478 sstrerror(errno, errbuf, sizeof(errbuf)));
483 switch (vpn_list[i]->version) {
485 vpn_read = single_read(vpn_list[i]->name, fh);
489 vpn_read = multi1_read(vpn_list[i]->name, fh);
493 vpn_read = multi2_read(vpn_list[i]->name, fh);
497 vpn_read = multi3_read(vpn_list[i]->name, fh);
501 vpn_read = multi4_read(vpn_list[i]->name, fh);
509 return (read ? 0 : -1);
510 } /* int openvpn_read */
512 static int version_detect(const char *filename) {
517 /* Sanity checking. We're called from the config handling routine, so
518 * better play it save. */
519 if ((filename == NULL) || (*filename == 0))
522 fh = fopen(filename, "r");
525 WARNING("openvpn plugin: Unable to read \"%s\": %s", filename,
526 sstrerror(errno, errbuf, sizeof(errbuf)));
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");
538 /* searching for multi version 1 */
539 else if (strcmp(buffer, V1STRING) == 0) {
540 DEBUG("openvpn plugin: found status file version MULTI1");
544 /* searching for multi version 2 */
545 else if (strcmp(buffer, V2STRING) == 0) {
546 DEBUG("openvpn plugin: found status file version MULTI2");
550 /* searching for multi version 3 */
551 else if (strcmp(buffer, V3STRING) == 0) {
552 DEBUG("openvpn plugin: found status file version MULTI3");
556 /* searching for multi version 4 */
557 else if (strcmp(buffer, V4STRING) == 0) {
558 DEBUG("openvpn plugin: found status file version MULTI4");
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 "
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;
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\".",
595 status_file = sstrdup(value);
596 if (status_file == NULL) {
598 WARNING("openvpn plugin: sstrdup failed: %s",
599 sstrerror(errno, errbuf, sizeof(errbuf)));
603 /* it determines the file name as string starting at location filename + 1
605 filename = strrchr(status_file, (int)'/');
606 if (filename == NULL) {
607 /* status_file is already the file name only */
608 status_name = status_file;
610 /* doesn't waste memory, uses status_file starting at filename + 1 */
611 status_name = filename + 1;
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 "
626 /* create a new vpn element since file, version and name are ok */
627 temp = malloc(sizeof(*temp));
630 ERROR("openvpn plugin: malloc failed: %s",
631 sstrerror(errno, errbuf, sizeof(errbuf)));
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) {
643 ERROR("openvpn plugin: realloc failed: %s",
644 sstrerror(errno, errbuf, sizeof(errbuf)));
653 vpn_list[vpn_num] = temp;
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 */
663 collect_compression = 0;
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;
672 new_naming_schema = 0;
674 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
675 else if (strcasecmp("CollectUserCount", key) == 0) {
677 collect_user_count = 1;
679 collect_user_count = 0;
680 } /* if (strcasecmp("CollectUserCount", key) == 0) */
681 else if (strcasecmp("CollectIndividualUsers", key) == 0) {
683 collect_individual_users = 0;
685 collect_individual_users = 1;
686 } /* if (strcasecmp("CollectIndividualUsers", key) == 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);
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.");
715 plugin_register_read("openvpn", openvpn_read);
716 plugin_register_shutdown("openvpn", openvpn_shutdown);
719 } /* int openvpn_init */
721 void module_register(void) {
722 plugin_register_config("openvpn", openvpn_config, config_keys,
724 plugin_register_init("openvpn", openvpn_init);
725 } /* void module_register */
727 /* vim: set sw=2 ts=2 : */