Home | History | Annotate | Download | only in src
      1 /*
      2  * src/nl-link-stats.c     Retrieve link statistics
      3  *
      4  *	This library is free software; you can redistribute it and/or
      5  *	modify it under the terms of the GNU Lesser General Public
      6  *	License as published by the Free Software Foundation version 2.1
      7  *	of the License.
      8  *
      9  * Copyright (c) 2003-2009 Thomas Graf <tgraf (at) suug.ch>
     10  */
     11 
     12 #include <netlink/cli/utils.h>
     13 #include <netlink/cli/link.h>
     14 
     15 static void print_usage(void)
     16 {
     17 	printf(
     18 	"Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n"
     19 	"\n"
     20 	"Options\n"
     21 	" -l, --list            List available statistic names\n"
     22 	" -h, --help            Show this help\n"
     23 	" -v, --version         Show versioning information\n"
     24 	"\n"
     25 	"Link Options\n"
     26 	" -n, --name=NAME	link name\n"
     27 	" -i, --index=NUM       interface index\n"
     28 	);
     29 	exit(0);
     30 }
     31 
     32 static void list_stat_names(void)
     33 {
     34 	char buf[64];
     35 	int i;
     36 
     37 	for (i = 0; i < RTNL_LINK_STATS_MAX; i++)
     38 		printf("%s\n", rtnl_link_stat2str(i, buf, sizeof(buf)));
     39 
     40 	exit(0);
     41 }
     42 
     43 static int gargc;
     44 
     45 static void dump_stat(struct rtnl_link *link, int id)
     46 {
     47 	uint64_t st = rtnl_link_get_stat(link, id);
     48 	char buf[64];
     49 
     50 	printf("%s.%s %" PRIu64 "\n", rtnl_link_get_name(link),
     51 	       rtnl_link_stat2str(id, buf, sizeof(buf)), st);
     52 }
     53 
     54 static void dump_stats(struct nl_object *obj, void *arg)
     55 {
     56 	struct rtnl_link *link = (struct rtnl_link *) obj;
     57 	char **argv = arg;
     58 
     59 	if (optind >= gargc) {
     60 		int i;
     61 
     62 		for (i = 0; i < RTNL_LINK_STATS_MAX; i++)
     63 			dump_stat(link, i);
     64 	} else {
     65 		while (optind < gargc) {
     66 			int id = rtnl_link_str2stat(argv[optind]);
     67 
     68 			if (id < 0)
     69 				fprintf(stderr, "Warning: Unknown statistic "
     70 					"\"%s\"\n", argv[optind]);
     71 			else
     72 				dump_stat(link, id);
     73 
     74 			optind++;
     75 		}
     76 	}
     77 }
     78 
     79 int main(int argc, char *argv[])
     80 {
     81 	struct nl_sock *sock;
     82 	struct nl_cache *link_cache;
     83 	struct rtnl_link *link;
     84 
     85 	sock = nl_cli_alloc_socket();
     86 	nl_cli_connect(sock, NETLINK_ROUTE);
     87 	link_cache = nl_cli_link_alloc_cache(sock);
     88 	link = nl_cli_link_alloc();
     89 
     90 	for (;;) {
     91 		int c, optidx = 0;
     92 		static struct option long_opts[] = {
     93 			{ "list", 0, 0, 'l' },
     94 			{ "help", 0, 0, 'h' },
     95 			{ "version", 0, 0, 'v' },
     96 			{ "name", 1, 0, 'n' },
     97 			{ "index", 1, 0, 'i' },
     98 			{ 0, 0, 0, 0 }
     99 		};
    100 
    101 		c = getopt_long(argc, argv, "lhvn:i:", long_opts, &optidx);
    102 		if (c == -1)
    103 			break;
    104 
    105 		switch (c) {
    106 		case 'l': list_stat_names(); break;
    107 		case 'h': print_usage(); break;
    108 		case 'v': nl_cli_print_version(); break;
    109 		case 'n': nl_cli_link_parse_name(link, optarg); break;
    110 		case 'i': nl_cli_link_parse_ifindex(link, optarg); break;
    111 		}
    112 	}
    113 
    114 	gargc = argc;
    115 	nl_cache_foreach_filter(link_cache, OBJ_CAST(link), dump_stats, argv);
    116 
    117 	return 0;
    118 }
    119 
    120