-
Notifications
You must be signed in to change notification settings - Fork 95
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
feat(host_analyzer): add host sysctl analyzer #1681
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d65278
feat(host_analyzer): add host sysctl analyzer
JGAntunes b534116
Merge branch 'main' into feat/host_sysctl_analyzer
JGAntunes 6474958
chore: add e2e tests to support bundle collection
JGAntunes 54cf316
chore: missing spec e2e test update
JGAntunes 258d883
chore: cleanup remote collector and use parse operator
JGAntunes 77b1e89
chore: update schemas
JGAntunes 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
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,101 @@ | ||
package analyzer | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" | ||
"github.com/replicatedhq/troubleshoot/pkg/collect" | ||
) | ||
|
||
// Ensure `AnalyzeHostSysctl` implements `HostAnalyzer` interface at compile time. | ||
var _ HostAnalyzer = (*AnalyzeHostSysctl)(nil) | ||
|
||
type AnalyzeHostSysctl struct { | ||
hostAnalyzer *troubleshootv1beta2.HostSysctlAnalyze | ||
} | ||
|
||
func (a *AnalyzeHostSysctl) Title() string { | ||
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Sysctl") | ||
} | ||
|
||
func (a *AnalyzeHostSysctl) IsExcluded() (bool, error) { | ||
return isExcluded(a.hostAnalyzer.Exclude) | ||
} | ||
|
||
func (a *AnalyzeHostSysctl) Analyze( | ||
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents, | ||
) ([]*AnalyzeResult, error) { | ||
result := AnalyzeResult{Title: a.Title()} | ||
|
||
// Use the generic function to collect both local and remote data | ||
collectedContents, err := retrieveCollectedContents( | ||
getCollectedFileContents, | ||
collect.HostSysctlPath, // Local path | ||
collect.NodeInfoBaseDir, // Remote base directory | ||
collect.HostSysctlFileName, // Remote file name | ||
) | ||
if err != nil { | ||
return []*AnalyzeResult{&result}, err | ||
} | ||
|
||
results, err := analyzeHostCollectorResults(collectedContents, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title()) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to analyze sysctl output") | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
// checkCondition checks the condition of the when clause | ||
func (a *AnalyzeHostSysctl) CheckCondition(when string, data []byte) (bool, error) { | ||
|
||
sysctl := map[string]string{} | ||
if err := json.Unmarshal(data, &sysctl); err != nil { | ||
return false, errors.Wrap(err, "failed to unmarshal data") | ||
} | ||
|
||
// <1:key> <2:operator> <3:value> | ||
matches := sysctlWhenRX.FindStringSubmatch(when) | ||
if len(matches) < 4 { | ||
return false, fmt.Errorf("expected 3 parts in when %q", when) | ||
} | ||
|
||
param := matches[1] | ||
operator := matches[2] | ||
expected := matches[3] | ||
if _, ok := sysctl[param]; !ok { | ||
return false, fmt.Errorf("kernel parameter %q does not exist on collected sysctl output", param) | ||
} | ||
|
||
switch operator { | ||
case "=", "==", "===": | ||
return expected == sysctl[param], nil | ||
} | ||
|
||
// operator used is an inequality operator, the only valid inputs should be ints, if not we'll error out | ||
value, err := strconv.Atoi(sysctl[param]) | ||
if err != nil { | ||
return false, fmt.Errorf("collected sysctl param %q has value %q, cannot be used with provided operator %q", param, sysctl[param], operator) | ||
} | ||
expectedInt, err := strconv.Atoi(expected) | ||
if err != nil { | ||
return false, fmt.Errorf("expected value for sysctl param %q has value %q, cannot be used with provided operator %q", param, expected, operator) | ||
} | ||
|
||
switch operator { | ||
case "<": | ||
return value < expectedInt, nil | ||
case "<=": | ||
return value <= expectedInt, nil | ||
case ">": | ||
return value > expectedInt, nil | ||
case ">=": | ||
return value >= expectedInt, nil | ||
default: | ||
return false, fmt.Errorf("unsupported operator %q", operator) | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
You can use ParseComparisonOperator to parse the operator
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.
Ah thank you! This is pretty useful. I'll use that one too.
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.
Done via - 258d883