33c94ed62f90defc67c558c10696c6a378e2b04a
1 /**
2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2009 Florian octo Forster
5 * Copyright (C) 2009 Marco Chiappero
6 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Doug MacEachern <dougm at hyperic.com>
22 * Florian octo Forster <octo at verplant.org>
23 * Marco Chiappero <marco at absence.it>
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
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"
36 struct vpn_status_s
37 {
38 char *file;
39 enum
40 {
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 */
45 } version;
46 char *name;
47 };
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;
55 static const char *config_keys[] =
56 {
57 "StatusFile",
58 "Compression"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63 /* Helper function
64 * copy-n-pasted from common.c - changed delim to "," */
65 static int openvpn_strsplit (char *string, char **fields, size_t size)
66 {
67 size_t i;
68 char *ptr;
69 char *saveptr;
71 i = 0;
72 ptr = string;
73 saveptr = NULL;
74 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
75 {
76 ptr = NULL;
77 i++;
79 if (i >= size)
80 break;
81 }
83 return (i);
84 } /* int openvpn_strsplit */
87 /* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
88 static void iostats_submit (char *name, char *type, counter_t rx, counter_t tx)
89 {
90 value_t values[2];
91 value_list_t vl = VALUE_LIST_INIT;
93 values[0].counter = rx;
94 values[1].counter = tx;
96 /* NOTE: using plugin_instance to identify each vpn config (and
97 * status) file; using type_instance to identify the endpoint
98 * host when in multimode, traffic or overhead when in single.
99 */
101 vl.values = values;
102 vl.values_len = STATIC_ARRAY_SIZE (values);
103 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
104 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
105 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
106 sstrncpy (vl.type, "io_octets", sizeof (vl.type));
107 sstrncpy (vl.type_instance, type, sizeof (vl.type_instance));
109 plugin_dispatch_values (&vl);
110 } /* void traffic_submit */
112 /* dispatches stats about data compression shown when in single mode */
113 static void compression_submit (char *name, char *type, counter_t uncompressed, counter_t compressed)
114 {
115 value_t values[2];
116 value_list_t vl = VALUE_LIST_INIT;
118 values[0].counter = uncompressed;
119 values[1].counter = compressed;
121 vl.values = values;
122 vl.values_len = STATIC_ARRAY_SIZE (values);
123 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
124 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
125 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
126 sstrncpy (vl.type, "compression", sizeof (vl.type));
127 sstrncpy (vl.type_instance, type, sizeof (vl.type_instance));
129 plugin_dispatch_values (&vl);
130 } /* void compression_submit */
132 static int single_read (char *name, FILE *fh)
133 {
134 char buffer[1024];
135 char *fields[4];
136 const int max_fields = STATIC_ARRAY_SIZE (fields);
137 int fields_num, read = 0;
139 counter_t link_rx, link_tx;
140 counter_t tun_rx, tun_tx;
141 counter_t pre_compress, post_compress;
142 counter_t pre_decompress, post_decompress;
143 counter_t overhead_rx, overhead_tx;
145 link_rx = 0;
146 link_tx = 0;
147 tun_rx = 0;
148 tun_tx = 0;
149 pre_compress = 0;
150 post_compress = 0;
151 pre_decompress = 0;
152 post_decompress = 0;
153 overhead_rx = 0;
154 overhead_tx = 0;
156 while (fgets (buffer, sizeof (buffer), fh) != NULL)
157 {
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
162 *
163 * The line we're expecting has 2 fields. We ignore all lines
164 * with more or less fields.
165 */
166 if (fields_num != 2)
167 {
168 continue;
169 }
170 else
171 {
172 if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
173 {
174 /* read from the system and sent over the tunnel */
175 tun_tx = atoll (fields[1]);
176 }
177 else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
178 {
179 /* read from the tunnel and written in the system */
180 tun_rx = atoll (fields[1]);
181 }
182 else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
183 {
184 link_rx = atoll (fields[1]);
185 }
186 else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
187 {
188 link_tx = atoll (fields[1]);
189 }
190 else if (strcmp (fields[0], "pre-compress bytes") == 0)
191 {
192 pre_compress = atoll (fields[1]);
193 }
194 else if (strcmp (fields[0], "post-compress bytes") == 0)
195 {
196 post_compress = atoll (fields[1]);
197 }
198 else if (strcmp (fields[0], "pre-decompress bytes") == 0)
199 {
200 pre_decompress = atoll (fields[1]);
201 }
202 else if (strcmp (fields[0], "post-decompress bytes") == 0)
203 {
204 post_decompress = atoll (fields[1]);
205 }
206 }
207 }
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)
218 {
219 compression_submit (name, "data_in", post_decompress, pre_decompress);
220 compression_submit (name, "data_out", pre_compress, post_compress);
221 }
223 read = 1;
225 return (read);
226 } /* int single_read */
228 /* for reading status version 1 */
229 static int multi1_read (char *name, FILE *fh)
230 {
231 char buffer[1024];
232 char *fields[10];
233 int fields_num, read = 0, found_header = 0;
235 /* read the file until the "ROUTING TABLE" line is found (no more info after) */
236 while (fgets (buffer, sizeof (buffer), fh) != NULL)
237 {
238 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
239 break;
241 if (strcmp (buffer, V1STRING) == 0)
242 {
243 found_header = 1;
244 continue;
245 }
247 /* skip the first lines until the client list section is found */
248 if (found_header == 0)
249 /* we can't start reading data until this string is found */
250 continue;
252 fields_num = openvpn_strsplit (buffer,
253 fields, STATIC_ARRAY_SIZE (fields));
254 if (fields_num < 4)
255 continue;
257 iostats_submit (name, /* vpn instance */
258 fields[0], /* "Common Name" */
259 atoll (fields[2]), /* "Bytes Received" */
260 atoll (fields[3])); /* "Bytes Sent" */
261 read = 1;
262 }
264 return (read);
265 } /* int multi1_read */
267 /* for reading status version 2 */
268 static int multi2_read (char *name, FILE *fh)
269 {
270 char buffer[1024];
271 char *fields[10];
272 const int max_fields = STATIC_ARRAY_SIZE (fields);
273 int fields_num, read = 0;
275 while (fgets (buffer, sizeof (buffer), fh) != NULL)
276 {
277 fields_num = openvpn_strsplit (buffer, fields, max_fields);
279 /* status file is generated by openvpn/multi.c:multi_print_status()
280 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
281 *
282 * The line we're expecting has 8 fields. We ignore all lines
283 * with more or less fields.
284 */
285 if (fields_num != 8)
286 {
287 continue;
288 }
289 else
290 {
291 if (strcmp (fields[0], "CLIENT_LIST") == 0)
292 {
293 iostats_submit (name, /* vpn instance */
294 fields[1], /* "Common Name" */
295 atoll (fields[4]), /* "Bytes Received" */
296 atoll (fields[5])); /* "Bytes Sent" */
297 read = 1;
298 }
299 }
300 }
302 return (read);
303 } /* int multi2_read */
305 /* for reading status version 3 */
306 static int multi3_read (char *name, FILE *fh)
307 {
308 char buffer[1024];
309 char *fields[15];
310 const int max_fields = STATIC_ARRAY_SIZE (fields);
311 int fields_num, read = 0;
313 while (fgets (buffer, sizeof (buffer), fh) != NULL)
314 {
315 fields_num = strsplit (buffer, fields, max_fields);
317 /* status file is generated by openvpn/multi.c:multi_print_status()
318 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
319 *
320 * The line we're expecting has 12 fields. We ignore all lines
321 * with more or less fields.
322 */
323 if (fields_num != 12)
324 {
325 continue;
326 }
327 else
328 {
329 if (strcmp (fields[0], "CLIENT_LIST") == 0)
330 {
331 iostats_submit (name, /* vpn instance */
332 fields[1], /* "Common Name" */
333 atoll (fields[4]), /* "Bytes Received" */
334 atoll (fields[5])); /* "Bytes Sent" */
335 read = 1;
336 }
337 }
338 }
340 return (read);
341 } /* int multi3_read */
343 /* read callback */
344 static int openvpn_read (void)
345 {
346 FILE *fh;
347 int i, read;
349 read = 0;
351 /* call the right read function for every status entry in the list */
352 for (i = 0; i < vpn_num; i++)
353 {
354 fh = fopen (vpn_list[i]->file, "r");
355 if (fh == NULL)
356 {
357 char errbuf[1024];
358 WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
359 sstrerror (errno, errbuf, sizeof (errbuf)));
361 continue;
362 }
364 switch (vpn_list[i]->version)
365 {
366 case SINGLE:
367 read = single_read(vpn_list[i]->name, fh);
368 break;
370 case MULTI1:
371 read = multi1_read(vpn_list[i]->name, fh);
372 break;
374 case MULTI2:
375 read = multi2_read(vpn_list[i]->name, fh);
376 break;
378 case MULTI3:
379 read = multi3_read(vpn_list[i]->name, fh);
380 break;
381 }
383 fclose (fh);
384 }
386 return (read ? 0 : -1);
387 } /* int openvpn_read */
389 static int version_detect (const char *filename)
390 {
391 FILE *fh;
392 char buffer[1024];
393 int version = 0;
395 /* Sanity checking. We're called from the config handling routine, so
396 * better play it save. */
397 if ((filename == NULL) || (*filename == 0))
398 return (0);
400 fh = fopen (filename, "r");
401 if (fh == NULL)
402 {
403 char errbuf[1024];
404 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
405 sstrerror (errno, errbuf, sizeof (errbuf)));
406 return (0);
407 }
409 /* now search for the specific multimode data format */
410 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
411 {
412 /* we look at the first line searching for SINGLE mode configuration */
413 if (strcmp (buffer, VSSTRING) == 0)
414 {
415 DEBUG ("openvpn plugin: found status file version SINGLE");
416 version = SINGLE;
417 break;
418 }
419 /* searching for multi version 1 */
420 else if (strcmp (buffer, V1STRING) == 0)
421 {
422 DEBUG ("openvpn plugin: found status file version MULTI1");
423 version = MULTI1;
424 break;
425 }
426 /* searching for multi version 2 */
427 else if (strcmp (buffer, V2STRING) == 0)
428 {
429 DEBUG ("openvpn plugin: found status file version MULTI2");
430 version = MULTI2;
431 break;
432 }
433 /* searching for multi version 3 */
434 else if (strcmp (buffer, V3STRING) == 0)
435 {
436 DEBUG ("openvpn plugin: found status file version MULTI3");
437 version = MULTI3;
438 break;
439 }
440 }
442 if (version == 0)
443 {
444 /* This is only reached during configuration, so complaining to
445 * the user is in order. */
446 NOTICE ("openvpn plugin: %s: Unknown file format, please "
447 "report this as bug. Make sure to include "
448 "your status file, so the plugin can "
449 "be adapted.", filename);
450 }
452 fclose (fh);
454 return version;
455 } /* int version_detect */
457 static int openvpn_config (const char *key, const char *value)
458 {
459 if (strcasecmp ("StatusFile", key) == 0)
460 {
461 char *status_file, *status_name, *filename;
462 int status_version, i;
463 vpn_status_t *temp;
465 /* try to detect the status file format */
466 status_version = version_detect (value);
468 if (status_version == 0)
469 {
470 WARNING ("openvpn plugin: unable to detect status version, \
471 discarding status file \"%s\".", value);
472 return (1);
473 }
475 status_file = sstrdup (value);
476 if (status_file == NULL)
477 {
478 char errbuf[1024];
479 WARNING ("openvpn plugin: sstrdup failed: %s",
480 sstrerror (errno, errbuf, sizeof (errbuf)));
481 return (1);
482 }
484 /* it determines the file name as string starting at location filename + 1 */
485 filename = strrchr (status_file, (int) '/');
486 if (filename == NULL)
487 {
488 /* status_file is already the file name only */
489 status_name = status_file;
490 }
491 else
492 {
493 /* doesn't waist memory, uses status_file starting at filename + 1 */
494 status_name = filename + 1;
495 }
497 /* scan the list looking for a clone */
498 for (i = 0; i < vpn_num; i++)
499 {
500 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
501 {
502 WARNING ("openvpn plugin: status filename \"%s\" "
503 "already used, please choose a "
504 "different one.", status_name);
505 sfree (status_file);
506 return (1);
507 }
508 }
510 /* create a new vpn element since file, version and name are ok */
511 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
512 temp->file = status_file;
513 temp->version = status_version;
514 temp->name = status_name;
516 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
517 if (vpn_list == NULL)
518 {
519 char errbuf[1024];
520 ERROR ("openvpn plugin: malloc failed: %s",
521 sstrerror (errno, errbuf, sizeof (errbuf)));
523 sfree (temp->file);
524 sfree (temp);
525 return (1);
526 }
528 vpn_list[vpn_num] = temp;
529 vpn_num++;
531 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
533 }
534 else if (strcasecmp ("Compression", key) == 0)
535 {
536 if (IS_TRUE (value))
537 store_compression = 1;
538 else
539 {
540 store_compression = 0;
541 DEBUG ("openvpn plugin: no 'compression statistcs' collected");
542 }
543 }
544 else
545 {
546 return (-1);
547 }
549 return (0);
550 } /* int openvpn_config */
552 /* shutdown callback */
553 static int openvpn_shutdown (void)
554 {
555 int i;
557 for (i = 0; i < vpn_num; i++)
558 {
559 sfree (vpn_list[i]->file);
560 sfree (vpn_list[i]);
561 }
563 sfree (vpn_list);
565 return (0);
566 } /* int openvpn_shutdown */
568 void module_register (void)
569 {
570 plugin_register_config ("openvpn", openvpn_config,
571 config_keys, config_keys_num);
572 plugin_register_read ("openvpn", openvpn_read);
573 plugin_register_shutdown ("openvpn", openvpn_shutdown);
574 } /* void module_register */