Skip to content

Commit

Permalink
[Bug] fix bug with empty filter values returning no results in system…
Browse files Browse the repository at this point in the history
…/service (elastic#19812) (elastic#19914)

* fix bug with empty filter values returning no results

* add changelog entry

(cherry picked from commit 1e531b1)
  • Loading branch information
fearful-symmetry authored Jul 14, 2020
1 parent 564678f commit dd98a99
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix a rate limit related issue in httpjson input for Okta module. {issue}18530[18530] {pull}18534[18534]
- Fix tls mapping in suricata module {issue}19492[19492] {pull}19494[19494]
- Fix Cisco ASA dissect pattern for 313008 & 313009 messages. {pull}19149[19149]
- Fix bug with empty filter values in system/service {pull}19812[19812]

*Heartbeat*

Expand Down
40 changes: 25 additions & 15 deletions metricbeat/module/system/service/dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,43 @@ func listUnitsWrapper(conn *dbus.Conn, states, patterns []string) ([]dbus.UnitSt
if err != nil {
return nil, errors.Wrap(err, "ListUnits error")
}
if len(patterns) > 0 {
units, err = matchUnitPatterns(patterns, units)
if err != nil {
return nil, errors.Wrap(err, "error matching unit patterns")
}

units, err = matchUnitPatterns(patterns, units)
if err != nil {
return nil, errors.Wrap(err, "error matching unit patterns")
}

if len(states) > 0 {
var finalUnits []dbus.UnitStatus
for _, unit := range units {
for _, state := range states {
if unit.LoadState == state || unit.ActiveState == state || unit.SubState == state {
finalUnits = append(finalUnits, unit)
break
}
finalUnits := matchUnitState(states, units)

return finalUnits, nil
}

// matchUnitState returns a list of units that match the pattern list
// This checks the LoadState, ActiveState, and SubState for a matching status string
func matchUnitState(states []string, units []dbus.UnitStatus) []dbus.UnitStatus {
if len(states) == 0 {
return units
}
var finalUnits []dbus.UnitStatus
for _, unit := range units {
for _, state := range states {
if unit.LoadState == state || unit.ActiveState == state || unit.SubState == state {
finalUnits = append(finalUnits, unit)
break
}
}
return finalUnits, nil
}
return finalUnits

return units, nil
}

// matchUnitPatterns returns a list of units that match the pattern list.
// This algo, including filepath.Match, is designed to (somewhat) emulate the behavior of ListUnitsByPatterns, which uses `fnmatch`.
func matchUnitPatterns(patterns []string, units []dbus.UnitStatus) ([]dbus.UnitStatus, error) {
var matchUnits []dbus.UnitStatus
if len(patterns) == 0 {
return units, nil
}
for _, unit := range units {
for _, pattern := range patterns {
match, err := filepath.Match(pattern, unit.Name)
Expand Down
26 changes: 24 additions & 2 deletions metricbeat/module/system/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ import (

var exampleUnits = []dbus.UnitStatus{
dbus.UnitStatus{
Name: "sshd.service",
Name: "sshd.service",
LoadState: "active",
},
dbus.UnitStatus{
Name: "metricbeat.service",
Name: "metricbeat.service",
LoadState: "active",
},
dbus.UnitStatus{
Name: "filebeat.service",
Expand Down Expand Up @@ -104,3 +106,23 @@ func TestFilterMatches(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, shouldMatch, 1)
}

func TestNoFilter(t *testing.T) {
shouldReturnResults, err := matchUnitPatterns([]string{}, exampleUnits)
assert.NoError(t, err)
assert.Len(t, shouldReturnResults, 3)
}

func TestUnitStateFilter(t *testing.T) {
stateFilter := []string{
"active",
}
shouldReturnResults := matchUnitState(stateFilter, exampleUnits)
assert.Len(t, shouldReturnResults, 2)

}

func TestUnitStateNoFilter(t *testing.T) {
shouldReturnResults := matchUnitState([]string{}, exampleUnits)
assert.Len(t, shouldReturnResults, 3)
}

0 comments on commit dd98a99

Please sign in to comment.