From: Florian Forster Date: Fri, 25 Sep 2009 09:49:45 +0000 (+0200) Subject: openvpn plugin: multi1_read: Replace a hard to read for-loop with a while-loop. X-Git-Tag: collectd-4.9.0~37^2~8 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=52d020c880a5cf741d00ea3675966e7e0e9d2f36;p=collectd.git openvpn plugin: multi1_read: Replace a hard to read for-loop with a while-loop. --- diff --git a/src/openvpn.c b/src/openvpn.c index 62b022a5..647c9e13 100644 --- a/src/openvpn.c +++ b/src/openvpn.c @@ -231,30 +231,35 @@ static int multi1_read (char *name, FILE *fh) { char buffer[1024]; char *fields[10]; - const int max_fields = STATIC_ARRAY_SIZE (fields); - int fields_num, read = 0, skip = 1; + int fields_num, read = 0, found_header = 0; /* read the file until the "ROUTING TABLE" line is found (no more info after) */ - for ( ; strcmp (buffer, "ROUTING TABLE\n"); fgets (buffer, sizeof (buffer), fh)) + while (fgets (buffer, sizeof (buffer), fh) != NULL) { - if (skip) /* skip the first lines until the client list section is found */ + if (strcmp (buffer, "ROUTING TABLE\n") == 0) + break; + + if (strcmp (buffer, V1STRING) == 0) { + found_header = 1; + continue; + } + + /* skip the first lines until the client list section is found */ + if (found_header == 0) /* we can't start reading data until this string is found */ - if (strcmp (buffer, V1STRING) == 0) - skip = 0; + continue; + fields_num = openvpn_strsplit (buffer, + fields, STATIC_ARRAY_SIZE (fields)); + if (fields_num < 4) continue; - } - else - { - fields_num = openvpn_strsplit (buffer, fields, max_fields); - iostats_submit (name, /* vpn instance */ - fields[0], /* "Common Name" */ - atoll (fields[2]), /* "Bytes Received" */ - atoll (fields[3])); /* "Bytes Sent" */ - read = 1; - } + iostats_submit (name, /* vpn instance */ + fields[0], /* "Common Name" */ + atoll (fields[2]), /* "Bytes Received" */ + atoll (fields[3])); /* "Bytes Sent" */ + read = 1; } return (read);