This commit was generated by cvs2svn to compensate for changes in r786,
[libnl.git] / src / nl-link-stats.c
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-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 static void print_usage(void)
15 {
16         printf(
17 "Usage: nl-link-stats <ifindex> [<statistic> ...]\n"
18 "  ifindex   := { all | number }\n"
19 "  statistic := { (rx|tx)_packets | (rx|tx)_bytes | (rx|tx)_errors |\n"
20 "                 (rx|tx)_dropped | (rx|tx)_compressed | (rx|tx)_fifo_err |\n" \
21 "                 rx_len_err | rx_over_err | rx_crc_err | rx_frame_err |\n"
22 "                 rx_missed_err | tx_abort_err | tx_carrier_err |\n"
23 "                 tx_hbeat_err | tx_win_err | tx_collision | multicast }\n");
24         exit(1);
25 }
26
27 static char **gargv;
28 static int gargc;
29
30 static void dump_stat(struct rtnl_link *link, int id)
31 {
32         uint64_t st = rtnl_link_get_stat(link, id);
33         char buf[62];
34
35         printf("%s.%s %" PRIu64 "\n", rtnl_link_get_name(link),
36                rtnl_link_stat2str(id, buf, sizeof(buf)), st);
37 }
38
39 static void dump_stats(struct nl_object *obj, void *arg)
40 {
41         int i;
42         struct rtnl_link *link = (struct rtnl_link *) obj;
43
44         if (!strcasecmp(gargv[0], "all")) {
45                 for (i = 0; i < RTNL_LINK_STATS_MAX; i++)
46                         dump_stat(link, i);
47         } else {
48                 for (i = 0; i < gargc; i++) {
49                         int id = rtnl_link_str2stat(gargv[i]);
50
51                         if (id < 0)
52                                 fprintf(stderr, "Warning: Unknown statistic "
53                                         "\"%s\"\n", gargv[i]);
54                         else
55                                 dump_stat(link, id);
56                 }
57         }
58 }
59
60 int main(int argc, char *argv[])
61 {
62         struct nl_handle *nlh;
63         struct nl_cache *link_cache;
64         int err = 1;
65
66         if (nltool_init(argc, argv) < 0)
67                 return -1;
68
69         if (argc < 3 || !strcmp(argv[1], "-h"))
70                 print_usage();
71
72         nlh = nl_handle_alloc_nondefault(nltool_cbset);
73         if (!nlh)
74                 return -1;
75
76         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
77                 goto errout;
78
79         link_cache = nltool_alloc_link_cache(nlh);
80         if (!link_cache)
81                 goto errout_close;
82
83         gargv = &argv[2];
84         gargc = argc - 2;
85
86         if (!strcasecmp(argv[1], "all"))
87                 nl_cache_foreach(link_cache, dump_stats, NULL);
88         else {
89                 int ifindex = strtoul(argv[1], NULL, 0);
90                 struct rtnl_link *link = rtnl_link_get(link_cache, ifindex);
91
92                 if (!link) {
93                         fprintf(stderr, "Could not find ifindex %d\n", ifindex);
94                         goto errout_link_cache;
95                 }
96
97                 dump_stats((struct nl_object *) link, NULL);
98                 rtnl_link_put(link);
99         }
100
101         err = 0;
102 errout_link_cache:
103         nl_cache_free(link_cache);
104 errout_close:
105         nl_close(nlh);
106 errout:
107         nl_handle_destroy(nlh);
108         return err;
109 }