Print info for users to stderr when socket files are missing 33/20733/1
authorOndrej Fabry <ofabry@cisco.com>
Fri, 19 Jul 2019 03:42:41 +0000 (05:42 +0200)
committerOndrej Fabry <ofabry@cisco.com>
Fri, 19 Jul 2019 03:42:41 +0000 (05:42 +0200)
Change-Id: Ibdbfcb78b05cf80945a93ed424a7a2a03b6ea06d
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
adapter/socketclient/socketclient.go
adapter/statsclient/stat_segment.go
adapter/statsclient/statsclient.go
api/stats.go
examples/stats-api/README.md
examples/stats-api/stats_api.go

index 96f23e6..2144d24 100644 (file)
@@ -40,6 +40,20 @@ const (
        DefaultSocketName = adapter.DefaultBinapiSocket
 )
 
+const socketMissing = `
+------------------------------------------------------------
+ VPP binary API socket file %s is missing!
+
+  - is VPP running with socket for binapi enabled?
+  - is the correct socket name configured?
+
+ To enable it add following section to your VPP config:
+   socksvr {
+     default
+   }
+------------------------------------------------------------
+`
+
 var (
        // DefaultConnectTimeout is default timeout for connecting
        DefaultConnectTimeout = time.Second * 3
@@ -165,7 +179,13 @@ func (c *vppClient) SetMsgCallback(cb adapter.MsgCallback) {
 }
 
 func (c *vppClient) Connect() error {
-       Log.Debugf("Connecting to: %v", c.sockAddr)
+       // check if socket exists
+       if _, err := os.Stat(c.sockAddr); os.IsNotExist(err) {
+               fmt.Fprintf(os.Stderr, socketMissing, c.sockAddr)
+               return fmt.Errorf("VPP API socket file %s does not exist", c.sockAddr)
+       } else if err != nil {
+               return fmt.Errorf("VPP API socket error: %v", err)
+       }
 
        if err := c.connect(c.sockAddr); err != nil {
                return err
@@ -212,12 +232,14 @@ func (c *vppClient) Disconnect() error {
 func (c *vppClient) connect(sockAddr string) error {
        addr := &net.UnixAddr{Name: sockAddr, Net: "unix"}
 
+       Log.Debugf("Connecting to: %v", c.sockAddr)
+
        conn, err := net.DialUnix("unix", nil, addr)
        if err != nil {
                // we try different type of socket for backwards compatbility with VPP<=19.04
                if strings.Contains(err.Error(), "wrong type for socket") {
                        addr.Net = "unixpacket"
-                       Log.Warnf("%s, retrying connect with type unixpacket", err)
+                       Log.Debugf("%s, retrying connect with type unixpacket", err)
                        conn, err = net.DialUnix("unixpacket", nil, addr)
                }
                if err != nil {
index 1875f17..e8d20b0 100644 (file)
@@ -117,11 +117,11 @@ func (c *statSegment) connect(sockName string) error {
        header := c.readHeader()
        Log.Debugf("stat segment header: %+v", header)
 
-       // older VPP (19.04) did not have version in stat segment header
+       // older VPP (<=19.04) did not have version in stat segment header
        // we try to provide fallback support by skipping it in header
        if header.version > MaxVersion && header.inProgress > 1 && header.epoch == 0 {
                h := c.readHeaderOld()
-               Log.Warnf("statsclient: falling back to old stat segment version (VPP 19.04): %+v", h)
+               Log.Debugf("statsclient: falling back to old stat segment version (VPP <=19.04): %+v", h)
                c.oldHeader = true
        }
 
index f715a70..6381b9f 100644 (file)
@@ -27,6 +27,25 @@ import (
        "git.fd.io/govpp.git/adapter"
 )
 
+const (
+       // DefaultSocketName is default VPP stats socket file path.
+       DefaultSocketName = adapter.DefaultStatsSocket
+)
+
+const socketMissing = `
+------------------------------------------------------------
+ VPP stats socket file %s is missing!
+
+  - is VPP running with stats segment enabled?
+  - is the correct socket name configured?
+
+ To enable it add following section to your VPP config:
+   statseg {
+     default
+   }
+------------------------------------------------------------
+`
+
 var (
        // Debug is global variable that determines debug mode
        Debug = os.Getenv("DEBUG_GOVPP_STATS") != ""
@@ -55,29 +74,17 @@ type StatsClient struct {
 // NewStatsClient returns new VPP stats API client.
 func NewStatsClient(sockAddr string) *StatsClient {
        if sockAddr == "" {
-               sockAddr = adapter.DefaultStatsSocket
+               sockAddr = DefaultSocketName
        }
        return &StatsClient{
                sockAddr: sockAddr,
        }
 }
 
-const sockNotFoundWarn = `stats socket not found at: %s
-------------------------------------------------------------
- VPP stats socket is missing!
- Is VPP running with stats segment enabled?
-
- To enable it add following section to startup config:
-   statseg {
-     default
-   }
-------------------------------------------------------------
-`
-
 func (c *StatsClient) Connect() error {
        // check if socket exists
        if _, err := os.Stat(c.sockAddr); os.IsNotExist(err) {
-               Log.Warnf(sockNotFoundWarn, c.sockAddr)
+               fmt.Fprintf(os.Stderr, socketMissing, c.sockAddr)
                return fmt.Errorf("stats socket file %s does not exist", c.sockAddr)
        } else if err != nil {
                return fmt.Errorf("stats socket error: %v", err)
index 0bf9908..e254eae 100644 (file)
@@ -1,3 +1,17 @@
+//  Copyright (c) 2019 Cisco and/or its affiliates.
+//
+//  Licensed under the Apache License, Version 2.0 (the "License");
+//  you may not use this file except in compliance with the License.
+//  You may obtain a copy of the License at:
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+
 package api
 
 // SystemStats represents global system statistics.
index 5511bf4..f3d33b1 100644 (file)
@@ -6,7 +6,7 @@ This example demonstrates how to retrieve statistics from VPP using [the new Sta
 
 The following requirements are required to run this example:
 
-- install **VPP 18.10+** (VPP 19.04+ for goclient)
+- install VPP **18.10+** (VPP 19.04+ for statsclient)
 - enable stats in VPP
 
 To enable stats add following section to you VPP config:
index a42e3d2..175bb27 100644 (file)
@@ -35,10 +35,9 @@ import (
 // ------------------------------------------------------------------
 
 var (
-       statsSocket = flag.String("socket", adapter.DefaultStatsSocket, "Path to VPP stats socket")
+       statsSocket = flag.String("socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
        dumpAll     = flag.Bool("all", false, "Dump all stats including ones with zero values")
-
-       goclient = flag.Bool("goclient", false, "Use pure Go client for stats API")
+       oldclient   = flag.Bool("oldclient", false, "Use old client for stats API (vppapiclient)")
 )
 
 func init() {
@@ -66,10 +65,10 @@ func main() {
        }
 
        var client adapter.StatsAPI
-       if *goclient {
-               client = statsclient.NewStatsClient(*statsSocket)
-       } else {
+       if *oldclient {
                client = vppapiclient.NewStatClient(*statsSocket)
+       } else {
+               client = statsclient.NewStatsClient(*statsSocket)
        }
 
        fmt.Printf("Connecting to stats socket: %s\n", *statsSocket)
@@ -138,6 +137,7 @@ func main() {
 
        case "dump":
                dumpStats(client, patterns, skipZeros)
+
        default:
                listStats(client, patterns)
        }