ifctrstat: Add ability to output counter values

This commit is contained in:
A. Wilcox 2020-08-14 10:49:45 -05:00
parent 95d943ee44
commit 6106164d7c
No known key found for this signature in database
GPG key ID: CB29CB51922B9D14
3 changed files with 111 additions and 17 deletions

View file

@ -14,7 +14,9 @@
*/
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "multicall.h"
const char *avail_counters[] = {
@ -30,9 +32,69 @@ const char *avail_counters[] = {
size_t avail_counters_count = ARRAY_SIZE(avail_counters);
const char *
char *
read_counter(const char *interface, const char *counter)
{
errno = ENOSYS;
return NULL;
FILE *fp;
const char *path;
char full_path[PATH_MAX];
char buffer[1024];
size_t in_count;
errno = 0;
if (strcasecmp(counter, "rx.octets") == 0)
{
path = "rx_bytes";
} else if (strcasecmp(counter, "rx.packets") == 0) {
path = "rx_packets";
} else if (strcasecmp(counter, "rx.discard") == 0) {
path = "rx_dropped";
} else if (strcasecmp(counter, "rx.errors") == 0) {
path = "rx_errors";
} else if (strcasecmp(counter, "tx.octets") == 0) {
path = "tx_bytes";
} else if (strcasecmp(counter, "tx.packets") == 0) {
path = "tx_packets";
} else if (strcasecmp(counter, "tx.discard") == 0) {
path = "tx_dropped";
} else if (strcasecmp(counter, "tx.errors") == 0) {
path = "tx_errors";
} else {
errno = ENOSYS;
return NULL;
}
if (snprintf(full_path, PATH_MAX, "/sys/class/net/%s/statistics/%s", interface, path) > PATH_MAX)
{
errno = ENOMEM;
return NULL;
}
fp = fopen(full_path, "r");
if (!fp)
{
return NULL;
}
in_count = fread(buffer, 1, sizeof(buffer), fp);
if (in_count == sizeof(buffer))
{
errno = ENOMEM;
fclose(fp);
return NULL;
}
if (ferror(fp))
{
return NULL;
}
fclose(fp);
/* take away the \n, we add our own */
buffer[in_count - 1] = '\0';
return strdup(buffer);
}