mirror of
https://github.com/collectd/collectd.git
synced 2026-02-09 04:09:15 +08:00
openvpn: Changes after review, clang-formatting
This commit is contained in:
117
src/openvpn.c
117
src/openvpn.c
@@ -68,8 +68,8 @@
|
||||
**/
|
||||
|
||||
#define TITLE_SINGLE "OpenVPN STATISTICS\n"
|
||||
#define TITLE_V1 "OpenVPN CLIENT LIST\n"
|
||||
#define TITLE_V2 "TITLE"
|
||||
#define TITLE_V1 "OpenVPN CLIENT LIST\n"
|
||||
#define TITLE_V2 "TITLE"
|
||||
|
||||
#define V1HEADER \
|
||||
"Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
|
||||
@@ -94,13 +94,10 @@ static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
|
||||
/* Helper function
|
||||
* copy-n-pasted from common.c - changed delim to ",\t" */
|
||||
static int openvpn_strsplit(char *string, char **fields, size_t size) {
|
||||
size_t i;
|
||||
char *ptr;
|
||||
char *saveptr;
|
||||
size_t i = 0;
|
||||
char *ptr = string;
|
||||
char *saveptr = NULL;
|
||||
|
||||
i = 0;
|
||||
ptr = string;
|
||||
saveptr = NULL;
|
||||
while ((fields[i] = strtok_r(ptr, ",\t", &saveptr)) != NULL) {
|
||||
ptr = NULL;
|
||||
i++;
|
||||
@@ -114,7 +111,7 @@ static int openvpn_strsplit(char *string, char **fields, size_t size) {
|
||||
|
||||
static void openvpn_free(void *arg) {
|
||||
vpn_status_t *st = arg;
|
||||
|
||||
|
||||
sfree(st->file);
|
||||
sfree(st);
|
||||
} /* void openvpn_free */
|
||||
@@ -187,25 +184,14 @@ static int single_read(const char *name, FILE *fh) {
|
||||
char buffer[1024];
|
||||
char *fields[4];
|
||||
const int max_fields = STATIC_ARRAY_SIZE(fields);
|
||||
int fields_num;
|
||||
|
||||
derive_t link_rx, link_tx;
|
||||
derive_t tun_rx, tun_tx;
|
||||
derive_t pre_compress, post_compress;
|
||||
derive_t pre_decompress, post_decompress;
|
||||
derive_t overhead_rx, overhead_tx;
|
||||
|
||||
link_rx = 0;
|
||||
link_tx = 0;
|
||||
tun_rx = 0;
|
||||
tun_tx = 0;
|
||||
pre_compress = 0;
|
||||
post_compress = 0;
|
||||
pre_decompress = 0;
|
||||
post_decompress = 0;
|
||||
derive_t link_rx = 0, link_tx = 0;
|
||||
derive_t tun_rx = 0, tun_tx = 0;
|
||||
derive_t pre_compress = 0, post_compress = 0;
|
||||
derive_t pre_decompress = 0, post_decompress = 0;
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), fh) != NULL) {
|
||||
fields_num = openvpn_strsplit(buffer, fields, max_fields);
|
||||
int fields_num = openvpn_strsplit(buffer, fields, max_fields);
|
||||
|
||||
/* status file is generated by openvpn/sig.c:print_status()
|
||||
* http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
|
||||
@@ -241,8 +227,9 @@ static int single_read(const char *name, FILE *fh) {
|
||||
iostats_submit(name, "traffic", link_rx, link_tx);
|
||||
|
||||
/* we need to force this order to avoid negative values with these unsigned */
|
||||
overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
|
||||
overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
|
||||
derive_t overhead_rx =
|
||||
(((link_rx - pre_decompress) + post_decompress) - tun_rx);
|
||||
derive_t overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
|
||||
|
||||
iostats_submit(name, "overhead", overhead_rx, overhead_tx);
|
||||
|
||||
@@ -258,8 +245,9 @@ static int single_read(const char *name, FILE *fh) {
|
||||
static int multi1_read(const char *name, FILE *fh) {
|
||||
char buffer[1024];
|
||||
char *fields[10];
|
||||
int fields_num, found_header = 0;
|
||||
const int max_fields = STATIC_ARRAY_SIZE(fields);
|
||||
long long sum_users = 0;
|
||||
_Bool found_header = 0;
|
||||
|
||||
/* read the file until the "ROUTING TABLE" line is found (no more info after)
|
||||
*/
|
||||
@@ -277,7 +265,7 @@ static int multi1_read(const char *name, FILE *fh) {
|
||||
/* we can't start reading data until this string is found */
|
||||
continue;
|
||||
|
||||
fields_num = openvpn_strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
|
||||
int fields_num = openvpn_strsplit(buffer, fields, max_fields);
|
||||
if (fields_num < 4)
|
||||
continue;
|
||||
|
||||
@@ -330,17 +318,16 @@ static int multi2_read(const char *name, FILE *fh) {
|
||||
*/
|
||||
char *fields[20];
|
||||
const int max_fields = STATIC_ARRAY_SIZE(fields);
|
||||
int fields_num;
|
||||
long long sum_users = 0;
|
||||
|
||||
int found_header = 0;
|
||||
|
||||
_Bool found_header = 0;
|
||||
int idx_cname = 0;
|
||||
int idx_bytes_recv = 0;
|
||||
int idx_bytes_sent = 0;
|
||||
int columns = 0;
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), fh) != NULL) {
|
||||
fields_num = openvpn_strsplit(buffer, fields, max_fields);
|
||||
int fields_num = openvpn_strsplit(buffer, fields, max_fields);
|
||||
|
||||
/* Try to find section header */
|
||||
if (found_header == 0) {
|
||||
@@ -354,11 +341,9 @@ static int multi2_read(const char *name, FILE *fh) {
|
||||
for (int i = 2; i < fields_num; i++) {
|
||||
if (strcmp(fields[i], "Common Name") == 0) {
|
||||
idx_cname = i - 1;
|
||||
}
|
||||
else if (strcmp(fields[i], "Bytes Received") == 0) {
|
||||
} else if (strcmp(fields[i], "Bytes Received") == 0) {
|
||||
idx_bytes_recv = i - 1;
|
||||
}
|
||||
else if (strcmp(fields[i], "Bytes Sent") == 0) {
|
||||
} else if (strcmp(fields[i], "Bytes Sent") == 0) {
|
||||
idx_bytes_sent = i - 1;
|
||||
}
|
||||
}
|
||||
@@ -387,7 +372,8 @@ static int multi2_read(const char *name, FILE *fh) {
|
||||
/* Check if the data line fields count matches header line. */
|
||||
if (fields_num != columns) {
|
||||
ERROR("openvpn plugin: File format error in instance %s: Fields count "
|
||||
"mismatch.", name);
|
||||
"mismatch.",
|
||||
name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -407,7 +393,7 @@ static int multi2_read(const char *name, FILE *fh) {
|
||||
atoll(fields[idx_bytes_sent])); /* "Bytes Sent" */
|
||||
} else {
|
||||
/* plugin inst = fields[idx_cname], type inst = "" */
|
||||
iostats_submit(fields[idx_cname], /* "Common Name" */
|
||||
iostats_submit(fields[idx_cname], /* "Common Name" */
|
||||
NULL, /* unused when in multimode */
|
||||
atoll(fields[idx_bytes_recv]), /* "Bytes Received" */
|
||||
atoll(fields[idx_bytes_sent])); /* "Bytes Sent" */
|
||||
@@ -436,14 +422,12 @@ static int multi2_read(const char *name, FILE *fh) {
|
||||
|
||||
/* read callback */
|
||||
static int openvpn_read(user_data_t *user_data) {
|
||||
FILE *fh;
|
||||
char buffer[1024];
|
||||
int read = 0;
|
||||
|
||||
vpn_status_t *st;
|
||||
st = user_data->data;
|
||||
vpn_status_t *st = user_data->data;
|
||||
|
||||
fh = fopen(st->file, "r");
|
||||
FILE *fh = fopen(st->file, "r");
|
||||
if (fh == NULL) {
|
||||
char errbuf[1024];
|
||||
WARNING("openvpn plugin: fopen(%s) failed: %s", st->file,
|
||||
@@ -452,31 +436,28 @@ static int openvpn_read(user_data_t *user_data) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Try to detect file format by its first line
|
||||
// Try to detect file format by its first line
|
||||
if ((fgets(buffer, sizeof(buffer), fh)) == NULL) {
|
||||
WARNING("openvpn plugin: failed to get data from: %s", st->file);
|
||||
fclose(fh);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp(buffer, TITLE_SINGLE) == 0) { //OpenVPN STATISTICS
|
||||
if (strcmp(buffer, TITLE_SINGLE) == 0) { // OpenVPN STATISTICS
|
||||
DEBUG("openvpn plugin: found status file SINGLE");
|
||||
read = single_read(st->name, fh);
|
||||
}
|
||||
else if (strcmp(buffer, TITLE_V1) == 0) { //OpenVPN CLIENT LIST
|
||||
} else if (strcmp(buffer, TITLE_V1) == 0) { // OpenVPN CLIENT LIST
|
||||
DEBUG("openvpn plugin: found status file MULTI version 1");
|
||||
read = multi1_read(st->name, fh);
|
||||
}
|
||||
else if (strncmp(buffer, TITLE_V2, strlen(TITLE_V2)) == 0) { //TITLE
|
||||
} else if (strncmp(buffer, TITLE_V2, strlen(TITLE_V2)) == 0) { // TITLE
|
||||
DEBUG("openvpn plugin: found status file MULTI version 2/3");
|
||||
read = multi2_read(st->name, fh);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
NOTICE("openvpn plugin: %s: Unknown file format, please "
|
||||
"report this as bug. Make sure to include "
|
||||
"your status file, so the plugin can "
|
||||
"be adapted. BUF %s",
|
||||
st->file, buffer);
|
||||
"be adapted.",
|
||||
st->file);
|
||||
read = -1;
|
||||
}
|
||||
fclose(fh);
|
||||
@@ -486,20 +467,19 @@ static int openvpn_read(user_data_t *user_data) {
|
||||
static int openvpn_config(const char *key, const char *value) {
|
||||
if (strcasecmp("StatusFile", key) == 0) {
|
||||
char callback_name[3 * DATA_MAX_NAME_LEN];
|
||||
char *status_file, *status_name, *filename;
|
||||
vpn_status_t *instance;
|
||||
char *status_name;
|
||||
|
||||
status_file = sstrdup(value);
|
||||
char *status_file = strdup(value);
|
||||
if (status_file == NULL) {
|
||||
char errbuf[1024];
|
||||
ERROR("openvpn plugin: sstrdup failed: %s",
|
||||
ERROR("openvpn plugin: strdup failed: %s",
|
||||
sstrerror(errno, errbuf, sizeof(errbuf)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* it determines the file name as string starting at location filename + 1
|
||||
*/
|
||||
filename = strrchr(status_file, (int)'/');
|
||||
char *filename = strrchr(status_file, (int)'/');
|
||||
if (filename == NULL) {
|
||||
/* status_file is already the file name only */
|
||||
status_name = status_file;
|
||||
@@ -509,7 +489,7 @@ static int openvpn_config(const char *key, const char *value) {
|
||||
}
|
||||
|
||||
/* create a new vpn element */
|
||||
instance = malloc(sizeof(*instance));
|
||||
vpn_status_t *instance = calloc(1, sizeof(*instance));
|
||||
if (instance == NULL) {
|
||||
char errbuf[1024];
|
||||
ERROR("openvpn plugin: malloc failed: %s",
|
||||
@@ -520,17 +500,16 @@ static int openvpn_config(const char *key, const char *value) {
|
||||
instance->file = status_file;
|
||||
instance->name = status_name;
|
||||
|
||||
int status;
|
||||
snprintf(callback_name, sizeof(callback_name), "openvpn/%s", status_name);
|
||||
|
||||
ssnprintf(callback_name, sizeof(callback_name), "openvpn/%s", status_name);
|
||||
|
||||
status = plugin_register_complex_read(
|
||||
/* group = */ "openvpn",
|
||||
/* name = */ callback_name,
|
||||
/* callback = */ openvpn_read,
|
||||
/* interval = */ 0, &(user_data_t){
|
||||
.data = instance, .free_func = openvpn_free,
|
||||
});
|
||||
int status = plugin_register_complex_read(
|
||||
/* group = */ "openvpn",
|
||||
/* name = */ callback_name,
|
||||
/* callback = */ openvpn_read,
|
||||
/* interval = */ 0,
|
||||
&(user_data_t){
|
||||
.data = instance, .free_func = openvpn_free,
|
||||
});
|
||||
|
||||
if (status == EINVAL) {
|
||||
WARNING("openvpn plugin: status filename \"%s\" "
|
||||
|
||||
Reference in New Issue
Block a user