2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2009 Florian octo Forster
5 * Copyright (C) 2009 Marco Chiappero
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Doug MacEachern <dougm at hyperic.com>
22 * Florian octo Forster <octo at verplant.org>
23 * Marco Chiappero <marco at absence.it>
30 #define V1STRING "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
31 #define V2STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
32 #define V3STRING "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)\n"
33 #define VSSTRING "OpenVPN STATISTICS\n"
41 MULTI1 = 1, /* status-version 1 */
42 MULTI2, /* status-version 2 */
43 MULTI3, /* status-version 3 */
44 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
48 typedef struct vpn_status_s vpn_status_t;
50 static vpn_status_t **vpn_list = NULL;
51 static int vpn_num = 0;
53 static int store_compression = 1;
54 static int new_naming_schema = 0;
55 static int number_connectedusers = 0;
57 static const char *config_keys[] =
61 "ImprovedNamingSchema",
64 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
68 * copy-n-pasted from common.c - changed delim to "," */
69 static int openvpn_strsplit (char *string, char **fields, size_t size)
78 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
88 } /* int openvpn_strsplit */
90 /* dispatches number of users */
91 static void numusers_submit (char *pinst, char *tinst, gauge_t value)
94 value_list_t vl = VALUE_LIST_INIT;
96 values[0].gauge = value;
99 vl.values_len = STATIC_ARRAY_SIZE (values);
100 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
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 per single endpoint */
112 static void iostats_submit (char *pinst, char *tinst, counter_t rx, counter_t tx)
115 value_list_t vl = VALUE_LIST_INIT;
117 values[0].counter = rx;
118 values[1].counter = 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.host, hostname_g, sizeof (vl.host));
129 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
131 sstrncpy (vl.plugin_instance, pinst,
132 sizeof (vl.plugin_instance));
133 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
135 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
137 plugin_dispatch_values (&vl);
138 } /* void traffic_submit */
140 /* dispatches stats about data compression shown when in single mode */
141 static void compression_submit (char *pinst, char *tinst,
142 counter_t uncompressed, counter_t compressed)
145 value_list_t vl = VALUE_LIST_INIT;
147 values[0].counter = uncompressed;
148 values[1].counter = compressed;
151 vl.values_len = STATIC_ARRAY_SIZE (values);
152 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
153 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
155 sstrncpy (vl.plugin_instance, pinst,
156 sizeof (vl.plugin_instance));
157 sstrncpy (vl.type, "compression", sizeof (vl.type));
159 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
161 plugin_dispatch_values (&vl);
162 } /* void compression_submit */
164 static int single_read (char *name, FILE *fh)
168 const int max_fields = STATIC_ARRAY_SIZE (fields);
169 int fields_num, read = 0;
171 counter_t link_rx, link_tx;
172 counter_t tun_rx, tun_tx;
173 counter_t pre_compress, post_compress;
174 counter_t pre_decompress, post_decompress;
175 counter_t overhead_rx, overhead_tx;
188 while (fgets (buffer, sizeof (buffer), fh) != NULL)
190 fields_num = openvpn_strsplit (buffer, fields, max_fields);
192 /* status file is generated by openvpn/sig.c:print_status()
193 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
195 * The line we're expecting has 2 fields. We ignore all lines
196 * with more or less fields.
203 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
205 /* read from the system and sent over the tunnel */
206 tun_tx = atoll (fields[1]);
208 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
210 /* read from the tunnel and written in the system */
211 tun_rx = atoll (fields[1]);
213 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
215 link_rx = atoll (fields[1]);
217 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
219 link_tx = atoll (fields[1]);
221 else if (strcmp (fields[0], "pre-compress bytes") == 0)
223 pre_compress = atoll (fields[1]);
225 else if (strcmp (fields[0], "post-compress bytes") == 0)
227 post_compress = atoll (fields[1]);
229 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
231 pre_decompress = atoll (fields[1]);
233 else if (strcmp (fields[0], "post-decompress bytes") == 0)
235 post_decompress = atoll (fields[1]);
239 iostats_submit (name, "traffic", link_rx, link_tx);
241 /* we need to force this order to avoid negative values with these unsigned */
242 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
243 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
245 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
247 if (store_compression)
249 compression_submit (name, "data_in", post_decompress, pre_decompress);
250 compression_submit (name, "data_out", pre_compress, post_compress);
256 } /* int single_read */
258 /* for reading status version 1 */
259 static int multi1_read (char *name, FILE *fh)
263 int fields_num, read = 0, found_header = 0;
264 long long sum_users = 0;
266 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
267 while (fgets (buffer, sizeof (buffer), fh) != NULL)
269 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
272 if (strcmp (buffer, V1STRING) == 0)
278 /* skip the first lines until the client list section is found */
279 if (found_header == 0)
280 /* we can't start reading data until this string is found */
283 fields_num = openvpn_strsplit (buffer,
284 fields, STATIC_ARRAY_SIZE (fields));
288 if (number_connectedusers)
289 /* If so, sum all users, ignore the individuals*/
294 if (new_naming_schema)
296 iostats_submit (fields[0], /* "Common Name" */
297 NULL, /* unused when in multimode */
298 atoll (fields[2]), /* "Bytes Received" */
299 atoll (fields[3])); /* "Bytes Sent" */
303 iostats_submit (name, /* vpn instance */
304 fields[0], /* "Common Name" */
305 atoll (fields[2]), /* "Bytes Received" */
306 atoll (fields[3])); /* "Bytes Sent" */
312 if (number_connectedusers && read == 1)
314 numusers_submit(name, name, sum_users);
318 } /* int multi1_read */
320 /* for reading status version 2 */
321 static int multi2_read (char *name, FILE *fh)
325 const int max_fields = STATIC_ARRAY_SIZE (fields);
326 int fields_num, read = 0;
327 long long sum_users = 0;
329 while (fgets (buffer, sizeof (buffer), fh) != NULL)
331 fields_num = openvpn_strsplit (buffer, fields, max_fields);
333 /* status file is generated by openvpn/multi.c:multi_print_status()
334 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
336 * The line we're expecting has 8 fields. We ignore all lines
337 * with more or less fields.
342 if (strcmp (fields[0], "CLIENT_LIST") != 0)
345 if (number_connectedusers)
346 /* If so, sum all users, ignore the individuals*/
351 if (new_naming_schema)
353 /* plugin inst = file name, type inst = fields[1] */
354 iostats_submit (name, /* vpn instance */
355 fields[1], /* "Common Name" */
356 atoll (fields[4]), /* "Bytes Received" */
357 atoll (fields[5])); /* "Bytes Sent" */
361 /* plugin inst = fields[1], type inst = "" */
362 iostats_submit (fields[1], /* "Common Name" */
363 NULL, /* unused when in multimode */
364 atoll (fields[4]), /* "Bytes Received" */
365 atoll (fields[5])); /* "Bytes Sent" */
371 if (number_connectedusers && read == 1)
373 numusers_submit(name, name, sum_users);
377 } /* int multi2_read */
379 /* for reading status version 3 */
380 static int multi3_read (char *name, FILE *fh)
384 const int max_fields = STATIC_ARRAY_SIZE (fields);
385 int fields_num, read = 0;
386 long long sum_users = 0;
388 while (fgets (buffer, sizeof (buffer), fh) != NULL)
390 fields_num = strsplit (buffer, fields, max_fields);
392 /* status file is generated by openvpn/multi.c:multi_print_status()
393 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
395 * The line we're expecting has 12 fields. We ignore all lines
396 * with more or less fields.
398 if (fields_num != 12)
404 if (strcmp (fields[0], "CLIENT_LIST") != 0)
407 if (number_connectedusers)
408 /* If so, sum all users, ignore the individuals*/
413 if (new_naming_schema)
415 iostats_submit (name, /* vpn instance */
416 fields[1], /* "Common Name" */
417 atoll (fields[4]), /* "Bytes Received" */
418 atoll (fields[5])); /* "Bytes Sent" */
422 iostats_submit (fields[1], /* "Common Name" */
423 NULL, /* unused when in multimode */
424 atoll (fields[4]), /* "Bytes Received" */
425 atoll (fields[5])); /* "Bytes Sent" */
432 if (number_connectedusers && read == 1)
434 numusers_submit(name, name, sum_users);
438 } /* int multi3_read */
441 static int openvpn_read (void)
448 /* call the right read function for every status entry in the list */
449 for (i = 0; i < vpn_num; i++)
451 fh = fopen (vpn_list[i]->file, "r");
455 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
456 sstrerror (errno, errbuf, sizeof (errbuf)));
461 switch (vpn_list[i]->version)
464 read = single_read(vpn_list[i]->name, fh);
468 read = multi1_read(vpn_list[i]->name, fh);
472 read = multi2_read(vpn_list[i]->name, fh);
476 read = multi3_read(vpn_list[i]->name, fh);
483 return (read ? 0 : -1);
484 } /* int openvpn_read */
486 static int version_detect (const char *filename)
492 /* Sanity checking. We're called from the config handling routine, so
493 * better play it save. */
494 if ((filename == NULL) || (*filename == 0))
497 fh = fopen (filename, "r");
501 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
502 sstrerror (errno, errbuf, sizeof (errbuf)));
506 /* now search for the specific multimode data format */
507 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
509 /* we look at the first line searching for SINGLE mode configuration */
510 if (strcmp (buffer, VSSTRING) == 0)
512 DEBUG ("openvpn plugin: found status file version SINGLE");
516 /* searching for multi version 1 */
517 else if (strcmp (buffer, V1STRING) == 0)
519 DEBUG ("openvpn plugin: found status file version MULTI1");
523 /* searching for multi version 2 */
524 else if (strcmp (buffer, V2STRING) == 0)
526 DEBUG ("openvpn plugin: found status file version MULTI2");
530 /* searching for multi version 3 */
531 else if (strcmp (buffer, V3STRING) == 0)
533 DEBUG ("openvpn plugin: found status file version MULTI3");
541 /* This is only reached during configuration, so complaining to
542 * the user is in order. */
543 NOTICE ("openvpn plugin: %s: Unknown file format, please "
544 "report this as bug. Make sure to include "
545 "your status file, so the plugin can "
546 "be adapted.", filename);
552 } /* int version_detect */
554 static int openvpn_config (const char *key, const char *value)
556 if (strcasecmp ("StatusFile", key) == 0)
558 char *status_file, *status_name, *filename;
559 int status_version, i;
562 /* try to detect the status file format */
563 status_version = version_detect (value);
565 if (status_version == 0)
567 WARNING ("openvpn plugin: unable to detect status version, \
568 discarding status file \"%s\".", value);
572 status_file = sstrdup (value);
573 if (status_file == NULL)
576 WARNING ("openvpn plugin: sstrdup failed: %s",
577 sstrerror (errno, errbuf, sizeof (errbuf)));
581 /* it determines the file name as string starting at location filename + 1 */
582 filename = strrchr (status_file, (int) '/');
583 if (filename == NULL)
585 /* status_file is already the file name only */
586 status_name = status_file;
590 /* doesn't waste memory, uses status_file starting at filename + 1 */
591 status_name = filename + 1;
594 /* scan the list looking for a clone */
595 for (i = 0; i < vpn_num; i++)
597 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
599 WARNING ("openvpn plugin: status filename \"%s\" "
600 "already used, please choose a "
601 "different one.", status_name);
607 /* create a new vpn element since file, version and name are ok */
608 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
609 temp->file = status_file;
610 temp->version = status_version;
611 temp->name = status_name;
613 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
614 if (vpn_list == NULL)
617 ERROR ("openvpn plugin: malloc failed: %s",
618 sstrerror (errno, errbuf, sizeof (errbuf)));
625 vpn_list[vpn_num] = temp;
628 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
630 } /* if (strcasecmp ("StatusFile", key) == 0) */
631 else if (strcasecmp ("Compression", key) == 0)
634 store_compression = 1;
637 store_compression = 0;
638 DEBUG ("openvpn plugin: no 'compression statistcs' collected");
640 } /* if (strcasecmp ("Compression", key) == 0) */
641 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
645 DEBUG ("openvpn plugin: using the new naming schema");
646 new_naming_schema = 1;
650 new_naming_schema = 0;
652 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
653 else if (strcasecmp("AggregateUsers", key) == 0)
657 DEBUG ("openvpn plugin: Summing up all users");
658 number_connectedusers = 1;
662 number_connectedusers = 0;
664 } /* if (strcasecmp("AggregateUsers", key) == 0) */
671 } /* int openvpn_config */
673 /* shutdown callback */
674 static int openvpn_shutdown (void)
678 for (i = 0; i < vpn_num; i++)
680 sfree (vpn_list[i]->file);
687 } /* int openvpn_shutdown */
689 void module_register (void)
691 plugin_register_config ("openvpn", openvpn_config,
692 config_keys, config_keys_num);
693 plugin_register_read ("openvpn", openvpn_read);
694 plugin_register_shutdown ("openvpn", openvpn_shutdown);
695 } /* void module_register */
697 /* vim: set sw=2 ts=2 : */