Skip to content

Commit

Permalink
parseNames support for unconnected non-SDR lines (#18)
Browse files Browse the repository at this point in the history
* parseNames support for unconnected non-SDR lines

* add test case
  • Loading branch information
3th1nk authored Dec 21, 2023
1 parent 9abd620 commit 49b34c0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions collectors/fixtures/ibnetdiscover/test2.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ CA 88 1 0xb83fd20300da1138 4x HDR - SW 51 79 0x946dae0300630bf6 ( 'worker
SW 24 1 0x946dae0300618c82 4x SDR '5FB0406-spine-IB01 '
SW 25 1 0x946dae0300618c83 4x SDR ' 5FB0406-spine-IB02 '
SW 9 81 0x946dae030053ec1a 4x HDR - CA 60 1 0x946dae0300630bfe ( ' 5FB0406-spine-IB03 ' - 'Mellanox Technologies Aggregation Node' )
SW 478 22 0x0002c9020040f160 4x QDR 'Infiniscale-IV Mellanox Technologies'
1 change: 1 addition & 0 deletions collectors/fixtures/ibnetdiscover/test3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SW 479 22 0x0002c9020040f161 4x QDR Infiniscale-IV Mellanox Technologies
65 changes: 41 additions & 24 deletions collectors/ibnetdiscover.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,18 @@ func ibnetdiscoverParse(out string, logger log.Logger) (*[]InfinibandDevice, *[]
}
// check the last item, because name may have space so that it is split into multiple items
name := items[len(items)-1]
if strings.HasSuffix(name, `'`) && !isPairedQuotesName(name) {
for i := len(items) - 2; i > 5; i-- {
name = items[i] + name
if isPairedQuotesName(name) {
items = append(items[:i], name)
break
if strings.HasSuffix(name, `'`) {
if !isPairedQuotesName(name) {
for i := len(items) - 2; i > 5; i-- {
name = items[i] + ` ` + name
if isPairedQuotesName(name) {
items = append(items[:i], name)
break
}
}
}
name = strings.Trim(name, `'`)
items[len(items)-1] = name
}
if items[5] == "SDR" && len(items) == 7 {
level.Debug(logger).Log("msg", "Skipping split mode port", "line", line)
Expand All @@ -173,18 +177,21 @@ func ibnetdiscoverParse(out string, logger log.Logger) (*[]InfinibandDevice, *[]
device.Rate = effectiveRate
device.RawRate = rawRate
}

portName, uplinkName, err := parseNames(line)
if err != nil {
level.Error(logger).Log("msg", "Unable to parse names", "err", err, "type", device.Type, "guid", device.GUID, "line", line)
return nil, nil, err
}
if uplinkName != "" {
uplink.Type = items[7]
uplink.LID = items[8]
uplink.PortNumber = items[9]
uplink.GUID = items[10]
uplink.Name = uplinkName
device.Uplinks[portNumber] = uplink
}
device.Name = portName
uplink.Type = items[7]
uplink.LID = items[8]
uplink.PortNumber = items[9]
uplink.GUID = items[10]
uplink.Name = uplinkName
device.Uplinks[portNumber] = uplink
devices[guid] = device
}
deviceGUIDs := getDeviceGUIDs(devices)
Expand Down Expand Up @@ -226,20 +233,30 @@ func isPairedQuotesName(name string) bool {
}

func parseNames(line string) (string, string, error) {
re := regexp.MustCompile(`\( '(.+)' - '(.+)' \)`)
matches := re.FindStringSubmatch(line)
if len(matches) != 3 {
return "", "", fmt.Errorf("Unable to extract names using regexp")
}
portName := strings.TrimSpace(matches[1])
uplinkName := strings.TrimSpace(matches[2])
if strings.Contains(portName, " HCA") {
portName = strings.Split(portName, " ")[0]
if strings.Contains(line, `(`) {
re := regexp.MustCompile(`\( '(.+)' - '(.+)' \)`)
matches := re.FindStringSubmatch(line)
if len(matches) != 3 {
return "", "", fmt.Errorf("Unable to extract names using regexp")
}
portName := strings.TrimSpace(matches[1])
uplinkName := strings.TrimSpace(matches[2])
if strings.Contains(portName, " HCA") {
portName = strings.Split(portName, " ")[0]
}
if strings.Contains(uplinkName, " HCA") {
uplinkName = strings.Split(uplinkName, " ")[0]
}
return portName, uplinkName, nil
}
if strings.Contains(uplinkName, " HCA") {
uplinkName = strings.Split(uplinkName, " ")[0]

if idx := strings.Index(line, `'`); idx != -1 {
portName := strings.TrimSpace(line[idx+1:])
if idx = strings.Index(portName, `'`); idx != -1 {
return strings.TrimSpace(portName[:idx]), "", nil
}
}
return portName, uplinkName, nil
return "", "", fmt.Errorf("Unable to extract names")
}

func getDeviceGUIDs(devices map[string]InfinibandDevice) []string {
Expand Down
17 changes: 17 additions & 0 deletions collectors/ibnetdiscover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ func TestIbnetdiscoverParse2(t *testing.T) {
}

expectSwitches := []InfinibandDevice{
{Type: "SW", LID: "478", GUID: "0x0002c9020040f160", Rate: 8 * 4 * 125000000, RawRate: 10 * 4 * 125000000, Name: "Infiniscale-IV Mellanox Technologies",
Uplinks: map[string]InfinibandUplink{},
},
{Type: "SW", LID: "9", GUID: "0x946dae030053ec1a", Rate: 50 * 4 * 125000000, RawRate: 50 * 4 * 125000000, Name: "5FB0406-spine-IB03",
Uplinks: map[string]InfinibandUplink{
"81": {Type: "CA", LID: "60", PortNumber: "1", GUID: "0x946dae0300630bfe", Name: "Mellanox Technologies Aggregation Node"},
Expand Down Expand Up @@ -247,6 +250,20 @@ func TestIbnetdiscoverParse2(t *testing.T) {
}
}

func TestIbnetdiscoverParse3(t *testing.T) {
out, err := ReadFixture("ibnetdiscover", "test3")
if err != nil {
t.Fatal("Unable to read fixture")
}
w := log.NewSyncWriter(os.Stderr)
logger := log.NewLogfmtLogger(w)
_, _, err = ibnetdiscoverParse(out, logger)
if err == nil || !strings.Contains(err.Error(), "Unable to extract names") {
t.Errorf("Unexpected error: Unable to extract names")
return
}
}

func TestIbnetdiscoverParseErrors(t *testing.T) {
tests := []struct {
Input string
Expand Down

0 comments on commit 49b34c0

Please sign in to comment.