-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetmonn.c
57 lines (45 loc) · 1.17 KB
/
netmonn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "format_bytes.h"
FILE *pNetDev() {
FILE *pNet = fopen("/proc/net/dev", "r");
if (pNet == NULL) {
perror("fopen");
return NULL;
}
return pNet;
}
void read_network_data(FILE *pNet) {
char buffer[512];
fgets(buffer, sizeof(buffer), pNet);
fgets(buffer, sizeof(buffer), pNet);
while (fgets(buffer, sizeof(buffer), pNet)) {
char iface[32], rx_formatted[20], tx_formatted[20];
unsigned long rx_bytes, tx_bytes;
sscanf(buffer, " %31[^:]: %lu %*s %*s %*s %*s %*s %*s %*s %lu", iface,
&rx_bytes, &tx_bytes);
format_bytes(rx_bytes, rx_formatted);
format_bytes(tx_bytes, tx_formatted);
printf("%-8s | %-12s | %-12s\n", iface, rx_formatted, tx_formatted);
}
}
void display_network_usage() {
while (1) {
FILE *pNet = pNetDev();
if (pNet == NULL) {
return;
}
system("clear");
printf("%-8s | %-12s | %-12s\n", "Iface", "Rx Bytes", "Tx Bytes");
printf("--------------------------------------\n");
read_network_data(pNet);
fclose(pNet);
sleep(1);
}
}
int main() {
display_network_usage();
return 0;
}