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