| 1 |
/** |
| 2 |
* collectd - src/openvpn.c |
| 3 |
* Copyright (C) 2008 Doug MacEachern |
| 4 |
* Copyright (C) 2009,2010 Florian octo Forster |
| 5 |
* Copyright (C) 2009 Marco Chiappero |
| 6 |
* Copyright (C) 2009 Fabian Schuh |
| 7 |
* Copyright (C) 2017-2020 Pavel Rochnyak |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU General Public License as published by the |
| 11 |
* Free Software Foundation; only version 2 of the License is applicable. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License along |
| 19 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
* Authors: |
| 23 |
* Doug MacEachern <dougm at hyperic.com> |
| 24 |
* Florian octo Forster <octo at collectd.org> |
| 25 |
* Marco Chiappero <marco at absence.it> |
| 26 |
* Fabian Schuh <mail at xeroc.org> |
| 27 |
* Pavel Rochnyak <pavel2000 ngs.ru> |
| 28 |
**/ |
| 29 |
|
| 30 |
#include "collectd.h" |
| 31 |
|
| 32 |
#include "plugin.h" |
| 33 |
#include "utils/common/common.h" |
| 34 |
|
| 35 |
/** |
| 36 |
* There is two main kinds of OpenVPN status file: |
| 37 |
* - for 'single' mode (point-to-point or client mode) |
| 38 |
* - for 'multi' mode (server with multiple clients) |
| 39 |
* |
| 40 |
* For 'multi' there is 3 versions of status file format: |
| 41 |
* - version 1 - First version of status file: without line type tokens, |
| 42 |
* comma delimited for easy machine parsing. Currently used by default. |
| 43 |
* Added in openvpn-2.0-beta3. |
| 44 |
* - version 2 - with line type tokens, with 'HEADER' line type, uses comma |
| 45 |
* as a delimiter. |
| 46 |
* Added in openvpn-2.0-beta15. |
| 47 |
* - version 3 - The only difference from version 2 is delimiter: in version 3 |
| 48 |
* tabs are used instead of commas. Set of fields is the same. |
| 49 |
* Added in openvpn-2.1_rc14. |
| 50 |
* |
| 51 |
* For versions 2/3 there may be different sets of fields in different |
| 52 |
* OpenVPN versions. |
| 53 |
* |
| 54 |
* Versions 2.0, 2.1, 2.2: |
| 55 |
* Common Name,Real Address,Virtual Address, |
| 56 |
* Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t) |
| 57 |
* |
| 58 |
* Version 2.3: |
| 59 |
* Common Name,Real Address,Virtual Address, |
| 60 |
* Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t),Username |
| 61 |
* |
| 62 |
* Version 2.4: |
| 63 |
* Common Name,Real Address,Virtual Address,Virtual IPv6 Address, |
| 64 |
* Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t),Username, |
| 65 |
* Client ID,Peer ID |
| 66 |
* |
| 67 |
* Current Collectd code tries to handle changes in this field set, |
| 68 |
* if they are backward-compatible. |
| 69 |
**/ |
| 70 |
|
| 71 |
#define TITLE_SINGLE "OpenVPN STATISTICS\n" |
| 72 |
#define TITLE_V1 "OpenVPN CLIENT LIST\n" |
| 73 |
#define TITLE_V2 "TITLE," |
| 74 |
#define TITLE_V3 "TITLE\t" |
| 75 |
|
| 76 |
#define V1HEADER \ |
| 77 |
"Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n" |
| 78 |
|
| 79 |
struct vpn_status_s { |
| 80 |
char *file; |
| 81 |
char *name; |
| 82 |
}; |
| 83 |
typedef struct vpn_status_s vpn_status_t; |
| 84 |
|
| 85 |
static bool new_naming_schema; |
| 86 |
static bool collect_compression = true; |
| 87 |
static bool collect_user_count; |
| 88 |
static bool collect_individual_users = true; |
| 89 |
|
| 90 |
static const char *config_keys[] = { |
| 91 |
"StatusFile", "Compression", /* old, deprecated name */ |
| 92 |
"ImprovedNamingSchema", "CollectCompression", |
| 93 |
"CollectUserCount", "CollectIndividualUsers"}; |
| 94 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 95 |
|
| 96 |
/* Helper function |
| 97 |
* copy-n-pasted from common.c - changed delim to ",\t" */ |
| 98 |
static int openvpn_strsplit(char *string, char **fields, size_t size) { |
| 99 |
size_t i = 0; |
| 100 |
char *ptr = string; |
| 101 |
char *saveptr = NULL; |
| 102 |
|
| 103 |
while ((fields[i] = strtok_r(ptr, ",\t", &saveptr)) != NULL) { |
| 104 |
ptr = NULL; |
| 105 |
i++; |
| 106 |
|
| 107 |
if (i >= size) |
| 108 |
break; |
| 109 |
} |
| 110 |
|
| 111 |
return i; |
| 112 |
} /* int openvpn_strsplit */ |
| 113 |
|
| 114 |
static void openvpn_free(void *arg) { |
| 115 |
vpn_status_t *st = arg; |
| 116 |
|
| 117 |
sfree(st->file); |
| 118 |
sfree(st); |
| 119 |
} /* void openvpn_free */ |
| 120 |
|
| 121 |
/* dispatches number of users */ |
| 122 |
static void numusers_submit(const char *pinst, const char *tinst, |
| 123 |
gauge_t value) { |
| 124 |
value_list_t vl = VALUE_LIST_INIT; |
| 125 |
|
| 126 |
vl.values = &(value_t){.gauge = value}; |
| 127 |
vl.values_len = 1; |
| 128 |
sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin)); |
| 129 |
sstrncpy(vl.type, "users", sizeof(vl.type)); |
| 130 |
if (pinst != NULL) |
| 131 |
sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance)); |
| 132 |
if (tinst != NULL) |
| 133 |
sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance)); |
| 134 |
|
| 135 |
plugin_dispatch_values(&vl); |
| 136 |
} /* void numusers_submit */ |
| 137 |
|
| 138 |
/* dispatches stats about traffic (TCP or UDP) generated by the tunnel |
| 139 |
* per single endpoint */ |
| 140 |
static void iostats_submit(const char *pinst, const char *tinst, derive_t rx, |
| 141 |
derive_t tx) { |
| 142 |
value_list_t vl = VALUE_LIST_INIT; |
| 143 |
value_t values[] = { |
| 144 |
{.derive = rx}, |
| 145 |
{.derive = tx}, |
| 146 |
}; |
| 147 |
|
| 148 |
/* NOTE ON THE NEW NAMING SCHEMA: |
| 149 |
* using plugin_instance to identify each vpn config (and |
| 150 |
* status) file; using type_instance to identify the endpoint |
| 151 |
* host when in multimode, traffic or overhead when in single. |
| 152 |
*/ |
| 153 |
|
| 154 |
vl.values = values; |
| 155 |
vl.values_len = STATIC_ARRAY_SIZE(values); |
| 156 |
sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin)); |
| 157 |
if (pinst != NULL) |
| 158 |
sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance)); |
| 159 |
sstrncpy(vl.type, "if_octets", sizeof(vl.type)); |
| 160 |
if (tinst != NULL) |
| 161 |
sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance)); |
| 162 |
|
| 163 |
plugin_dispatch_values(&vl); |
| 164 |
} /* void traffic_submit */ |
| 165 |
|
| 166 |
/* dispatches stats about data compression shown when in single mode */ |
| 167 |
static void compression_submit(const char *pinst, const char *tinst, |
| 168 |
derive_t uncompressed, derive_t compressed) { |
| 169 |
value_list_t vl = VALUE_LIST_INIT; |
| 170 |
value_t values[] = { |
| 171 |
{.derive = uncompressed}, |
| 172 |
{.derive = compressed}, |
| 173 |
}; |
| 174 |
|
| 175 |
vl.values = values; |
| 176 |
vl.values_len = STATIC_ARRAY_SIZE(values); |
| 177 |
sstrncpy(vl.plugin, "openvpn", sizeof(vl.plugin)); |
| 178 |
if (pinst != NULL) |
| 179 |
sstrncpy(vl.plugin_instance, pinst, sizeof(vl.plugin_instance)); |
| 180 |
sstrncpy(vl.type, "compression", sizeof(vl.type)); |
| 181 |
if (tinst != NULL) |
| 182 |
sstrncpy(vl.type_instance, tinst, sizeof(vl.type_instance)); |
| 183 |
|
| 184 |
plugin_dispatch_values(&vl); |
| 185 |
} /* void compression_submit */ |
| 186 |
|
| 187 |
static int single_read(const char *name, FILE *fh) { |
| 188 |
char buffer[1024]; |
| 189 |
char *fields[4]; |
| 190 |
const int max_fields = STATIC_ARRAY_SIZE(fields); |
| 191 |
|
| 192 |
derive_t link_rx = 0, link_tx = 0; |
| 193 |
derive_t tun_rx = 0, tun_tx = 0; |
| 194 |
derive_t pre_compress = 0, post_compress = 0; |
| 195 |
derive_t pre_decompress = 0, post_decompress = 0; |
| 196 |
|
| 197 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 198 |
int fields_num = openvpn_strsplit(buffer, fields, max_fields); |
| 199 |
|
| 200 |
/* status file is generated by openvpn/sig.c:print_status() |
| 201 |
* http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c |
| 202 |
* |
| 203 |
* The line we're expecting has 2 fields. We ignore all lines |
| 204 |
* with more or less fields. |
| 205 |
*/ |
| 206 |
if (fields_num != 2) { |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
if (strcmp(fields[0], "TUN/TAP read bytes") == 0) { |
| 211 |
/* read from the system and sent over the tunnel */ |
| 212 |
tun_tx = atoll(fields[1]); |
| 213 |
} else if (strcmp(fields[0], "TUN/TAP write bytes") == 0) { |
| 214 |
/* read from the tunnel and written in the system */ |
| 215 |
tun_rx = atoll(fields[1]); |
| 216 |
} else if (strcmp(fields[0], "TCP/UDP read bytes") == 0) { |
| 217 |
link_rx = atoll(fields[1]); |
| 218 |
} else if (strcmp(fields[0], "TCP/UDP write bytes") == 0) { |
| 219 |
link_tx = atoll(fields[1]); |
| 220 |
} else if (strcmp(fields[0], "pre-compress bytes") == 0) { |
| 221 |
pre_compress = atoll(fields[1]); |
| 222 |
} else if (strcmp(fields[0], "post-compress bytes") == 0) { |
| 223 |
post_compress = atoll(fields[1]); |
| 224 |
} else if (strcmp(fields[0], "pre-decompress bytes") == 0) { |
| 225 |
pre_decompress = atoll(fields[1]); |
| 226 |
} else if (strcmp(fields[0], "post-decompress bytes") == 0) { |
| 227 |
post_decompress = atoll(fields[1]); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
iostats_submit(name, "traffic", link_rx, link_tx); |
| 232 |
|
| 233 |
/* we need to force this order to avoid negative values with these unsigned */ |
| 234 |
derive_t overhead_rx = |
| 235 |
(((link_rx - pre_decompress) + post_decompress) - tun_rx); |
| 236 |
derive_t overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx); |
| 237 |
|
| 238 |
iostats_submit(name, "overhead", overhead_rx, overhead_tx); |
| 239 |
|
| 240 |
if (collect_compression) { |
| 241 |
compression_submit(name, "data_in", post_decompress, pre_decompress); |
| 242 |
compression_submit(name, "data_out", pre_compress, post_compress); |
| 243 |
} |
| 244 |
|
| 245 |
return 0; |
| 246 |
} /* int single_read */ |
| 247 |
|
| 248 |
/* for reading status version 1 */ |
| 249 |
static int multi1_read(const char *name, FILE *fh) { |
| 250 |
char buffer[1024]; |
| 251 |
char *fields[10]; |
| 252 |
const int max_fields = STATIC_ARRAY_SIZE(fields); |
| 253 |
long long sum_users = 0; |
| 254 |
bool found_header = false; |
| 255 |
|
| 256 |
/* read the file until the "ROUTING TABLE" line is found (no more info after) |
| 257 |
*/ |
| 258 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 259 |
if (strcmp(buffer, "ROUTING TABLE\n") == 0) |
| 260 |
break; |
| 261 |
|
| 262 |
if (strcmp(buffer, V1HEADER) == 0) { |
| 263 |
found_header = true; |
| 264 |
continue; |
| 265 |
} |
| 266 |
|
| 267 |
/* skip the first lines until the client list section is found */ |
| 268 |
if (found_header == false) |
| 269 |
/* we can't start reading data until this string is found */ |
| 270 |
continue; |
| 271 |
|
| 272 |
int fields_num = openvpn_strsplit(buffer, fields, max_fields); |
| 273 |
if (fields_num < 4) |
| 274 |
continue; |
| 275 |
|
| 276 |
if (collect_user_count) |
| 277 |
/* If so, sum all users, ignore the individuals*/ |
| 278 |
{ |
| 279 |
sum_users += 1; |
| 280 |
} |
| 281 |
if (collect_individual_users) { |
| 282 |
if (new_naming_schema) { |
| 283 |
iostats_submit(name, /* vpn instance */ |
| 284 |
fields[0], /* "Common Name" */ |
| 285 |
atoll(fields[2]), /* "Bytes Received" */ |
| 286 |
atoll(fields[3])); /* "Bytes Sent" */ |
| 287 |
} else { |
| 288 |
iostats_submit(fields[0], /* "Common Name" */ |
| 289 |
NULL, /* unused when in multimode */ |
| 290 |
atoll(fields[2]), /* "Bytes Received" */ |
| 291 |
atoll(fields[3])); /* "Bytes Sent" */ |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if (ferror(fh)) |
| 297 |
return -1; |
| 298 |
|
| 299 |
if (found_header == false) { |
| 300 |
NOTICE("openvpn plugin: Unknown file format in instance %s, please " |
| 301 |
"report this as bug. Make sure to include " |
| 302 |
"your status file, so the plugin can " |
| 303 |
"be adapted.", |
| 304 |
name); |
| 305 |
return -1; |
| 306 |
} |
| 307 |
|
| 308 |
if (collect_user_count) |
| 309 |
numusers_submit(name, name, sum_users); |
| 310 |
|
| 311 |
return 0; |
| 312 |
} /* int multi1_read */ |
| 313 |
|
| 314 |
/* for reading status version 2 / version 3 |
| 315 |
* status file is generated by openvpn/multi.c:multi_print_status() |
| 316 |
* http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c |
| 317 |
*/ |
| 318 |
static int multi2_read(const char *name, FILE *fh, const char *delim) { |
| 319 |
char buffer[1024]; |
| 320 |
/* OpenVPN-2.4 has 11 fields of data + 2 fields for "HEADER" and "CLIENT_LIST" |
| 321 |
* So, set array size to 20 elements, to support future extensions. |
| 322 |
*/ |
| 323 |
char *fields[20]; |
| 324 |
const int max_fields = STATIC_ARRAY_SIZE(fields); |
| 325 |
long long sum_users = 0; |
| 326 |
|
| 327 |
bool found_header = false; |
| 328 |
int idx_cname = 0; |
| 329 |
int idx_bytes_recv = 0; |
| 330 |
int idx_bytes_sent = 0; |
| 331 |
int columns = 0; |
| 332 |
|
| 333 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 334 |
int fields_num = 0; |
| 335 |
|
| 336 |
char *field = fields[fields_num++] = buffer; |
| 337 |
|
| 338 |
while ((field = strstr(field, delim)) != NULL) { |
| 339 |
*field = '\0'; |
| 340 |
fields[fields_num++] = ++field; |
| 341 |
if (fields_num >= max_fields) |
| 342 |
break; |
| 343 |
} |
| 344 |
|
| 345 |
/* Try to find section header */ |
| 346 |
if (found_header == false) { |
| 347 |
if (fields_num < 2) |
| 348 |
continue; |
| 349 |
if (strcmp(fields[0], "HEADER") != 0) |
| 350 |
continue; |
| 351 |
if (strcmp(fields[1], "CLIENT_LIST") != 0) |
| 352 |
continue; |
| 353 |
|
| 354 |
for (int i = 2; i < fields_num; i++) { |
| 355 |
if (strcmp(fields[i], "Common Name") == 0) { |
| 356 |
idx_cname = i - 1; |
| 357 |
} else if (strcmp(fields[i], "Bytes Received") == 0) { |
| 358 |
idx_bytes_recv = i - 1; |
| 359 |
} else if (strcmp(fields[i], "Bytes Sent") == 0) { |
| 360 |
idx_bytes_sent = i - 1; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
DEBUG("openvpn plugin: found MULTI v2/v3 HEADER. " |
| 365 |
"Column idx: cname: %d, bytes_recv: %d, bytes_sent: %d", |
| 366 |
idx_cname, idx_bytes_recv, idx_bytes_sent); |
| 367 |
|
| 368 |
if (idx_cname == 0 || idx_bytes_recv == 0 || idx_bytes_sent == 0) |
| 369 |
break; |
| 370 |
|
| 371 |
/* Data row has 1 field ("HEADER") less than header row */ |
| 372 |
columns = fields_num - 1; |
| 373 |
|
| 374 |
found_header = true; |
| 375 |
continue; |
| 376 |
} |
| 377 |
|
| 378 |
/* Header already found. Check if the line is the section data. |
| 379 |
* If no match, then section was finished and there is no more data. |
| 380 |
* Empty section is OK too. |
| 381 |
*/ |
| 382 |
if (fields_num == 0 || strcmp(fields[0], "CLIENT_LIST") != 0) |
| 383 |
break; |
| 384 |
|
| 385 |
/* Check if the data line fields count matches header line. */ |
| 386 |
if (fields_num != columns) { |
| 387 |
ERROR("openvpn plugin: File format error in instance %s: Fields count " |
| 388 |
"mismatch.", |
| 389 |
name); |
| 390 |
return -1; |
| 391 |
} |
| 392 |
|
| 393 |
DEBUG("openvpn plugin: found MULTI v2/v3 CLIENT_LIST. " |
| 394 |
"Columns: cname: %s, bytes_recv: %s, bytes_sent: %s", |
| 395 |
fields[idx_cname], fields[idx_bytes_recv], fields[idx_bytes_sent]); |
| 396 |
|
| 397 |
if (collect_user_count) |
| 398 |
sum_users += 1; |
| 399 |
|
| 400 |
if (collect_individual_users) { |
| 401 |
if (new_naming_schema) { |
| 402 |
/* plugin inst = file name, type inst = fields[1] */ |
| 403 |
iostats_submit(name, /* vpn instance */ |
| 404 |
fields[idx_cname], /* "Common Name" */ |
| 405 |
atoll(fields[idx_bytes_recv]), /* "Bytes Received" */ |
| 406 |
atoll(fields[idx_bytes_sent])); /* "Bytes Sent" */ |
| 407 |
} else { |
| 408 |
/* plugin inst = fields[idx_cname], type inst = "" */ |
| 409 |
iostats_submit(fields[idx_cname], /* "Common Name" */ |
| 410 |
NULL, /* unused when in multimode */ |
| 411 |
atoll(fields[idx_bytes_recv]), /* "Bytes Received" */ |
| 412 |
atoll(fields[idx_bytes_sent])); /* "Bytes Sent" */ |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
if (ferror(fh)) |
| 418 |
return -1; |
| 419 |
|
| 420 |
if (found_header == false) { |
| 421 |
NOTICE("openvpn plugin: Unknown file format in instance %s, please " |
| 422 |
"report this as bug. Make sure to include " |
| 423 |
"your status file, so the plugin can " |
| 424 |
"be adapted.", |
| 425 |
name); |
| 426 |
return -1; |
| 427 |
} |
| 428 |
|
| 429 |
if (collect_user_count) { |
| 430 |
numusers_submit(name, name, sum_users); |
| 431 |
} |
| 432 |
|
| 433 |
return 0; |
| 434 |
} /* int multi2_read */ |
| 435 |
|
| 436 |
/* read callback */ |
| 437 |
static int openvpn_read(user_data_t *user_data) { |
| 438 |
char buffer[1024]; |
| 439 |
int read = 0; |
| 440 |
|
| 441 |
vpn_status_t *st = user_data->data; |
| 442 |
|
| 443 |
FILE *fh = fopen(st->file, "r"); |
| 444 |
if (fh == NULL) { |
| 445 |
WARNING("openvpn plugin: fopen(%s) failed: %s", st->file, STRERRNO); |
| 446 |
|
| 447 |
return -1; |
| 448 |
} |
| 449 |
|
| 450 |
// Try to detect file format by its first line |
| 451 |
if ((fgets(buffer, sizeof(buffer), fh)) == NULL) { |
| 452 |
WARNING("openvpn plugin: failed to get data from: %s", st->file); |
| 453 |
fclose(fh); |
| 454 |
return -1; |
| 455 |
} |
| 456 |
|
| 457 |
if (strcmp(buffer, TITLE_SINGLE) == 0) { // OpenVPN STATISTICS |
| 458 |
DEBUG("openvpn plugin: found status file SINGLE"); |
| 459 |
read = single_read(st->name, fh); |
| 460 |
} else if (strcmp(buffer, TITLE_V1) == 0) { // OpenVPN CLIENT LIST |
| 461 |
DEBUG("openvpn plugin: found status file MULTI version 1"); |
| 462 |
read = multi1_read(st->name, fh); |
| 463 |
} else if (strncmp(buffer, TITLE_V2, strlen(TITLE_V2)) == 0) { // TITLE, |
| 464 |
DEBUG("openvpn plugin: found status file MULTI version 2"); |
| 465 |
read = multi2_read(st->name, fh, ","); |
| 466 |
} else if (strncmp(buffer, TITLE_V3, strlen(TITLE_V3)) == 0) { // TITLE\t |
| 467 |
DEBUG("openvpn plugin: found status file MULTI version 3"); |
| 468 |
read = multi2_read(st->name, fh, "\t"); |
| 469 |
} else { |
| 470 |
NOTICE("openvpn plugin: %s: Unknown file format, please " |
| 471 |
"report this as bug. Make sure to include " |
| 472 |
"your status file, so the plugin can " |
| 473 |
"be adapted.", |
| 474 |
st->file); |
| 475 |
read = -1; |
| 476 |
} |
| 477 |
fclose(fh); |
| 478 |
return read; |
| 479 |
} /* int openvpn_read */ |
| 480 |
|
| 481 |
static int openvpn_config(const char *key, const char *value) { |
| 482 |
if (strcasecmp("StatusFile", key) == 0) { |
| 483 |
char callback_name[3 * DATA_MAX_NAME_LEN]; |
| 484 |
char *status_name; |
| 485 |
|
| 486 |
char *status_file = strdup(value); |
| 487 |
if (status_file == NULL) { |
| 488 |
ERROR("openvpn plugin: strdup failed: %s", STRERRNO); |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
/* it determines the file name as string starting at location filename + 1 |
| 493 |
*/ |
| 494 |
char *filename = strrchr(status_file, (int)'/'); |
| 495 |
if (filename == NULL) { |
| 496 |
/* status_file is already the file name only */ |
| 497 |
status_name = status_file; |
| 498 |
} else { |
| 499 |
/* doesn't waste memory, uses status_file starting at filename + 1 */ |
| 500 |
status_name = filename + 1; |
| 501 |
} |
| 502 |
|
| 503 |
/* create a new vpn element */ |
| 504 |
vpn_status_t *instance = calloc(1, sizeof(*instance)); |
| 505 |
if (instance == NULL) { |
| 506 |
ERROR("openvpn plugin: malloc failed: %s", STRERRNO); |
| 507 |
sfree(status_file); |
| 508 |
return 1; |
| 509 |
} |
| 510 |
instance->file = status_file; |
| 511 |
instance->name = status_name; |
| 512 |
|
| 513 |
snprintf(callback_name, sizeof(callback_name), "openvpn/%s", status_name); |
| 514 |
|
| 515 |
int status = plugin_register_complex_read( |
| 516 |
/* group = */ "openvpn", |
| 517 |
/* name = */ callback_name, |
| 518 |
/* callback = */ openvpn_read, |
| 519 |
/* interval = */ 0, |
| 520 |
&(user_data_t){ |
| 521 |
.data = instance, |
| 522 |
.free_func = openvpn_free, |
| 523 |
}); |
| 524 |
|
| 525 |
if (status == EINVAL) { |
| 526 |
ERROR("openvpn plugin: status filename \"%s\" " |
| 527 |
"already used, please choose a " |
| 528 |
"different one.", |
| 529 |
status_name); |
| 530 |
return -1; |
| 531 |
} |
| 532 |
|
| 533 |
DEBUG("openvpn plugin: status file \"%s\" added", instance->file); |
| 534 |
} /* if (strcasecmp ("StatusFile", key) == 0) */ |
| 535 |
else if ((strcasecmp("CollectCompression", key) == 0) || |
| 536 |
(strcasecmp("Compression", key) == 0)) /* old, deprecated name */ |
| 537 |
{ |
| 538 |
if (IS_FALSE(value)) |
| 539 |
collect_compression = false; |
| 540 |
else |
| 541 |
collect_compression = true; |
| 542 |
} /* if (strcasecmp ("CollectCompression", key) == 0) */ |
| 543 |
else if (strcasecmp("ImprovedNamingSchema", key) == 0) { |
| 544 |
if (IS_TRUE(value)) { |
| 545 |
DEBUG("openvpn plugin: using the new naming schema"); |
| 546 |
new_naming_schema = true; |
| 547 |
} else { |
| 548 |
new_naming_schema = false; |
| 549 |
} |
| 550 |
} /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */ |
| 551 |
else if (strcasecmp("CollectUserCount", key) == 0) { |
| 552 |
if (IS_TRUE(value)) |
| 553 |
collect_user_count = true; |
| 554 |
else |
| 555 |
collect_user_count = false; |
| 556 |
} /* if (strcasecmp("CollectUserCount", key) == 0) */ |
| 557 |
else if (strcasecmp("CollectIndividualUsers", key) == 0) { |
| 558 |
if (IS_FALSE(value)) |
| 559 |
collect_individual_users = false; |
| 560 |
else |
| 561 |
collect_individual_users = true; |
| 562 |
} /* if (strcasecmp("CollectIndividualUsers", key) == 0) */ |
| 563 |
else { |
| 564 |
return -1; |
| 565 |
} |
| 566 |
|
| 567 |
return 0; |
| 568 |
} /* int openvpn_config */ |
| 569 |
|
| 570 |
static int openvpn_init(void) { |
| 571 |
if (!collect_individual_users && !collect_compression && |
| 572 |
!collect_user_count) { |
| 573 |
WARNING("OpenVPN plugin: Neither `CollectIndividualUsers', " |
| 574 |
"`CollectCompression', nor `CollectUserCount' is true. There's no " |
| 575 |
"data left to collect."); |
| 576 |
return -1; |
| 577 |
} |
| 578 |
|
| 579 |
return 0; |
| 580 |
} /* int openvpn_init */ |
| 581 |
|
| 582 |
void module_register(void) { |
| 583 |
plugin_register_config("openvpn", openvpn_config, config_keys, |
| 584 |
config_keys_num); |
| 585 |
plugin_register_init("openvpn", openvpn_init); |
| 586 |
} /* void module_register */ |