1 // Copyright (c) 2018 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
24 "git.fd.io/govpp.git/adapter"
25 "git.fd.io/govpp.git/adapter/vppapiclient"
26 "git.fd.io/govpp.git/core"
29 // ------------------------------------------------------------------
30 // Example - Stats API
31 // ------------------------------------------------------------------
32 // The example stats_api demonstrates how to retrieve stats
33 // from the VPP using the new stats API.
34 // ------------------------------------------------------------------
37 statsSocket = flag.String("socket", vppapiclient.DefaultStatSocket, "VPP stats segment socket")
38 dumpAll = flag.Bool("all", false, "Dump all stats including ones with zero values")
43 fmt.Fprintf(os.Stderr, "%s: usage [ls|dump|errors|interfaces|nodes|system|buffers] <patterns>...\n", os.Args[0])
51 skipZeros := !*dumpAll
55 case "", "ls", "dump", "errors", "interfaces", "nodes", "system", "buffers":
62 patterns = flag.Args()[1:]
65 client := vppapiclient.NewStatClient(*statsSocket)
67 fmt.Printf("Connecting to stats socket: %s\n", *statsSocket)
69 c, err := core.ConnectStats(client)
71 log.Fatalln("Connecting failed:", err)
77 stats, err := c.GetSystemStats()
79 log.Fatalln("getting system stats failed:", err)
81 fmt.Printf("System stats: %+v\n", stats)
84 fmt.Println("Listing node stats..")
85 stats, err := c.GetNodeStats()
87 log.Fatalln("getting node stats failed:", err)
89 for _, node := range stats.Nodes {
90 if node.Calls == 0 && node.Suspends == 0 && node.Clocks == 0 && node.Vectors == 0 && skipZeros {
93 fmt.Printf(" - %+v\n", node)
95 fmt.Printf("Listed %d node counters\n", len(stats.Nodes))
98 fmt.Println("Listing interface stats..")
99 stats, err := c.GetInterfaceStats()
101 log.Fatalln("getting interface stats failed:", err)
103 for _, iface := range stats.Interfaces {
104 fmt.Printf(" - %+v\n", iface)
106 fmt.Printf("Listed %d interface counters\n", len(stats.Interfaces))
109 fmt.Printf("Listing error stats.. %s\n", strings.Join(patterns, " "))
110 stats, err := c.GetErrorStats(patterns...)
112 log.Fatalln("getting error stats failed:", err)
115 for _, counter := range stats.Errors {
116 if counter.Value == 0 && skipZeros {
119 fmt.Printf(" - %v\n", counter)
122 fmt.Printf("Listed %d (%d) error counters\n", n, len(stats.Errors))
125 stats, err := c.GetBufferStats()
127 log.Fatalln("getting buffer stats failed:", err)
129 fmt.Printf("Buffer stats: %+v\n", stats)
132 dumpStats(client, patterns, skipZeros)
134 listStats(client, patterns)
138 func listStats(client adapter.StatsAPI, patterns []string) {
139 fmt.Printf("Listing stats.. %s\n", strings.Join(patterns, " "))
141 list, err := client.ListStats(patterns...)
143 log.Fatalln("listing stats failed:", err)
146 for _, stat := range list {
147 fmt.Printf(" - %v\n", stat)
150 fmt.Printf("Listed %d stats\n", len(list))
153 func dumpStats(client adapter.StatsAPI, patterns []string, skipZeros bool) {
154 fmt.Printf("Dumping stats.. %s\n", strings.Join(patterns, " "))
156 stats, err := client.DumpStats(patterns...)
158 log.Fatalln("dumping stats failed:", err)
162 for _, stat := range stats {
163 if isZero(stat.Data) && skipZeros {
166 fmt.Printf(" - %-25s %25v %+v\n", stat.Name, stat.Type, stat.Data)
170 fmt.Printf("Dumped %d (%d) stats\n", n, len(stats))
173 func isZero(stat adapter.Stat) bool {
174 switch s := stat.(type) {
175 case adapter.ScalarStat:
177 case adapter.ErrorStat:
179 case adapter.SimpleCounterStat:
180 for _, ss := range s {
181 for _, sss := range ss {
188 case adapter.CombinedCounterStat:
189 for _, ss := range s {
190 for _, sss := range ss {
191 if sss.Bytes != 0 || sss.Packets != 0 {