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;
56 static int number_connectedusers_only = 0;
58 static const char *config_keys[] =
62 "ImprovedNamingSchema",
66 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
70 * copy-n-pasted from common.c - changed delim to "," */
71 static int openvpn_strsplit (char *string, char **fields, size_t size)
80 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
90 } /* int openvpn_strsplit */
92 /* dispatches number of users */
93 static void numusers_submit (char *pinst, char *tinst, gauge_t value)
96 value_list_t vl = VALUE_LIST_INIT;
98 values[0].gauge = value;
101 vl.values_len = STATIC_ARRAY_SIZE (values);
102 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
103 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
104 sstrncpy (vl.type, "users", sizeof (vl.type));
106 sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
108 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
110 plugin_dispatch_values (&vl);
111 } /* void numusers_submit */
113 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
114 static void iostats_submit (char *pinst, char *tinst, counter_t rx, counter_t tx)
117 value_list_t vl = VALUE_LIST_INIT;
119 values[0].counter = rx;
120 values[1].counter = tx;
122 /* NOTE ON THE NEW NAMING SCHEMA:
123 * using plugin_instance to identify each vpn config (and
124 * status) file; using type_instance to identify the endpoint
125 * host when in multimode, traffic or overhead when in single.
129 vl.values_len = STATIC_ARRAY_SIZE (values);
130 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
131 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
133 sstrncpy (vl.plugin_instance, pinst,
134 sizeof (vl.plugin_instance));
135 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
137 sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
139 plugin_dispatch_values (&vl);
140 } /* void traffic_submit */
142 /* dispatches stats about data compression shown when in single mode */
143 static void compression_submit (char *pinst, char *tinst,
144 counter_t uncompressed, counter_t compressed)
147 value_list_t vl = VALUE_LIST_INIT;
149 values[0].counter = uncompressed;
150 values[1].counter = compressed;
153 vl.values_len = STATIC_ARRAY_SIZE (values);
154 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
155 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
157 sstrncpy (vl.plugin_instance, pinst,
158 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 (char *name, FILE *fh)
170 const int max_fields = STATIC_ARRAY_SIZE (fields);
171 int fields_num, read = 0;
173 counter_t link_rx, link_tx;
174 counter_t tun_rx, tun_tx;
175 counter_t pre_compress, post_compress;
176 counter_t pre_decompress, post_decompress;
177 counter_t overhead_rx, overhead_tx;
190 while (fgets (buffer, sizeof (buffer), fh) != NULL)
192 fields_num = openvpn_strsplit (buffer, fields, max_fields);
194 /* status file is generated by openvpn/sig.c:print_status()
195 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
197 * The line we're expecting has 2 fields. We ignore all lines
198 * with more or less fields.
205 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
207 /* read from the system and sent over the tunnel */
208 tun_tx = atoll (fields[1]);
210 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
212 /* read from the tunnel and written in the system */
213 tun_rx = atoll (fields[1]);
215 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
217 link_rx = atoll (fields[1]);
219 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
221 link_tx = atoll (fields[1]);
223 else if (strcmp (fields[0], "pre-compress bytes") == 0)
225 pre_compress = atoll (fields[1]);
227 else if (strcmp (fields[0], "post-compress bytes") == 0)
229 post_compress = atoll (fields[1]);
231 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
233 pre_decompress = atoll (fields[1]);
235 else if (strcmp (fields[0], "post-decompress bytes") == 0)
237 post_decompress = atoll (fields[1]);
241 iostats_submit (name, "traffic", link_rx, link_tx);
243 /* we need to force this order to avoid negative values with these unsigned */
244 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
245 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
247 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
249 if (store_compression)
251 compression_submit (name, "data_in", post_decompress, pre_decompress);
252 compression_submit (name, "data_out", pre_compress, post_compress);
258 } /* int single_read */
260 /* for reading status version 1 */
261 static int multi1_read (char *name, FILE *fh)
265 int fields_num, read = 0, found_header = 0;
266 long long sum_users = 0;
268 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
269 while (fgets (buffer, sizeof (buffer), fh) != NULL)
271 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
274 if (strcmp (buffer, V1STRING) == 0)
280 /* skip the first lines until the client list section is found */
281 if (found_header == 0)
282 /* we can't start reading data until this string is found */
285 fields_num = openvpn_strsplit (buffer,
286 fields, STATIC_ARRAY_SIZE (fields));
290 if (number_connectedusers)
291 /* If so, sum all users, ignore the individuals*/
295 if (number_connectedusers_only==0)
297 if (new_naming_schema)
299 iostats_submit (fields[0], /* "Common Name" */
300 NULL, /* unused when in multimode */
301 atoll (fields[2]), /* "Bytes Received" */
302 atoll (fields[3])); /* "Bytes Sent" */
306 iostats_submit (name, /* vpn instance */
307 fields[0], /* "Common Name" */
308 atoll (fields[2]), /* "Bytes Received" */
309 atoll (fields[3])); /* "Bytes Sent" */
316 if (number_connectedusers)
318 numusers_submit(name, name, sum_users);
323 } /* int multi1_read */
325 /* for reading status version 2 */
326 static int multi2_read (char *name, FILE *fh)
330 const int max_fields = STATIC_ARRAY_SIZE (fields);
331 int fields_num, read = 0;
332 long long sum_users = 0;
334 while (fgets (buffer, sizeof (buffer), fh) != NULL)
336 fields_num = openvpn_strsplit (buffer, fields, max_fields);
338 /* status file is generated by openvpn/multi.c:multi_print_status()
339 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
341 * The line we're expecting has 8 fields. We ignore all lines
342 * with more or less fields.
347 if (strcmp (fields[0], "CLIENT_LIST") != 0)
350 if (number_connectedusers)
351 /* If so, sum all users, ignore the individuals*/
355 if (number_connectedusers_only==0)
357 if (new_naming_schema)
359 /* plugin inst = file name, type inst = fields[1] */
360 iostats_submit (name, /* vpn instance */
361 fields[1], /* "Common Name" */
362 atoll (fields[4]), /* "Bytes Received" */
363 atoll (fields[5])); /* "Bytes Sent" */
367 /* plugin inst = fields[1], type inst = "" */
368 iostats_submit (fields[1], /* "Common Name" */
369 NULL, /* unused when in multimode */
370 atoll (fields[4]), /* "Bytes Received" */
371 atoll (fields[5])); /* "Bytes Sent" */
378 if (number_connectedusers)
380 numusers_submit(name, name, sum_users);
385 } /* int multi2_read */
387 /* for reading status version 3 */
388 static int multi3_read (char *name, FILE *fh)
392 const int max_fields = STATIC_ARRAY_SIZE (fields);
393 int fields_num, read = 0;
394 long long sum_users = 0;
396 while (fgets (buffer, sizeof (buffer), fh) != NULL)
398 fields_num = strsplit (buffer, fields, max_fields);
400 /* status file is generated by openvpn/multi.c:multi_print_status()
401 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
403 * The line we're expecting has 12 fields. We ignore all lines
404 * with more or less fields.
406 if (fields_num != 12)
412 if (strcmp (fields[0], "CLIENT_LIST") != 0)
415 if (number_connectedusers)
416 /* If so, sum all users, ignore the individuals*/
421 if (number_connectedusers_only==0)
423 if (new_naming_schema)
425 iostats_submit (name, /* vpn instance */
426 fields[1], /* "Common Name" */
427 atoll (fields[4]), /* "Bytes Received" */
428 atoll (fields[5])); /* "Bytes Sent" */
432 iostats_submit (fields[1], /* "Common Name" */
433 NULL, /* unused when in multimode */
434 atoll (fields[4]), /* "Bytes Received" */
435 atoll (fields[5])); /* "Bytes Sent" */
443 if (number_connectedusers)
445 numusers_submit(name, name, sum_users);
450 } /* int multi3_read */
453 static int openvpn_read (void)
460 /* call the right read function for every status entry in the list */
461 for (i = 0; i < vpn_num; i++)
463 fh = fopen (vpn_list[i]->file, "r");
467 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
468 sstrerror (errno, errbuf, sizeof (errbuf)));
473 switch (vpn_list[i]->version)
476 read = single_read(vpn_list[i]->name, fh);
480 read = multi1_read(vpn_list[i]->name, fh);
484 read = multi2_read(vpn_list[i]->name, fh);
488 read = multi3_read(vpn_list[i]->name, fh);
495 return (read ? 0 : -1);
496 } /* int openvpn_read */
498 static int version_detect (const char *filename)
504 /* Sanity checking. We're called from the config handling routine, so
505 * better play it save. */
506 if ((filename == NULL) || (*filename == 0))
509 fh = fopen (filename, "r");
513 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
514 sstrerror (errno, errbuf, sizeof (errbuf)));
518 /* now search for the specific multimode data format */
519 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
521 /* we look at the first line searching for SINGLE mode configuration */
522 if (strcmp (buffer, VSSTRING) == 0)
524 DEBUG ("openvpn plugin: found status file version SINGLE");
528 /* searching for multi version 1 */
529 else if (strcmp (buffer, V1STRING) == 0)
531 DEBUG ("openvpn plugin: found status file version MULTI1");
535 /* searching for multi version 2 */
536 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)
545 DEBUG ("openvpn plugin: found status file version MULTI3");
553 /* This is only reached during configuration, so complaining to
554 * the user is in order. */
555 NOTICE ("openvpn plugin: %s: Unknown file format, please "
556 "report this as bug. Make sure to include "
557 "your status file, so the plugin can "
558 "be adapted.", filename);
564 } /* int version_detect */
566 static int openvpn_config (const char *key, const char *value)
568 if (strcasecmp ("StatusFile", key) == 0)
570 char *status_file, *status_name, *filename;
571 int status_version, i;
574 /* try to detect the status file format */
575 status_version = version_detect (value);
577 if (status_version == 0)
579 WARNING ("openvpn plugin: unable to detect status version, \
580 discarding status file \"%s\".", value);
584 status_file = sstrdup (value);
585 if (status_file == NULL)
588 WARNING ("openvpn plugin: sstrdup failed: %s",
589 sstrerror (errno, errbuf, sizeof (errbuf)));
593 /* it determines the file name as string starting at location filename + 1 */
594 filename = strrchr (status_file, (int) '/');
595 if (filename == NULL)
597 /* status_file is already the file name only */
598 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 (i = 0; i < vpn_num; i++)
609 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
611 WARNING ("openvpn plugin: status filename \"%s\" "
612 "already used, please choose a "
613 "different one.", status_name);
619 /* create a new vpn element since file, version and name are ok */
620 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
621 temp->file = status_file;
622 temp->version = status_version;
623 temp->name = status_name;
625 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
626 if (vpn_list == NULL)
629 ERROR ("openvpn plugin: malloc failed: %s",
630 sstrerror (errno, errbuf, sizeof (errbuf)));
637 vpn_list[vpn_num] = temp;
640 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
642 } /* if (strcasecmp ("StatusFile", key) == 0) */
643 else if (strcasecmp ("Compression", key) == 0)
646 store_compression = 1;
649 store_compression = 0;
650 DEBUG ("openvpn plugin: no 'compression statistcs' collected");
652 } /* if (strcasecmp ("Compression", key) == 0) */
653 else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
657 DEBUG ("openvpn plugin: using the new naming schema");
658 new_naming_schema = 1;
662 new_naming_schema = 0;
664 } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
665 else if (strcasecmp("AggregateUsers", key) == 0)
669 DEBUG ("openvpn plugin: Summing up all users");
670 number_connectedusers = 1;
674 number_connectedusers = 0;
676 } /* if (strcasecmp("AggregateUsers", key) == 0) */
677 else if (strcasecmp("OnlyAggregateUsers", key) == 0)
681 DEBUG ("openvpn plugin: Summing up all users");
682 number_connectedusers_only = 1;
683 number_connectedusers = 1;
687 number_connectedusers_only = 0;
689 } /* if (strcasecmp("OnlyAggregateUsers", key) == 0) */
696 } /* int openvpn_config */
698 /* shutdown callback */
699 static int openvpn_shutdown (void)
703 for (i = 0; i < vpn_num; i++)
705 sfree (vpn_list[i]->file);
712 } /* int openvpn_shutdown */
714 void module_register (void)
716 plugin_register_config ("openvpn", openvpn_config,
717 config_keys, config_keys_num);
718 plugin_register_read ("openvpn", openvpn_read);
719 plugin_register_shutdown ("openvpn", openvpn_shutdown);
720 } /* void module_register */
722 /* vim: set sw=2 ts=2 : */