Skip to content

Commit 079232f

Browse files
Read aliases from linuxptp-daemon socket
1 parent 0377f5a commit 079232f

File tree

4 files changed

+69
-24
lines changed

4 files changed

+69
-24
lines changed

plugins/ptp_operator/metrics/metrics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var (
1919
// NodeName from the env
2020
ptpNodeName = ""
2121
masterOffsetSource = ""
22+
23+
aliasRegex = regexp.MustCompile(`ALIAS:\s*(?P<interface>[\w|-|_]+)\s+->\s(?P<alias>[\w|-|_]+)`)
2224
)
2325

2426
const (
@@ -90,6 +92,12 @@ func (p *PTPEventManager) ExtractMetrics(msg string) {
9092
log.Errorf("failed to extract %s", msg)
9193
}
9294
}()
95+
96+
if match := aliasRegex.FindStringSubmatch(msg); len(match) >= 3 {
97+
utils.Aliases.SetAlias(match[1], match[2])
98+
return
99+
}
100+
93101
replacer := strings.NewReplacer("[", " ", "]", " ", ":", " ")
94102
output := replacer.Replace(msg)
95103
fields := strings.Fields(output)

plugins/ptp_operator/metrics/metrics_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ var testCases = []TestCase{
209209
log: "ptp4l[4270543.688]: [ptp4l.1.config:5] port 2 (ens3f1): SLAVE to FAULTY on FAULT_DETECTED (FT_UNSPECIFIED)",
210210
from: "master",
211211
process: "ptp4l",
212-
iface: "ens3fx",
212+
iface: "ens3f1",
213213
logPtp4lConfigName: logPtp4lConfigDualFollower.Name,
214214
expectedSyncStateCheck: true,
215215
expectedSyncState: float64(types.HOLDOVER),
@@ -236,7 +236,7 @@ var testCases = []TestCase{
236236
log: "dpll[1000000100]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 5 phase_status 3 pps_status 1 s2",
237237
from: "master",
238238
process: "dpll",
239-
iface: "ens7fx",
239+
iface: "ens7f0",
240240
lastSyncState: ptp.FREERUN,
241241
expectedSyncStateCheck: true,
242242
expectedSyncState: float64(types.LOCKED),
@@ -249,7 +249,7 @@ var testCases = []TestCase{
249249
log: "dpll[1000000110]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 5 phase_status 3 pps_status 0 s0",
250250
from: "master",
251251
process: "dpll",
252-
iface: "ens7fx",
252+
iface: "ens7f0",
253253
lastSyncState: ptp.LOCKED,
254254
expectedSyncStateCheck: true,
255255
expectedSyncState: float64(types.FREERUN),
@@ -262,7 +262,7 @@ var testCases = []TestCase{
262262
log: "dpll[1000000120]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 7 phase_status 3 pps_status 0 s1",
263263
from: "master",
264264
process: "dpll",
265-
iface: "ens7fx",
265+
iface: "ens7f0",
266266
expectedSyncStateCheck: true,
267267
expectedSyncState: float64(types.HOLDOVER),
268268
expectedPpsStatusCheck: true,
@@ -274,7 +274,7 @@ var testCases = []TestCase{
274274
log: "ts2phc[1000000200]:[ts2phc.0.config] ens2f0 nmea_status 0 offset 999999 s0",
275275
from: "master",
276276
process: "ts2phc",
277-
iface: "ens2fx",
277+
iface: "ens2f0",
278278
lastSyncState: ptp.LOCKED,
279279
expectedNmeaStatusCheck: true,
280280
expectedNmeaStatus: 0,
@@ -285,7 +285,7 @@ var testCases = []TestCase{
285285
log: "ts2phc[1000000210]:[ts2phc.0.config] ens2f0 nmea_status 1 offset 0 s2",
286286
from: "master",
287287
process: "ts2phc",
288-
iface: "ens2fx",
288+
iface: "ens2f0",
289289
expectedNmeaStatusCheck: true,
290290
expectedNmeaStatus: 1,
291291
expectedEvent: []ptp.EventType{},
@@ -295,7 +295,7 @@ var testCases = []TestCase{
295295
log: "ts2phc[1000000300]: [ts2phc.0.config] ens2f0 master offset 0 s2 freq -0",
296296
from: "master",
297297
process: "ts2phc",
298-
iface: "ens2fx",
298+
iface: "ens2f0",
299299
expectedPtpOffsetCheck: true,
300300
expectedPtpOffset: 0,
301301
expectedEvent: []ptp.EventType{ptp.PtpStateChange, ptp.SyncStateChange},
@@ -305,7 +305,7 @@ var testCases = []TestCase{
305305
log: "ts2phc[1000000310]: [ts2phc.0.config] ens7f0 master offset 999 s0 freq -0",
306306
from: "master",
307307
process: "ts2phc",
308-
iface: "ens7fx",
308+
iface: "ens7f0",
309309
expectedPtpOffsetCheck: true,
310310
expectedPtpOffset: 999,
311311
expectedSyncStateCheck: true,
@@ -317,7 +317,7 @@ var testCases = []TestCase{
317317
log: "GM[1000000400]:[ts2phc.0.config] ens2f0 T-GM-STATUS s0",
318318
from: "master",
319319
process: "GM",
320-
iface: "ens2fx",
320+
iface: "ens2f0",
321321
expectedSyncStateCheck: true,
322322
expectedSyncState: float64(types.FREERUN),
323323
expectedEvent: []ptp.EventType{ptp.PtpStateChange, ptp.SyncStateChange},
@@ -327,7 +327,7 @@ var testCases = []TestCase{
327327
log: "gnss[1000000500]:[ts2phc.0.config] ens2f1 gnss_status 3 offset 5 s2",
328328
from: "gnss",
329329
process: "gnss",
330-
iface: "ens2fx",
330+
iface: "ens2f1",
331331
lastSyncState: ptp.FREERUN,
332332
expectedPtpOffsetCheck: true,
333333
expectedPtpOffset: 5,
@@ -400,7 +400,7 @@ func setup() {
400400
statsMaster := stats.NewStats(logPtp4lConfig.Name)
401401
statsMaster.SetOffsetSource("master")
402402
statsMaster.SetProcessName("ts2phc")
403-
statsMaster.SetAlias("ens2fx")
403+
statsMaster.SetAlias("ens2f0")
404404

405405
statsSlave := stats.NewStats(logPtp4lConfig.Name)
406406
statsSlave.SetOffsetSource("phc")
@@ -414,7 +414,7 @@ func setup() {
414414
statsPHCDualFollower.SetOffsetSource("master")
415415
statsPHCDualFollower.SetProcessName("ptp4l")
416416
statsPHCDualFollower.SetLastSyncState("LOCKED")
417-
statsPHCDualFollower.SetAlias("ens3fx")
417+
statsPHCDualFollower.SetAlias("ens3f1")
418418

419419
statsRTDualFollower := stats.NewStats(logPtp4lConfigDualFollower.Name)
420420
statsRTDualFollower.SetOffsetSource("phc")

plugins/ptp_operator/utils/utils.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
package utils
22

3-
import "strings"
3+
import (
4+
"sync"
5+
)
6+
7+
// Aliases instance of the alias manager
8+
var Aliases = &AliasManager{}
9+
10+
// AliasManager ...
11+
type AliasManager struct {
12+
lock sync.RWMutex
13+
values map[string]string
14+
}
15+
16+
// Clear will remove all aliases
17+
func (a *AliasManager) Clear() {
18+
a.lock.Lock()
19+
defer a.lock.Unlock()
20+
a.values = make(map[string]string)
21+
}
22+
23+
// SetAlias...
24+
func (a *AliasManager) SetAlias(name, alias string) {
25+
a.lock.Lock()
26+
defer a.lock.Unlock()
27+
if a.values == nil {
28+
a.values = make(map[string]string)
29+
}
30+
a.values[name] = alias
31+
}
32+
33+
// GetAlias returns a interface name and returns the alias
34+
func (a *AliasManager) GetAlias(ifname string) string {
35+
a.lock.RLock()
36+
defer a.lock.RUnlock()
437

5-
func GetAlias(ifname string) string {
6-
alias := ""
738
if ifname != "" {
8-
// Aliases the interface name or <interface_name>.<vlan>
9-
dotIndex := strings.Index(ifname, ".")
10-
if dotIndex == -1 {
11-
// e.g. ens1f0 -> ens1fx
12-
alias = ifname[:len(ifname)-1] + "x"
13-
} else {
14-
// e.g ens1f0.100 -> ens1fx.100
15-
alias = ifname[:dotIndex-1] + "x" + ifname[dotIndex:]
39+
if alias, ok := a.values[ifname]; ok {
40+
return alias
1641
}
1742
}
18-
return alias
43+
return ifname
44+
}
45+
46+
// GetAlias masks interface names for metric reporting
47+
func GetAlias(ifname string) string {
48+
return Aliases.GetAlias(ifname)
1949
}

plugins/ptp_operator/utils/utils_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ func Test_GetAlias(t *testing.T) {
1919
{"eth1.100", "ethx.100"},
2020
{"eth1.100.XYZ", "ethx.100.XYZ"},
2121
}
22+
23+
defer utils.Aliases.Clear()
24+
25+
for _, tc := range testCases {
26+
utils.Aliases.SetAlias(tc.ifname, tc.expectedAlias)
27+
}
28+
2229
for _, tc := range testCases {
2330
t.Run(fmt.Sprintf("%s->%s", tc.ifname, tc.expectedAlias), func(t *testing.T) {
2431
assert.Equal(t, tc.expectedAlias, utils.GetAlias(tc.ifname))

0 commit comments

Comments
 (0)