made it parse /proc/net/dev correctly, even for interfaces with huge amounts of data.
made it parse /proc/net/dev correctly, even for interfaces with huge amounts of data.
box@home root committed
Jun 19, 2013 at 00:58 UTC
7c9eed9b25e5c913de1dde70d76756e0c35ef500
1 file changed
+20
-11
netdata.c
+20
-11
@@ -11,6 +11,7 @@
11
#define SAVE_PATH "/tmp"
12
13
// configuration
14
+#define MAX_LINE 4096
15
#define MAX_IFACE_NAME 1024
16
char save_path[FILENAME_MAX+1] = SAVE_PATH;
17
int update_every = UPDATE_EVERY;
@@ -71,7 +72,8 @@ void update_iface_history(char *name, unsigned long long rbytes, unsigned long l
72
73
void save_stats() {
74
struct iface_stats *iface = NULL;
74
-
75
+ int r;
76
+
77
for(iface = interfaces; iface != NULL; iface = iface->next) {
78
char tmp[FILENAME_MAX+1];
79
char filename[FILENAME_MAX+1];
@@ -128,12 +130,13 @@ void save_stats() {
130
fprintf(fp, " ]\n}\n");
131
fclose(fp);
132
unlink(filename);
131
- link(tmp, filename);
133
+ r = link(tmp, filename);
134
unlink(tmp);
135
}
136
}
137
138
int main(int argc, char **argv) {
139
+ char buffer[MAX_LINE+1] = "";
140
char iface[MAX_IFACE_NAME + 1] = "";
141
unsigned long long rbytes, rpackets, rerrors, rdrops, rfifo, rframe, rcompressed, rmulticast;
142
unsigned long long tbytes, tpackets, terrors, tdrops, tfifo, tcollisions, tcarrier, tcompressed;
@@ -200,6 +203,7 @@ int main(int argc, char **argv) {
203
for(;1;) {
204
FILE *fp = fopen("/proc/net/dev", "r");
205
int r;
206
+ char *p;
207
208
if(!fp) {
209
perror("/proc/net/dev");
@@ -208,24 +212,29 @@ int main(int argc, char **argv) {
212
}
213
214
// skip the first two lines
211
- fgets(iface, MAX_IFACE_NAME, fp);
212
- fgets(iface, MAX_IFACE_NAME, fp);
213
-
215
+ p = fgets(buffer, MAX_LINE, fp);
216
+ p = fgets(buffer, MAX_LINE, fp);
217
+
218
+ printf("ok\n");
219
// read the rest of the lines
220
for(;1;) {
216
- r = fscanf(fp, "%s\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\n",
221
+ char *c;
222
+ p = fgets(buffer, MAX_LINE, fp);
223
+ if(!p) break;
224
+
225
+ c = strchr(buffer, ':');
226
+ if(c) *c = '\t';
227
+
228
+ printf("%s\n", buffer);
229
+ r = sscanf(buffer, "%s\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\t%llu\n",
230
iface,
231
&rbytes, &rpackets, &rerrors, &rdrops, &rfifo, &rframe, &rcompressed, &rmulticast,
232
&tbytes, &tpackets, &terrors, &tdrops, &tfifo, &tcollisions, &tcarrier, &tcompressed);
233
if(r == EOF) break;
221
- if(r != 17) printf("Cannot read line\n");
234
+ if(r != 17) printf("Cannot read line. Expected 17 params, read %d\n", r);
235
else {
236
// printf("Read %d items\n", r);
237
225
- // remove the ':' from the end of the name
226
- int l = strlen(iface);
227
- if(l > 0) iface[l-1] = '\0';
228
-
238
// update our data
239
update_iface_history(iface, rbytes, tbytes);
240
}