-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RHEL/Centos support to the system/users metricset #16902
Merged
fearful-symmetry
merged 12 commits into
elastic:master
from
fearful-symmetry:services-rhel-support
Mar 12, 2020
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7814adf
major refactor to support different systemd version
fearful-symmetry 2cd0644
Merge branch 'master' into services-rhel-support
fearful-symmetry f97a6c5
format and updates
fearful-symmetry 2b6a457
update ref docs
fearful-symmetry a2990bc
update ref, again
fearful-symmetry ffa8e68
Merge branch 'master' into services-rhel-support
fearful-symmetry 4bedeb1
add newline
fearful-symmetry 1178054
Fix error string
fearful-symmetry 48ef07c
add changelog entry
fearful-symmetry 5461eec
make update
fearful-symmetry d1d69df
add build target
fearful-symmetry c3699b7
Merge branch 'services-rhel-support' of github.com:fearful-symmetry/b…
fearful-symmetry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package service | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/coreos/go-systemd/v22/dbus" | ||
dbusRaw "github.com/godbus/dbus" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type unitFetcher func(conn *dbus.Conn, states, patterns []string) ([]dbus.UnitStatus, error) | ||
|
||
// instrospectForUnitMethods determines what methods are available via dbus for listing systemd units. | ||
// We have a number of functions, some better than others, for getting and filtering unit lists. | ||
// This will attempt to find the most optimal method, and move down to methods that require more work. | ||
func instrospectForUnitMethods() (unitFetcher, error) { | ||
//setup a dbus connection | ||
conn, err := dbusRaw.SystemBusPrivate() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error getting connection to system bus") | ||
} | ||
|
||
auth := dbusRaw.AuthExternal(strconv.Itoa(os.Getuid())) | ||
err = conn.Auth([]dbusRaw.Auth{auth}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error authenticating") | ||
} | ||
|
||
err = conn.Hello() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error in Hello") | ||
} | ||
|
||
var props string | ||
|
||
//call "introspect" on the systemd1 path to see what ListUnit* methods are available | ||
obj := conn.Object("org.freedesktop.systemd1", dbusRaw.ObjectPath("/org/freedesktop/systemd1")) | ||
err = obj.Call("org.freedesktop.DBus.Introspectable.Introspect", 0).Store(&props) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting! |
||
if err != nil { | ||
return nil, errors.Wrap(err, "error calling dbus") | ||
} | ||
|
||
unitMap, err := parseXMLAndReturnMethods(props) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error handling XML") | ||
} | ||
|
||
//return a function callback ordered by desirability | ||
if _, ok := unitMap["ListUnitsByPatterns"]; ok { | ||
return listUnitsByPatternWrapper, nil | ||
} else if _, ok := unitMap["ListUnitsFiltered"]; ok { | ||
return listUnitsFilteredWrapper, nil | ||
} else if _, ok := unitMap["ListUnits"]; ok { | ||
return listUnitsWrapper, nil | ||
} | ||
return nil, fmt.Errorf("no supported list Units function: %v", unitMap) | ||
} | ||
|
||
func parseXMLAndReturnMethods(str string) (map[string]bool, error) { | ||
|
||
type Method struct { | ||
Name string `xml:"name,attr"` | ||
} | ||
|
||
type Iface struct { | ||
Name string `xml:"name,attr"` | ||
Method []Method `xml:"method"` | ||
} | ||
|
||
type IntrospectData struct { | ||
XMLName xml.Name `xml:"node"` | ||
Interface []Iface `xml:"interface"` | ||
} | ||
|
||
methods := IntrospectData{} | ||
|
||
err := xml.Unmarshal([]byte(str), &methods) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error unmarshalling XML") | ||
} | ||
|
||
if len(methods.Interface) == 0 { | ||
return nil, errors.Wrap(err, "no methods found on introspect") | ||
} | ||
methodMap := make(map[string]bool) | ||
for _, iface := range methods.Interface { | ||
for _, method := range iface.Method { | ||
if strings.Contains(method.Name, "ListUnits") { | ||
methodMap[method.Name] = true | ||
} | ||
} | ||
} | ||
|
||
return methodMap, nil | ||
} | ||
|
||
// listUnitsByPatternWrapper is a bare wrapper for the unitFetcher type | ||
func listUnitsByPatternWrapper(conn *dbus.Conn, states, patterns []string) ([]dbus.UnitStatus, error) { | ||
return conn.ListUnitsByPatterns(states, patterns) | ||
} | ||
|
||
//listUnitsFilteredWrapper wraps the dbus ListUnitsFiltered method | ||
func listUnitsFilteredWrapper(conn *dbus.Conn, states, patterns []string) ([]dbus.UnitStatus, error) { | ||
units, err := conn.ListUnitsFiltered(states) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "ListUnitsFiltered error") | ||
} | ||
|
||
return matchUnitPatterns(patterns, units) | ||
} | ||
|
||
// listUnitsWrapper wraps the dbus ListUnits method | ||
func listUnitsWrapper(conn *dbus.Conn, states, patterns []string) ([]dbus.UnitStatus, error) { | ||
units, err := conn.ListUnits() | ||
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") | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} | ||
return finalUnits, nil | ||
} | ||
|
||
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 | ||
for _, unit := range units { | ||
for _, pattern := range patterns { | ||
match, err := filepath.Match(pattern, unit.Name) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "error matching with pattern %s", pattern) | ||
} | ||
if match { | ||
matchUnits = append(matchUnits, unit) | ||
break | ||
} | ||
} | ||
} | ||
return matchUnits, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a moment I thought that it should say
system/users
metricset, but is the title of the issue what is wrong. Remember this when forging the commit message! 🙂