2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2008 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 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));
84 plugin_dispatch_values (&vl);
85 } /* void openvpn_submit */
87 static void compression_submit (char *type_instance, gauge_t ratio)
90 value_list_t vl = VALUE_LIST_INIT;
92 values[0].gauge = ratio;
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));
102 plugin_dispatch_values (&vl);
103 } /* void compression_submit */
105 static int openvpn_read (void)
110 const int max_fields = STATIC_ARRAY_SIZE (fields);
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;
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;
122 fh = fopen ((status_file != NULL)
124 : DEFAULT_STATUS_FILE, "r");
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
132 while (fgets (buffer, sizeof (buffer), fh) != NULL)
134 fields_num = openvpn_strsplit (buffer, fields, max_fields);
136 /* Expect at least ``key,value''. */
140 if (strcmp (fields[0], "CLIENT_LIST") == 0)
146 /* The line we're expecting has 8 fields. We ignore all lines
147 * with more or less fields. */
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);
156 else if (strcmp (fields[0], "pre-compress") == 0)
158 pre_compress_new = atoll (fields[1]);
159 compression_counter_valid |= 0x01;
161 else if (strcmp (fields[0], "post-compress") == 0)
163 post_compress_new = atoll (fields[1]);
164 compression_counter_valid |= 0x02;
166 else if (strcmp (fields[0], "pre-decompress") == 0)
168 pre_decompress_new = atoll (fields[1]);
169 compression_counter_valid |= 0x04;
171 else if (strcmp (fields[0], "post-decompress") == 0)
173 post_decompress_new = atoll (fields[1]);
174 compression_counter_valid |= 0x08;
179 /* Check that all four counters are valid, {pre,post}_*_{old,new}. */
180 if ((compression_counter_valid & 0x33) == 0x33)
185 pre_diff = counter_diff (pre_compress_old, pre_compress_new);
186 post_diff = counter_diff (post_compress_old, post_compress_new);
188 /* If we compress, we're sending. */
189 compression_submit ("tx",
190 ((gauge_t) post_diff) / ((gauge_t) pre_diff));
193 /* Now check the other found counters. */
194 if ((compression_counter_valid & 0xcc) == 0xcc)
199 pre_diff = counter_diff (pre_decompress_old, pre_decompress_new);
200 post_diff = counter_diff (post_decompress_old, post_decompress_new);
202 /* If we decompress, we're receiving. */
203 compression_submit ("rx",
204 ((gauge_t) pre_diff) / ((gauge_t) post_diff));
207 /* Now copy all the new counters to the old counters and move the flags
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;
216 } /* int openvpn_read */
218 static int openvpn_config (const char *key, const char *value)
220 if (strcasecmp ("StatusFile", key) == 0)
223 status_file = sstrdup (value);
230 } /* int openvpn_config */
232 void module_register (void)
234 plugin_register_config ("openvpn", openvpn_config,
235 config_keys, config_keys_num);
236 plugin_register_read ("openvpn", openvpn_read);
237 } /* void module_register */