Skip to content

Commit 6a1fe10

Browse files
committed
add total number of open connections to node statistics
1 parent 9d82197 commit 6a1fe10

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

pkg/primitives/statistics.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"os/exec"
8+
"strconv"
9+
"strings"
710
"time"
811

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

34-
var (
35-
_ provision.Provisioner = (*Statistics)(nil)
36-
)
37+
var _ provision.Provisioner = (*Statistics)(nil)
3738

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

@@ -146,7 +147,6 @@ func (s *Statistics) hasEnoughCapacity(wl *gridtypes.WorkloadWithID) (gridtypes.
146147
id, _ := gridtypes.NewWorkloadID(dl_.TwinID, dl_.ContractID, wl_.Name)
147148
return id == wl.ID
148149
})
149-
150150
if err != nil {
151151
return used, errors.Wrap(err, "failed to get available memory")
152152
}
@@ -155,7 +155,7 @@ func (s *Statistics) hasEnoughCapacity(wl *gridtypes.WorkloadWithID) (gridtypes.
155155
return used, fmt.Errorf("cannot fulfil required memory size %d bytes out of usable %d bytes", required.MRU, usable)
156156
}
157157

158-
//check other resources as well?
158+
// check other resources as well?
159159
return used, nil
160160
}
161161

@@ -235,6 +235,21 @@ func (s *statsStream) Total() gridtypes.Capacity {
235235
return s.stats.Total()
236236
}
237237

238+
func (s *statsStream) OpenConnections() ([]byte, error) {
239+
args := []string{"-ptn", "state", "established"}
240+
cmd := exec.Command("ss", args...)
241+
return cmd.Output()
242+
}
243+
244+
func (s *statsStream) openConnectionsCount() (int, error) {
245+
cmd := exec.Command("/bin/sh", "-c", "ss -ptn state established | wc -l")
246+
out, err := cmd.Output()
247+
if err != nil {
248+
return 0, err
249+
}
250+
return strconv.Atoi(strings.TrimSpace(string(out)))
251+
}
252+
238253
func (s *statsStream) Workloads() (int, error) {
239254
capacity, err := s.stats.storage.Capacity()
240255
if err != nil {
@@ -253,10 +268,17 @@ func (s *statsStream) GetCounters() (pkg.Counters, error) {
253268
if err != nil {
254269
return pkg.Counters{}, err
255270
}
271+
272+
connCount, err := s.openConnectionsCount()
273+
if err != nil {
274+
return pkg.Counters{}, err
275+
}
276+
256277
return pkg.Counters{
257-
Total: s.stats.Total(),
258-
Used: activeCounters.cap,
259-
System: reserved,
278+
Total: s.stats.Total(),
279+
Used: activeCounters.cap,
280+
System: reserved,
281+
OpenConnecions: connCount,
260282
Users: pkg.UsersCounters{
261283
Deployments: activeCounters.deployments,
262284
Workloads: activeCounters.workloads,
@@ -298,10 +320,6 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) {
298320
return nil, errors.Wrap(err, "failed to list available devices")
299321
}
300322

301-
if err != nil {
302-
return nil, errors.Wrap(err, "failed to list active deployments")
303-
}
304-
305323
used, err := usedGpus()
306324
if err != nil {
307325
return nil, errors.Wrap(err, "failed to list used gpus")

pkg/provision.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Statistics interface {
2929
Workloads() (int, error)
3030
GetCounters() (Counters, error)
3131
ListGPUs() ([]GPUInfo, error)
32+
OpenConnections() ([]byte, error)
3233
}
3334

3435
type Counters struct {
@@ -40,6 +41,8 @@ type Counters struct {
4041
System gridtypes.Capacity `json:"system"`
4142
// Users statistics by zos
4243
Users UsersCounters `json:"users"`
44+
// OpenConnecions number of open connections in the node
45+
OpenConnecions int `json:"open_connections"`
4346
}
4447

4548
// UsersCounters the expected counters for deployments and workloads

pkg/stubs/api_gateway_stub.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ package stubs
66

77
import (
88
"context"
9-
"time"
10-
119
types "github.com/centrifuge/go-substrate-rpc-client/v4/types"
1210
tfchainclientgo "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1311
zbus "github.com/threefoldtech/zbus"
1412
pkg "github.com/threefoldtech/zos/pkg"
13+
"time"
1514
)
1615

1716
type SubstrateGatewayStub struct {
@@ -31,25 +30,6 @@ func NewSubstrateGatewayStub(client zbus.Client) *SubstrateGatewayStub {
3130
}
3231
}
3332

34-
func (s *SubstrateGatewayStub) GetZosVersion(ctx context.Context) (ret0 string, ret1 error) {
35-
args := []interface{}{}
36-
result, err := s.client.RequestContext(ctx, s.module, s.object, "GetZosVersion", args...)
37-
if err != nil {
38-
panic(err)
39-
}
40-
41-
result.PanicOnError()
42-
ret1 = result.CallError()
43-
loader := zbus.Loader{
44-
&ret0,
45-
}
46-
47-
if err := result.Unmarshal(&loader); err != nil {
48-
panic(err)
49-
}
50-
return
51-
}
52-
5333
func (s *SubstrateGatewayStub) CreateNode(ctx context.Context, arg0 tfchainclientgo.Node) (ret0 uint32, ret1 error) {
5434
args := []interface{}{arg0}
5535
result, err := s.client.RequestContext(ctx, s.module, s.object, "CreateNode", args...)
@@ -305,6 +285,23 @@ func (s *SubstrateGatewayStub) GetTwinByPubKey(ctx context.Context, arg0 []uint8
305285
return
306286
}
307287

288+
func (s *SubstrateGatewayStub) GetZosVersion(ctx context.Context) (ret0 string, ret1 error) {
289+
args := []interface{}{}
290+
result, err := s.client.RequestContext(ctx, s.module, s.object, "GetZosVersion", args...)
291+
if err != nil {
292+
panic(err)
293+
}
294+
result.PanicOnError()
295+
ret1 = result.CallError()
296+
loader := zbus.Loader{
297+
&ret0,
298+
}
299+
if err := result.Unmarshal(&loader); err != nil {
300+
panic(err)
301+
}
302+
return
303+
}
304+
308305
func (s *SubstrateGatewayStub) Report(ctx context.Context, arg0 []tfchainclientgo.NruConsumption) (ret0 types.Hash, ret1 error) {
309306
args := []interface{}{arg0}
310307
result, err := s.client.RequestContext(ctx, s.module, s.object, "Report", args...)

pkg/stubs/statistics_stub.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ func (s *StatisticsStub) ListGPUs(ctx context.Context) (ret0 []pkg.GPUInfo, ret1
7979
return
8080
}
8181

82+
func (s *StatisticsStub) OpenConnections(ctx context.Context) (ret0 []uint8, ret1 error) {
83+
args := []interface{}{}
84+
result, err := s.client.RequestContext(ctx, s.module, s.object, "OpenConnections", args...)
85+
if err != nil {
86+
panic(err)
87+
}
88+
result.PanicOnError()
89+
ret1 = result.CallError()
90+
loader := zbus.Loader{
91+
&ret0,
92+
}
93+
if err := result.Unmarshal(&loader); err != nil {
94+
panic(err)
95+
}
96+
return
97+
}
98+
8299
func (s *StatisticsStub) ReservedStream(ctx context.Context) (<-chan gridtypes.Capacity, error) {
83100
ch := make(chan gridtypes.Capacity, 1)
84101
recv, err := s.client.Stream(ctx, s.module, s.object, "ReservedStream")

0 commit comments

Comments
 (0)