826b011b00d14f78e1db2b9b801fd5386d4399ef
[vpp.git] / extras / vpp_stats_fs / cmd.go
1 /*
2  * Copyright (c) 2021 Cisco Systems 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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /*
17  * Copyright 2016 the Go-FUSE Authors. All rights reserved.
18  * Use of this source code is governed by a BSD-style
19  * license that can be found in the LICENSE file.
20  */
21
22 // This file is the main program driver to mount the stats segment filesystem.
23 package main
24
25 import (
26         "flag"
27         "fmt"
28         "os"
29         "os/signal"
30         "runtime"
31         "strings"
32         "syscall"
33
34         "git.fd.io/govpp.git/adapter/statsclient"
35         "git.fd.io/govpp.git/core"
36         "github.com/hanwen/go-fuse/v2/fs"
37 )
38
39 func main() {
40         statsSocket := flag.String("socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
41         debug := flag.Bool("debug", false, "print debugging messages.")
42         flag.Parse()
43         if flag.NArg() < 1 {
44                 fmt.Fprintf(os.Stderr, "usage: %s MOUNTPOINT\n", os.Args[0])
45                 os.Exit(2)
46         }
47         //Conection to the stat segment socket.
48         sc := statsclient.NewStatsClient(*statsSocket)
49         fmt.Printf("Waiting for the VPP socket to be available. Be sure a VPP instance is running.\n")
50         c, err := core.ConnectStats(sc)
51         if err != nil {
52                 fmt.Fprintf(os.Stderr, "Failed to connect to the stats socket: %v\n", err)
53                 os.Exit(1)
54         }
55         defer c.Disconnect()
56         fmt.Printf("Connected to the socket\n")
57         //Creating the filesystem instance
58         root, err := NewStatsFileSystem(sc)
59         if err != nil {
60                 fmt.Fprintf(os.Stderr, "NewStatsFileSystem failed: %v\n", err)
61                 os.Exit(1)
62         }
63
64         //Mounting the filesystem.
65         opts := &fs.Options{}
66         opts.Debug = *debug
67         opts.AllowOther = true
68         server, err := fs.Mount(flag.Arg(0), root, opts)
69         if err != nil {
70                 fmt.Fprintf(os.Stderr, "Mount fail: %v\n", err)
71                 os.Exit(1)
72         }
73
74         sigs := make(chan os.Signal)
75         signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
76
77         fmt.Printf("Successfully mounted the file system in directory: %s\n", flag.Arg(0))
78         runtime.GC()
79
80         for {
81                 go server.Wait()
82
83                 <-sigs
84                 fmt.Println("Unmounting...")
85                 err := server.Unmount()
86                 if err == nil || !strings.Contains(err.Error(), "Device or resource busy") {
87                         break
88                 }
89                 fmt.Fprintf(os.Stderr, "Unmount fail: %v\n", err)
90         }
91 }