openvpn plugin: Improve “version_detect”.
[collectd.git] / src / openvpn.c
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  **/
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29
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"
34
35
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;
49
50 static vpn_status_t **vpn_list = NULL;
51 static int vpn_num = 0;
52
53 static int store_compression = 1;
54
55 static const char *config_keys[] =
56 {
57         "StatusFile",
58         "Compression"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61
62
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;
70
71         i = 0;
72         ptr = string;
73         saveptr = NULL;
74         while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
75         {
76                 ptr = NULL;
77                 i++;
78
79                 if (i >= size)
80                         break;
81         }
82
83         return (i);
84 } /* int openvpn_strsplit */
85
86
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;
92
93         values[0].counter = rx;
94         values[1].counter = tx;
95
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          */
100
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));
108
109         plugin_dispatch_values (&vl);
110 } /* void traffic_submit */
111
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;
117
118         values[0].counter = uncompressed;
119         values[1].counter = compressed;
120
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));
128
129         plugin_dispatch_values (&vl);
130 } /* void compression_submit */
131
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;
138
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;
144
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;
155
156
157         while (fgets (buffer, sizeof (buffer), fh) != NULL)
158         {
159                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
160
161                 /* status file is generated by openvpn/sig.c:print_status()
162                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
163                  *
164                  * The line we're expecting has 2 fields. We ignore all lines
165                  *  with more or less fields.
166                  */
167                 if (fields_num != 2)
168                 {
169                         continue;
170                 }
171                 else
172                 {
173                         if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
174                         {
175                                 /* read from the system and sent over the tunnel */
176                                 tun_tx = atoll (fields[1]);
177                         }
178                         else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
179                         {
180                                 /* read from the tunnel and written in the system */
181                                 tun_rx = atoll (fields[1]);
182                         }
183                         else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
184                         {
185                                 link_rx = atoll (fields[1]);
186                         }
187                         else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
188                         {
189                                 link_tx = atoll (fields[1]);
190                         }
191                         else if (strcmp (fields[0], "pre-compress bytes") == 0)
192                         {
193                                 pre_compress = atoll (fields[1]);
194                         }
195                         else if (strcmp (fields[0], "post-compress bytes") == 0)
196                         {
197                                 post_compress = atoll (fields[1]);
198                         }
199                         else if (strcmp (fields[0], "pre-decompress bytes") == 0)
200                         {
201                                 pre_decompress = atoll (fields[1]);
202                         }
203                         else if (strcmp (fields[0], "post-decompress bytes") == 0)
204                         {
205                                 post_decompress = atoll (fields[1]);
206                         }
207                 }
208         }
209
210         iostats_submit (name, "traffic", link_rx, link_tx);
211
212         /* we need to force this order to avoid negative values with these unsigned */
213         overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
214         overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
215
216         iostats_submit (name, "overhead", overhead_rx, overhead_tx);
217
218         if (store_compression)
219         {
220                 compression_submit (name, "data_in", post_decompress, pre_decompress);
221                 compression_submit (name, "data_out", pre_compress, post_compress);
222         }
223
224         read = 1;
225
226         return (read);
227 } /* int single_read */
228
229 /* for reading status version 1 */
230 static int multi1_read (char *name, FILE *fh)
231 {
232         char buffer[1024];
233         char *fields[10];
234         const int max_fields = STATIC_ARRAY_SIZE (fields);
235         int  fields_num, read = 0, skip = 1;
236
237         /* read the file until the "ROUTING TABLE" line is found (no more info after) */
238         for ( ; strcmp (buffer, "ROUTING TABLE\n"); fgets (buffer, sizeof (buffer), fh))
239         {
240                 if (skip) /* skip the first lines until the client list section is found */
241                 {
242                         /* we can't start reading data until this string is found */
243                         if (strcmp (buffer, V1STRING) == 0)
244                                 skip = 0;
245
246                         continue;
247                 }
248                 else
249                 {
250                         fields_num = openvpn_strsplit (buffer, fields, max_fields);
251
252                         iostats_submit (name,                   /* vpn instance */
253                                         fields[0],              /* "Common Name" */
254                                         atoll (fields[2]),      /* "Bytes Received" */
255                                         atoll (fields[3]));     /* "Bytes Sent" */
256                         read = 1;
257                 }
258         }
259
260         return (read);
261 } /* int multi1_read */
262
263 /* for reading status version 2 */
264 static int multi2_read (char *name, FILE *fh)
265 {
266         char buffer[1024];
267         char *fields[10];
268         const int max_fields = STATIC_ARRAY_SIZE (fields);
269         int  fields_num, read = 0;
270
271         while (fgets (buffer, sizeof (buffer), fh) != NULL)
272         {
273                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
274
275                 /* status file is generated by openvpn/multi.c:multi_print_status()
276                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
277                  *
278                  * The line we're expecting has 8 fields. We ignore all lines
279                  *  with more or less fields.
280                  */
281                 if (fields_num != 8)
282                 {
283                         continue;
284                 }
285                 else
286                 {
287                         if (strcmp (fields[0], "CLIENT_LIST") == 0)
288                         {
289                                 iostats_submit (name,                   /* vpn instance */
290                                                 fields[1],              /* "Common Name" */
291                                                 atoll (fields[4]),      /* "Bytes Received" */
292                                                 atoll (fields[5]));     /* "Bytes Sent" */
293                                 read = 1;
294                         }
295                 }
296         }
297
298         return (read);
299 } /* int multi2_read */
300
301 /* for reading status version 3 */
302 static int multi3_read (char *name, FILE *fh)
303 {
304         char buffer[1024];
305         char *fields[15];
306         const int max_fields = STATIC_ARRAY_SIZE (fields);
307         int  fields_num, read = 0;
308
309         while (fgets (buffer, sizeof (buffer), fh) != NULL)
310         {
311                 fields_num = strsplit (buffer, fields, max_fields);
312
313                 /* status file is generated by openvpn/multi.c:multi_print_status()
314                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
315                  *
316                  * The line we're expecting has 12 fields. We ignore all lines
317                  *  with more or less fields.
318                  */
319                 if (fields_num != 12)
320                 {
321                         continue;
322                 }
323                 else
324                 {
325                         if (strcmp (fields[0], "CLIENT_LIST") == 0)
326                         {
327                                 iostats_submit (name,                   /* vpn instance */
328                                                 fields[1],              /* "Common Name" */
329                                                 atoll (fields[4]),      /* "Bytes Received" */
330                                                 atoll (fields[5]));     /* "Bytes Sent" */
331                                 read = 1;
332                         }
333                 }
334         }
335
336         return (read);
337 } /* int multi3_read */
338
339 /* read callback */
340 static int openvpn_read (void)
341 {
342         FILE *fh;
343         int  i, read;
344
345         read = 0;
346
347         /* call the right read function for every status entry in the list */
348         for (i = 0; i < vpn_num; i++)
349         {
350                 fh = fopen (vpn_list[i]->file, "r");
351                 if (fh == NULL)
352                 {
353                         char errbuf[1024];
354                         WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
355                                 sstrerror (errno, errbuf, sizeof (errbuf)));
356
357                         continue;
358                 }
359
360                 switch (vpn_list[i]->version)
361                 {
362                         case SINGLE:
363                                 read = single_read(vpn_list[i]->name, fh);
364                                 break;
365
366                         case MULTI1:
367                                 read = multi1_read(vpn_list[i]->name, fh);
368                                 break;
369
370                         case MULTI2:
371                                 read = multi2_read(vpn_list[i]->name, fh);
372                                 break;
373
374                         case MULTI3:
375                                 read = multi3_read(vpn_list[i]->name, fh);
376                                 break;
377                 }
378
379                 fclose (fh);
380         }
381
382         return (read ? 0 : -1);
383 } /* int openvpn_read */
384
385 static int version_detect (const char *filename)
386 {
387         FILE *fh;
388         char buffer[1024];
389         int version = 0;
390
391         /* Sanity checking. We're called from the config handling routine, so
392          * better play it save. */
393         if ((filename == NULL) || (*filename == 0))
394                 return (0);
395
396         fh = fopen (filename, "r");
397         if (fh == NULL)
398         {
399                 char errbuf[1024];
400                 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
401                                 sstrerror (errno, errbuf, sizeof (errbuf)));
402                 return (0);
403         }
404
405         /* now search for the specific multimode data format */
406         while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
407         {
408                 /* we look at the first line searching for SINGLE mode configuration */
409                 if (strcmp (buffer, VSSTRING) == 0)
410                 {
411                         DEBUG ("openvpn plugin: found status file version SINGLE");
412                         version = SINGLE;
413                         break;
414                 }
415                 /* searching for multi version 1 */
416                 else if (strcmp (buffer, V1STRING) == 0)
417                 {
418                         DEBUG ("openvpn plugin: found status file version MULTI1");
419                         version = MULTI1;
420                         break;
421                 }
422                 /* searching for multi version 2 */
423                 else if (strcmp (buffer, V2STRING) == 0)
424                 {
425                         DEBUG ("openvpn plugin: found status file version MULTI2");
426                         version = MULTI2;
427                         break;
428                 }
429                 /* searching for multi version 3 */
430                 else if (strcmp (buffer, V3STRING) == 0)
431                 {
432                         DEBUG ("openvpn plugin: found status file version MULTI3");
433                         version = MULTI3;
434                         break;
435                 }
436         }
437
438         if (version == 0)
439         {
440                 /* This is only reached during configuration, so complaining to
441                  * the user is in order. */
442                 NOTICE ("openvpn plugin: %s: Unknown file format, please "
443                                 "report this as bug. Make sure to include "
444                                 "your status file, so the plugin can "
445                                 "be adapted.", filename);
446         }
447
448         fclose (fh);
449
450         return version;
451 } /* int version_detect */
452
453 static int openvpn_config (const char *key, const char *value)
454 {
455         if (strcasecmp ("StatusFile", key) == 0)
456         {
457                 char    *status_file, *status_name, *filename;
458                 int     status_version, i;
459                 vpn_status_t *temp;
460
461                 /* try to detect the status file format */
462                 status_version = version_detect (value);
463
464                 if (status_version == 0)
465                 {
466                         WARNING ("openvpn plugin: unable to detect status version, \
467                                 discarding status file \"%s\".", value);
468                         return (1);
469                 }
470
471                 status_file = sstrdup (value);
472                 if (status_file == NULL)
473                 {
474                         char errbuf[1024];
475                         WARNING ("openvpn plugin: sstrdup failed: %s",
476                                 sstrerror (errno, errbuf, sizeof (errbuf)));
477                         return (1);
478                 }
479
480                 /* it determines the file name as string starting at location filename + 1 */
481                 filename = strrchr (status_file, (int) '/');
482                 if (filename == NULL)
483                 {
484                         /* status_file is already the file name only */
485                         status_name = status_file;
486                 }
487                 else
488                 {
489                         /* doesn't waist memory, uses status_file starting at filename + 1 */
490                         status_name = filename + 1;
491                 }
492
493                 /* scan the list looking for a clone */
494                 for (i = 0; i < vpn_num; i++)
495                 {
496                         if (strcasecmp (vpn_list[i]->name, status_name) == 0)
497                         {
498                                 WARNING ("openvpn plugin: status filename \"%s\" "
499                                                 "already used, please choose a "
500                                                 "different one.", status_name);
501                                 sfree (status_file);
502                                 return (1);
503                         }
504                 }
505
506                 /* create a new vpn element since file, version and name are ok */
507                 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
508                 temp->file = status_file;
509                 temp->version = status_version;
510                 temp->name = status_name;
511
512                 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
513                 if (vpn_list == NULL)
514                 {
515                         char errbuf[1024];
516                         ERROR ("openvpn plugin: malloc failed: %s",
517                                 sstrerror (errno, errbuf, sizeof (errbuf)));
518
519                         sfree (temp->file);
520                         sfree (temp);
521                         return (1);
522                 }
523
524                 vpn_list[vpn_num] = temp;
525                 vpn_num++;
526
527                 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
528
529         }
530         else if (strcasecmp ("Compression", key) == 0)
531         {
532                 if (IS_TRUE (value))
533                         store_compression = 1;
534                 else
535                 {
536                         store_compression = 0;
537                         DEBUG ("openvpn plugin: no 'compression statistcs' collected");
538                 }
539         }
540         else
541         {
542                 return (-1);
543         }
544
545         return (0);
546 } /* int openvpn_config */
547
548 /* shutdown callback */
549 static int openvpn_shutdown (void)
550 {
551         int i;
552
553         for (i = 0; i < vpn_num; i++)
554         {
555                 sfree (vpn_list[i]->file);
556                 sfree (vpn_list[i]);
557         }
558
559         sfree (vpn_list);
560
561         return (0);
562 } /* int openvpn_shutdown */
563
564 void module_register (void)
565 {
566         plugin_register_config ("openvpn", openvpn_config,
567                                 config_keys, config_keys_num);
568         plugin_register_read ("openvpn", openvpn_read);
569         plugin_register_shutdown ("openvpn", openvpn_shutdown);
570 } /* void module_register */