Update stats files
This commit is contained in:
parent
e553569469
commit
ee6316c41c
137
mib-tc-stats.c
137
mib-tc-stats.c
|
@ -6,98 +6,75 @@
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <netlink/route/class.h>
|
#include <netlink/route/class.h>
|
||||||
#include <netlink/route/link.h>
|
#include <netlink/route/link.h>
|
||||||
|
#include <netlink/cache-api.h>
|
||||||
|
#include <netlink/object.h>
|
||||||
|
|
||||||
static const char *mib_prefix = ".1.3.6.1.4.1.27934.2.2";
|
static struct nl_cache *link_cache, *class_cache;
|
||||||
static char *prog;
|
static struct rtnl_link *eth;
|
||||||
static int found;
|
static int ifindex;
|
||||||
|
|
||||||
static struct mib {
|
struct class_info {
|
||||||
char *mib_suffix;
|
|
||||||
char *name;
|
char *name;
|
||||||
char *id;
|
char *id;
|
||||||
} mibv[] = {
|
|
||||||
{ "1", "default", "01:02" },
|
|
||||||
{ "2", "orion", "01:03" },
|
|
||||||
{ "3", "campus", "01:04" },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int mibc = sizeof(mibv)/sizeof(*mibv);
|
struct class_info cogent_class = { "cogent", "01:02", };
|
||||||
|
struct class_info orion_class = { "orion", "01:03", };
|
||||||
static int mib_match(const char *arg, const char *mib_suffix) {
|
struct class_info campus_class = { "campus", "01:04", };
|
||||||
if (strlen(arg) < strlen(mib_prefix) + 1)
|
|
||||||
return 0;
|
|
||||||
if (strncmp(arg, mib_prefix, strlen(mib_prefix)))
|
|
||||||
return 0;
|
|
||||||
return !strcmp(arg + strlen(mib_prefix) + 1, mib_suffix);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int mib_find(const char *arg) {
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < mibc; i++) {
|
|
||||||
if (mib_match(arg, mibv[i].mib_suffix))
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct nl_handle *nl_handle;
|
static struct nl_handle *nl_handle;
|
||||||
|
|
||||||
static void dump_class_stats(struct nl_object *obj, void *arg)
|
|
||||||
{
|
|
||||||
struct mib *mib = arg;
|
|
||||||
struct rtnl_class *class = (struct rtnl_class *)obj;
|
|
||||||
uint32_t handle = rtnl_class_get_handle(class);
|
|
||||||
// uint64_t rate = rtnl_class_get_stat(class, RTNL_TC_RATE_BPS);
|
|
||||||
uint64_t bytes = rtnl_class_get_stat(class, RTNL_TC_BYTES);
|
|
||||||
char id[32];
|
|
||||||
|
|
||||||
rtnl_tc_handle2str(handle, id, sizeof(id));
|
|
||||||
|
|
||||||
if (strcmp(id, mib->id))
|
|
||||||
return;
|
|
||||||
|
|
||||||
fprintf(stdout, "%s.%s\ncounter\n%ld\n", mib_prefix, mib->mib_suffix, bytes);
|
|
||||||
found = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void die(const char *message) {
|
void die(const char *message) {
|
||||||
fprintf(stderr, "fatal: %s\n", message);
|
fprintf(stderr, "fatal: %s\n", message);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void usage(void) {
|
static void match_obj(struct nl_object *obj, void *arg) {
|
||||||
fprintf(stderr, "usage: %s [-g | -n] %s[.X]\n", prog, mib_prefix);
|
struct nl_object *needle = *(struct nl_object **)arg;
|
||||||
exit(2);
|
struct nl_object **ret = (struct nl_object **)arg + 1;
|
||||||
|
|
||||||
|
if (!*ret && nl_object_identical(obj, needle)) {
|
||||||
|
nl_object_get(obj);
|
||||||
|
*ret = obj;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
static struct rtnl_class *get_class_by_id(char *id, int ifindex) {
|
||||||
struct nl_cache *link_cache, *class_cache;
|
uint32_t handle;
|
||||||
struct rtnl_link *eth;
|
struct rtnl_class *needle;
|
||||||
int ifindex, mibindex;
|
struct nl_object *magic[2];
|
||||||
|
|
||||||
prog = basename(argv[0]);
|
if (rtnl_tc_str2handle(id, &handle))
|
||||||
|
die("invalid id");
|
||||||
|
|
||||||
if (argc < 2)
|
needle = rtnl_class_alloc();
|
||||||
usage();
|
rtnl_class_set_ifindex(needle, ifindex);
|
||||||
|
rtnl_class_set_handle(needle, handle);
|
||||||
|
|
||||||
mibindex = mib_find(argv[2]);
|
magic[0] = (struct nl_object *)needle;
|
||||||
|
magic[1] = (struct nl_object *)NULL;
|
||||||
|
|
||||||
if (!strcmp(argv[1], "-n")) {
|
nl_cache_foreach(class_cache, match_obj, magic);
|
||||||
if (mibindex < 0) {
|
|
||||||
if (!strcmp(argv[2], mib_prefix))
|
|
||||||
mibindex = 0;
|
|
||||||
} else if (mibindex == mibc - 1) {
|
|
||||||
exit(0);
|
|
||||||
} else {
|
|
||||||
mibindex++;
|
|
||||||
}
|
|
||||||
} else if (strcmp(argv[1], "-g")) {
|
|
||||||
usage();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mibindex < 0 || mibindex >= mibc)
|
rtnl_class_put(needle);
|
||||||
die("invalid mib");
|
return (struct rtnl_class *)magic[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t get_class_byte_count(struct class_info *info) {
|
||||||
|
struct rtnl_class *class = get_class_by_id(info->id, ifindex);
|
||||||
|
uint64_t bytes;
|
||||||
|
if (!class)
|
||||||
|
die("class not found");
|
||||||
|
bytes = rtnl_class_get_stat(class, RTNL_TC_BYTES);
|
||||||
|
rtnl_class_put(class);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mirror_stats_refresh(void) {
|
||||||
|
nl_cache_refill(nl_handle, class_cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mirror_stats_initialize(void) {
|
||||||
nl_handle = nl_handle_alloc();
|
nl_handle = nl_handle_alloc();
|
||||||
if (!nl_handle)
|
if (!nl_handle)
|
||||||
die("unable to allocate handle");
|
die("unable to allocate handle");
|
||||||
|
@ -108,7 +85,6 @@ int main(int argc, char *argv[]) {
|
||||||
link_cache = rtnl_link_alloc_cache(nl_handle);
|
link_cache = rtnl_link_alloc_cache(nl_handle);
|
||||||
if (!link_cache)
|
if (!link_cache)
|
||||||
die("unable to allocate link cache");
|
die("unable to allocate link cache");
|
||||||
// nl_cache_mgmt_provide(link_cache);
|
|
||||||
|
|
||||||
eth = rtnl_link_get_by_name(link_cache, "eth0");
|
eth = rtnl_link_get_by_name(link_cache, "eth0");
|
||||||
if (!eth)
|
if (!eth)
|
||||||
|
@ -118,16 +94,23 @@ int main(int argc, char *argv[]) {
|
||||||
class_cache = rtnl_class_alloc_cache(nl_handle, ifindex);
|
class_cache = rtnl_class_alloc_cache(nl_handle, ifindex);
|
||||||
if (!class_cache)
|
if (!class_cache)
|
||||||
die("unable to allocate class cache");
|
die("unable to allocate class cache");
|
||||||
|
}
|
||||||
|
|
||||||
nl_cache_foreach(class_cache, dump_class_stats, &mibv[mibindex]);
|
void mirror_stats_cleanup(void) {
|
||||||
|
|
||||||
if (!found)
|
|
||||||
die("class not found");
|
|
||||||
|
|
||||||
rtnl_link_put(eth);
|
rtnl_link_put(eth);
|
||||||
nl_cache_free(class_cache);
|
nl_cache_free(class_cache);
|
||||||
nl_cache_free(link_cache);
|
nl_cache_free(link_cache);
|
||||||
nl_close(nl_handle);
|
nl_close(nl_handle);
|
||||||
nl_handle_destroy(nl_handle);
|
nl_handle_destroy(nl_handle);
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
mirror_stats_initialize();
|
||||||
|
for (;;) {
|
||||||
|
printf("%s %"PRIu64"\n", cogent_class.id, get_class_byte_count(&cogent_class));
|
||||||
|
printf("%s %"PRIu64"\n", orion_class.id, get_class_byte_count(&orion_class));
|
||||||
|
printf("%s %"PRIu64"\n", campus_class.id, get_class_byte_count(&campus_class));
|
||||||
|
sleep(1);
|
||||||
|
mirror_stats_refresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <libgen.h>
|
||||||
|
#include <netlink/route/class.h>
|
||||||
|
#include <netlink/route/link.h>
|
||||||
|
#include <netlink/cache-api.h>
|
||||||
|
#include <netlink/object.h>
|
||||||
|
|
||||||
|
struct class_info;
|
||||||
|
extern struct class_info cogent_class;
|
||||||
|
extern struct class_info orion_class;
|
||||||
|
extern struct class_info campus_class;
|
||||||
|
|
||||||
|
void mirror_stats_refresh(void);
|
||||||
|
void mirror_stats_initialize(void);
|
||||||
|
void mirror_stats_cleanup(void);
|
||||||
|
void die(char *);
|
||||||
|
uint64_t get_class_byte_count(struct class_info *);
|
Loading…
Reference in New Issue