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"
40 MULTI1 = 1, /* status-version 1 */
41 MULTI2, /* status-version 2 */
42 MULTI3, /* status-version 3 */
43 SINGLE = 10 /* currently no versions for single mode, maybe in the future */
47 typedef struct vpn_status_s vpn_status_t;
49 static vpn_status_t **vpn_list = NULL;
50 static int vpn_num = 0;
52 static int store_compression = 1;
54 static const char *config_keys[] =
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63 /* copy-n-pasted from common.c - changed delim to "," */
64 static int openvpn_strsplit (char *string, char **fields, size_t size)
73 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
83 } /* int openvpn_strsplit */
86 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
87 static void iostats_submit (char *name, char *type, counter_t rx, counter_t tx)
90 value_list_t vl = VALUE_LIST_INIT;
92 values[0].counter = rx;
93 values[1].counter = tx;
95 /* NOTE: using plugin_instance to identify each vpn config (and
96 * status) file; using type_instance to identify the endpoint
97 * host when in multimode, traffic or overhead when in single.
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.plugin_instance, name, sizeof (vl.plugin_instance));
105 sstrncpy (vl.type, "io_octets", sizeof (vl.type));
106 sstrncpy (vl.type_instance, type, sizeof (vl.type_instance));
108 plugin_dispatch_values (&vl);
109 } /* void traffic_submit */
111 /* dispatches stats about data compression shown when in single mode */
112 static void compression_submit (char *name, char *type, counter_t uncompressed, counter_t compressed)
115 value_list_t vl = VALUE_LIST_INIT;
117 values[0].counter = uncompressed;
118 values[1].counter = compressed;
121 vl.values_len = STATIC_ARRAY_SIZE (values);
122 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
123 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
124 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
125 sstrncpy (vl.type, "compression", sizeof (vl.type));
126 sstrncpy (vl.type_instance, type, sizeof (vl.type_instance));
128 plugin_dispatch_values (&vl);
129 } /* void compression_submit */
131 static int single_read (char *name, FILE *fh)
135 const int max_fields = STATIC_ARRAY_SIZE (fields);
136 int fields_num, read = 0;
138 counter_t link_rx, link_tx;
139 counter_t tun_rx, tun_tx;
140 counter_t pre_compress, post_compress;
141 counter_t pre_decompress, post_decompress;
142 counter_t overhead_rx, overhead_tx;
156 while (fgets (buffer, sizeof (buffer), fh) != NULL)
158 fields_num = openvpn_strsplit (buffer, fields, max_fields);
160 /* status file is generated by openvpn/sig.c:print_status()
161 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
163 * The line we're expecting has 2 fields. We ignore all lines
164 * with more or less fields.
172 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
174 /* read from the system and sent over the tunnel */
175 tun_tx = atoll (fields[1]);
177 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
179 /* read from the tunnel and written in the system */
180 tun_rx = atoll (fields[1]);
182 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
184 link_rx = atoll (fields[1]);
186 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
188 link_tx = atoll (fields[1]);
190 else if (strcmp (fields[0], "pre-compress bytes") == 0)
192 pre_compress = atoll (fields[1]);
194 else if (strcmp (fields[0], "post-compress bytes") == 0)
196 post_compress = atoll (fields[1]);
198 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
200 pre_decompress = atoll (fields[1]);
202 else if (strcmp (fields[0], "post-decompress bytes") == 0)
204 post_decompress = atoll (fields[1]);
209 iostats_submit (name, "traffic", link_rx, link_tx);
211 /* we need to force this order to avoid negative values with these unsigned */
212 overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
213 overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
215 iostats_submit (name, "overhead", overhead_rx, overhead_tx);
217 if (store_compression)
219 compression_submit (name, "data_in", post_decompress, pre_decompress);
220 compression_submit (name, "data_out", pre_compress, post_compress);
226 } /* int single_read */
228 /* for reading status version 1 */
229 static int multi1_read (char *name, FILE *fh)
233 const int max_fields = STATIC_ARRAY_SIZE (fields);
234 int fields_num, read = 0, skip = 1;
236 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
237 for ( ; strcmp (buffer, "ROUTING TABLE\n"); fgets (buffer, sizeof (buffer), fh))
239 if (skip) /* skip the first lines until the client list section is found */
241 /* we can't start reading data until this string is found */
242 if (strcmp (buffer, V1STRING) == 0)
249 fields_num = openvpn_strsplit (buffer, fields, max_fields);
251 iostats_submit (name, /* vpn instance */
252 fields[0], /* "Common Name" */
253 atoll (fields[2]), /* "Bytes Received" */
254 atoll (fields[3])); /* "Bytes Sent" */
260 } /* int multi1_read */
262 /* for reading status version 2 */
263 static int multi2_read (char *name, FILE *fh)
267 const int max_fields = STATIC_ARRAY_SIZE (fields);
268 int fields_num, read = 0;
270 while (fgets (buffer, sizeof (buffer), fh) != NULL)
272 fields_num = openvpn_strsplit (buffer, fields, max_fields);
274 /* status file is generated by openvpn/multi.c:multi_print_status()
275 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
277 * The line we're expecting has 8 fields. We ignore all lines
278 * with more or less fields.
286 if (strcmp (fields[0], "CLIENT_LIST") == 0)
288 iostats_submit (name, /* vpn instance */
289 fields[1], /* "Common Name" */
290 atoll (fields[4]), /* "Bytes Received" */
291 atoll (fields[5])); /* "Bytes Sent" */
298 } /* int multi2_read */
300 /* for reading status version 3 */
301 static int multi3_read (char *name, FILE *fh)
305 const int max_fields = STATIC_ARRAY_SIZE (fields);
306 int fields_num, read = 0;
308 while (fgets (buffer, sizeof (buffer), fh) != NULL)
310 fields_num = strsplit (buffer, fields, max_fields);
312 /* status file is generated by openvpn/multi.c:multi_print_status()
313 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
315 * The line we're expecting has 12 fields. We ignore all lines
316 * with more or less fields.
318 if (fields_num != 12)
324 if (strcmp (fields[0], "CLIENT_LIST") == 0)
326 iostats_submit (name, /* vpn instance */
327 fields[1], /* "Common Name" */
328 atoll (fields[4]), /* "Bytes Received" */
329 atoll (fields[5])); /* "Bytes Sent" */
336 } /* int multi3_read */
339 static int openvpn_read (void)
346 /* call the right read function for every status entry in the list */
347 for (i = 0; i < vpn_num; i++)
349 fh = fopen (vpn_list[i]->file, "r");
353 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
354 sstrerror (errno, errbuf, sizeof (errbuf)));
359 switch (vpn_list[i]->version)
362 read = single_read(vpn_list[i]->name, fh);
366 read = multi1_read(vpn_list[i]->name, fh);
370 read = multi2_read(vpn_list[i]->name, fh);
374 read = multi3_read(vpn_list[i]->name, fh);
381 return (read ? 0 : -1);
382 } /* int openvpn_read */
384 static int version_detect (FILE *fh)
389 /* we look at the first line searching for SINGLE mode configuration */
390 if ((fscanf (fh, "%*s %s", buffer) == 1) && (strcmp (buffer, "STATISTICS") == 0))
392 DEBUG ("openvpn plugin: found status file version SINGLE");
395 else /* else multimode */
397 /* now search for the specific multimode data format */
398 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
401 /* searching for multi version 1 */
402 if (strcmp (buffer, V1STRING) == 0)
404 DEBUG ("openvpn plugin: found status file version MULTI1");
408 /* searching for multi version 2 */
409 else if (strcmp (buffer, V2STRING) == 0)
411 DEBUG ("openvpn plugin: found status file version MULTI2");
415 /* searching for multi version 3 */
416 else if (strcmp (buffer, V3STRING) == 0)
418 DEBUG ("openvpn plugin: found status file version MULTI3");
427 DEBUG ("openvpn plugin: unknown file format, please report this as bug");
431 } /* int version_detect */
433 static int openvpn_config (const char *key, const char *value)
435 if (strcasecmp ("StatusFile", key) == 0)
438 char *status_file, *status_name, *filename;
439 int status_version, i;
442 /* check whether the status file provided is readable */
443 fh = fopen (value, "r");
447 WARNING ("openvpn plugin: unable to read \"%s\": %s",
448 value, sstrerror (errno, errbuf, sizeof (errbuf)));
452 /* once open try to detect the status file format */
453 status_version = version_detect (fh);
457 if (status_version == 0)
459 WARNING ("openvpn plugin: unable to detect status version, \
460 discarding status file \"%s\".", value);
464 status_file = sstrdup (value);
465 if (status_file == NULL)
468 WARNING ("openvpn plugin: sstrdup failed: %s",
469 sstrerror (errno, errbuf, sizeof (errbuf)));
473 /* it determines the file name as string starting at location filename + 1 */
474 filename = strrchr (status_file, (int) '/');
475 if (filename == NULL)
477 /* status_file is already the file name only */
478 status_name = status_file;
482 /* doesn't waist memory, uses status_file starting at filename + 1 */
483 status_name = filename + 1;
486 /* if not empty, it scans the list looking for a clone */
489 for (i = 0; i < vpn_num; i++)
491 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
493 WARNING ("status filename \"%s\" already used, \
494 please choose a different one.", status_name);
502 /* create a new vpn element since file, version and name are ok */
503 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
504 temp->file = status_file;
505 temp->version = status_version;
506 temp->name = status_name;
508 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
509 if (vpn_list == NULL)
512 ERROR ("openvpn plugin: malloc failed: %s",
513 sstrerror (errno, errbuf, sizeof (errbuf)));
520 vpn_list[vpn_num] = temp;
523 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
526 else if (strcasecmp ("Compression", key) == 0)
529 store_compression = 1;
532 store_compression = 0;
533 DEBUG ("openvpn plugin: no 'compression statistcs' collected");
542 } /* int openvpn_config */
544 /* shutdown callback */
545 static int openvpn_shutdown (void)
549 for (i = 0; i < vpn_num; i++)
551 sfree (vpn_list[i]->file);
558 } /* int openvpn_shutdown */
560 void module_register (void)
562 plugin_register_config ("openvpn", openvpn_config,
563 config_keys, config_keys_num);
564 plugin_register_read ("openvpn", openvpn_read);
565 plugin_register_shutdown ("openvpn", openvpn_shutdown);
566 } /* void module_register */