Skip to content

Commit

Permalink
Merge pull request #2518 from threefoldtech/update-node-statistics
Browse files Browse the repository at this point in the history
add number of open connections to node statistics
  • Loading branch information
Eslam-Nawara authored Jan 9, 2025
2 parents 401b8de + 9b56087 commit e44237a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 23 additions & 12 deletions pkg/primitives/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -31,9 +34,7 @@ func GetCapacity(ctx context.Context) gridtypes.Capacity {
return val.(gridtypes.Capacity)
}

var (
_ provision.Provisioner = (*Statistics)(nil)
)
var _ provision.Provisioner = (*Statistics)(nil)

type Reserved func() (gridtypes.Capacity, error)

Expand Down Expand Up @@ -146,7 +147,6 @@ func (s *Statistics) hasEnoughCapacity(wl *gridtypes.WorkloadWithID) (gridtypes.
id, _ := gridtypes.NewWorkloadID(dl_.TwinID, dl_.ContractID, wl_.Name)
return id == wl.ID
})

if err != nil {
return used, errors.Wrap(err, "failed to get available memory")
}
Expand All @@ -155,7 +155,7 @@ func (s *Statistics) hasEnoughCapacity(wl *gridtypes.WorkloadWithID) (gridtypes.
return used, fmt.Errorf("cannot fulfil required memory size %d bytes out of usable %d bytes", required.MRU, usable)
}

//check other resources as well?
// check other resources as well?
return used, nil
}

Expand Down Expand Up @@ -253,10 +253,16 @@ func (s *statsStream) GetCounters() (pkg.Counters, error) {
if err != nil {
return pkg.Counters{}, err
}

conn, err := s.openConnectionsCount()
if err != nil {
return pkg.Counters{}, err
}
return pkg.Counters{
Total: s.stats.Total(),
Used: activeCounters.cap,
System: reserved,
Total: s.stats.Total(),
Used: activeCounters.cap,
System: reserved,
OpenConnecions: conn,
Users: pkg.UsersCounters{
Deployments: activeCounters.deployments,
Workloads: activeCounters.workloads,
Expand Down Expand Up @@ -298,10 +304,6 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) {
return nil, errors.Wrap(err, "failed to list available devices")
}

if err != nil {
return nil, errors.Wrap(err, "failed to list active deployments")
}

used, err := usedGpus()
if err != nil {
return nil, errors.Wrap(err, "failed to list used gpus")
Expand Down Expand Up @@ -332,3 +334,12 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) {

return list, nil
}

func (s *statsStream) openConnectionsCount() (int, error) {
cmd := exec.Command("/bin/sh", "-c", "ss -ptn state established | wc -l")
out, err := cmd.Output()
if err != nil {
return 0, err
}
return strconv.Atoi(strings.TrimSpace(string(out)))
}
2 changes: 2 additions & 0 deletions pkg/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Counters struct {
System gridtypes.Capacity `json:"system"`
// Users statistics by zos
Users UsersCounters `json:"users"`
// OpenConnecions number of open connections in the node
OpenConnecions int `json:"open_connections"`
}

// UsersCounters the expected counters for deployments and workloads
Expand Down

0 comments on commit e44237a

Please sign in to comment.