diff --git a/go.sum b/go.sum index a9133b440..669284e9c 100644 --- a/go.sum +++ b/go.sum @@ -1066,10 +1066,6 @@ github.com/threefoldtech/go-rmb v0.1.4-0.20211001084350-a0f5b8f508f5 h1:4t0Lf/DZ github.com/threefoldtech/go-rmb v0.1.4-0.20211001084350-a0f5b8f508f5/go.mod h1:L0Lg63BUqalwynE9l037UHSBY9+q00nySO/QZRZPVIM= github.com/threefoldtech/substrate-client v0.0.0-20210930125201-9bd88e80762d h1:dc2KuqZkCIEOxmatevfYb3vVHB8jqqfPMxW3/u1IWCA= github.com/threefoldtech/substrate-client v0.0.0-20210930125201-9bd88e80762d/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk= -github.com/threefoldtech/substrate-client v0.0.0-20211007130322-ece81d9a8d18 h1:gubNcFGsKwC5oxMHrvMUfqCxgS0jyk8xov04BCOgonE= -github.com/threefoldtech/substrate-client v0.0.0-20211007130322-ece81d9a8d18/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk= -github.com/threefoldtech/substrate-client v0.0.0-20211007133201-97a478b0363a h1:m/JrjdDySfNO1Muok3A8ofBx7Cm2qgca0tvDHWZ6j8g= -github.com/threefoldtech/substrate-client v0.0.0-20211007133201-97a478b0363a/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk= github.com/threefoldtech/substrate-client v0.0.0-20211007134519-74137b8f68ec h1:dGqu3OQmNJS/ikcGIMPGUBrizO0YeSsXxzNEjjx3Hhg= github.com/threefoldtech/substrate-client v0.0.0-20211007134519-74137b8f68ec/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk= github.com/threefoldtech/zbus v0.1.5 h1:S9kXbjejoRRnJw1yKHEXFGF2vqL+Drae2n4vpj0pGHo= diff --git a/pkg/gridtypes/zos/qsfs.go b/pkg/gridtypes/zos/qsfs.go index 6e9378a76..ba0410793 100644 --- a/pkg/gridtypes/zos/qsfs.go +++ b/pkg/gridtypes/zos/qsfs.go @@ -223,7 +223,7 @@ func (q QuantumSafeFS) Capacity() (gridtypes.Capacity, error) { } type QuatumSafeFSResult struct { - Path string `json:"path"` - PrometheusEndpoint string `json:"prometheus_endpoint"` - PrometheusPort int `json:"prometheus_port"` + Path string `json:"path"` + MetricsEndpoint string `json:"metrics_endpoint"` + MetricsPort int `json:"metrics_port"` } diff --git a/pkg/primitives/qsfs.go b/pkg/primitives/qsfs.go index 12a84ba2b..7ceeb713f 100644 --- a/pkg/primitives/qsfs.go +++ b/pkg/primitives/qsfs.go @@ -23,8 +23,8 @@ func (p *Primitives) qsfsProvision(ctx context.Context, wl *gridtypes.WorkloadWi return nil, errors.Wrap(err, "failed to setup create qsfs mount") } result.Path = info.Path - result.PrometheusEndpoint = info.PrometheusEndpoint - result.PrometheusPort = info.PrometheusPort + result.MetricsEndpoint = info.MetricsEndpoint + result.MetricsPort = info.MetricsPort return result, nil } diff --git a/pkg/qsfsd.go b/pkg/qsfsd.go index 1d84bd81a..564f7925d 100644 --- a/pkg/qsfsd.go +++ b/pkg/qsfsd.go @@ -13,9 +13,9 @@ type QSFSMetrics struct { } type QSFSInfo struct { - Path string - PrometheusEndpoint string - PrometheusPort int + Path string + MetricsEndpoint string + MetricsPort int } func (q *QSFSMetrics) Nu(wlID string) (result uint64) { diff --git a/pkg/qsfsd/metrics.go b/pkg/qsfsd/metrics.go index c59876b18..d28f267df 100644 --- a/pkg/qsfsd/metrics.go +++ b/pkg/qsfsd/metrics.go @@ -4,11 +4,6 @@ import ( "context" "fmt" "io/ioutil" - "os" - "path/filepath" - "strconv" - "strings" - "syscall" "time" "github.com/containernetworking/plugins/pkg/ns" @@ -17,6 +12,7 @@ import ( "github.com/threefoldtech/zos/pkg" "github.com/threefoldtech/zos/pkg/network/namespace" "github.com/threefoldtech/zos/pkg/stubs" + "github.com/vishvananda/netlink" ) // Metrics gets running qsfs network metrics @@ -41,25 +37,7 @@ func (m *QSFS) Metrics() (pkg.QSFSMetrics, error) { defer netNs.Close() metrics := pkg.NetMetric{} err = netNs.Do(func(_ ns.NetNS) error { - dir, err := ioutil.TempDir("/tmp", "qsfs-sysfs") - if err != nil { - return errors.Wrap(err, "coudln't create temp dir") - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - log.Error().Err(err).Msg(fmt.Sprintf("qsfs metrics: couldn't remove: %s", dir)) - } - }() - if err := syscall.Mount("newns", dir, "sysfs", 0, ""); err != nil { - return errors.Wrap(err, "couldn't mount sysfs") - } - defer func() { - if err := syscall.Unmount(dir, syscall.MNT_DETACH); err != nil { - log.Error().Err(err).Msg("qsfs metrics: couldn't detach sysfs: %s") - } - }() - - metrics, err = metricsForNics(dir, []string{"public", "ygg0"}) + metrics, err = metricsForNics([]string{"public", "ygg0"}) return err }) if err != nil { @@ -72,37 +50,19 @@ func (m *QSFS) Metrics() (pkg.QSFSMetrics, error) { return pkg.QSFSMetrics{Consumption: result}, nil } -func readFileUint64(p string) (uint64, error) { - bytes, err := ioutil.ReadFile(p) - if err != nil { - // we do skip but may be this is not crre - return 0, err - } - - return strconv.ParseUint(strings.TrimSpace(string(bytes)), 10, 64) -} - -func metricsForNics(sysfsPath string, nics []string) (m pkg.NetMetric, err error) { - template := filepath.Join(sysfsPath, "class/net/%s/statistics/") - dict := map[string]*uint64{ - "rx_bytes": &m.NetRxBytes, - "rx_packets": &m.NetRxPackets, - "tx_bytes": &m.NetTxBytes, - "tx_packets": &m.NetTxPackets, - } +func metricsForNics(nics []string) (pkg.NetMetric, error) { + var m pkg.NetMetric for _, nic := range nics { - base := fmt.Sprintf(template, nic) - for metric, ptr := range dict { - path := filepath.Join(base, metric) - value, err := readFileUint64(path) - if err != nil { - log.Error().Err(err).Str("path", path).Msg("failed to read statistics") - continue - } - - *ptr += value + link, err := netlink.LinkByName(nic) + if err != nil { + return m, errors.Wrapf(err, "couldn't get nic %s info", nic) } + stats := link.Attrs().Statistics + m.NetRxBytes += stats.RxBytes + m.NetRxPackets += stats.RxPackets + m.NetTxBytes += stats.TxBytes + m.NetTxPackets += stats.TxPackets } - return + return m, nil } diff --git a/pkg/qsfsd/qsfs.go b/pkg/qsfsd/qsfs.go index f9abe6cd1..70f8c0a9e 100644 --- a/pkg/qsfsd/qsfs.go +++ b/pkg/qsfsd/qsfs.go @@ -26,7 +26,7 @@ const ( qsfsRootFsPropagation = pkg.RootFSPropagationSlave zstorSocket = "/var/run/zstor.sock" zstorZDBFSMountPoint = "/mnt" // hardcoded in the container - zstorPrometheusPort = 9100 + zstorMetricsPort = 9100 zstorZDBDataDirPath = "/data" ) @@ -40,7 +40,7 @@ type zstorConfig struct { zos.QuantumSafeFSConfig ZDBDataDirPath string `toml:"zdb_data_dir_path"` Socket string `toml:"socket"` - PrometheusPort uint32 `toml:"prometheus_port"` + MetricsPort uint32 `toml:"prometheus_port"` ZDBFSMountpoint string `toml:"zdbfs_mountpoint"` Root string `toml:"root"` } @@ -63,7 +63,7 @@ func setQSFSDefaults(cfg *zos.QuantumSafeFS) zstorConfig { return zstorConfig{ QuantumSafeFSConfig: cfg.Config, Socket: zstorSocket, - PrometheusPort: zstorPrometheusPort, + MetricsPort: zstorMetricsPort, ZDBFSMountpoint: zstorZDBFSMountPoint, ZDBDataDirPath: zstorZDBDataDirPath, Root: zstorZDBFSMountPoint, @@ -135,8 +135,8 @@ func (q *QSFS) Mount(wlID string, cfg zos.QuantumSafeFS) (info pkg.QSFSInfo, err return } info.Path = mountPath - info.PrometheusPort = 9100 - info.PrometheusEndpoint, err = q.waitYggIPs(ctx, networkd, netns) + info.MetricsPort = 9100 + info.MetricsEndpoint, err = q.waitYggIPs(ctx, networkd, netns) return }