**/
#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"
/* 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++;
static void openvpn_free(void *arg) {
vpn_status_t *st = arg;
-
+
sfree(st->file);
sfree(st);
} /* void openvpn_free */
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
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);
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)
*/
/* 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;
*/
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) {
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;
}
}
/* 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;
}
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" */
/* 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,
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);
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;
}
/* 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",
instance->file = status_file;
instance->name = status_name;
- int status;
-
- ssnprintf(callback_name, sizeof(callback_name), "openvpn/%s", status_name);
+ snprintf(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\" "