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 VSSTRING "OpenVPN STATISTICS\n"
43 MULTI1 = 1, /* status-version 1 */
44 MULTI2, /* status-version 2 */
45 MULTI3, /* status-version 3 */
46 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
50 typedef struct vpn_status_s vpn_status_t;
52 static vpn_status_t **vpn_list = NULL;
53 static int vpn_num = 0;
55 static _Bool new_naming_schema = 0;
56 static _Bool collect_compression = 1;
57 static _Bool collect_user_count = 0;
58 static _Bool collect_individual_users = 1;
60 static const char *config_keys[] =
63 "Compression", /* old, deprecated name */
64 "ImprovedNamingSchema",
67 "CollectIndividualUsers"
69 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 * copy-n-pasted from common.c - changed delim to "," */
74 static int openvpn_strsplit (char *string, char **fields, size_t size)
83 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
93 } /* int openvpn_strsplit */
95 /* dispatches number of users */
96 static void numusers_submit (char *pinst, char *tinst, gauge_t value)
99 value_list_t vl = VALUE_LIST_INIT;
101 values[0].gauge = value;
104 vl.values_len = STATIC_ARRAY_SIZE (values);
105 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
106 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
107 sstrncpy (vl.type, "users", sizeof (vl.type));
109 sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
111 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
113 plugin_dispatch_values (&vl);
114 } /* void numusers_submit */
116 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
117 static void iostats_submit (char *pinst, char *tinst, derive_t rx, derive_t tx)
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,
137 sizeof (vl.plugin_instance));
138 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
140 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
142 plugin_dispatch_values (&vl);
143 } /* void traffic_submit */
145 /* dispatches stats about data compression shown when in single mode */
146 static void compression_submit (char *pinst, char *tinst,
147 derive_t uncompressed, derive_t compressed)
150 value_list_t vl = VALUE_LIST_INIT;
152 values[0].derive = uncompressed;
153 values[1].derive = compressed;
156 vl.values_len = STATIC_ARRAY_SIZE (values);
157 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
158 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
160 sstrncpy (vl.plugin_instance, pinst,
161 sizeof (vl.plugin_instance));
162 sstrncpy (vl.type, "compression", sizeof (vl.type));
164 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
166 plugin_dispatch_values (&vl);
167 } /* void compression_submit */
169 static int single_read (char *name, FILE *fh)
173 const int max_fields = STATIC_ARRAY_SIZE (fields);
174 int fields_num, read = 0;
176 derive_t link_rx, link_tx;
177 derive_t tun_rx, tun_tx;
178 derive_t pre_compress, post_compress;
179 derive_t pre_decompress, post_decompress;
180 derive_t overhead_rx, overhead_tx;
193 while (fgets (buffer, sizeof (buffer), fh) != NULL)
195 fields_num = openvpn_strsplit (buffer, fields, max_fields);
197 /* status file is generated by openvpn/sig.c:print_status()
198 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
200 * The line we're expecting has 2 fields. We ignore all lines
201 * with more or less fields.
208 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
210 /* read from the system and sent over the tunnel */
211 tun_tx = atoll (fields[1]);
213 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
215 /* read from the tunnel and written in the system */
216 tun_rx = atoll (fields[1]);
218 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
220 link_rx = atoll (fields[1]);
222 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
224 link_tx = atoll (fields[1]);
226 else if (strcmp (fields[0], "pre-compress bytes") == 0)
228 pre_compress = atoll (fields[1]);
230 else if (strcmp (fields[0], "post-compress bytes") == 0)
232 post_compress = atoll (fields[1]);
234 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
236 pre_decompress = atoll (fields[1]);
238 else if (strcmp (fields[0], "post-decompress bytes") == 0)
240 post_decompress = atoll (fields[1]);
244 iostats_submit (name, "traffic", link_rx, link_tx);
246 /* we need to force this order to avoid negative values with these unsigned */
247 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
248 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
250 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
252 if (collect_compression)
254 compression_submit (name, "data_in", post_decompress, pre_decompress);
255 compression_submit (name, "data_out", pre_compress, post_compress);
261 } /* int single_read */
263 /* for reading status version 1 */
264 static int multi1_read (char *name, FILE *fh)
268 int fields_num, found_header = 0;
269 long long sum_users = 0;
271 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
272 while (fgets (buffer, sizeof (buffer), fh) != NULL)
274 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
277 if (strcmp (buffer, V1STRING) == 0)
283 /* skip the first lines until the client list section is found */
284 if (found_header == 0)
285 /* we can't start reading data until this string is found */
288 fields_num = openvpn_strsplit (buffer,
289 fields, STATIC_ARRAY_SIZE (fields));
293 if (collect_user_count)
294 /* If so, sum all users, ignore the individuals*/
298 if (collect_individual_users)
300 if (new_naming_schema)
302 iostats_submit (name, /* vpn instance */
303 fields[0], /* "Common Name" */
304 atoll (fields[2]), /* "Bytes Received" */
305 atoll (fields[3])); /* "Bytes Sent" */
309 iostats_submit (fields[0], /* "Common Name" */
310 NULL, /* unused when in multimode */
311 atoll (fields[2]), /* "Bytes Received" */
312 atoll (fields[3])); /* "Bytes Sent" */
320 if (collect_user_count)
321 numusers_submit(name, name, sum_users);
324 } /* int multi1_read */
326 /* for reading status version 2 */
327 static int multi2_read (char *name, FILE *fh)
331 const int max_fields = STATIC_ARRAY_SIZE (fields);
332 int fields_num, read = 0;
333 long long sum_users = 0;
335 while (fgets (buffer, sizeof (buffer), fh) != NULL)
337 fields_num = openvpn_strsplit (buffer, fields, max_fields);
339 /* status file is generated by openvpn/multi.c:multi_print_status()
340 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
342 * The line we're expecting has 8 fields. We ignore all lines
343 * with more or less fields.
348 if (strcmp (fields[0], "CLIENT_LIST") != 0)
351 if (collect_user_count)
352 /* If so, sum all users, ignore the individuals*/
356 if (collect_individual_users)
358 if (new_naming_schema)
360 /* plugin inst = file name, type inst = fields[1] */
361 iostats_submit (name, /* vpn instance */
362 fields[1], /* "Common Name" */
363 atoll (fields[4]), /* "Bytes Received" */
364 atoll (fields[5])); /* "Bytes Sent" */
368 /* plugin inst = fields[1], type inst = "" */
369 iostats_submit (fields[1], /* "Common Name" */
370 NULL, /* unused when in multimode */
371 atoll (fields[4]), /* "Bytes Received" */
372 atoll (fields[5])); /* "Bytes Sent" */
379 if (collect_user_count)
381 numusers_submit(name, name, sum_users);
386 } /* int multi2_read */
388 /* for reading status version 3 */
389 static int multi3_read (char *name, FILE *fh)
393 const int max_fields = STATIC_ARRAY_SIZE (fields);
394 int fields_num, read = 0;
395 long long sum_users = 0;
397 while (fgets (buffer, sizeof (buffer), fh) != NULL)
399 fields_num = strsplit (buffer, fields, max_fields);
401 /* status file is generated by openvpn/multi.c:multi_print_status()
402 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
404 * The line we're expecting has 12 fields. We ignore all lines
405 * with more or less fields.
407 if (fields_num != 12)
413 if (strcmp (fields[0], "CLIENT_LIST") != 0)
416 if (collect_user_count)
417 /* If so, sum all users, ignore the individuals*/
422 if (collect_individual_users)
424 if (new_naming_schema)
426 iostats_submit (name, /* vpn instance */
427 fields[1], /* "Common Name" */
428 atoll (fields[4]), /* "Bytes Received" */
429 atoll (fields[5])); /* "Bytes Sent" */
433 iostats_submit (fields[1], /* "Common Name" */
434 NULL, /* unused when in multimode */
435 atoll (fields[4]), /* "Bytes Received" */
436 atoll (fields[5])); /* "Bytes Sent" */
444 if (collect_user_count)
446 numusers_submit(name, name, sum_users);
451 } /* int multi3_read */
454 static int openvpn_read (void)
461 /* call the right read function for every status entry in the list */
462 for (i = 0; i < vpn_num; i++)
464 fh = fopen (vpn_list[i]->file, "r");
468 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
469 sstrerror (errno, errbuf, sizeof (errbuf)));
474 switch (vpn_list[i]->version)
477 read = single_read(vpn_list[i]->name, fh);
481 read = multi1_read(vpn_list[i]->name, fh);
485 read = multi2_read(vpn_list[i]->name, fh);
489 read = multi3_read(vpn_list[i]->name, fh);
496 return (read ? 0 : -1);
497 } /* int openvpn_read */
499 static int version_detect (const char *filename)
505 /* Sanity checking. We're called from the config handling routine, so
506 * better play it save. */
507 if ((filename == NULL) || (*filename == 0))
510 fh = fopen (filename, "r");
514 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
515 sstrerror (errno, errbuf, sizeof (errbuf)));
519 /* now search for the specific multimode data format */
520 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
522 /* we look at the first line searching for SINGLE mode configuration */
523 if (strcmp (buffer, VSSTRING) == 0)
525 DEBUG ("openvpn plugin: found status file version SINGLE");
529 /* searching for multi version 1 */
530 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)
539 DEBUG ("openvpn plugin: found status file version MULTI2");
543 /* searching for multi version 3 */
544 else if (strcmp (buffer, V3STRING) == 0)
546 DEBUG ("openvpn plugin: found status file version MULTI3");
554 /* This is only reached during configuration, so complaining to
555 * the user is in order. */
556 NOTICE ("openvpn plugin: %s: Unknown file format, please "
557 "report this as bug. Make sure to include "
558 "your status file, so the plugin can "
559 "be adapted.", filename);
565 } /* int version_detect */
567 static int openvpn_config (const char *key, const char *value)
569 if (strcasecmp ("StatusFile", key) == 0)
571 char *status_file, *status_name, *filename;
572 int status_version, i;
575 /* try to detect the status file format */
576 status_version = version_detect (value);
578 if (status_version == 0)
580 WARNING ("openvpn plugin: unable to detect status version, \
581 discarding status file \"%s\".", value);
585 status_file = sstrdup (value);
586 if (status_file == NULL)
589 WARNING ("openvpn plugin: sstrdup failed: %s",
590 sstrerror (errno, errbuf, sizeof (errbuf)));
594 /* it determines the file name as string starting at location filename + 1 */
595 filename = strrchr (status_file, (int) '/');
596 if (filename == NULL)
598 /* status_file is already the file name only */
599 status_name = status_file;
603 /* doesn't waste memory, uses status_file starting at filename + 1 */
604 status_name = filename + 1;
607 /* scan the list looking for a clone */
608 for (i = 0; i < vpn_num; i++)
610 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
612 WARNING ("openvpn plugin: status filename \"%s\" "
613 "already used, please choose a "
614 "different one.", status_name);
620 /* create a new vpn element since file, version and name are ok */
621 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
622 temp->file = status_file;
623 temp->version = status_version;
624 temp->name = status_name;
626 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
627 if (vpn_list == NULL)
630 ERROR ("openvpn plugin: malloc failed: %s",
631 sstrerror (errno, errbuf, sizeof (errbuf)));
638 vpn_list[vpn_num] = temp;
641 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
643 } /* if (strcasecmp ("StatusFile", key) == 0) */
644 else if ((strcasecmp ("CollectCompression", key) == 0)
645 || (strcasecmp ("Compression", key) == 0)) /* old, deprecated name */
647 if (IS_FALSE (value))
648 collect_compression = 0;
650 collect_compression = 1;
651 } /* if (strcasecmp ("CollectCompression", key) == 0) */
652 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
656 DEBUG ("openvpn plugin: using the new naming schema");
657 new_naming_schema = 1;
661 new_naming_schema = 0;
663 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
664 else if (strcasecmp("CollectUserCount", key) == 0)
667 collect_user_count = 1;
669 collect_user_count = 0;
670 } /* if (strcasecmp("CollectUserCount", key) == 0) */
671 else if (strcasecmp("CollectIndividualUsers", key) == 0)
673 if (IS_FALSE (value))
674 collect_individual_users = 0;
676 collect_individual_users = 1;
677 } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
684 } /* int openvpn_config */
686 /* shutdown callback */
687 static int openvpn_shutdown (void)
691 for (i = 0; i < vpn_num; i++)
693 sfree (vpn_list[i]->file);
700 } /* int openvpn_shutdown */
702 static int openvpn_init (void)
704 if (!collect_individual_users
705 && !collect_compression
706 && !collect_user_count)
708 WARNING ("OpenVPN plugin: Neither `CollectIndividualUsers', "
709 "`CollectCompression', nor `CollectUserCount' is true. There's no "
710 "data left to collect.");
714 plugin_register_read ("openvpn", openvpn_read);
715 plugin_register_shutdown ("openvpn", openvpn_shutdown);
718 } /* int openvpn_init */
720 void module_register (void)
722 plugin_register_config ("openvpn", openvpn_config,
723 config_keys, config_keys_num);
724 plugin_register_init ("openvpn", openvpn_init);
725 } /* void module_register */
727 /* vim: set sw=2 ts=2 : */