openvpn plugin: Add support for more versions of the “status file” (and more).
[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
34
35 struct vpn_status_s
36 {
37         char    *file;
38         enum
39         {
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 */
44         } version;
45         char    *name;
46 };
47 typedef struct vpn_status_s vpn_status_t;
48
49 static vpn_status_t **vpn_list = NULL;
50 static int vpn_num = 0;
51
52 static int store_compression = 1;
53
54 static const char *config_keys[] =
55 {
56         "StatusFile",
57         "NoCompression"
58 };
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
60
61
62 /*                      Helper function                 */
63 /*  copy-n-pasted from common.c - changed delim to ","  */
64 static int openvpn_strsplit (char *string, char **fields, size_t size)
65 {
66         size_t i;
67         char *ptr;
68         char *saveptr;
69
70         i = 0;
71         ptr = string;
72         saveptr = NULL;
73         while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
74         {
75                 ptr = NULL;
76                 i++;
77
78                 if (i >= size)
79                         break;
80         }
81
82         return (i);
83 } /* int openvpn_strsplit */
84
85
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)
88 {
89         value_t values[2];
90         value_list_t vl = VALUE_LIST_INIT;
91
92         values[0].counter = rx;
93         values[1].counter = tx;
94
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.
98          */
99
100         vl.values = values;
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));
107
108         plugin_dispatch_values (&vl);
109 } /* void traffic_submit */
110
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)
113 {
114         value_t values[2];
115         value_list_t vl = VALUE_LIST_INIT;
116
117         values[0].counter = uncompressed;
118         values[1].counter = compressed;
119
120         vl.values = values;
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));
127
128         plugin_dispatch_values (&vl);
129 } /* void compression_submit */
130
131 static int single_read (char *name, FILE *fh)
132 {
133         char buffer[1024];
134         char *fields[4];
135         const int max_fields = STATIC_ARRAY_SIZE (fields);
136         int  fields_num, read = 0;
137
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;
143
144         link_rx = 0;
145         link_tx = 0;
146         tun_rx = 0;
147         tun_tx = 0;
148         pre_compress = 0;
149         post_compress = 0;
150         pre_decompress = 0;
151         post_decompress = 0;
152         overhead_rx = 0;
153         overhead_tx = 0;
154
155
156         while (fgets (buffer, sizeof (buffer), fh) != NULL)
157         {
158                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
159
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         }
208
209         iostats_submit (name, "traffic", link_rx, link_tx);
210
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);
214
215         iostats_submit (name, "overhead", overhead_rx, overhead_tx);
216
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         }
222
223         read = 1;
224
225         return (read);
226 } /* int single_read */
227
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         const int max_fields = STATIC_ARRAY_SIZE (fields);
234         int  fields_num, read = 0, skip = 1;
235
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))
238         {
239                 if (skip) /* skip the first lines until the client list section is found */
240                 {
241                         /* we can't start reading data until this string is found */
242                         if (strcmp (buffer, V1STRING) == 0)
243                                 skip = 0;
244
245                         continue;
246                 }
247                 else
248                 {
249                         fields_num = openvpn_strsplit (buffer, fields, max_fields);
250
251                         iostats_submit (name,                   /* vpn instance */
252                                         fields[0],              /* "Common Name" */
253                                         atoll (fields[2]),      /* "Bytes Received" */
254                                         atoll (fields[3]));     /* "Bytes Sent" */
255                         read = 1;
256                 }
257         }
258
259         return (read);
260 } /* int multi1_read */
261
262 /* for reading status version 2 */
263 static int multi2_read (char *name, FILE *fh)
264 {
265         char buffer[1024];
266         char *fields[10];
267         const int max_fields = STATIC_ARRAY_SIZE (fields);
268         int  fields_num, read = 0;
269
270         while (fgets (buffer, sizeof (buffer), fh) != NULL)
271         {
272                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
273
274                 /* status file is generated by openvpn/multi.c:multi_print_status()
275                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
276                  *
277                  * The line we're expecting has 8 fields. We ignore all lines
278                  *  with more or less fields.
279                  */
280                 if (fields_num != 8)
281                 {
282                         continue;
283                 }
284                 else
285                 {
286                         if (strcmp (fields[0], "CLIENT_LIST") == 0)
287                         {
288                                 iostats_submit (name,                   /* vpn instance */
289                                                 fields[1],              /* "Common Name" */
290                                                 atoll (fields[4]),      /* "Bytes Received" */
291                                                 atoll (fields[5]));     /* "Bytes Sent" */
292                                 read = 1;
293                         }
294                 }
295         }
296
297         return (read);
298 } /* int multi2_read */
299
300 /* for reading status version 3 */
301 static int multi3_read (char *name, FILE *fh)
302 {
303         char buffer[1024];
304         char *fields[15];
305         const int max_fields = STATIC_ARRAY_SIZE (fields);
306         int  fields_num, read = 0;
307
308         while (fgets (buffer, sizeof (buffer), fh) != NULL)
309         {
310                 fields_num = strsplit (buffer, fields, max_fields);
311
312                 /* status file is generated by openvpn/multi.c:multi_print_status()
313                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
314                  *
315                  * The line we're expecting has 12 fields. We ignore all lines
316                  *  with more or less fields.
317                  */
318                 if (fields_num != 12)
319                 {
320                         continue;
321                 }
322                 else
323                 {
324                         if (strcmp (fields[0], "CLIENT_LIST") == 0)
325                         {
326                                 iostats_submit (name,                   /* vpn instance */
327                                                 fields[1],              /* "Common Name" */
328                                                 atoll (fields[4]),      /* "Bytes Received" */
329                                                 atoll (fields[5]));     /* "Bytes Sent" */
330                                 read = 1;
331                         }
332                 }
333         }
334
335         return (read);
336 } /* int multi3_read */
337
338 /* read callback */
339 static int openvpn_read (void)
340 {
341         FILE *fh;
342         int  i, read;
343
344         read = 0;
345
346         /* call the right read function for every status entry in the list */
347         for (i = 0; i < vpn_num; i++)
348         {
349                 fh = fopen (vpn_list[i]->file, "r");
350                 if (fh == NULL)
351                 {
352                         char errbuf[1024];
353                         WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
354                                 sstrerror (errno, errbuf, sizeof (errbuf)));
355
356                         continue;
357                 }
358
359                 switch (vpn_list[i]->version)
360                 {
361                         case SINGLE:
362                                 read = single_read(vpn_list[i]->name, fh);
363                                 break;
364
365                         case MULTI1:
366                                 read = multi1_read(vpn_list[i]->name, fh);
367                                 break;
368
369                         case MULTI2:
370                                 read = multi2_read(vpn_list[i]->name, fh);
371                                 break;
372
373                         case MULTI3:
374                                 read = multi3_read(vpn_list[i]->name, fh);
375                                 break;
376                 }
377
378                 fclose (fh);
379         }
380
381         return (read ? 0 : -1);
382 } /* int openvpn_read */
383
384 static int version_detect (FILE *fh)
385 {
386         char buffer[1024];
387         int version = 0;
388
389         /* we look at the first line searching for SINGLE mode configuration */
390         if ((fscanf (fh, "%*s %s", buffer) == 1) && (strcmp (buffer, "STATISTICS") == 0))
391         {
392                 DEBUG ("openvpn plugin: found status file version SINGLE");
393                 version = SINGLE;
394         }
395         else    /* else multimode */
396         {
397                 /* now search for the specific multimode data format */
398                 while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
399                 {
400
401                         /* searching for multi version 1 */
402                         if (strcmp (buffer, V1STRING) == 0)
403                         {
404                                 DEBUG ("openvpn plugin: found status file version MULTI1");
405                                 version = MULTI1;
406                                 break;
407                         }
408                         /* searching for multi version 2 */
409                         else if (strcmp (buffer, V2STRING) == 0)
410                         {
411                                 DEBUG ("openvpn plugin: found status file version MULTI2");
412                                 version = MULTI2;
413                                 break;
414                         }
415                         /* searching for multi version 3 */
416                         else if (strcmp (buffer, V3STRING) == 0)
417                         {
418                                 DEBUG ("openvpn plugin: found status file version MULTI3");
419                                 version = MULTI3;
420                                 break;
421                         }
422                 }
423         }
424
425         if (version == 0)
426         {
427                 DEBUG ("openvpn plugin: unknown file format, please report this as bug");
428         }
429
430         return version;
431 } /* int version_detect */
432
433 static int openvpn_config (const char *key, const char *value)
434 {
435         if (strcasecmp ("StatusFile", key) == 0)
436         {
437                 FILE    *fh;
438                 char    *status_file, *status_name, *filename;
439                 int     status_version, i;
440                 vpn_status_t *temp;
441
442                 /* check whether the status file provided is readable */
443                 fh = fopen (value, "r");
444                 if (fh == NULL)
445                 {
446                         char errbuf[1024];
447                         WARNING ("openvpn plugin: unable to read \"%s\": %s",
448                         value, sstrerror (errno, errbuf, sizeof (errbuf)));
449                         return (1);
450                 }
451
452                 /* once open try to detect the status file format */
453                 status_version = version_detect (fh);
454
455                 fclose (fh);
456
457                 if (status_version == 0)
458                 {       
459                         WARNING ("openvpn plugin: unable to detect status version, \
460                                 discarding status file \"%s\".", value);
461                         return (1);
462                 }
463
464                 status_file = sstrdup (value);
465                 if (status_file == NULL)
466                 {
467                         char errbuf[1024];
468                         WARNING ("openvpn plugin: sstrdup failed: %s",
469                                 sstrerror (errno, errbuf, sizeof (errbuf)));
470                         return (1);
471                 }
472         
473                 /* it determines the file name as string starting at location filename + 1 */
474                 filename = strrchr (status_file, (int) '/');
475                 if (filename == NULL)
476                 {
477                         /* status_file is already the file name only */
478                         status_name = status_file;
479                 }
480                 else
481                 {
482                         /* doesn't waist memory, uses status_file starting at filename + 1 */
483                         status_name = filename + 1;
484                 }
485
486                 /* if not empty, it scans the list looking for a clone */
487                 if (vpn_num)
488                 {
489                         for (i = 0; i < vpn_num; i++)
490                         {
491                                 if (strcasecmp (vpn_list[i]->name, status_name) == 0)
492                                 {
493                                         WARNING ("status filename \"%s\" already used, \
494                                                 please choose a different one.", status_name);
495                                         return (1);
496                                 }
497                         }
498                 }
499
500                 /* create a new vpn element since file and version are ok */    
501                 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
502                 temp->file = status_file;
503                 temp->version = status_version;
504                 temp->name = status_name;
505
506                 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
507                 if (vpn_list == NULL)
508                 {
509                     char errbuf[1024];
510                     ERROR ("openvpn plugin: malloc failed: %s",
511                             sstrerror (errno, errbuf, sizeof (errbuf)));
512                     return (1);
513                 }
514
515                 vpn_list[vpn_num] = temp;
516                 vpn_num++;
517
518                 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
519
520         }
521         else if (strcasecmp ("NoCompression", key) == 0)
522         {
523                 if ((strcasecmp ("True", value) == 0)
524                         || (strcasecmp ("Yes", value) == 0)
525                         || (strcasecmp ("On", value) == 0))
526                 {
527                         store_compression = 0;
528                         DEBUG ("openvpn plugin: no 'compression statistcs' collected");
529                 }
530                 else
531                 {
532                         store_compression = 1;
533                 }
534         }
535         else
536         {
537                 return (-1);
538         }
539
540         return (0);
541 } /* int openvpn_config */
542
543 /* shutdown callback */
544 static int openvpn_shutdown (void)
545 {
546         int i;
547         
548         for (i = 0; i < vpn_num; i++)
549         {
550                 sfree (vpn_list[i]->file);
551                 sfree (vpn_list[i]);
552         }
553
554         sfree (vpn_list);
555
556         return (0);
557 } /* int openvpn_shutdown */
558
559 void module_register (void)
560 {
561         plugin_register_config ("openvpn", openvpn_config,
562                                 config_keys, config_keys_num);
563         plugin_register_read ("openvpn", openvpn_read);
564         plugin_register_shutdown ("openvpn", openvpn_shutdown);
565 } /* void module_register */