Skip to content

Commit c37ecf5

Browse files
authored
Merge pull request #31 from karimra/async-sysinfo
async sysinfo
2 parents db7020e + e73ea0c commit c37ecf5

File tree

5 files changed

+61
-68
lines changed

5 files changed

+61
-68
lines changed

config.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ type stringValue struct {
104104
Value string `json:"value,omitempty"`
105105
}
106106

107-
// type uint64Value struct {
108-
// Value uint64 `json:"value,omitempty"`
109-
// }
110-
111107
type boolValue struct {
112108
Value bool `json:"value,omitempty"`
113109
}

example/acls.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ updates:
2121
source-port:
2222
range:
2323
start: 57401
24-
end: 57502
24+
end: 57502
25+
26+
- path: /system/gnmi-server/unix-socket
27+
encoding: json_ietf
28+
value:
29+
admin-state: enable
30+
use-authentication: false

example/run.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
22

33
# download the RPM file
4-
wget -qL https://github.com/karimra/srl-grpc-tunnel/releases/download/v0.0.1/srl-grpc-tunnel_0.0.1_Linux_x86_64.rpm -P rpm/
4+
5+
wget -qL https://github.com/karimra/srl-grpc-tunnel/releases/download/v0.0.1/srl-grpc-tunnel_0.1.5_Linux_x86_64.rpm -P rpm/
56

67
#deploy the lab
78
sudo clab deploy -t grpc_tunnel.clab.yaml --reconfigure

main.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,26 @@ CRAGENT:
5050

5151
a := newApp(ctx, WithAgent(app))
5252
//
53-
SYSINFO:
54-
log.Info("getting system info...")
55-
sysInfo, err := a.getSystemInfo(ctx)
56-
if err != nil {
57-
log.Errorf("failed to get system info %q: %v", agentName, err)
58-
log.Infof("retrying in %s", retryInterval)
59-
time.Sleep(retryInterval)
60-
goto SYSINFO
61-
}
62-
log.Infof("system info: %+v", sysInfo)
63-
a.config.sysInfo = *sysInfo
53+
go func() {
54+
for {
55+
select {
56+
case <-ctx.Done():
57+
return
58+
default:
59+
log.Info("getting system info...")
60+
sysInfo, err := a.getSystemInfo(ctx)
61+
if err != nil {
62+
log.Errorf("failed to get system info %q: %v", agentName, err)
63+
log.Infof("retrying in %s", retryInterval)
64+
time.Sleep(retryInterval)
65+
continue
66+
}
67+
log.Infof("system info: %+v", sysInfo)
68+
a.config.sysInfo = *sysInfo
69+
return
70+
}
71+
}
72+
}()
6473
log.Info("starting config handler...")
6574
a.start(ctx)
6675
}

sysinfo.go

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ var sysInfoPaths = []*gnmi.Path{
2121
{Name: "host-name"},
2222
},
2323
},
24-
{
25-
Elem: []*gnmi.PathElem{
26-
{Name: "interface",
27-
Key: map[string]string{"name": "mgmt0"},
28-
},
29-
{Name: "subinterface"},
30-
{Name: "ipv4"},
31-
{Name: "address"},
32-
{Name: "status"},
33-
},
34-
},
35-
{
36-
Elem: []*gnmi.PathElem{
37-
{Name: "interface",
38-
Key: map[string]string{"name": "mgmt0"},
39-
},
40-
{Name: "subinterface"},
41-
{Name: "ipv6"},
42-
{Name: "address"},
43-
{Name: "status"},
44-
},
45-
},
24+
// {
25+
// Elem: []*gnmi.PathElem{
26+
// {Name: "interface",
27+
// Key: map[string]string{"name": "mgmt0"},
28+
// },
29+
// {Name: "subinterface"},
30+
// {Name: "ipv4"},
31+
// {Name: "address"},
32+
// {Name: "status"},
33+
// },
34+
// },
35+
// {
36+
// Elem: []*gnmi.PathElem{
37+
// {Name: "interface",
38+
// Key: map[string]string{"name": "mgmt0"},
39+
// },
40+
// {Name: "subinterface"},
41+
// {Name: "ipv6"},
42+
// {Name: "address"},
43+
// {Name: "status"},
44+
// },
45+
// },
4646
{
4747
Elem: []*gnmi.PathElem{
4848
{Name: "system"},
@@ -61,14 +61,11 @@ var sysInfoPaths = []*gnmi.Path{
6161
type systemInfo struct {
6262
Name string
6363
Version string
64-
ChassisType string
65-
ChassisMacAddress string
66-
ChassisCLEICode string
67-
ChassisPartNumber string
68-
ChassisSerialNumber string
69-
//NetworkInstance string
70-
IPAddrV4 string
71-
IPAddrV6 string
64+
ChassisType string `json:"type,omitempty"`
65+
ChassisMacAddress string `json:"hw-mac-address,omitempty"`
66+
ChassisCLEICode string `json:"clei-code,omitempty"`
67+
ChassisPartNumber string `json:"part-number,omitempty"`
68+
ChassisSerialNumber string `json:"serial-number,omitempty"`
7269
}
7370

7471
func createGNMIClient(ctx context.Context) (gnmi.GNMIClient, error) {
@@ -84,7 +81,10 @@ func createGNMIClient(ctx context.Context) (gnmi.GNMIClient, error) {
8481
}
8582

8683
func (a *app) getSystemInfo(ctx context.Context) (*systemInfo, error) {
87-
ctx = metadata.AppendToOutgoingContext(ctx, "username", a.config.username, "password", a.config.password)
84+
ctx = metadata.AppendToOutgoingContext(ctx,
85+
"username", a.config.username,
86+
"password", a.config.password,
87+
)
8888
sctx, cancel := context.WithCancel(ctx)
8989
defer cancel()
9090
START:
@@ -94,7 +94,7 @@ START:
9494
default:
9595
gnmiClient, err := createGNMIClient(sctx)
9696
if err != nil {
97-
log.Infof("failed to create a gnmi connection to %q: %v", gnmiServerUnixSocket, err)
97+
log.Errorf("failed to create a gnmi connection to %q: %v", gnmiServerUnixSocket, err)
9898
time.Sleep(retryInterval)
9999
goto START
100100
}
@@ -115,16 +115,6 @@ START:
115115
for _, n := range rsp.GetNotification() {
116116
for _, u := range n.GetUpdate() {
117117
path := utils.GnmiPathToXPath(u.GetPath(), true)
118-
if strings.HasPrefix(path, "interface") {
119-
if strings.Contains(path, "/ipv4/address/status") {
120-
ip := getPathKeyVal(u.GetPath(), "address", "ip-prefix")
121-
sysInfo.IPAddrV4 = strings.Split(ip, "/")[0]
122-
}
123-
if strings.Contains(path, "/ipv6/address/status") {
124-
ip := getPathKeyVal(u.GetPath(), "address", "ip-prefix")
125-
sysInfo.IPAddrV6 = strings.Split(ip, "/")[0]
126-
}
127-
}
128118
if strings.Contains(path, "system/name") {
129119
sysInfo.Name = u.GetVal().GetStringVal()
130120
}
@@ -152,12 +142,3 @@ START:
152142
return sysInfo, nil
153143
}
154144
}
155-
156-
func getPathKeyVal(p *gnmi.Path, elem, key string) string {
157-
for _, e := range p.GetElem() {
158-
if e.Name == elem {
159-
return e.Key[key]
160-
}
161-
}
162-
return ""
163-
}

0 commit comments

Comments
 (0)