ifctrstat: Initial structure for new applet

This commit is contained in:
A. Wilcox 2020-08-12 17:38:15 -05:00
parent 36f0930c27
commit 05e0ac139b
No known key found for this signature in database
GPG key ID: CB29CB51922B9D14
5 changed files with 163 additions and 0 deletions

1
.gitignore vendored
View file

@ -12,4 +12,5 @@ ifupdown
ifquery
ifup
ifdown
ifctrstat
*.lock

View file

@ -62,6 +62,13 @@ MULTICALL_${CONFIG_IFQUERY}_OBJ += ${IFQUERY_SRC:.c=.o}
CMDS_${CONFIG_IFQUERY} += ifquery
CPPFLAGS_${CONFIG_IFQUERY} += -DCONFIG_IFQUERY
# enable ifctrstat applet (+9 KB)
CONFIG_IFCTRSTAT ?= Y
IFCTRSTAT_SRC = cmd/ifctrstat.c cmd/ifctrstat_linux.c
MULTICALL_${CONFIG_IFCTRSTAT}_OBJ += ${IFCTRSTAT_SRC:.c=.o}
CMDS_${CONFIG_IFCTRSTAT} += ifctrstat
CPPFLAGS_${CONFIG_IFCTRSTAT} += -DCONFIG_IFCTRSTAT
MULTICALL_OBJ += ${MULTICALL_Y_OBJ}
CMDS += ${CMDS_Y}
CPPFLAGS += ${CPPFLAGS_Y}

111
cmd/ifctrstat.c Normal file
View file

@ -0,0 +1,111 @@
/*
* cmd/ifctrstat.c
* Purpose: Display statistics about interfaces on the system
*
* Copyright (c) 2020 Adélie Software in the Public Benefit, Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "libifupdown/libifupdown.h"
#include "cmd/multicall.h"
extern const char *avail_counters[];
extern int avail_counters_count;
extern const char *read_counter(const char *interface, const char *counter);
static bool
counter_is_valid(const char *candidate)
{
for (int i = 0; i < avail_counters_count; i++)
{
if (strcasecmp(avail_counters[i], candidate) == 0)
return true;
}
return false;
}
void
ifstats_list_counters(int short_opt, const struct if_option *opt,
const char *opt_arg, const struct if_applet *applet)
{
(void) short_opt;
(void) opt;
(void) opt_arg;
(void) applet;
for (int i = 0; i < avail_counters_count; i++) {
fprintf(stdout, "%s\n", avail_counters[i]);
}
exit(EXIT_SUCCESS);
}
int
ifstats_main(int argc, char *argv[])
{
if (optind >= argc)
generic_usage(self_applet, EXIT_FAILURE);
int idx = optind;
if (argc - idx < 2)
{
fprintf(stderr, "%s: interface and counter(s) required\n",
argv0);
return EXIT_FAILURE;
}
const char *iface = argv[idx++];
for (; idx < argc; idx++)
{
if (!counter_is_valid(argv[idx]))
{
fprintf(stderr, "%s: counter %s is not valid or not "
"available\n", argv0, argv[idx]);
return EXIT_FAILURE;
}
errno = 0;
const char *res = read_counter(iface, argv[idx]);
if (!res)
{
fprintf(stderr, "%s: could not determine value of "
"%s for interface %s: %s\n", argv0,
argv[idx], iface, strerror(errno));
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
static struct if_option local_options[] = {
{'L', "list", NULL, "List available counters", false, ifstats_list_counters}
};
static struct if_option_group local_option_group = {
.desc = "Program-specific options",
.group_size = ARRAY_SIZE(local_options),
.group = local_options
};
struct if_applet ifctrstat_applet = {
.name = "ifctrstat",
.desc = "Display statistics about an interface",
.main = ifstats_main,
.usage = "ifctrstat [options] <interface> <counter>\n ifctrstat [options] --list\n",
.groups = { &global_option_group, &local_option_group, NULL }
};

37
cmd/ifctrstat_linux.c Normal file
View file

@ -0,0 +1,37 @@
/*
* cmd/ifctrstat_linux.c
* Purpose: Implement ifstats system-specific routines for Linux
*
* Copyright (c) 2020 Adélie Software in the Public Benefit, Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <errno.h>
#include <stdio.h>
const char *avail_counters[] = {
"rx.octets",
"rx.packets",
"rx.discard",
"rx.errors",
"tx.octets",
"tx.packets",
"tx.discard",
"tx.errors"
};
size_t avail_counters_count = sizeof(avail_counters) / sizeof(*avail_counters);
const char *
read_counter(const char *interface, const char *counter)
{
errno = ENOSYS;
return NULL;
}

View file

@ -32,10 +32,17 @@ extern struct if_applet ifup_applet;
extern struct if_applet ifdown_applet;
#endif
#ifdef CONFIG_IFCTRSTAT
extern struct if_applet ifctrstat_applet;
#endif
struct if_applet ifupdown_applet;
const struct if_applet *self_applet = NULL;
struct if_applet *applet_table[] = {
#ifdef CONFIG_IFCTRSTAT
&ifctrstat_applet,
#endif
#ifdef CONFIG_IFUPDOWN
&ifdown_applet,
#endif