Skip to content

Commit

Permalink
Merge pull request #30 from treydock/fix-errs
Browse files Browse the repository at this point in the history
Fix possible index out of range parsing errors
  • Loading branch information
treydock authored Mar 31, 2021
2 parents 92900c8 + 017d80a commit 9f29ae0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
16 changes: 12 additions & 4 deletions collectors/mmdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ func (c *MmdfCollector) mmdfCollect(fs string) (DFMetric, error) {
if err != nil {
return DFMetric{}, err
}
dfMetric := parse_mmdf(out, c.logger)
return dfMetric, nil
dfMetric, err := parse_mmdf(out, c.logger)
return dfMetric, err
}

func mmlfsfsFilesystems(ctx context.Context, logger log.Logger) ([]string, error) {
Expand Down Expand Up @@ -204,7 +204,7 @@ func mmdf(fs string, ctx context.Context) (string, error) {
return out.String(), nil
}

func parse_mmdf(out string, logger log.Logger) DFMetric {
func parse_mmdf(out string, logger log.Logger) (DFMetric, error) {
var dfMetrics DFMetric
headers := make(map[string][]string)
values := make(map[string][]string)
Expand All @@ -229,6 +229,14 @@ func parse_mmdf(out string, logger log.Logger) DFMetric {
ps := reflect.ValueOf(&dfMetrics) // pointer to struct - addressable
s := ps.Elem() // struct
for k, vals := range headers {
if _, ok := values[k]; !ok {
level.Error(logger).Log("msg", "Header section missing from values", "header", k)
return dfMetrics, fmt.Errorf("Header section missing from values: %s", k)
}
if len(vals) != len(values[k]) {
level.Error(logger).Log("msg", "Length of headers does not equal length of values", "header", k, "values", len(values[k]), "headers", len(vals))
return dfMetrics, fmt.Errorf("Length of headers does not equal length of values: %s", k)
}
for i, v := range vals {
mapKey := fmt.Sprintf("%s:%s", k, v)
value := values[k][i]
Expand All @@ -249,5 +257,5 @@ func parse_mmdf(out string, logger log.Logger) DFMetric {
}
}
}
return dfMetrics
return dfMetrics, nil
}
48 changes: 47 additions & 1 deletion collectors/mmdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ mmdf:data:0:1:::3647786188800:475190722560:13:12059515296:0:
mmdf:metadata:0:1:::13891534848:6011299328:43:58139768:0:
mmdf:fsTotal:0:1:::3661677723648:481202021888:14:12117655064:0:
mmdf:inode:0:1:::430741822:484301506:915043328:1332164000:
`
mmdfStdoutErrValues = `
mmdf:nsd:HEADER:version:reserved:reserved:nsdName:storagePool:diskSize:failureGroup:metadata:data:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:diskAvailableForAlloc:
mmdf:poolTotal:HEADER:version:reserved:reserved:poolName:poolSize:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:maxDiskSize:
mmdf:data:HEADER:version:reserved:reserved:totalData:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:metadata:HEADER:version:reserved:reserved:totalMetadata:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:fsTotal:HEADER:version:reserved:reserved:fsSize:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:inode:HEADER:version:reserved:reserved:usedInodes:freeInodes:allocatedInodes:maxInodes:
mmdf:nsd:0:1:::P_META_VD102:system:771751936:300:Yes:No:320274944:41:5005384:1::
mmdf:nsd:0:1:::P_DATA_VD02:data:46766489600:200:No:Yes:6092915712:13:154966272:0::
mmdf:poolTotal:0:1:::data:3647786188800:475190722560:13:12059515296:0:3860104580096:
mmdf:data:0:1:::3647786188800:475190722560:13:12059515296:0:
mmdf:fsTotal:0:1:::3661677723648:481202021888:14:12117655064:0:
mmdf:inode:0:1:::430741822:484301506:915043328:1332164000:
`
mmdfStdoutErrLen = `
mmdf:nsd:HEADER:version:reserved:reserved:nsdName:storagePool:diskSize:failureGroup:metadata:data:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:diskAvailableForAlloc:
mmdf:poolTotal:HEADER:version:reserved:reserved:poolName:poolSize:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:maxDiskSize:
mmdf:data:HEADER:version:reserved:reserved:totalData:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:metadata:HEADER:version:reserved:reserved:totalMetadata:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:fsTotal:HEADER:version:reserved:reserved:fsSize:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:inode:HEADER:version:reserved:reserved:usedInodes:freeInodes:allocatedInodes:maxInodes:
mmdf:nsd:0:1:::P_META_VD102:system:771751936:300:Yes:No:320274944:41:5005384:1::
mmdf:nsd:0:1:::P_DATA_VD02:data:46766489600:200:No:Yes:6092915712:13:154966272:0::
mmdf:poolTotal:0:1:::data:3647786188800:475190722560:13:12059515296:0:3860104580096:
mmdf:data:0:1:::3647786188800:475190722560:13:12059515296:0:
mmdf:metadata:0:1:::13891534848:6011299328:43:58139768:0:
mmdf:fsTotal:0:1:::3661677723648:481202021888:14:12117655064:
mmdf:inode:0:1:::430741822:484301506:915043328:1332164000:
`
)

Expand Down Expand Up @@ -93,7 +122,11 @@ func TestMmdfTimeout(t *testing.T) {
}

func TestParseMmdf(t *testing.T) {
dfmetrics := parse_mmdf(mmdfStdout, log.NewNopLogger())
dfmetrics, err := parse_mmdf(mmdfStdout, log.NewNopLogger())
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
}
if dfmetrics.InodesFree != 484301506 {
t.Errorf("Unexpected value for InodesFree, got %v", dfmetrics.InodesFree)
}
Expand All @@ -105,6 +138,19 @@ func TestParseMmdf(t *testing.T) {
}
}

func TestParseMmdfErrors(t *testing.T) {
_, err := parse_mmdf(mmdfStdoutErrValues, log.NewNopLogger())
if err == nil {
t.Errorf("Expected error")
return
}
_, err = parse_mmdf(mmdfStdoutErrLen, log.NewNopLogger())
if err == nil {
t.Errorf("Expected error")
return
}
}

func TestMmdfCollector(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{}); err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 9f29ae0

Please sign in to comment.