-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysinfo.go
144 lines (136 loc) · 3.61 KB
/
sysinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"strings"
"time"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmic/utils"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
var sysInfoPaths = []*gnmi.Path{
{
Elem: []*gnmi.PathElem{
{Name: "system"},
{Name: "name"},
{Name: "host-name"},
},
},
// {
// Elem: []*gnmi.PathElem{
// {Name: "interface",
// Key: map[string]string{"name": "mgmt0"},
// },
// {Name: "subinterface"},
// {Name: "ipv4"},
// {Name: "address"},
// {Name: "status"},
// },
// },
// {
// Elem: []*gnmi.PathElem{
// {Name: "interface",
// Key: map[string]string{"name": "mgmt0"},
// },
// {Name: "subinterface"},
// {Name: "ipv6"},
// {Name: "address"},
// {Name: "status"},
// },
// },
{
Elem: []*gnmi.PathElem{
{Name: "system"},
{Name: "information"},
{Name: "version"},
},
},
{
Elem: []*gnmi.PathElem{
{Name: "platform"},
{Name: "chassis"},
},
},
}
type systemInfo struct {
Name string
Version string
ChassisType string `json:"type,omitempty"`
ChassisMacAddress string `json:"hw-mac-address,omitempty"`
ChassisCLEICode string `json:"clei-code,omitempty"`
ChassisPartNumber string `json:"part-number,omitempty"`
ChassisSerialNumber string `json:"serial-number,omitempty"`
}
func createGNMIClient(ctx context.Context) (gnmi.GNMIClient, error) {
timeoutCtx, cancel := context.WithTimeout(ctx, retryInterval)
defer cancel()
conn, err := grpc.DialContext(timeoutCtx, gnmiServerUnixSocket,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock())
if err != nil {
return nil, err
}
return gnmi.NewGNMIClient(conn), nil
}
func (a *app) getSystemInfo(ctx context.Context) (*systemInfo, error) {
ctx = metadata.AppendToOutgoingContext(ctx,
"username", a.config.username,
"password", a.config.password,
)
sctx, cancel := context.WithCancel(ctx)
defer cancel()
START:
select {
case <-sctx.Done():
return nil, ctx.Err()
default:
gnmiClient, err := createGNMIClient(sctx)
if err != nil {
log.Errorf("failed to create a gnmi connection to %q: %v", gnmiServerUnixSocket, err)
time.Sleep(retryInterval)
goto START
}
rsp, err := gnmiClient.Get(sctx,
&gnmi.GetRequest{
Path: sysInfoPaths,
Type: gnmi.GetRequest_STATE,
Encoding: gnmi.Encoding_ASCII,
})
if err != nil {
log.Errorf("failed Get response: %v", err)
time.Sleep(retryInterval)
goto START
}
sysInfo := new(systemInfo)
for _, n := range rsp.GetNotification() {
for _, u := range n.GetUpdate() {
path := utils.GnmiPathToXPath(u.GetPath(), true)
if strings.Contains(path, "system/name") {
sysInfo.Name = u.GetVal().GetStringVal()
}
if strings.Contains(path, "system/information/version") {
sysInfo.Version = u.GetVal().GetStringVal()
}
if strings.Contains(path, "platform/chassis/type") {
sysInfo.ChassisType = u.GetVal().GetStringVal()
}
if strings.Contains(path, "platform/chassis/hw-mac-address") {
sysInfo.ChassisMacAddress = u.GetVal().GetStringVal()
}
if strings.Contains(path, "platform/chassis/part-number") {
sysInfo.ChassisPartNumber = u.GetVal().GetStringVal()
}
if strings.Contains(path, "platform/chassis/clei-code") {
sysInfo.ChassisCLEICode = u.GetVal().GetStringVal()
}
if strings.Contains(path, "platform/chassis/serial-number") {
sysInfo.ChassisSerialNumber = u.GetVal().GetStringVal()
}
}
}
log.Debugf("system info: %+v", sysInfo)
return sysInfo, nil
}
}