a2f48236bbca9cf1c11b72276cc2558e1fe897d0
[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         vl.time = time (NULL);
79         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
80         sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
81         sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
82         sstrncpy (vl.type, "if_octets", sizeof (vl.type));
83
84         plugin_dispatch_values (&vl);
85 } /* void openvpn_submit */
86
87 static void compression_submit (char *type_instance, gauge_t ratio)
88 {
89         value_t values[1];
90         value_list_t vl = VALUE_LIST_INIT;
91
92         values[0].gauge = ratio;
93
94         vl.values = values;
95         vl.values_len = STATIC_ARRAY_SIZE (values);
96         vl.time = time (NULL);
97         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
98         sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
99         sstrncpy (vl.type, "compression_ratio", sizeof (vl.type));
100         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type));
101
102         plugin_dispatch_values (&vl);
103 } /* void compression_submit */
104
105 static int openvpn_read (void)
106 {
107         FILE *fh;
108         char buffer[1024];
109         char *fields[10];
110         const int max_fields = STATIC_ARRAY_SIZE (fields);
111         int   fields_num;
112
113         counter_t pre_compress_new    = 0;
114         counter_t post_compress_new   = 0;
115         counter_t pre_decompress_new  = 0;
116         counter_t post_decompress_new = 0;
117
118         /* Clear the least significant four bits, just to make sure all four
119          * counters above are considered to be invalid. */
120         compression_counter_valid &= ~0x0f;
121
122         fh = fopen ((status_file != NULL)
123                         ? status_file
124                         : DEFAULT_STATUS_FILE, "r");
125         if (fh == NULL)
126                 return (-1);
127
128         /* status file is generated by openvpn/multi.c:multi_print_status()
129          * this plugin requires server.conf: "status-version 2"
130          * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
131          */
132         while (fgets (buffer, sizeof (buffer), fh) != NULL)
133         {
134                 fields_num = openvpn_strsplit (buffer, fields, max_fields);
135
136                 /* Expect at least ``key,value''. */
137                 if (fields_num < 2)
138                         continue;
139
140                 if (strcmp (fields[0], "CLIENT_LIST") == 0)
141                 {
142                         char *name;
143                         counter_t rx;
144                         counter_t tx;
145
146                         /* The line we're expecting has 8 fields. We ignore all lines
147                          * with more or less fields. */
148                         if (fields_num != 8)
149                                 continue;
150
151                         name =      fields[1];  /* "Common Name" */
152                         rx = atoll (fields[4]); /* "Bytes Received */
153                         tx = atoll (fields[5]); /* "Bytes Sent" */
154                         openvpn_submit (name, rx, tx);
155                 }
156                 else if (strcmp (fields[0], "pre-compress") == 0)
157                 {
158                         pre_compress_new = atoll (fields[1]);
159                         compression_counter_valid |= 0x01;
160                 }
161                 else if (strcmp (fields[0], "post-compress") == 0)
162                 {
163                         post_compress_new = atoll (fields[1]);
164                         compression_counter_valid |= 0x02;
165                 }
166                 else if (strcmp (fields[0], "pre-decompress") == 0)
167                 {
168                         pre_decompress_new = atoll (fields[1]);
169                         compression_counter_valid |= 0x04;
170                 }
171                 else if (strcmp (fields[0], "post-decompress") == 0)
172                 {
173                         post_decompress_new = atoll (fields[1]);
174                         compression_counter_valid |= 0x08;
175                 }
176         }
177         fclose (fh);
178
179         /* Check that all four counters are valid, {pre,post}_*_{old,new}. */
180         if ((compression_counter_valid & 0x33) == 0x33)
181         {
182                 counter_t pre_diff;
183                 counter_t post_diff;
184
185                 pre_diff = counter_diff (pre_compress_old, pre_compress_new);
186                 post_diff = counter_diff (post_compress_old, post_compress_new);
187
188                 /* If we compress, we're sending. */
189                 compression_submit ("tx",
190                                 ((gauge_t) post_diff) / ((gauge_t) pre_diff));
191         }
192
193         /* Now check the other found counters. */
194         if ((compression_counter_valid & 0xcc) == 0xcc)
195         {
196                 counter_t pre_diff;
197                 counter_t post_diff;
198
199                 pre_diff = counter_diff (pre_decompress_old, pre_decompress_new);
200                 post_diff = counter_diff (post_decompress_old, post_decompress_new);
201
202                 /* If we decompress, we're receiving. */
203                 compression_submit ("rx",
204                                 ((gauge_t) pre_diff) / ((gauge_t) post_diff));
205         }
206
207         /* Now copy all the new counters to the old counters and move the flags
208          * up. */
209         pre_compress_old = pre_compress_new;
210         post_compress_old = post_compress_new;
211         pre_decompress_old = pre_decompress_new;
212         post_decompress_old = post_decompress_new;
213         compression_counter_valid = (compression_counter_valid & 0x0f) << 4;
214
215         return (0);
216 } /* int openvpn_read */
217
218 static int openvpn_config (const char *key, const char *value)
219 {
220         if (strcasecmp ("StatusFile", key) == 0)
221         {
222                 sfree (status_file);
223                 status_file = sstrdup (value);
224         }
225         else
226         {
227                 return (-1);
228         }
229         return (0);
230 } /* int openvpn_config */
231
232 void module_register (void)
233 {
234         plugin_register_config ("openvpn", openvpn_config,
235                                 config_keys, config_keys_num);
236         plugin_register_read ("openvpn", openvpn_read);
237 } /* void module_register */