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,
97 value_list_t vl = VALUE_LIST_INIT;
99 vl.values = &(value_t){.gauge = value};
101 sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin));
102 sstrncpy(vl.type, "users", sizeof(vl.type));
104 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
106 sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance));
108 plugin_dispatch_values(&vl);
109 } /* void numusers_submit */
111 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel
112 * per single endpoint */
113 static void iostats_submit(const char *pinst, const char *tinst, derive_t rx,
115 value_list_t vl = VALUE_LIST_INIT;
117 {.derive = rx}, {.derive = tx},
120 /* NOTE ON THE NEW NAMING SCHEMA:
121 * using plugin_instance to identify each vpn config (and
122 * status) file; using type_instance to identify the endpoint
123 * host when in multimode, traffic or overhead when in single.
127 vl.values_len = STATIC_ARRAY_SIZE(values);
128 sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin));
130 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
131 sstrncpy(vl.type, "if_octets", sizeof(vl.type));
133 sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance));
135 plugin_dispatch_values(&vl);
136 } /* void traffic_submit */
138 /* dispatches stats about data compression shown when in single mode */
139 static void compression_submit(const char *pinst, const char *tinst,
140 derive_t uncompressed, derive_t compressed) {
141 value_list_t vl = VALUE_LIST_INIT;
143 {.derive = uncompressed}, {.derive = compressed},
147 vl.values_len = STATIC_ARRAY_SIZE(values);
148 sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin));
150 sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance));
151 sstrncpy(vl.type, "compression", sizeof(vl.type));
153 sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance));
155 plugin_dispatch_values(&vl);
156 } /* void compression_submit */
158 static int single_read(const char *name, FILE *fh) {
161 const int max_fields = STATIC_ARRAY_SIZE(fields);
162 int fields_num, read = 0;
164 derive_t link_rx, link_tx;
165 derive_t tun_rx, tun_tx;
166 derive_t pre_compress, post_compress;
167 derive_t pre_decompress, post_decompress;
168 derive_t overhead_rx, overhead_tx;
179 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
180 fields_num = openvpn_strsplit(buffer, fields, max_fields);
182 /* status file is generated by openvpn/sig.c:print_status()
183 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
185 * The line we're expecting has 2 fields. We ignore all lines
186 * with more or less fields.
188 if (fields_num != 2) {
192 if (strcmp(fields[0], "TUN/TAP read bytes") == 0) {
193 /* read from the system and sent over the tunnel */
194 tun_tx = atoll(fields[1]);
195 } else if (strcmp(fields[0], "TUN/TAP write bytes") == 0) {
196 /* read from the tunnel and written in the system */
197 tun_rx = atoll(fields[1]);
198 } else if (strcmp(fields[0], "TCP/UDP read bytes") == 0) {
199 link_rx = atoll(fields[1]);
200 } else if (strcmp(fields[0], "TCP/UDP write bytes") == 0) {
201 link_tx = atoll(fields[1]);
202 } else if (strcmp(fields[0], "pre-compress bytes") == 0) {
203 pre_compress = atoll(fields[1]);
204 } else if (strcmp(fields[0], "post-compress bytes") == 0) {
205 post_compress = atoll(fields[1]);
206 } else if (strcmp(fields[0], "pre-decompress bytes") == 0) {
207 pre_decompress = atoll(fields[1]);
208 } else if (strcmp(fields[0], "post-decompress bytes") == 0) {
209 post_decompress = atoll(fields[1]);
213 iostats_submit(name, "traffic", link_rx, link_tx);
215 /* we need to force this order to avoid negative values with these unsigned */
216 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
217 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
219 iostats_submit(name, "overhead", overhead_rx, overhead_tx);
221 if (collect_compression) {
222 compression_submit(name, "data_in", post_decompress, pre_decompress);
223 compression_submit(name, "data_out", pre_compress, post_compress);
229 } /* int single_read */
231 /* for reading status version 1 */
232 static int multi1_read(const char *name, FILE *fh) {
235 int fields_num, found_header = 0;
236 long long sum_users = 0;
238 /* read the file until the "ROUTING TABLE" line is found (no more info after)
240 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
241 if (strcmp(buffer, "ROUTING TABLE\n") == 0)
244 if (strcmp(buffer, V1STRING) == 0) {
249 /* skip the first lines until the client list section is found */
250 if (found_header == 0)
251 /* we can't start reading data until this string is found */
254 fields_num = openvpn_strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
258 if (collect_user_count)
259 /* If so, sum all users, ignore the individuals*/
263 if (collect_individual_users) {
264 if (new_naming_schema) {
265 iostats_submit(name, /* vpn instance */
266 fields[0], /* "Common Name" */
267 atoll(fields[2]), /* "Bytes Received" */
268 atoll(fields[3])); /* "Bytes Sent" */
270 iostats_submit(fields[0], /* "Common Name" */
271 NULL, /* unused when in multimode */
272 atoll(fields[2]), /* "Bytes Received" */
273 atoll(fields[3])); /* "Bytes Sent" */
281 if (collect_user_count)
282 numusers_submit(name, name, sum_users);
285 } /* int multi1_read */
287 /* for reading status version 2 */
288 static int multi2_read(const char *name, FILE *fh) {
291 const int max_fields = STATIC_ARRAY_SIZE(fields);
292 int fields_num, read = 0;
293 long long sum_users = 0;
295 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
296 fields_num = openvpn_strsplit(buffer, fields, max_fields);
298 /* status file is generated by openvpn/multi.c:multi_print_status()
299 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
301 * The line we're expecting has 8 fields. We ignore all lines
302 * with more or less fields.
307 if (strcmp(fields[0], "CLIENT_LIST") != 0)
310 if (collect_user_count)
311 /* If so, sum all users, ignore the individuals*/
315 if (collect_individual_users) {
316 if (new_naming_schema) {
317 /* plugin inst = file name, type inst = fields[1] */
318 iostats_submit(name, /* vpn instance */
319 fields[1], /* "Common Name" */
320 atoll(fields[4]), /* "Bytes Received" */
321 atoll(fields[5])); /* "Bytes Sent" */
323 /* plugin inst = fields[1], type inst = "" */
324 iostats_submit(fields[1], /* "Common Name" */
325 NULL, /* unused when in multimode */
326 atoll(fields[4]), /* "Bytes Received" */
327 atoll(fields[5])); /* "Bytes Sent" */
334 if (collect_user_count) {
335 numusers_submit(name, name, sum_users);
340 } /* int multi2_read */
342 /* for reading status version 3 */
343 static int multi3_read(const char *name, FILE *fh) {
346 const int max_fields = STATIC_ARRAY_SIZE(fields);
347 int fields_num, read = 0;
348 long long sum_users = 0;
350 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
351 fields_num = strsplit(buffer, fields, max_fields);
353 /* status file is generated by openvpn/multi.c:multi_print_status()
354 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
356 * The line we're expecting has 12 fields. We ignore all lines
357 * with more or less fields.
359 if (fields_num != 12) {
362 if (strcmp(fields[0], "CLIENT_LIST") != 0)
365 if (collect_user_count)
366 /* If so, sum all users, ignore the individuals*/
371 if (collect_individual_users) {
372 if (new_naming_schema) {
373 iostats_submit(name, /* vpn instance */
374 fields[1], /* "Common Name" */
375 atoll(fields[4]), /* "Bytes Received" */
376 atoll(fields[5])); /* "Bytes Sent" */
378 iostats_submit(fields[1], /* "Common Name" */
379 NULL, /* unused when in multimode */
380 atoll(fields[4]), /* "Bytes Received" */
381 atoll(fields[5])); /* "Bytes Sent" */
389 if (collect_user_count) {
390 numusers_submit(name, name, sum_users);
395 } /* int multi3_read */
397 /* for reading status version 4 */
398 static int multi4_read(const char *name, FILE *fh) {
401 const int max_fields = STATIC_ARRAY_SIZE(fields);
402 int fields_num, read = 0;
403 long long sum_users = 0;
405 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
406 fields_num = openvpn_strsplit(buffer, fields, max_fields);
408 /* status file is generated by openvpn/multi.c:multi_print_status()
409 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
411 * The line we're expecting has 9 fields. We ignore all lines
412 * with more or less fields.
417 if (strcmp(fields[0], "CLIENT_LIST") != 0)
420 if (collect_user_count)
421 /* If so, sum all users, ignore the individuals*/
425 if (collect_individual_users) {
426 if (new_naming_schema) {
427 /* plugin inst = file name, type inst = fields[1] */
428 iostats_submit(name, /* vpn instance */
429 fields[1], /* "Common Name" */
430 atoll(fields[4]), /* "Bytes Received" */
431 atoll(fields[5])); /* "Bytes Sent" */
433 /* plugin inst = fields[1], type inst = "" */
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" */
444 if (collect_user_count) {
445 numusers_submit(name, name, sum_users);
450 } /* int multi4_read */
453 static int openvpn_read(void) {
462 /* call the right read function for every status entry in the list */
463 for (int i = 0; i < vpn_num; i++) {
466 fh = fopen(vpn_list[i]->file, "r");
469 WARNING("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
470 sstrerror(errno, errbuf, sizeof(errbuf)));
475 switch (vpn_list[i]->version) {
477 vpn_read = single_read(vpn_list[i]->name, fh);
481 vpn_read = multi1_read(vpn_list[i]->name, fh);
485 vpn_read = multi2_read(vpn_list[i]->name, fh);
489 vpn_read = multi3_read(vpn_list[i]->name, fh);
493 vpn_read = multi4_read(vpn_list[i]->name, fh);
501 return (read ? 0 : -1);
502 } /* int openvpn_read */
504 static int version_detect(const char *filename) {
509 /* Sanity checking. We're called from the config handling routine, so
510 * better play it save. */
511 if ((filename == NULL) || (*filename == 0))
514 fh = fopen(filename, "r");
517 WARNING("openvpn plugin: Unable to read \"%s\": %s", filename,
518 sstrerror(errno, errbuf, sizeof(errbuf)));
522 /* now search for the specific multimode data format */
523 while ((fgets(buffer, sizeof(buffer), fh)) != NULL) {
524 /* we look at the first line searching for SINGLE mode configuration */
525 if (strcmp(buffer, VSSTRING) == 0) {
526 DEBUG("openvpn plugin: found status file version SINGLE");
530 /* searching for multi version 1 */
531 else if (strcmp(buffer, V1STRING) == 0) {
532 DEBUG("openvpn plugin: found status file version MULTI1");
536 /* searching for multi version 2 */
537 else if (strcmp(buffer, V2STRING) == 0) {
538 DEBUG("openvpn plugin: found status file version MULTI2");
542 /* searching for multi version 3 */
543 else if (strcmp(buffer, V3STRING) == 0) {
544 DEBUG("openvpn plugin: found status file version MULTI3");
548 /* searching for multi version 4 */
549 else if (strcmp(buffer, V4STRING) == 0) {
550 DEBUG("openvpn plugin: found status file version MULTI4");
557 /* This is only reached during configuration, so complaining to
558 * the user is in order. */
559 NOTICE("openvpn plugin: %s: Unknown file format, please "
560 "report this as bug. Make sure to include "
561 "your status file, so the plugin can "
569 } /* int version_detect */
571 static int openvpn_config(const char *key, const char *value) {
572 if (strcasecmp("StatusFile", key) == 0) {
573 char *status_file, *status_name, *filename;
577 /* try to detect the status file format */
578 status_version = version_detect(value);
580 if (status_version == 0) {
581 WARNING("openvpn plugin: unable to detect status version, "
582 "discarding status file \"%s\".",
587 status_file = sstrdup(value);
588 if (status_file == NULL) {
590 WARNING("openvpn plugin: sstrdup failed: %s",
591 sstrerror(errno, errbuf, sizeof(errbuf)));
595 /* it determines the file name as string starting at location filename + 1
597 filename = strrchr(status_file, (int)'/');
598 if (filename == NULL) {
599 /* status_file is already the file name only */
600 status_name = status_file;
602 /* doesn't waste memory, uses status_file starting at filename + 1 */
603 status_name = filename + 1;
606 /* scan the list looking for a clone */
607 for (int i = 0; i < vpn_num; i++) {
608 if (strcasecmp(vpn_list[i]->name, status_name) == 0) {
609 WARNING("openvpn plugin: status filename \"%s\" "
610 "already used, please choose a "
618 /* create a new vpn element since file, version and name are ok */
619 temp = malloc(sizeof(*temp));
622 ERROR("openvpn plugin: malloc failed: %s",
623 sstrerror(errno, errbuf, sizeof(errbuf)));
627 temp->file = status_file;
628 temp->version = status_version;
629 temp->name = status_name;
631 vpn_status_t **tmp_list =
632 realloc(vpn_list, (vpn_num + 1) * sizeof(*vpn_list));
633 if (tmp_list == NULL) {
635 ERROR("openvpn plugin: realloc failed: %s",
636 sstrerror(errno, errbuf, sizeof(errbuf)));
645 vpn_list[vpn_num] = temp;
648 DEBUG("openvpn plugin: status file \"%s\" added", temp->file);
650 } /* if (strcasecmp ("StatusFile", key) == 0) */
651 else if ((strcasecmp("CollectCompression", key) == 0) ||
652 (strcasecmp("Compression", key) == 0)) /* old, deprecated name */
655 collect_compression = 0;
657 collect_compression = 1;
658 } /* if (strcasecmp ("CollectCompression", key) == 0) */
659 else if (strcasecmp("ImprovedNamingSchema", key) == 0) {
660 if (IS_TRUE(value)) {
661 DEBUG("openvpn plugin: using the new naming schema");
662 new_naming_schema = 1;
664 new_naming_schema = 0;
666 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
667 else if (strcasecmp("CollectUserCount", key) == 0) {
669 collect_user_count = 1;
671 collect_user_count = 0;
672 } /* if (strcasecmp("CollectUserCount", key) == 0) */
673 else if (strcasecmp("CollectIndividualUsers", key) == 0) {
675 collect_individual_users = 0;
677 collect_individual_users = 1;
678 } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
684 } /* int openvpn_config */
686 /* shutdown callback */
687 static int openvpn_shutdown(void) {
688 for (int i = 0; i < vpn_num; i++) {
689 sfree(vpn_list[i]->file);
696 } /* int openvpn_shutdown */
698 static int openvpn_init(void) {
699 if (!collect_individual_users && !collect_compression &&
700 !collect_user_count) {
701 WARNING("OpenVPN plugin: Neither `CollectIndividualUsers', "
702 "`CollectCompression', nor `CollectUserCount' is true. There's no "
703 "data left to collect.");
707 plugin_register_read("openvpn", openvpn_read);
708 plugin_register_shutdown("openvpn", openvpn_shutdown);
711 } /* int openvpn_init */
713 void module_register(void) {
714 plugin_register_config("openvpn", openvpn_config, config_keys,
716 plugin_register_init("openvpn", openvpn_init);
717 } /* void module_register */