mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 02:28:09 +00:00 
			
		
		
		
	Segment out disk checks to disable on openbsd/arm (#11749)
* Segment out disk checks to disable on openbsd/arm Also add a spot skipped helper. * Expected results may be fewer than actual because of variable length tests like disk usage * Move to os_common and build on windows
This commit is contained in:
		| @@ -211,9 +211,7 @@ func (c *OperatorDiagnoseCommand) offlineDiagnostics(ctx context.Context) error | |||||||
| 	defer span.End() | 	defer span.End() | ||||||
|  |  | ||||||
| 	// OS Specific checks | 	// OS Specific checks | ||||||
| 	// Check open file count |  | ||||||
| 	diagnose.OSChecks(ctx) | 	diagnose.OSChecks(ctx) | ||||||
| 	diagnose.Test(ctx, "disk-usage", diagnose.DiskUsageCheck) |  | ||||||
|  |  | ||||||
| 	server.flagConfigs = c.flagConfigs | 	server.flagConfigs = c.flagConfigs | ||||||
| 	config, err := server.parseConfig() | 	config, err := server.parseConfig() | ||||||
|   | |||||||
| @@ -40,9 +40,20 @@ func TestOperatorDiagnoseCommand_Run(t *testing.T) { | |||||||
| 			}, | 			}, | ||||||
| 			[]*diagnose.Result{ | 			[]*diagnose.Result{ | ||||||
| 				{ | 				{ | ||||||
| 					Name:   "open file limits", | 					Name:   "operating system", | ||||||
| 					Status: diagnose.OkStatus, | 					Status: diagnose.OkStatus, | ||||||
|  | 					Children: []*diagnose.Result{ | ||||||
|  | 						{ | ||||||
|  | 							Name:   "open file limits", | ||||||
|  | 							Status: diagnose.OkStatus, | ||||||
|  | 						}, | ||||||
|  | 						{ | ||||||
|  | 							Name:   "disk usage", | ||||||
|  | 							Status: diagnose.OkStatus, | ||||||
|  | 						}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
|  |  | ||||||
| 				{ | 				{ | ||||||
| 					Name:   "parse-config", | 					Name:   "parse-config", | ||||||
| 					Status: diagnose.OkStatus, | 					Status: diagnose.OkStatus, | ||||||
| @@ -331,7 +342,7 @@ func compareResult(exp *diagnose.Result, act *diagnose.Result) error { | |||||||
| 			return fmt.Errorf("section %s, warning message not found: %s in %s", exp.Name, exp.Warnings[j], act.Warnings[j]) | 			return fmt.Errorf("section %s, warning message not found: %s in %s", exp.Name, exp.Warnings[j], act.Warnings[j]) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if len(exp.Children) != len(act.Children) { | 	if len(exp.Children) > len(act.Children) { | ||||||
| 		errStrings := []string{} | 		errStrings := []string{} | ||||||
| 		for _, c := range act.Children { | 		for _, c := range act.Children { | ||||||
| 			errStrings = append(errStrings, fmt.Sprintf("%+v", c)) | 			errStrings = append(errStrings, fmt.Sprintf("%+v", c)) | ||||||
|   | |||||||
| @@ -4,10 +4,8 @@ import ( | |||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"strings" |  | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/shirou/gopsutil/disk" |  | ||||||
| 	"go.opentelemetry.io/otel/attribute" | 	"go.opentelemetry.io/otel/attribute" | ||||||
| 	"go.opentelemetry.io/otel/codes" | 	"go.opentelemetry.io/otel/codes" | ||||||
| 	sdktrace "go.opentelemetry.io/otel/sdk/trace" | 	sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||||||
| @@ -15,15 +13,16 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
| 	warningEventName        = "warning" | 	warningEventName          = "warning" | ||||||
| 	skippedEventName        = "skipped" | 	skippedEventName          = "skipped" | ||||||
| 	actionKey               = "actionKey" | 	actionKey                 = "actionKey" | ||||||
| 	spotCheckOkEventName    = "spot-check-ok" | 	spotCheckOkEventName      = "spot-check-ok" | ||||||
| 	spotCheckWarnEventName  = "spot-check-warn" | 	spotCheckWarnEventName    = "spot-check-warn" | ||||||
| 	spotCheckErrorEventName = "spot-check-error" | 	spotCheckErrorEventName   = "spot-check-error" | ||||||
| 	errorMessageKey         = attribute.Key("error.message") | 	spotCheckSkippedEventName = "spot-check-skipped" | ||||||
| 	nameKey                 = attribute.Key("name") | 	errorMessageKey           = attribute.Key("error.message") | ||||||
| 	messageKey              = attribute.Key("message") | 	nameKey                   = attribute.Key("name") | ||||||
|  | 	messageKey                = attribute.Key("message") | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| @@ -162,6 +161,11 @@ func SpotError(ctx context.Context, checkName string, err error, options ...trac | |||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // SpotSkipped adds a Skipped result without adding a new Span. | ||||||
|  | func SpotSkipped(ctx context.Context, checkName, message string, options ...trace.EventOption) { | ||||||
|  | 	addSpotCheckResult(ctx, spotCheckSkippedEventName, checkName, message, options...) | ||||||
|  | } | ||||||
|  |  | ||||||
| func addSpotCheckResult(ctx context.Context, eventName, checkName, message string, options ...trace.EventOption) { | func addSpotCheckResult(ctx context.Context, eventName, checkName, message string, options ...trace.EventOption) { | ||||||
| 	span := trace.SpanFromContext(ctx) | 	span := trace.SpanFromContext(ctx) | ||||||
| 	attrs := append(options, trace.WithAttributes(nameKey.String(checkName))) | 	attrs := append(options, trace.WithAttributes(nameKey.String(checkName))) | ||||||
| @@ -229,35 +233,3 @@ func Skippable(skipName string, f testFunction) testFunction { | |||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func DiskUsageCheck(ctx context.Context) error { |  | ||||||
| 	partitions, err := disk.Partitions(false) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	partitionExcludes := []string{"/boot"} |  | ||||||
| partLoop: |  | ||||||
| 	for _, partition := range partitions { |  | ||||||
| 		for _, exc := range partitionExcludes { |  | ||||||
| 			if strings.HasPrefix(partition.Mountpoint, exc) { |  | ||||||
| 				continue partLoop |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		usage, err := disk.Usage(partition.Mountpoint) |  | ||||||
| 		testName := "disk-usage: " + partition.Mountpoint |  | ||||||
| 		if err != nil { |  | ||||||
| 			Warn(ctx, fmt.Sprintf("could not obtain partition usage for %s: %v", partition.Mountpoint, err)) |  | ||||||
| 		} else { |  | ||||||
| 			if usage.UsedPercent > 95 { |  | ||||||
| 				SpotWarn(ctx, testName, "more than 95% full") |  | ||||||
| 			} else if usage.Free < 2<<30 { |  | ||||||
| 				SpotWarn(ctx, testName, "less than 1GB free") |  | ||||||
| 			} else { |  | ||||||
| 				SpotOk(ctx, testName, "ok") |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 	} |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|   | |||||||
							
								
								
									
										44
									
								
								vault/diagnose/os_common.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								vault/diagnose/os_common.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  | // +build !openbsd,!arm | ||||||
|  |  | ||||||
|  | package diagnose | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"fmt" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
|  | 	"github.com/shirou/gopsutil/disk" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func diskUsage(ctx context.Context) error { | ||||||
|  | 	// Disk usage | ||||||
|  | 	partitions, err := disk.Partitions(false) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	partitionExcludes := []string{"/boot"} | ||||||
|  | partLoop: | ||||||
|  | 	for _, partition := range partitions { | ||||||
|  | 		for _, exc := range partitionExcludes { | ||||||
|  | 			if strings.HasPrefix(partition.Mountpoint, exc) { | ||||||
|  | 				continue partLoop | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		usage, err := disk.Usage(partition.Mountpoint) | ||||||
|  | 		testName := "disk usage" | ||||||
|  | 		if err != nil { | ||||||
|  | 			Warn(ctx, fmt.Sprintf("could not obtain partition usage for %s: %v", partition.Mountpoint, err)) | ||||||
|  | 		} else { | ||||||
|  | 			if usage.UsedPercent > 95 { | ||||||
|  | 				SpotWarn(ctx, testName, partition.Mountpoint+" more than 95% full") | ||||||
|  | 			} else if usage.Free < 2<<30 { | ||||||
|  | 				SpotWarn(ctx, testName, partition.Mountpoint+" less than 1GB free") | ||||||
|  | 			} else { | ||||||
|  | 				SpotOk(ctx, testName, partition.Mountpoint+" ok") | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
							
								
								
									
										7
									
								
								vault/diagnose/os_openbsd_arm.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								vault/diagnose/os_openbsd_arm.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | // +build openbsd,arm | ||||||
|  |  | ||||||
|  | package diagnose | ||||||
|  |  | ||||||
|  | func diskUsage(ctx context.Context) error { | ||||||
|  | 	SpotSkipped("disk usage", "unsupported on this platform") | ||||||
|  | } | ||||||
| @@ -9,6 +9,9 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func OSChecks(ctx context.Context) { | func OSChecks(ctx context.Context) { | ||||||
|  | 	ctx, span := StartSpan(ctx, "operating system") | ||||||
|  | 	defer span.End() | ||||||
|  |  | ||||||
| 	var limit unix.Rlimit | 	var limit unix.Rlimit | ||||||
| 	if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit); err != nil { | 	if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit); err != nil { | ||||||
| 		SpotError(ctx, "open file limits", fmt.Errorf("could not determine open file limit: %w", err)) | 		SpotError(ctx, "open file limits", fmt.Errorf("could not determine open file limit: %w", err)) | ||||||
| @@ -23,4 +26,6 @@ func OSChecks(ctx context.Context) { | |||||||
| 			SpotOk(ctx, "open file limits", fmt.Sprintf("set to %d", min)) | 			SpotOk(ctx, "open file limits", fmt.Sprintf("set to %d", min)) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	diskUsage(ctx) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,5 +7,7 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func OSChecks(ctx context.Context) { | func OSChecks(ctx context.Context) { | ||||||
| 	// None so far | 	ctx, span := StartSpan(ctx, "operating system") | ||||||
|  | 	defer span.End() | ||||||
|  | 	diskUsage(ctx) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -266,7 +266,28 @@ func (t *TelemetryCollector) getOrBuildResult(id trace.SpanID) *Result { | |||||||
| 							Time:    e.Time, | 							Time:    e.Time, | ||||||
| 						}) | 						}) | ||||||
| 				} | 				} | ||||||
|  | 			case spotCheckSkippedEventName: | ||||||
|  | 				var checkName string | ||||||
|  | 				var message string | ||||||
|  | 				for _, a := range e.Attributes { | ||||||
|  | 					switch a.Key { | ||||||
|  | 					case nameKey: | ||||||
|  | 						checkName = a.Value.AsString() | ||||||
|  | 					case messageKey: | ||||||
|  | 						message = a.Value.AsString() | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 				if checkName != "" { | ||||||
|  | 					r.Children = append(r.Children, | ||||||
|  | 						&Result{ | ||||||
|  | 							Name:    checkName, | ||||||
|  | 							Status:  SkippedStatus, | ||||||
|  | 							Message: message, | ||||||
|  | 							Time:    e.Time, | ||||||
|  | 						}) | ||||||
|  | 				} | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 		} | 		} | ||||||
| 		switch s.StatusCode() { | 		switch s.StatusCode() { | ||||||
| 		case codes.Unset: | 		case codes.Unset: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Scott Miller
					Scott Miller