Skip to content
Snippets Groups Projects
Commit 483bcac7 authored by Robin Obůrka's avatar Robin Obůrka
Browse files

[nethist] Experimental implementation of taking snapshots

parent 66342722
Branches
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#define BUFFSIZE 512
......@@ -11,7 +13,7 @@
#define MAX_LEN_INTERFACE_LIST 20
struct network_configuration {
char interfaces[MAX_LEN_INTERFACE_LIST];
char interfaces[MAX_LEN_INTERFACE_LIST][MAX_LEN_INTERFACE_NAME];
size_t interfaces_cnt;
};
......@@ -21,8 +23,13 @@ typedef struct {
configuration g_config;
struct newtwork_interface_data {
unsigned long long r_bytes;
unsigned long long t_bytes;
};
struct network_snapshot {
unsigned long long interface[MAX_LEN_INTERFACE_LIST];
struct newtwork_interface_data interface[MAX_LEN_INTERFACE_LIST];
};
typedef struct {
......@@ -30,11 +37,21 @@ typedef struct {
//struct cpu_snapshot cpu; //etc. etc.
} snapshot;
static void network_init_snapshot(struct network_snapshot *snapshot) {
for (size_t i = 0; i < MAX_LEN_INTERFACE_LIST; i++) {
snapshot->interface[i] = 0;
snapshot->interface[i].r_bytes = 0;
snapshot->interface[i].t_bytes = 0;
}
}
static size_t network_get_interface_number(char *name) {
for (size_t i = 0; i < g_config.network.interfaces_cnt; i++) {
if (strcmp(name, g_config.network.interfaces[i]) == 0) {
return i;
}
}
return g_config.network.interfaces_cnt;
}
static bool network_init_global() {
......@@ -52,7 +69,7 @@ static bool network_init_global() {
g_config.network.interfaces_cnt = 0;
while (fgets(buffer, BUFFSIZE, f) != NULL) {
sscanf(buffer, "%20s", &g_config.network.interfaces[g_config.network.interfaces_cnt++]);
sscanf(buffer, "%20s", g_config.network.interfaces[g_config.network.interfaces_cnt++]);
}
fclose(f);
......@@ -72,11 +89,27 @@ static bool network_take_snapshot(struct network_snapshot *snap) {
}
}
char name[MAX_LEN_INTERFACE_NAME];
size_t pos;
unsigned long long r_bytes, t_bytes, dummy;
while (fgets(buffer, BUFFSIZE, f) != NULL) {
sscanf(buffer, "%20s%llu%llu%llu%llu%llu%llu%llu%llu%llu%llu%llu%llu%llu%llu%llu%llu",
name, &r_bytes,
&dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy,
&t_bytes,
&dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy
);
pos = network_get_interface_number(name);
if (pos == g_config.network.interfaces_cnt) {
printf("INTERRUPT\n");
exit(1);
}
snap->interface[pos].r_bytes = r_bytes;
snap->interface[pos].t_bytes = t_bytes;
}
fclose(f);
return true;
......@@ -94,6 +127,22 @@ static void take_snapshot(snapshot *snap) {
network_take_snapshot(&(snap->network));
}
static void network_print_history(FILE *stream, int time, struct network_snapshot *snap) {
for (size_t i = 0; i < g_config.network.interfaces_cnt; i++) {
fprintf(stream, "%d,%s,%llu,%llu\n", time, g_config.network.interfaces[i], snap->interface[i].r_bytes, snap->interface[i].t_bytes);
}
}
static void print_history(FILE *stream, snapshot *snapshots, size_t from) {
size_t pos = from;
for (int i = 0; i < SNAPSHOT_COUNT; i++) {
network_print_history(stream, i, &(snapshots[pos].network));
pos = (pos + 1) % SNAPSHOT_COUNT;
}
}
int main(int argc, char **argv) {
(void) argc; (void) argv;
......@@ -112,8 +161,11 @@ int main(int argc, char **argv) {
take_snapshot(&(snapshots[write_to]));
print_history(stdout, snapshots, write_to);
write_to++;
usleep(SLEEP_TIME_USEC);
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment