Code

openvpn plugin: multi1_read: Replace a hard to read for-loop with a while-loop.
[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  **/
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)
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)
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;
157         while (fgets (buffer, sizeof (buffer), fh) != NULL)
158         {
159                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
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         }
210         iostats_submit (name, "traffic", link_rx, link_tx);
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);
216         iostats_submit (name, "overhead", overhead_rx, overhead_tx);
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         }
224         read = 1;
226         return (read);
227 } /* int single_read */
229 /* for reading status version 1 */
230 static int multi1_read (char *name, FILE *fh)
232         char buffer[1024];
233         char *fields[10];
234         int  fields_num, read = 0, found_header = 0;
236         /* read the file until the "ROUTING TABLE" line is found (no more info after) */
237         while (fgets (buffer, sizeof (buffer), fh) != NULL)
238         {
239                 if (strcmp (buffer, "ROUTING TABLE\n") == 0)
240                         break;
242                 if (strcmp (buffer, V1STRING) == 0)
243                 {
244                         found_header = 1;
245                         continue;
246                 }
248                 /* skip the first lines until the client list section is found */
249                 if (found_header == 0)
250                         /* we can't start reading data until this string is found */
251                         continue;
253                 fields_num = openvpn_strsplit (buffer,
254                                 fields, STATIC_ARRAY_SIZE (fields));
255                 if (fields_num < 4)
256                         continue;
258                 iostats_submit (name,                   /* vpn instance */
259                                 fields[0],              /* "Common Name" */
260                                 atoll (fields[2]),      /* "Bytes Received" */
261                                 atoll (fields[3]));     /* "Bytes Sent" */
262                 read = 1;
263         }
265         return (read);
266 } /* int multi1_read */
268 /* for reading status version 2 */
269 static int multi2_read (char *name, FILE *fh)
271         char buffer[1024];
272         char *fields[10];
273         const int max_fields = STATIC_ARRAY_SIZE (fields);
274         int  fields_num, read = 0;
276         while (fgets (buffer, sizeof (buffer), fh) != NULL)
277         {
278                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
280                 /* status file is generated by openvpn/multi.c:multi_print_status()
281                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
282                  *
283                  * The line we're expecting has 8 fields. We ignore all lines
284                  *  with more or less fields.
285                  */
286                 if (fields_num != 8)
287                 {
288                         continue;
289                 }
290                 else
291                 {
292                         if (strcmp (fields[0], "CLIENT_LIST") == 0)
293                         {
294                                 iostats_submit (name,                   /* vpn instance */
295                                                 fields[1],              /* "Common Name" */
296                                                 atoll (fields[4]),      /* "Bytes Received" */
297                                                 atoll (fields[5]));     /* "Bytes Sent" */
298                                 read = 1;
299                         }
300                 }
301         }
303         return (read);
304 } /* int multi2_read */
306 /* for reading status version 3 */
307 static int multi3_read (char *name, FILE *fh)
309         char buffer[1024];
310         char *fields[15];
311         const int max_fields = STATIC_ARRAY_SIZE (fields);
312         int  fields_num, read = 0;
314         while (fgets (buffer, sizeof (buffer), fh) != NULL)
315         {
316                 fields_num = strsplit (buffer, fields, max_fields);
318                 /* status file is generated by openvpn/multi.c:multi_print_status()
319                  * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
320                  *
321                  * The line we're expecting has 12 fields. We ignore all lines
322                  *  with more or less fields.
323                  */
324                 if (fields_num != 12)
325                 {
326                         continue;
327                 }
328                 else
329                 {
330                         if (strcmp (fields[0], "CLIENT_LIST") == 0)
331                         {
332                                 iostats_submit (name,                   /* vpn instance */
333                                                 fields[1],              /* "Common Name" */
334                                                 atoll (fields[4]),      /* "Bytes Received" */
335                                                 atoll (fields[5]));     /* "Bytes Sent" */
336                                 read = 1;
337                         }
338                 }
339         }
341         return (read);
342 } /* int multi3_read */
344 /* read callback */
345 static int openvpn_read (void)
347         FILE *fh;
348         int  i, read;
350         read = 0;
352         /* call the right read function for every status entry in the list */
353         for (i = 0; i < vpn_num; i++)
354         {
355                 fh = fopen (vpn_list[i]->file, "r");
356                 if (fh == NULL)
357                 {
358                         char errbuf[1024];
359                         WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
360                                 sstrerror (errno, errbuf, sizeof (errbuf)));
362                         continue;
363                 }
365                 switch (vpn_list[i]->version)
366                 {
367                         case SINGLE:
368                                 read = single_read(vpn_list[i]->name, fh);
369                                 break;
371                         case MULTI1:
372                                 read = multi1_read(vpn_list[i]->name, fh);
373                                 break;
375                         case MULTI2:
376                                 read = multi2_read(vpn_list[i]->name, fh);
377                                 break;
379                         case MULTI3:
380                                 read = multi3_read(vpn_list[i]->name, fh);
381                                 break;
382                 }
384                 fclose (fh);
385         }
387         return (read ? 0 : -1);
388 } /* int openvpn_read */
390 static int version_detect (const char *filename)
392         FILE *fh;
393         char buffer[1024];
394         int version = 0;
396         /* Sanity checking. We're called from the config handling routine, so
397          * better play it save. */
398         if ((filename == NULL) || (*filename == 0))
399                 return (0);
401         fh = fopen (filename, "r");
402         if (fh == NULL)
403         {
404                 char errbuf[1024];
405                 WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
406                                 sstrerror (errno, errbuf, sizeof (errbuf)));
407                 return (0);
408         }
410         /* now search for the specific multimode data format */
411         while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
412         {
413                 /* we look at the first line searching for SINGLE mode configuration */
414                 if (strcmp (buffer, VSSTRING) == 0)
415                 {
416                         DEBUG ("openvpn plugin: found status file version SINGLE");
417                         version = SINGLE;
418                         break;
419                 }
420                 /* searching for multi version 1 */
421                 else if (strcmp (buffer, V1STRING) == 0)
422                 {
423                         DEBUG ("openvpn plugin: found status file version MULTI1");
424                         version = MULTI1;
425                         break;
426                 }
427                 /* searching for multi version 2 */
428                 else if (strcmp (buffer, V2STRING) == 0)
429                 {
430                         DEBUG ("openvpn plugin: found status file version MULTI2");
431                         version = MULTI2;
432                         break;
433                 }
434                 /* searching for multi version 3 */
435                 else if (strcmp (buffer, V3STRING) == 0)
436                 {
437                         DEBUG ("openvpn plugin: found status file version MULTI3");
438                         version = MULTI3;
439                         break;
440                 }
441         }
443         if (version == 0)
444         {
445                 /* This is only reached during configuration, so complaining to
446                  * the user is in order. */
447                 NOTICE ("openvpn plugin: %s: Unknown file format, please "
448                                 "report this as bug. Make sure to include "
449                                 "your status file, so the plugin can "
450                                 "be adapted.", filename);
451         }
453         fclose (fh);
455         return version;
456 } /* int version_detect */
458 static int openvpn_config (const char *key, const char *value)
460         if (strcasecmp ("StatusFile", key) == 0)
461         {
462                 char    *status_file, *status_name, *filename;
463                 int     status_version, i;
464                 vpn_status_t *temp;
466                 /* try to detect the status file format */
467                 status_version = version_detect (value);
469                 if (status_version == 0)
470                 {
471                         WARNING ("openvpn plugin: unable to detect status version, \
472                                 discarding status file \"%s\".", value);
473                         return (1);
474                 }
476                 status_file = sstrdup (value);
477                 if (status_file == NULL)
478                 {
479                         char errbuf[1024];
480                         WARNING ("openvpn plugin: sstrdup failed: %s",
481                                 sstrerror (errno, errbuf, sizeof (errbuf)));
482                         return (1);
483                 }
485                 /* it determines the file name as string starting at location filename + 1 */
486                 filename = strrchr (status_file, (int) '/');
487                 if (filename == NULL)
488                 {
489                         /* status_file is already the file name only */
490                         status_name = status_file;
491                 }
492                 else
493                 {
494                         /* doesn't waist memory, uses status_file starting at filename + 1 */
495                         status_name = filename + 1;
496                 }
498                 /* scan the list looking for a clone */
499                 for (i = 0; i < vpn_num; i++)
500                 {
501                         if (strcasecmp (vpn_list[i]->name, status_name) == 0)
502                         {
503                                 WARNING ("openvpn plugin: status filename \"%s\" "
504                                                 "already used, please choose a "
505                                                 "different one.", status_name);
506                                 sfree (status_file);
507                                 return (1);
508                         }
509                 }
511                 /* create a new vpn element since file, version and name are ok */
512                 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
513                 temp->file = status_file;
514                 temp->version = status_version;
515                 temp->name = status_name;
517                 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
518                 if (vpn_list == NULL)
519                 {
520                         char errbuf[1024];
521                         ERROR ("openvpn plugin: malloc failed: %s",
522                                 sstrerror (errno, errbuf, sizeof (errbuf)));
524                         sfree (temp->file);
525                         sfree (temp);
526                         return (1);
527                 }
529                 vpn_list[vpn_num] = temp;
530                 vpn_num++;
532                 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
534         }
535         else if (strcasecmp ("Compression", key) == 0)
536         {
537                 if (IS_TRUE (value))
538                         store_compression = 1;
539                 else
540                 {
541                         store_compression = 0;
542                         DEBUG ("openvpn plugin: no 'compression statistcs' collected");
543                 }
544         }
545         else
546         {
547                 return (-1);
548         }
550         return (0);
551 } /* int openvpn_config */
553 /* shutdown callback */
554 static int openvpn_shutdown (void)
556         int i;
558         for (i = 0; i < vpn_num; i++)
559         {
560                 sfree (vpn_list[i]->file);
561                 sfree (vpn_list[i]);
562         }
564         sfree (vpn_list);
566         return (0);
567 } /* int openvpn_shutdown */
569 void module_register (void)
571         plugin_register_config ("openvpn", openvpn_config,
572                                 config_keys, config_keys_num);
573         plugin_register_read ("openvpn", openvpn_read);
574         plugin_register_shutdown ("openvpn", openvpn_shutdown);
575 } /* void module_register */