Let plugin_dispatch_values() set value_list.time in case of 'now'.
[collectd.git] / src / openvpn.c
1 /**
2  * collectd - src/openvpn.c
3  * Copyright (C) 2008  Doug MacEachern
4  * Copyright (C) 2008  Florian octo Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Doug MacEachern <dougm at hyperic.com>
21  *   Florian octo Forster <octo at verplant.org>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #define DEFAULT_STATUS_FILE "/etc/openvpn/openvpn-status.log"
29 #define CLIENT_LIST_PREFIX  "CLIENT_LIST,"
30
31 static char *status_file = NULL;
32
33 /* For compression stats we need to convert these counters to a rate. */
34 static counter_t pre_compress_old    = 0;
35 static counter_t post_compress_old   = 0;
36 static counter_t pre_decompress_old  = 0;
37 static counter_t post_decompress_old = 0;
38 static int compression_counter_valid = 0;
39
40 static const char *config_keys[] =
41 {
42         "StatusFile"
43 };
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45
46 /* copy-n-pasted from common.c - changed delim to "," */
47 static int openvpn_strsplit (char *string, char **fields, size_t size)
48 {
49         size_t i;
50         char *ptr;
51         char *saveptr;
52
53         i = 0;
54         ptr = string;
55         saveptr = NULL;
56         while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
57         {
58                 ptr = NULL;
59                 i++;
60
61                 if (i >= size)
62                         break;
63         }
64
65         return (i);
66 } /* int openvpn_strsplit */
67
68 static void openvpn_submit (char *name, counter_t rx, counter_t tx)
69 {
70         value_t values[2];
71         value_list_t vl = VALUE_LIST_INIT;
72
73         values[0].counter = rx;
74         values[1].counter = tx;
75
76         vl.values = values;
77         vl.values_len = STATIC_ARRAY_SIZE (values);
78         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
79         sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
80         sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
81         sstrncpy (vl.type, "if_octets", sizeof (vl.type));
82
83         plugin_dispatch_values (&vl);
84 } /* void openvpn_submit */
85
86 static void compression_submit (char *type_instance, gauge_t ratio)
87 {
88         value_t values[1];
89         value_list_t vl = VALUE_LIST_INIT;
90
91         values[0].gauge = ratio;
92
93         vl.values = values;
94         vl.values_len = STATIC_ARRAY_SIZE (values);
95         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
96         sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
97         sstrncpy (vl.type, "compression_ratio", sizeof (vl.type));
98         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type));
99
100         plugin_dispatch_values (&vl);
101 } /* void compression_submit */
102
103 static int openvpn_read (void)
104 {
105         FILE *fh;
106         char buffer[1024];
107         char *fields[10];
108         const int max_fields = STATIC_ARRAY_SIZE (fields);
109         int   fields_num;
110
111         counter_t pre_compress_new    = 0;
112         counter_t post_compress_new   = 0;
113         counter_t pre_decompress_new  = 0;
114         counter_t post_decompress_new = 0;
115
116         /* Clear the least significant four bits, just to make sure all four
117          * counters above are considered to be invalid. */
118         compression_counter_valid &= ~0x0f;
119
120         fh = fopen ((status_file != NULL)
121                         ? status_file
122                         : DEFAULT_STATUS_FILE, "r");
123         if (fh == NULL)
124                 return (-1);
125
126         /* status file is generated by openvpn/multi.c:multi_print_status()
127          * this plugin requires server.conf: "status-version 2"
128          * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
129          */
130         while (fgets (buffer, sizeof (buffer), fh) != NULL)
131         {
132                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
133
134                 /* Expect at least ``key,value''. */
135                 if (fields_num < 2)
136                         continue;
137
138                 if (strcmp (fields[0], "CLIENT_LIST") == 0)
139                 {
140                         char *name;
141                         counter_t rx;
142                         counter_t tx;
143
144                         /* The line we're expecting has 8 fields. We ignore all lines
145                          * with more or less fields. */
146                         if (fields_num != 8)
147                                 continue;
148
149                         name =      fields[1];  /* "Common Name" */
150                         rx = atoll (fields[4]); /* "Bytes Received */
151                         tx = atoll (fields[5]); /* "Bytes Sent" */
152                         openvpn_submit (name, rx, tx);
153                 }
154                 else if (strcmp (fields[0], "pre-compress") == 0)
155                 {
156                         pre_compress_new = atoll (fields[1]);
157                         compression_counter_valid |= 0x01;
158                 }
159                 else if (strcmp (fields[0], "post-compress") == 0)
160                 {
161                         post_compress_new = atoll (fields[1]);
162                         compression_counter_valid |= 0x02;
163                 }
164                 else if (strcmp (fields[0], "pre-decompress") == 0)
165                 {
166                         pre_decompress_new = atoll (fields[1]);
167                         compression_counter_valid |= 0x04;
168                 }
169                 else if (strcmp (fields[0], "post-decompress") == 0)
170                 {
171                         post_decompress_new = atoll (fields[1]);
172                         compression_counter_valid |= 0x08;
173                 }
174         }
175         fclose (fh);
176
177         /* Check that all four counters are valid, {pre,post}_*_{old,new}. */
178         if ((compression_counter_valid & 0x33) == 0x33)
179         {
180                 counter_t pre_diff;
181                 counter_t post_diff;
182
183                 pre_diff = counter_diff (pre_compress_old, pre_compress_new);
184                 post_diff = counter_diff (post_compress_old, post_compress_new);
185
186                 /* If we compress, we're sending. */
187                 compression_submit ("tx",
188                                 ((gauge_t) post_diff) / ((gauge_t) pre_diff));
189         }
190
191         /* Now check the other found counters. */
192         if ((compression_counter_valid & 0xcc) == 0xcc)
193         {
194                 counter_t pre_diff;
195                 counter_t post_diff;
196
197                 pre_diff = counter_diff (pre_decompress_old, pre_decompress_new);
198                 post_diff = counter_diff (post_decompress_old, post_decompress_new);
199
200                 /* If we decompress, we're receiving. */
201                 compression_submit ("rx",
202                                 ((gauge_t) pre_diff) / ((gauge_t) post_diff));
203         }
204
205         /* Now copy all the new counters to the old counters and move the flags
206          * up. */
207         pre_compress_old = pre_compress_new;
208         post_compress_old = post_compress_new;
209         pre_decompress_old = pre_decompress_new;
210         post_decompress_old = post_decompress_new;
211         compression_counter_valid = (compression_counter_valid & 0x0f) << 4;
212
213         return (0);
214 } /* int openvpn_read */
215
216 static int openvpn_config (const char *key, const char *value)
217 {
218         if (strcasecmp ("StatusFile", key) == 0)
219         {
220                 sfree (status_file);
221                 status_file = sstrdup (value);
222         }
223         else
224         {
225                 return (-1);
226         }
227         return (0);
228 } /* int openvpn_config */
229
230 void module_register (void)
231 {
232         plugin_register_config ("openvpn", openvpn_config,
233                                 config_keys, config_keys_num);
234         plugin_register_read ("openvpn", openvpn_read);
235 } /* void module_register */