Host preflights exclude

This commit is contained in:
Ethan Mosbaugh
2021-03-01 22:45:16 +00:00
parent bdf843f84d
commit 09d16ff185
29 changed files with 335 additions and 224 deletions

View File

@@ -1,6 +1,7 @@
package analyzer
import (
"fmt"
"strconv"
"github.com/pkg/errors"
@@ -40,100 +41,32 @@ func isExcluded(excludeVal multitype.BoolOrString) (bool, error) {
return parsed, nil
}
func HostAnalyze(hostAnalyzer *troubleshootv1beta2.HostAnalyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
if hostAnalyzer.CPU != nil {
result, err := analyzeHostCPU(hostAnalyzer.CPU, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.TCPLoadBalancer != nil {
result, err := analyzeHostTCPLoadBalancer(hostAnalyzer.TCPLoadBalancer, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.HTTPLoadBalancer != nil {
result, err := analyzeHostHTTPLoadBalancer(hostAnalyzer.HTTPLoadBalancer, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.DiskUsage != nil {
result, err := analyzeHostDiskUsage(hostAnalyzer.DiskUsage, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.Memory != nil {
result, err := analyzeHostMemory(hostAnalyzer.Memory, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.TCPPortStatus != nil {
result, err := analyzeHostTCPPortStatus(hostAnalyzer.TCPPortStatus, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.HTTP != nil {
result, err := analyzeHostHTTP(hostAnalyzer.HTTP, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.Time != nil {
result, err := analyzeHostTime(hostAnalyzer.Time, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.BlockDevices != nil {
result, err := analyzeHostBlockDevices(hostAnalyzer.BlockDevices, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.TCPConnect != nil {
result, err := analyzeHostTCPConnect(hostAnalyzer.TCPConnect, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.IPV4Interfaces != nil {
result, err := analyzeHostIPV4Interfaces(hostAnalyzer.IPV4Interfaces, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.FilesystemPerformance != nil {
result, err := analyzeHostFilesystemPerformance(hostAnalyzer.FilesystemPerformance, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.Certificate != nil {
result, err := analyzeHostCertificate(hostAnalyzer.Certificate, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
func HostAnalyze(hostAnalyzer *troubleshootv1beta2.HostAnalyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) []*AnalyzeResult {
analyzer, ok := GetHostAnalyzer(hostAnalyzer)
if !ok {
return NewAnalyzeResultError(analyzer, errors.New("invalid analyzer"))
}
return nil, errors.New("invalid analyzer")
isExcluded, err := analyzer.IsExcluded()
if err != nil {
return NewAnalyzeResultError(analyzer, errors.Wrap(err, "is excluded"))
} else if isExcluded {
return nil
}
result, err := analyzer.Analyze(getFile)
if err != nil {
return NewAnalyzeResultError(analyzer, errors.Wrap(err, "analyze"))
}
return []*AnalyzeResult{result}
}
func NewAnalyzeResultError(analyzer HostAnalyzer, err error) []*AnalyzeResult {
return []*AnalyzeResult{{
IsFail: true,
Title: analyzer.Title(),
Message: fmt.Sprintf("Analyzer Failed: %v", err),
}}
}
func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {

View File

@@ -0,0 +1,47 @@
package analyzer
import troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
type HostAnalyzer interface {
Title() string
IsExcluded() (bool, error)
Analyze(getFile func(string) ([]byte, error)) (*AnalyzeResult, error)
}
func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, bool) {
switch {
case analyzer.CPU != nil:
return &AnalyzeHostCPU{analyzer.CPU}, true
case analyzer.TCPLoadBalancer != nil:
return &AnalyzeHostTCPLoadBalancer{analyzer.TCPLoadBalancer}, true
case analyzer.HTTPLoadBalancer != nil:
return &AnalyzeHostHTTPLoadBalancer{analyzer.HTTPLoadBalancer}, true
case analyzer.DiskUsage != nil:
return &AnalyzeHostDiskUsage{analyzer.DiskUsage}, true
case analyzer.Memory != nil:
return &AnalyzeHostMemory{analyzer.Memory}, true
case analyzer.TCPPortStatus != nil:
return &AnalyzeHostTCPPortStatus{analyzer.TCPPortStatus}, true
case analyzer.HTTP != nil:
return &AnalyzeHostHTTP{analyzer.HTTP}, true
case analyzer.BlockDevices != nil:
return &AnalyzeHostBlockDevices{analyzer.BlockDevices}, true
case analyzer.TCPConnect != nil:
return &AnalyzeHostTCPConnect{analyzer.TCPConnect}, true
case analyzer.IPV4Interfaces != nil:
return &AnalyzeHostIPV4Interfaces{analyzer.IPV4Interfaces}, true
case analyzer.FilesystemPerformance != nil:
return &AnalyzeHostFilesystemPerformance{analyzer.FilesystemPerformance}, true
case analyzer.Certificate != nil:
return &AnalyzeHostCertificate{analyzer.Certificate}, true
default:
return nil, false
}
}
func hostAnalyzerTitleOrDefault(meta troubleshootv1beta2.AnalyzeMeta, defaultTitle string) string {
if meta.CheckName != "" {
return meta.CheckName
}
return defaultTitle
}

View File

@@ -12,7 +12,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
func analyzeHostBlockDevices(hostAnalyzer *troubleshootv1beta2.BlockDevicesAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostBlockDevices struct {
hostAnalyzer *troubleshootv1beta2.BlockDevicesAnalyze
}
func (a *AnalyzeHostBlockDevices) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Block Devices")
}
func (a *AnalyzeHostBlockDevices) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostBlockDevices) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
contents, err := getCollectedFileContents("system/block_devices.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
@@ -25,11 +39,7 @@ func analyzeHostBlockDevices(hostAnalyzer *troubleshootv1beta2.BlockDevicesAnaly
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "Block Devices"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -162,7 +162,7 @@ func TestAnalyzeBlockDevices(t *testing.T) {
return b, nil
}
result, err := analyzeHostBlockDevices(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostBlockDevices{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -7,7 +7,21 @@ import (
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)
func analyzeHostCertificate(hostAnalyzer *troubleshootv1beta2.CertificateAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostCertificate struct {
hostAnalyzer *troubleshootv1beta2.CertificateAnalyze
}
func (a *AnalyzeHostCertificate) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Certificate Key Pair")
}
func (a *AnalyzeHostCertificate) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostCertificate) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
collectorName := hostAnalyzer.CollectorName
if collectorName == "" {
collectorName = "certificate"
@@ -21,11 +35,7 @@ func analyzeHostCertificate(hostAnalyzer *troubleshootv1beta2.CertificateAnalyze
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "Certificate Key Pair"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -320,7 +320,7 @@ func TestAnalyzeCertificate(t *testing.T) {
return []byte(test.status), nil
}
result, err := analyzeHostCertificate(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostCertificate{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -10,7 +10,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
func analyzeHostCPU(hostAnalyzer *troubleshootv1beta2.CPUAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostCPU struct {
hostAnalyzer *troubleshootv1beta2.CPUAnalyze
}
func (a *AnalyzeHostCPU) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Number of CPUs")
}
func (a *AnalyzeHostCPU) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostCPU) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
contents, err := getCollectedFileContents("system/cpu.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
@@ -23,11 +37,7 @@ func analyzeHostCPU(hostAnalyzer *troubleshootv1beta2.CPUAnalyze, getCollectedFi
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "Number of CPUs"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -12,7 +12,21 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
)
func analyzeHostDiskUsage(hostAnalyzer *troubleshootv1beta2.DiskUsageAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostDiskUsage struct {
hostAnalyzer *troubleshootv1beta2.DiskUsageAnalyze
}
func (a *AnalyzeHostDiskUsage) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, fmt.Sprintf("Disk Usage %s", a.hostAnalyzer.CollectorName))
}
func (a *AnalyzeHostDiskUsage) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostDiskUsage) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
key := collect.HostDiskUsageKey(hostAnalyzer.CollectorName)
contents, err := getCollectedFileContents(key)
if err != nil {
@@ -26,11 +40,7 @@ func analyzeHostDiskUsage(hostAnalyzer *troubleshootv1beta2.DiskUsageAnalyze, ge
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = fmt.Sprintf("Disk Usage %s", hostAnalyzer.CollectorName)
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -367,7 +367,7 @@ func TestAnalyzeHostDiskUsage(t *testing.T) {
return b, nil
}
result, err := analyzeHostDiskUsage(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostDiskUsage{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -13,7 +13,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
func analyzeHostFilesystemPerformance(hostAnalyzer *troubleshootv1beta2.FilesystemPerformanceAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostFilesystemPerformance struct {
hostAnalyzer *troubleshootv1beta2.FilesystemPerformanceAnalyze
}
func (a *AnalyzeHostFilesystemPerformance) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Filesystem Performance")
}
func (a *AnalyzeHostFilesystemPerformance) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostFilesystemPerformance) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
collectorName := hostAnalyzer.CollectorName
if collectorName == "" {
collectorName = "filesystemPerformance"
@@ -31,11 +45,7 @@ func analyzeHostFilesystemPerformance(hostAnalyzer *troubleshootv1beta2.Filesyst
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "Filesystem Performance"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -352,7 +352,7 @@ func TestAnalyzeHostFilesystemPerformance(t *testing.T) {
return b, nil
}
result, err := analyzeHostFilesystemPerformance(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostFilesystemPerformance{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -17,7 +17,21 @@ type httpResult struct {
Response *collect.HTTPResponse
}
func analyzeHostHTTP(hostAnalyzer *troubleshootv1beta2.HTTPAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostHTTP struct {
hostAnalyzer *troubleshootv1beta2.HTTPAnalyze
}
func (a *AnalyzeHostHTTP) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "HTTP Request")
}
func (a *AnalyzeHostHTTP) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostHTTP) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
name := filepath.Join("http", "result.json")
if hostAnalyzer.CollectorName != "" {
name = filepath.Join("http", hostAnalyzer.CollectorName+".json")
@@ -34,11 +48,7 @@ func analyzeHostHTTP(hostAnalyzer *troubleshootv1beta2.HTTPAnalyze, getCollected
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "HTTP Request"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -91,7 +91,7 @@ func TestAnalyzeHostHTTP(t *testing.T) {
return b, nil
}
result, err := analyzeHostHTTP(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostHTTP{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -10,7 +10,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
func analyzeHostHTTPLoadBalancer(hostAnalyzer *troubleshootv1beta2.HTTPLoadBalancerAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostHTTPLoadBalancer struct {
hostAnalyzer *troubleshootv1beta2.HTTPLoadBalancerAnalyze
}
func (a *AnalyzeHostHTTPLoadBalancer) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "HTTP Load Balancer")
}
func (a *AnalyzeHostHTTPLoadBalancer) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostHTTPLoadBalancer) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
collectorName := hostAnalyzer.CollectorName
if collectorName == "" {
collectorName = "httpLoadBalancer"
@@ -28,11 +42,7 @@ func analyzeHostHTTPLoadBalancer(hostAnalyzer *troubleshootv1beta2.HTTPLoadBalan
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "HTTP Load Balancer"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -11,7 +11,21 @@ import (
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)
func analyzeHostIPV4Interfaces(hostAnalyzer *troubleshootv1beta2.IPV4InterfacesAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostIPV4Interfaces struct {
hostAnalyzer *troubleshootv1beta2.IPV4InterfacesAnalyze
}
func (a *AnalyzeHostIPV4Interfaces) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "IPv4 Interfaces")
}
func (a *AnalyzeHostIPV4Interfaces) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostIPV4Interfaces) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
contents, err := getCollectedFileContents("system/ipv4Interfaces.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
@@ -24,11 +38,7 @@ func analyzeHostIPV4Interfaces(hostAnalyzer *troubleshootv1beta2.IPV4InterfacesA
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "IPv4 Interfaces"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -88,7 +88,7 @@ func TestAnalyzeIPV4Interfaces(t *testing.T) {
return b, nil
}
result, err := analyzeHostIPV4Interfaces(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostIPV4Interfaces{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -11,7 +11,21 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
)
func analyzeHostMemory(hostAnalyzer *troubleshootv1beta2.MemoryAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostMemory struct {
hostAnalyzer *troubleshootv1beta2.MemoryAnalyze
}
func (a *AnalyzeHostMemory) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Amount of Memory")
}
func (a *AnalyzeHostMemory) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostMemory) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
contents, err := getCollectedFileContents("system/memory.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
@@ -24,11 +38,7 @@ func analyzeHostMemory(hostAnalyzer *troubleshootv1beta2.MemoryAnalyze, getColle
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "Amount of Memory"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -164,7 +164,7 @@ func TestAnalyzeHostMemory(t *testing.T) {
return b, nil
}
result, err := analyzeHostMemory(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostMemory{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -10,7 +10,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
func analyzeHostTCPConnect(hostAnalyzer *troubleshootv1beta2.TCPConnectAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostTCPConnect struct {
hostAnalyzer *troubleshootv1beta2.TCPConnectAnalyze
}
func (a *AnalyzeHostTCPConnect) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "TCP Connection Attempt")
}
func (a *AnalyzeHostTCPConnect) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostTCPConnect) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
fullPath := path.Join("connect", fmt.Sprintf("%s.json", hostAnalyzer.CollectorName))
collected, err := getCollectedFileContents(fullPath)
@@ -24,11 +38,7 @@ func analyzeHostTCPConnect(hostAnalyzer *troubleshootv1beta2.TCPConnectAnalyze,
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "TCP Connection Attempt"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -79,7 +79,7 @@ func TestAnalyzeTCPConnect(t *testing.T) {
return b, nil
}
result, err := analyzeHostTCPConnect(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostTCPConnect{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -10,7 +10,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
func analyzeHostTCPLoadBalancer(hostAnalyzer *troubleshootv1beta2.TCPLoadBalancerAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostTCPLoadBalancer struct {
hostAnalyzer *troubleshootv1beta2.TCPLoadBalancerAnalyze
}
func (a *AnalyzeHostTCPLoadBalancer) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "TCP Load Balancer")
}
func (a *AnalyzeHostTCPLoadBalancer) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostTCPLoadBalancer) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
collectorName := hostAnalyzer.CollectorName
if collectorName == "" {
collectorName = "tcpLoadBalancer"
@@ -29,11 +43,7 @@ func analyzeHostTCPLoadBalancer(hostAnalyzer *troubleshootv1beta2.TCPLoadBalance
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "TCP Load Balancer"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -10,7 +10,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
func analyzeHostTCPPortStatus(hostAnalyzer *troubleshootv1beta2.TCPPortStatusAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostTCPPortStatus struct {
hostAnalyzer *troubleshootv1beta2.TCPPortStatusAnalyze
}
func (a *AnalyzeHostTCPPortStatus) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "TCP Port Status")
}
func (a *AnalyzeHostTCPPortStatus) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostTCPPortStatus) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
fullPath := path.Join("tcpPortStatus", "tcpPortStatus.json")
if hostAnalyzer.CollectorName != "" {
fullPath = path.Join("tcpPortStatus", fmt.Sprintf("%s.json", hostAnalyzer.CollectorName))
@@ -27,11 +41,7 @@ func analyzeHostTCPPortStatus(hostAnalyzer *troubleshootv1beta2.TCPPortStatusAna
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "TCP Port Status"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -17,7 +17,21 @@ const (
UnsynchronizedInactive = "unsynchronized+inactive"
)
func analyzeHostTime(hostAnalyzer *troubleshootv1beta2.TimeAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostTime struct {
hostAnalyzer *troubleshootv1beta2.TimeAnalyze
}
func (a *AnalyzeHostTime) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Time")
}
func (a *AnalyzeHostTime) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}
func (a *AnalyzeHostTime) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
contents, err := getCollectedFileContents("system/time.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
@@ -30,11 +44,7 @@ func analyzeHostTime(hostAnalyzer *troubleshootv1beta2.TimeAnalyze, getCollected
result := AnalyzeResult{}
title := hostAnalyzer.CheckName
if title == "" {
title = "Time"
}
result.Title = title
result.Title = a.Title()
for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {

View File

@@ -219,7 +219,7 @@ func TestAnalyzeHostTime(t *testing.T) {
return b, nil
}
result, err := analyzeHostTime(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostTime{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {

View File

@@ -1,7 +1,5 @@
package v1beta2
import "github.com/replicatedhq/troubleshoot/pkg/multitype"
type CPUAnalyze struct {
AnalyzeMeta `json:",inline" yaml:",inline"`
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
@@ -100,6 +98,4 @@ type HostAnalyze struct {
FilesystemPerformance *FilesystemPerformanceAnalyze `json:"filesystemPerformance,omitempty" yaml:"filesystemPerformance,omitempty"`
Certificate *CertificateAnalyze `json:"certificate,omitempty" yaml:"certificate,omitempty"`
Exclude multitype.BoolOrString `json:"exclude,omitempty" yaml:"exclude,omitempty"`
}

View File

@@ -103,7 +103,6 @@ type HostCollect struct {
TCPConnect *TCPConnect `json:"tcpConnect,omitempty" yaml:"tcpConnect,omitempty"`
FilesystemPerformance *FilesystemPerformance `json:"filesystemPerformance,omitempty" yaml:"filesystemPerformance,omitempty"`
Certificate *Certificate `json:"certificate,omitempty" yaml:"certificate,omitempty"`
Exclude multitype.BoolOrString `json:"exclude,omitempty" yaml:"exclude,omitempty"`
}
func (c *HostCollect) GetName() string {

View File

@@ -3,6 +3,7 @@ package collect
import (
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/multitype"
)
type HostCollector struct {
@@ -18,6 +19,10 @@ func (c *HostCollector) RunCollectorSync() (result map[string][]byte, err error)
}
}()
if c.IsExcluded() {
return
}
if c.Collect.CPU != nil {
result, err = HostCPU(c)
} else if c.Collect.Memory != nil {
@@ -55,6 +60,45 @@ func (c *HostCollector) RunCollectorSync() (result map[string][]byte, err error)
return
}
func (c *HostCollector) IsExcluded() bool {
exclude := multitype.BoolOrString{}
if c.Collect.CPU != nil {
exclude = c.Collect.CPU.Exclude
} else if c.Collect.Memory != nil {
exclude = c.Collect.Memory.Exclude
} else if c.Collect.TCPLoadBalancer != nil {
exclude = c.Collect.TCPLoadBalancer.Exclude
} else if c.Collect.HTTPLoadBalancer != nil {
exclude = c.Collect.HTTPLoadBalancer.Exclude
} else if c.Collect.DiskUsage != nil {
exclude = c.Collect.DiskUsage.Exclude
} else if c.Collect.TCPPortStatus != nil {
exclude = c.Collect.TCPPortStatus.Exclude
} else if c.Collect.HTTP != nil {
exclude = c.Collect.HTTP.Exclude
} else if c.Collect.Time != nil {
exclude = c.Collect.Time.Exclude
} else if c.Collect.BlockDevices != nil {
exclude = c.Collect.BlockDevices.Exclude
} else if c.Collect.TCPConnect != nil {
exclude = c.Collect.TCPConnect.Exclude
} else if c.Collect.IPV4Interfaces != nil {
exclude = c.Collect.IPV4Interfaces.Exclude
} else if c.Collect.FilesystemPerformance != nil {
exclude = c.Collect.FilesystemPerformance.Exclude
} else if c.Collect.Certificate != nil {
exclude = c.Collect.Certificate.Exclude
} else {
return true
}
isExcludedResult, err := isExcluded(exclude)
if err != nil {
return true
}
return isExcludedResult
}
func (c *HostCollector) GetDisplayName() string {
return c.Collect.GetName()
}

View File

@@ -3,13 +3,10 @@ package preflight
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/multitype"
)
// Analyze runs the analyze phase of preflight checks
@@ -67,40 +64,8 @@ func doAnalyze(allCollectedData map[string][]byte, analyzers []*troubleshootv1be
}
for _, hostAnalyzer := range hostAnalyzers {
if excluded, _ := isExcluded(hostAnalyzer.Exclude); excluded {
continue
}
analyzeResult, err := analyze.HostAnalyze(hostAnalyzer, getCollectedFileContents, getChildCollectedFileContents)
if err != nil {
analyzeResult = []*analyze.AnalyzeResult{
{
IsFail: true,
Title: "Analyzer Failed",
Message: err.Error(),
},
}
}
if analyzeResult != nil {
analyzeResults = append(analyzeResults, analyzeResult...)
}
analyzeResult := analyze.HostAnalyze(hostAnalyzer, getCollectedFileContents, getChildCollectedFileContents)
analyzeResults = append(analyzeResults, analyzeResult...)
}
return analyzeResults
}
func isExcluded(excludeVal multitype.BoolOrString) (bool, error) {
if excludeVal.Type == multitype.Bool {
return excludeVal.BoolVal, nil
}
if excludeVal.StrVal == "" {
return false, nil
}
parsed, err := strconv.ParseBool(excludeVal.StrVal)
if err != nil {
return false, errors.Wrap(err, "failed to parse bool string")
}
return parsed, nil
}

View File

@@ -67,9 +67,6 @@ func CollectHost(opts CollectOpts, p *troubleshootv1beta2.HostPreflight) (Collec
}
for _, collector := range collectors {
if excluded, _ := isExcluded(collector.Collect.Exclude); excluded {
continue
}
result, err := collector.RunCollectorSync()
if err != nil {
opts.ProgressChan <- errors.Errorf("failed to run collector: %s: %v\n", collector.GetDisplayName(), err)