2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2009 Florian octo Forster
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.
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.
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
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at verplant.org>
28 #define DEFAULT_STATUS_FILE "/etc/openvpn/openvpn-status.log"
29 #define CLIENT_LIST_PREFIX "CLIENT_LIST,"
31 static char *status_file = NULL;
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;
40 static const char *config_keys[] =
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46 /* copy-n-pasted from common.c - changed delim to "," */
47 static int openvpn_strsplit (char *string, char **fields, size_t size)
56 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
66 } /* int openvpn_strsplit */
68 static void openvpn_submit (char *name, counter_t rx, counter_t tx)
71 value_list_t vl = VALUE_LIST_INIT;
73 values[0].counter = rx;
74 values[1].counter = tx;
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));
83 plugin_dispatch_values (&vl);
84 } /* void openvpn_submit */
86 static void compression_submit (char *type_instance, gauge_t ratio)
89 value_list_t vl = VALUE_LIST_INIT;
91 values[0].gauge = ratio;
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));
100 plugin_dispatch_values (&vl);
101 } /* void compression_submit */
103 static int openvpn_read (void)
108 const int max_fields = STATIC_ARRAY_SIZE (fields);
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;
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;
120 fh = fopen ((status_file != NULL)
122 : DEFAULT_STATUS_FILE, "r");
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
130 while (fgets (buffer, sizeof (buffer), fh) != NULL)
132 fields_num = openvpn_strsplit (buffer, fields, max_fields);
134 /* Expect at least ``key,value''. */
138 if (strcmp (fields[0], "CLIENT_LIST") == 0)
144 /* The line we're expecting has 8 fields. We ignore all lines
145 * with more or less fields. */
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);
154 else if (strcmp (fields[0], "pre-compress") == 0)
156 pre_compress_new = atoll (fields[1]);
157 compression_counter_valid |= 0x01;
159 else if (strcmp (fields[0], "post-compress") == 0)
161 post_compress_new = atoll (fields[1]);
162 compression_counter_valid |= 0x02;
164 else if (strcmp (fields[0], "pre-decompress") == 0)
166 pre_decompress_new = atoll (fields[1]);
167 compression_counter_valid |= 0x04;
169 else if (strcmp (fields[0], "post-decompress") == 0)
171 post_decompress_new = atoll (fields[1]);
172 compression_counter_valid |= 0x08;
177 /* Check that all four counters are valid, {pre,post}_*_{old,new}. */
178 if ((compression_counter_valid & 0x33) == 0x33)
183 pre_diff = counter_diff (pre_compress_old, pre_compress_new);
184 post_diff = counter_diff (post_compress_old, post_compress_new);
186 /* If we compress, we're sending. */
187 compression_submit ("tx",
188 ((gauge_t) post_diff) / ((gauge_t) pre_diff));
191 /* Now check the other found counters. */
192 if ((compression_counter_valid & 0xcc) == 0xcc)
197 pre_diff = counter_diff (pre_decompress_old, pre_decompress_new);
198 post_diff = counter_diff (post_decompress_old, post_decompress_new);
200 /* If we decompress, we're receiving. */
201 compression_submit ("rx",
202 ((gauge_t) pre_diff) / ((gauge_t) post_diff));
205 /* Now copy all the new counters to the old counters and move the flags
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;
214 } /* int openvpn_read */
216 static int openvpn_config (const char *key, const char *value)
218 if (strcasecmp ("StatusFile", key) == 0)
221 status_file = sstrdup (value);
228 } /* int openvpn_config */
230 void module_register (void)
232 plugin_register_config ("openvpn", openvpn_config,
233 config_keys, config_keys_num);
234 plugin_register_read ("openvpn", openvpn_read);
235 } /* void module_register */