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.
25 "git.fd.io/govpp.git/adapter"
26 "git.fd.io/govpp.git/adapter/statsclient"
27 "git.fd.io/govpp.git/api"
28 "git.fd.io/govpp.git/core"
31 // ------------------------------------------------------------------
32 // Example - Stats API
33 // ------------------------------------------------------------------
34 // The example stats_api demonstrates how to retrieve stats
35 // from the VPP using the new stats API.
36 // ------------------------------------------------------------------
39 statsSocket = flag.String("socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
40 dumpAll = flag.Bool("all", false, "Dump all stats including ones with zero values")
41 pollPeriod = flag.Duration("period", time.Second*5, "Polling interval period")
46 fmt.Fprintf(os.Stderr, "%s: usage [ls|dump|poll|errors|interfaces|nodes|system|buffers] <patterns>...\n", os.Args[0])
54 skipZeros := !*dumpAll
58 patterns = flag.Args()[1:]
61 client := statsclient.NewStatsClient(*statsSocket)
63 c, err := core.ConnectStats(client)
65 log.Fatalln("Connecting failed:", err)
69 switch cmd := flag.Arg(0); cmd {
71 stats := new(api.SystemStats)
72 if err := c.GetSystemStats(stats); err != nil {
73 log.Fatalln("getting system stats failed:", err)
75 fmt.Printf("System stats: %+v\n", stats)
81 fmt.Println("Listing node stats..")
82 stats := new(api.NodeStats)
83 if err := c.GetNodeStats(stats); err != nil {
84 log.Fatalln("getting node stats failed:", err)
87 for _, node := range stats.Nodes {
88 if skipZeros && node.Calls == 0 && node.Suspends == 0 && node.Clocks == 0 && node.Vectors == 0 {
91 fmt.Printf(" - %+v\n", node)
93 fmt.Printf("Listed %d node counters\n", len(stats.Nodes))
96 fmt.Println("Listing interface stats..")
97 stats := new(api.InterfaceStats)
98 if err := c.GetInterfaceStats(stats); err != nil {
99 log.Fatalln("getting interface stats failed:", err)
101 for _, iface := range stats.Interfaces {
102 fmt.Printf(" - %+v\n", iface)
104 fmt.Printf("Listed %d interface counters\n", len(stats.Interfaces))
106 case "poll-interfaces":
110 fmt.Printf("Listing error stats.. %s\n", strings.Join(patterns, " "))
111 stats := new(api.ErrorStats)
112 if err := c.GetErrorStats(stats); err != nil {
113 log.Fatalln("getting error stats failed:", err)
116 for _, counter := range stats.Errors {
117 if skipZeros && counter.Value == 0 {
120 fmt.Printf(" - %v\n", counter)
123 fmt.Printf("Listed %d (%d) error counters\n", n, len(stats.Errors))
126 stats := new(api.BufferStats)
127 if err := c.GetBufferStats(stats); err != nil {
128 log.Fatalln("getting buffer stats failed:", err)
130 fmt.Printf("Buffer stats: %+v\n", stats)
133 fmt.Printf("Dumping stats.. %s\n", strings.Join(patterns, " "))
135 dumpStats(client, patterns, skipZeros)
138 fmt.Printf("Polling stats.. %s\n", strings.Join(patterns, " "))
140 pollStats(client, patterns, skipZeros)
142 case "list", "ls", "":
143 fmt.Printf("Listing stats.. %s\n", strings.Join(patterns, " "))
145 listStats(client, patterns)
148 fmt.Printf("invalid command: %q\n", cmd)
152 func listStats(client adapter.StatsAPI, patterns []string) {
153 list, err := client.ListStats(patterns...)
155 log.Fatalln("listing stats failed:", err)
158 for _, stat := range list {
159 fmt.Printf(" - %v\n", stat)
162 fmt.Printf("Listed %d stats\n", len(list))
165 func dumpStats(client adapter.StatsAPI, patterns []string, skipZeros bool) {
166 stats, err := client.DumpStats(patterns...)
168 log.Fatalln("dumping stats failed:", err)
172 for _, stat := range stats {
173 if skipZeros && (stat.Data == nil || stat.Data.IsZero()) {
176 fmt.Printf(" - %-50s %25v %+v\n", stat.Name, stat.Type, stat.Data)
180 fmt.Printf("Dumped %d (%d) stats\n", n, len(stats))
183 func pollStats(client adapter.StatsAPI, patterns []string, skipZeros bool) {
184 dir, err := client.PrepareDir(patterns...)
186 log.Fatalln("preparing dir failed:", err)
189 tick := time.Tick(*pollPeriod)
192 fmt.Println(time.Now().Format(time.Stamp))
193 for _, stat := range dir.Entries {
194 if skipZeros && (stat.Data == nil || stat.Data.IsZero()) {
197 fmt.Printf("%-50s %+v\n", stat.Name, stat.Data)
204 if err := client.UpdateDir(dir); err != nil {
205 if err == adapter.ErrStatsDirStale {
206 if dir, err = client.PrepareDir(patterns...); err != nil {
207 log.Fatalln("preparing dir failed:", err)
211 log.Fatalln("updating dir failed:", err)
217 func pollSystem(client api.StatsProvider) {
218 stats := new(api.SystemStats)
220 if err := client.GetSystemStats(stats); err != nil {
221 log.Fatalln("updating system stats failed:", err)
224 tick := time.Tick(*pollPeriod)
226 fmt.Printf("System stats: %+v\n", stats)
231 if err := client.GetSystemStats(stats); err != nil {
232 log.Println("updating system stats failed:", err)
238 func pollInterfaces(client api.StatsProvider) {
239 stats := new(api.InterfaceStats)
241 if err := client.GetInterfaceStats(stats); err != nil {
242 log.Fatalln("updating system stats failed:", err)
245 tick := time.Tick(*pollPeriod)
247 fmt.Printf("Interface stats (%d interfaces)\n", len(stats.Interfaces))
248 for i := range stats.Interfaces {
249 fmt.Printf(" - %+v\n", stats.Interfaces[i])
255 if err := client.GetInterfaceStats(stats); err != nil {
256 log.Println("updating system stats failed:", err)