Code

openvpn plugin: Don't use negated config options.
[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"
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;
49 static vpn_status_t **vpn_list = NULL;
50 static int vpn_num = 0;
52 static int store_compression = 1;
54 static const char *config_keys[] =
55 {
56         "StatusFile",
57         "Compression"
58 };
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
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;
70         i = 0;
71         ptr = string;
72         saveptr = NULL;
73         while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
74         {
75                 ptr = NULL;
76                 i++;
78                 if (i >= size)
79                         break;
80         }
82         return (i);
83 } /* int openvpn_strsplit */
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;
92         values[0].counter = rx;
93         values[1].counter = tx;
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          */
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));
108         plugin_dispatch_values (&vl);
109 } /* void traffic_submit */
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)
114         value_t values[2];
115         value_list_t vl = VALUE_LIST_INIT;
117         values[0].counter = uncompressed;
118         values[1].counter = compressed;
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));
128         plugin_dispatch_values (&vl);
129 } /* void compression_submit */
131 static int single_read (char *name, FILE *fh)
133         char buffer[1024];
134         char *fields[4];
135         const int max_fields = STATIC_ARRAY_SIZE (fields);
136         int  fields_num, read = 0;
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;
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;
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)
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;
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;
245                         continue;
246                 }
247                 else
248                 {
249                         fields_num = openvpn_strsplit (buffer, fields, max_fields);
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         }
259         return (read);
260 } /* int multi1_read */
262 /* for reading status version 2 */
263 static int multi2_read (char *name, FILE *fh)
265         char buffer[1024];
266         char *fields[10];
267         const int max_fields = STATIC_ARRAY_SIZE (fields);
268         int  fields_num, read = 0;
270         while (fgets (buffer, sizeof (buffer), fh) != NULL)
271         {
272                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
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         }
297         return (read);
298 } /* int multi2_read */
300 /* for reading status version 3 */
301 static int multi3_read (char *name, FILE *fh)
303         char buffer[1024];
304         char *fields[15];
305         const int max_fields = STATIC_ARRAY_SIZE (fields);
306         int  fields_num, read = 0;
308         while (fgets (buffer, sizeof (buffer), fh) != NULL)
309         {
310                 fields_num = strsplit (buffer, fields, max_fields);
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         }
335         return (read);
336 } /* int multi3_read */
338 /* read callback */
339 static int openvpn_read (void)
341         FILE *fh;
342         int  i, read;
344         read = 0;
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)));
356                         continue;
357                 }
359                 switch (vpn_list[i]->version)
360                 {
361                         case SINGLE:
362                                 read = single_read(vpn_list[i]->name, fh);
363                                 break;
365                         case MULTI1:
366                                 read = multi1_read(vpn_list[i]->name, fh);
367                                 break;
369                         case MULTI2:
370                                 read = multi2_read(vpn_list[i]->name, fh);
371                                 break;
373                         case MULTI3:
374                                 read = multi3_read(vpn_list[i]->name, fh);
375                                 break;
376                 }
378                 fclose (fh);
379         }
381         return (read ? 0 : -1);
382 } /* int openvpn_read */
384 static int version_detect (FILE *fh)
386         char buffer[1024];
387         int version = 0;
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                 {
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         }
425         if (version == 0)
426         {
427                 DEBUG ("openvpn plugin: unknown file format, please report this as bug");
428         }
430         return version;
431 } /* int version_detect */
433 static int openvpn_config (const char *key, const char *value)
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;
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                 }
452                 /* once open try to detect the status file format */
453                 status_version = version_detect (fh);
455                 fclose (fh);
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                 }
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                 }
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);
496                                         sfree (status_file);
497                                         return (1);
498                                 }
499                         }
500                 }
502                 /* create a new vpn element since file, version and name are ok */      
503                 temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
504                 temp->file = status_file;
505                 temp->version = status_version;
506                 temp->name = status_name;
508                 vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
509                 if (vpn_list == NULL)
510                 {
511                         char errbuf[1024];
512                         ERROR ("openvpn plugin: malloc failed: %s",
513                                 sstrerror (errno, errbuf, sizeof (errbuf)));
515                         sfree (temp->file);
516                         sfree (temp);
517                         return (1);
518                 }
520                 vpn_list[vpn_num] = temp;
521                 vpn_num++;
523                 DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
525         }
526         else if (strcasecmp ("Compression", key) == 0)
527         {
528                 if (IS_TRUE (value))
529                         store_compression = 1;
530                 else
531                 {
532                         store_compression = 0;
533                         DEBUG ("openvpn plugin: no 'compression statistcs' collected");
534                 }
535         }
536         else
537         {
538                 return (-1);
539         }
541         return (0);
542 } /* int openvpn_config */
544 /* shutdown callback */
545 static int openvpn_shutdown (void)
547         int i;
548         
549         for (i = 0; i < vpn_num; i++)
550         {
551                 sfree (vpn_list[i]->file);
552                 sfree (vpn_list[i]);
553         }
555         sfree (vpn_list);
557         return (0);
558 } /* int openvpn_shutdown */
560 void module_register (void)
562         plugin_register_config ("openvpn", openvpn_config,
563                                 config_keys, config_keys_num);
564         plugin_register_read ("openvpn", openvpn_read);
565         plugin_register_shutdown ("openvpn", openvpn_shutdown);
566 } /* void module_register */