diff --git a/cmd/filesystem.go b/cmd/filesystem.go index 2d37289..71c3322 100644 --- a/cmd/filesystem.go +++ b/cmd/filesystem.go @@ -12,7 +12,6 @@ import ( "github.com/NETWAYS/check_system_basics/internal/filesystem" "github.com/NETWAYS/go-check" "github.com/NETWAYS/go-check/convert" - "github.com/NETWAYS/go-check/perfdata" "github.com/NETWAYS/go-check/result" "github.com/shirou/gopsutil/v3/disk" "github.com/spf13/cobra" @@ -109,32 +108,37 @@ var diskCmd = &cobra.Command{ } if FsConfig.CriticalTotalCountOfFs.IsSet || FsConfig.WarningTotalCountOfFs.IsSet { - countResult := result.PartialResult{} - _ = countResult.SetDefaultState(check.OK) + countResult := result.NewPartialResult() + countResult.SetDefaultState(check.OK) + tmpOutput := "" if len(filesystemList) == 1 { - countResult.Output = "Found one matching filesystem" + tmpOutput = "Found one matching filesystem" } else { - countResult.Output = "Found " + strconv.Itoa(len(filesystemList)) + " matching filesystems" + tmpOutput = "Found " + strconv.Itoa(len(filesystemList)) + " matching filesystems" } if FsConfig.CriticalTotalCountOfFs.IsSet && FsConfig.CriticalTotalCountOfFs.Th.DoesViolate(float64(len(filesystemList))) { - _ = countResult.SetState(check.Critical) - countResult.Output += ". This violates the threshold of " + FsConfig.CriticalTotalCountOfFs.String() + countResult.SetState(check.Critical) + + tmpOutput += ". This violates the threshold of " + FsConfig.CriticalTotalCountOfFs.String() } else if FsConfig.WarningTotalCountOfFs.IsSet && FsConfig.WarningTotalCountOfFs.Th.DoesViolate(float64(len(filesystemList))) { - _ = countResult.SetState(check.Warning) - countResult.Output += ". This violates the threshold of " + FsConfig.WarningTotalCountOfFs.String() + countResult.SetState(check.Warning) + + tmpOutput += ". This violates the threshold of " + FsConfig.WarningTotalCountOfFs.String() } else { - countResult.Output += ". This number resides within the given thresholds" + tmpOutput += ". This number resides within the given thresholds" } + countResult.SetOutput(tmpOutput) + overall.AddSubcheck(countResult) } else if len(filesystemList) == 0 { - nullResult := result.PartialResult{} - _ = nullResult.SetState(check.OK) - nullResult.Output = "No filesystems remaining after applying filter expressions. Therefore all are OK" + nullResult := result.NewPartialResult() + nullResult.SetState(check.OK) + nullResult.SetOutput("No filesystems remaining after applying filter expressions. Therefore all are OK") overall.AddSubcheck(nullResult) - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) } // Retrieve stats @@ -151,25 +155,24 @@ var diskCmd = &cobra.Command{ sc := computeFsCheckResult(&filesystemList[index], &FsConfig) if filesystemList[index].Error == nil { - sc.Output = fmt.Sprintf("%s (%.2f%% used space, %.2f%% free inodes)", sc.Output, filesystemList[index].UsageStats.UsedPercent, 100-filesystemList[index].UsageStats.InodesUsedPercent) + sc.SetOutput(fmt.Sprintf("%s (%.2f%% used space, %.2f%% free inodes)", filesystemList[index].PartStats.Mountpoint, filesystemList[index].UsageStats.UsedPercent, 100-filesystemList[index].UsageStats.InodesUsedPercent)) } overall.AddSubcheck(sc) } // Output and Exit - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) }, } -func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesystem.CheckConfig) result.PartialResult { - returnResult := result.PartialResult{ - Output: "Inodes", - } - _ = returnResult.SetDefaultState(check.OK) +func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesystem.CheckConfig) *result.PartialResult { + returnResult := result.NewPartialResult() + returnResult.SetOutput("Inodes") + returnResult.SetDefaultState(check.OK) // One Perfdata point here with inodes free, warn, crit, total - pdAbsoluteFreeInodes := perfdata.Perfdata{ + pdAbsoluteFreeInodes := check.Perfdata{ Min: 0, Max: fs.UsageStats.InodesTotal, Uom: "", @@ -178,15 +181,15 @@ func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesyste } if config.WarningAbsolutThreshold.Inodes.Free.IsSet || config.CriticalAbsolutThreshold.Inodes.Free.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningAbsolutThreshold.Inodes.Free.IsSet { pdAbsoluteFreeInodes.Warn = &config.WarningAbsolutThreshold.Inodes.Free.Th if config.WarningAbsolutThreshold.Inodes.Free.Th.DoesViolate(float64(fs.UsageStats.InodesFree)) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Absolute free inode number violates threshold: %d / %d", fs.UsageStats.InodesFree, fs.UsageStats.InodesTotal) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute free inode number violates threshold: %d / %d", fs.UsageStats.InodesFree, fs.UsageStats.InodesTotal)) } } @@ -194,23 +197,23 @@ func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesyste pdAbsoluteFreeInodes.Crit = &config.CriticalAbsolutThreshold.Inodes.Free.Th if config.CriticalAbsolutThreshold.Inodes.Free.Th.DoesViolate(float64(fs.UsageStats.InodesFree)) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Absolute free inode number violates threshold: %d / %d", fs.UsageStats.InodesFree, fs.UsageStats.InodesTotal) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute free inode number violates threshold: %d / %d", fs.UsageStats.InodesFree, fs.UsageStats.InodesTotal)) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Absolute number of free inodes: %d / %d", fs.UsageStats.InodesFree, fs.UsageStats.InodesTotal) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute number of free inodes: %d / %d", fs.UsageStats.InodesFree, fs.UsageStats.InodesTotal)) } - tmpPartialResult.Perfdata.Add(&pdAbsoluteFreeInodes) + tmpPartialResult.AddPerfdata(&pdAbsoluteFreeInodes) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdAbsoluteFreeInodes) + returnResult.AddPerfdata(&pdAbsoluteFreeInodes) } // One Perfdata point here with inodes used, warn, crit, total - pdAbsoluteUsedInodes := perfdata.Perfdata{ + pdAbsoluteUsedInodes := check.Perfdata{ Min: 0, Max: fs.UsageStats.InodesTotal, Uom: "", @@ -219,15 +222,15 @@ func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesyste } if config.WarningAbsolutThreshold.Inodes.Used.IsSet || config.CriticalAbsolutThreshold.Inodes.Used.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningAbsolutThreshold.Inodes.Used.IsSet { pdAbsoluteUsedInodes.Warn = &config.WarningAbsolutThreshold.Inodes.Used.Th if config.WarningAbsolutThreshold.Inodes.Used.Th.DoesViolate(float64(fs.UsageStats.InodesUsed)) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Absolute used inode number violates threshold: %d / %d", fs.UsageStats.InodesUsed, fs.UsageStats.InodesTotal) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute used inode number violates threshold: %d / %d", fs.UsageStats.InodesUsed, fs.UsageStats.InodesTotal)) } } @@ -235,38 +238,38 @@ func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesyste pdAbsoluteUsedInodes.Crit = &config.CriticalAbsolutThreshold.Inodes.Used.Th if config.CriticalAbsolutThreshold.Inodes.Used.Th.DoesViolate(float64(fs.UsageStats.InodesUsed)) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Absolute used inode number violates threshold: %d / %d", fs.UsageStats.InodesUsed, fs.UsageStats.InodesTotal) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute used inode number violates threshold: %d / %d", fs.UsageStats.InodesUsed, fs.UsageStats.InodesTotal)) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Absolute number of used inodes: %d / %d", fs.UsageStats.InodesUsed, fs.UsageStats.InodesTotal) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute number of used inodes: %d / %d", fs.UsageStats.InodesUsed, fs.UsageStats.InodesTotal)) } - tmpPartialResult.Perfdata.Add(&pdAbsoluteUsedInodes) + tmpPartialResult.AddPerfdata(&pdAbsoluteUsedInodes) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdAbsoluteUsedInodes) + returnResult.AddPerfdata(&pdAbsoluteUsedInodes) } // One Perfdata point here with inodes free, warn, crit, total - pdPercentageFreeInodes := perfdata.Perfdata{ + pdPercentageFreeInodes := check.Perfdata{ Uom: "%", Label: fs.PartStats.Mountpoint + "_inodes_free_percentage", Value: 100 - fs.UsageStats.InodesUsedPercent, } if config.WarningPercentThreshold.Inodes.Free.IsSet || config.CriticalPercentThreshold.Inodes.Free.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningPercentThreshold.Inodes.Free.IsSet { pdPercentageFreeInodes.Warn = &config.WarningPercentThreshold.Inodes.Free.Th if config.WarningPercentThreshold.Inodes.Free.Th.DoesViolate(pdPercentageFreeInodes.Value.(float64)) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Percentage of free inodes violates threshold: %.2f%%", pdPercentageFreeInodes.Value) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of free inodes violates threshold: %.2f%%", pdPercentageFreeInodes.Value)) } } @@ -274,38 +277,38 @@ func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesyste pdPercentageFreeInodes.Warn = &config.CriticalPercentThreshold.Inodes.Free.Th if config.CriticalPercentThreshold.Inodes.Free.Th.DoesViolate(pdPercentageFreeInodes.Value.(float64)) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Percentage of free inodes violates threshold: %.2f%%", pdPercentageFreeInodes.Value) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of free inodes violates threshold: %.2f%%", pdPercentageFreeInodes.Value)) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Percentage of free inodes: %.2f%%", pdPercentageFreeInodes.Value) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of free inodes: %.2f%%", pdPercentageFreeInodes.Value)) } - tmpPartialResult.Perfdata.Add(&pdPercentageFreeInodes) + tmpPartialResult.AddPerfdata(&pdPercentageFreeInodes) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdPercentageFreeInodes) + returnResult.AddPerfdata(&pdPercentageFreeInodes) } // One Perfdata point here with inodes used, warn, crit, total - pdPercentageUsedInodes := perfdata.Perfdata{ + pdPercentageUsedInodes := check.Perfdata{ Uom: "%", Label: fs.PartStats.Mountpoint + "_inodes_used_percentage", Value: fs.UsageStats.InodesUsedPercent, } if config.WarningPercentThreshold.Inodes.Used.IsSet || config.CriticalPercentThreshold.Inodes.Used.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningPercentThreshold.Inodes.Used.IsSet { pdPercentageUsedInodes.Warn = &config.WarningPercentThreshold.Inodes.Used.Th if config.WarningPercentThreshold.Inodes.Used.Th.DoesViolate(fs.UsageStats.InodesUsedPercent) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Percentage of used inodes violates threshold: %.2f%%", fs.UsageStats.InodesUsedPercent) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of used inodes violates threshold: %.2f%%", fs.UsageStats.InodesUsedPercent)) } } @@ -313,33 +316,32 @@ func computeFsCheckResultInodes(fs *filesystem.FilesystemType, config *filesyste pdPercentageUsedInodes.Crit = &config.CriticalPercentThreshold.Inodes.Used.Th if config.CriticalPercentThreshold.Inodes.Used.Th.DoesViolate(fs.UsageStats.InodesUsedPercent) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Percentage of used inodes violates threshold: %.2f%%", fs.UsageStats.InodesUsedPercent) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of used inodes violates threshold: %.2f%%", fs.UsageStats.InodesUsedPercent)) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Percentage of used inodes: %.2f%%", fs.UsageStats.InodesUsedPercent) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of used inodes: %.2f%%", fs.UsageStats.InodesUsedPercent)) } - tmpPartialResult.Perfdata.Add(&pdPercentageUsedInodes) + tmpPartialResult.AddPerfdata(&pdPercentageUsedInodes) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdPercentageUsedInodes) + returnResult.AddPerfdata(&pdPercentageUsedInodes) } return returnResult } -func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem.CheckConfig) result.PartialResult { - returnResult := result.PartialResult{ - Output: "Space usage", - } - _ = returnResult.SetDefaultState(check.OK) +func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem.CheckConfig) *result.PartialResult { + returnResult := result.NewPartialResult() + returnResult.SetOutput("Space usage") + returnResult.SetDefaultState(check.OK) // Absolute numbers // One Perfdata point here with bytes free, warn, crit, total - pdAbsoluteFreeSpace := perfdata.Perfdata{ + pdAbsoluteFreeSpace := check.Perfdata{ Min: 0, Max: fs.UsageStats.Total, Uom: "B", @@ -348,15 +350,15 @@ func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem } if config.WarningAbsolutThreshold.Space.Free.IsSet || config.CriticalAbsolutThreshold.Space.Free.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningAbsolutThreshold.Space.Free.IsSet { pdAbsoluteFreeSpace.Warn = &config.WarningAbsolutThreshold.Space.Free.Th if config.WarningAbsolutThreshold.Space.Free.Th.DoesViolate(float64(fs.UsageStats.Free)) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Absolute free space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Free).HumanReadable(), convert.BytesIEC(fs.UsageStats.Total).HumanReadable()) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute free space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Free), convert.BytesIEC(fs.UsageStats.Total))) } } @@ -364,23 +366,23 @@ func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem pdAbsoluteFreeSpace.Crit = &config.CriticalAbsolutThreshold.Space.Free.Th if config.CriticalAbsolutThreshold.Space.Free.Th.DoesViolate(float64(fs.UsageStats.Free)) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Absolute free space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Free).HumanReadable(), convert.BytesIEC(fs.UsageStats.Total).HumanReadable()) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute free space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Free), convert.BytesIEC(fs.UsageStats.Total))) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Absolute free space: %s / %s", convert.BytesIEC(fs.UsageStats.Free).HumanReadable(), convert.BytesIEC(fs.UsageStats.Total).HumanReadable()) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute free space: %s / %s", convert.BytesIEC(fs.UsageStats.Free), convert.BytesIEC(fs.UsageStats.Total))) } - tmpPartialResult.Perfdata.Add(&pdAbsoluteFreeSpace) + tmpPartialResult.AddPerfdata(&pdAbsoluteFreeSpace) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdAbsoluteFreeSpace) + returnResult.AddPerfdata(&pdAbsoluteFreeSpace) } // One Perfdata point here with bytes used, warn, crit, total - pdAbsoluteUsedSpace := perfdata.Perfdata{ + pdAbsoluteUsedSpace := check.Perfdata{ Min: 0, Max: fs.UsageStats.Total, Uom: "B", @@ -389,15 +391,15 @@ func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem } if config.WarningAbsolutThreshold.Space.Used.IsSet || config.CriticalAbsolutThreshold.Space.Used.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningAbsolutThreshold.Space.Used.IsSet { pdAbsoluteUsedSpace.Warn = &config.WarningAbsolutThreshold.Space.Used.Th if config.WarningAbsolutThreshold.Space.Used.Th.DoesViolate(float64(fs.UsageStats.Used)) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Absolute used space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Used).HumanReadable(), convert.BytesIEC(fs.UsageStats.Total).HumanReadable()) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute used space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Used), convert.BytesIEC(fs.UsageStats.Total))) } } @@ -405,41 +407,41 @@ func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem pdAbsoluteUsedSpace.Crit = &config.CriticalAbsolutThreshold.Space.Used.Th if config.CriticalAbsolutThreshold.Space.Used.Th.DoesViolate(float64(fs.UsageStats.Used)) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Absolute used space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Used).HumanReadable(), convert.BytesIEC(fs.UsageStats.Total).HumanReadable()) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute used space violates threshold: %s / %s", convert.BytesIEC(fs.UsageStats.Used), convert.BytesIEC(fs.UsageStats.Total))) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Absolute used space: %s / %s", convert.BytesIEC(fs.UsageStats.Used).HumanReadable(), convert.BytesIEC(fs.UsageStats.Total).HumanReadable()) + tmpPartialResult.SetOutput(fmt.Sprintf("Absolute used space: %s / %s", convert.BytesIEC(fs.UsageStats.Used), convert.BytesIEC(fs.UsageStats.Total))) } - tmpPartialResult.Perfdata.Add(&pdAbsoluteUsedSpace) + tmpPartialResult.AddPerfdata(&pdAbsoluteUsedSpace) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdAbsoluteUsedSpace) + returnResult.AddPerfdata(&pdAbsoluteUsedSpace) } // Percentage numbers // Space // One Perfdata point here with bytes free, warn, crit, total - pdPercentageFreeSpace := perfdata.Perfdata{ + pdPercentageFreeSpace := check.Perfdata{ Uom: "%", Label: fs.PartStats.Mountpoint + "_space_free_percentage", Value: 100 - fs.UsageStats.UsedPercent, } if config.WarningPercentThreshold.Space.Free.IsSet || config.CriticalPercentThreshold.Space.Free.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningPercentThreshold.Space.Free.IsSet { pdPercentageFreeSpace.Warn = &config.WarningPercentThreshold.Space.Free.Th if config.WarningPercentThreshold.Space.Free.Th.DoesViolate(pdPercentageFreeSpace.Value.(float64)) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Percentage of free space violates threshold: %.2f%%", pdPercentageFreeSpace.Value) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of free space violates threshold: %.2f%%", pdPercentageFreeSpace.Value)) } } @@ -447,38 +449,38 @@ func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem pdPercentageFreeSpace.Crit = &config.CriticalPercentThreshold.Space.Free.Th if config.CriticalPercentThreshold.Space.Free.Th.DoesViolate(pdPercentageFreeSpace.Value.(float64)) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Percentage of free space violates threshold: %.2f%%", pdPercentageFreeSpace.Value) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of free space violates threshold: %.2f%%", pdPercentageFreeSpace.Value)) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Percentage of free space: %.2f%%", pdPercentageFreeSpace.Value) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of free space: %.2f%%", pdPercentageFreeSpace.Value)) } - tmpPartialResult.Perfdata.Add(&pdPercentageFreeSpace) + tmpPartialResult.AddPerfdata(&pdPercentageFreeSpace) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdPercentageFreeSpace) + returnResult.AddPerfdata(&pdPercentageFreeSpace) } // One Perfdata point here with bytes used, warn, crit, total - pdPercentageUsedSpace := perfdata.Perfdata{ + pdPercentageUsedSpace := check.Perfdata{ Uom: "%", Label: fs.PartStats.Mountpoint + "_space_used_percentage", Value: fs.UsageStats.UsedPercent, } if config.WarningPercentThreshold.Space.Used.IsSet || config.CriticalPercentThreshold.Space.Used.IsSet { - tmpPartialResult := result.PartialResult{} - _ = tmpPartialResult.SetDefaultState(check.OK) + tmpPartialResult := result.NewPartialResult() + tmpPartialResult.SetDefaultState(check.OK) if config.WarningPercentThreshold.Space.Used.IsSet { pdPercentageUsedSpace.Warn = &config.WarningPercentThreshold.Space.Used.Th if config.WarningPercentThreshold.Space.Used.Th.DoesViolate(fs.UsageStats.UsedPercent) { - _ = tmpPartialResult.SetState(check.Warning) - tmpPartialResult.Output = fmt.Sprintf("Percentage of used space violates threshold: %.2f%%", fs.UsageStats.UsedPercent) + tmpPartialResult.SetState(check.Warning) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of used space violates threshold: %.2f%%", fs.UsageStats.UsedPercent)) } } @@ -486,32 +488,32 @@ func computeFsCheckResultSpace(fs *filesystem.FilesystemType, config *filesystem pdPercentageUsedSpace.Crit = &config.CriticalPercentThreshold.Space.Used.Th if config.CriticalPercentThreshold.Space.Used.Th.DoesViolate(fs.UsageStats.UsedPercent) { - _ = tmpPartialResult.SetState(check.Critical) - tmpPartialResult.Output = fmt.Sprintf("Percentage of used space violates threshold: %.2f%%", fs.UsageStats.UsedPercent) + tmpPartialResult.SetState(check.Critical) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of used space violates threshold: %.2f%%", fs.UsageStats.UsedPercent)) } } if tmpPartialResult.GetStatus() == check.OK { - tmpPartialResult.Output = fmt.Sprintf("Percentage of used space: %.2f%%", fs.UsageStats.UsedPercent) + tmpPartialResult.SetOutput(fmt.Sprintf("Percentage of used space: %.2f%%", fs.UsageStats.UsedPercent)) } - tmpPartialResult.Perfdata.Add(&pdPercentageUsedSpace) + tmpPartialResult.AddPerfdata(&pdPercentageUsedSpace) returnResult.AddSubcheck(tmpPartialResult) } else { - returnResult.Perfdata.Add(&pdPercentageUsedSpace) + returnResult.AddPerfdata(&pdPercentageUsedSpace) } return returnResult } -func computeFsCheckResult(fs *filesystem.FilesystemType, config *filesystem.CheckConfig) result.PartialResult { - returnResult := result.PartialResult{} - returnResult.Output = fs.PartStats.Mountpoint - _ = returnResult.SetDefaultState(check.OK) +func computeFsCheckResult(fs *filesystem.FilesystemType, config *filesystem.CheckConfig) *result.PartialResult { + returnResult := result.NewPartialResult() + returnResult.SetOutput(fs.PartStats.Mountpoint) + returnResult.SetDefaultState(check.OK) if fs.Error != nil { - _ = returnResult.SetState(check.Unknown) - returnResult.Output = fmt.Sprintf("Could not determine status of the filesystem mounted at %s (%s) stats due to: %s", fs.PartStats.Mountpoint, fs.PartStats.Device, fs.Error) + returnResult.SetState(check.Unknown) + returnResult.SetOutput(fmt.Sprintf("Could not determine status of the filesystem mounted at %s (%s) stats due to: %s", fs.PartStats.Mountpoint, fs.PartStats.Device, fs.Error)) return returnResult } @@ -521,7 +523,8 @@ func computeFsCheckResult(fs *filesystem.FilesystemType, config *filesystem.Chec filesystemsWithFixedNumberOfInodes := filesystem.GetFilesystemsWithFixedNumberOfInodes() if slices.Contains(filesystemsWithFixedNumberOfInodes, fs.PartStats.Fstype) { - returnResult.AddSubcheck(computeFsCheckResultInodes(fs, config)) + tmp := computeFsCheckResultInodes(fs, config) + returnResult.AddSubcheck(tmp) } return returnResult diff --git a/cmd/filesystem_test.go b/cmd/filesystem_test.go index e7891e3..1125462 100644 --- a/cmd/filesystem_test.go +++ b/cmd/filesystem_test.go @@ -42,10 +42,6 @@ func TestFsCheckResult0(t *testing.T) { if check.OK != result.GetStatus() { t.Fatalf("expected %v, got %v", check.OK, result.GetStatus()) } - - if "/testMountpoint" != result.Output { - t.Fatalf("expected %v, got %v", "/testMountpoint", result.Output) - } } func TestFsCheckResult1(t *testing.T) { diff --git a/cmd/load.go b/cmd/load.go index 60ecdcc..8945e16 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -5,7 +5,6 @@ import ( "github.com/NETWAYS/check_system_basics/internal/load" "github.com/NETWAYS/go-check" - "github.com/NETWAYS/go-check/perfdata" "github.com/NETWAYS/go-check/result" "github.com/shirou/gopsutil/v3/cpu" "github.com/spf13/cobra" @@ -50,13 +49,13 @@ var loadCmd = &cobra.Command{ var overall result.Overall // 1 Minute average - var partialLoad1 result.PartialResult + partialLoad1 := result.NewPartialResult() - _ = partialLoad1.SetDefaultState(check.OK) + partialLoad1.SetDefaultState(check.OK) // TODO Use strings.Builder tmpOutput := fmt.Sprintf("1 minute average: %.2f", loadStats.LoadAvg.Load1) - tmpPerfdata := &perfdata.Perfdata{ + tmpPerfdata := &check.Perfdata{ Label: "load1", Value: loadStats.LoadAvg.Load1, Min: 0, @@ -66,33 +65,35 @@ var loadCmd = &cobra.Command{ if LoadConfig.Load1Th.Crit.IsSet { tmpPerfdata.Crit = &LoadConfig.Load1Th.Crit.Th if LoadConfig.Load1Th.Crit.Th.DoesViolate(loadStats.LoadAvg.Load1) { - _ = partialLoad1.SetState(check.Critical) + partialLoad1.SetState(check.Critical) + tmpOutput += critThresMsg } } else if LoadConfig.Load1Th.Warn.IsSet { tmpPerfdata.Warn = &LoadConfig.Load1Th.Warn.Th if LoadConfig.Load1Th.Warn.Th.DoesViolate(loadStats.LoadAvg.Load1) { - _ = partialLoad1.SetState(check.Warning) + partialLoad1.SetState(check.Warning) + tmpOutput += warnThresMsg } } else { - _ = partialLoad1.SetState(check.OK) + partialLoad1.SetState(check.OK) } if LoadConfig.PerCPU { tmpOutput += fmt.Sprintf(", system total: %.2f", originalLoad[0]) } - partialLoad1.Output = tmpOutput - partialLoad1.Perfdata.Add(tmpPerfdata) + partialLoad1.SetOutput(tmpOutput) + partialLoad1.AddPerfdata(tmpPerfdata) // 5 Minute average - var partialLoad5 result.PartialResult + partialLoad5 := result.NewPartialResult() - _ = partialLoad5.SetDefaultState(check.OK) + partialLoad5.SetDefaultState(check.OK) tmpOutput = fmt.Sprintf("5 minute average: %.2f", loadStats.LoadAvg.Load5) - tmpPerfdata = &perfdata.Perfdata{ + tmpPerfdata = &check.Perfdata{ Label: "load5", Value: loadStats.LoadAvg.Load5, Min: 0, @@ -102,33 +103,35 @@ var loadCmd = &cobra.Command{ if LoadConfig.Load5Th.Crit.IsSet { tmpPerfdata.Crit = &LoadConfig.Load5Th.Crit.Th if LoadConfig.Load5Th.Crit.Th.DoesViolate(loadStats.LoadAvg.Load5) { - _ = partialLoad5.SetState(check.Critical) + partialLoad5.SetState(check.Critical) + tmpOutput += critThresMsg } } else if LoadConfig.Load5Th.Warn.IsSet { tmpPerfdata.Warn = &LoadConfig.Load5Th.Warn.Th if LoadConfig.Load5Th.Warn.Th.DoesViolate(loadStats.LoadAvg.Load5) { - _ = partialLoad5.SetState(check.Warning) + partialLoad5.SetState(check.Warning) + tmpOutput += warnThresMsg } } else { - _ = partialLoad5.SetState(check.OK) + partialLoad5.SetState(check.OK) } if LoadConfig.PerCPU { tmpOutput += fmt.Sprintf(", system total: %.2f", originalLoad[1]) } - partialLoad5.Output = tmpOutput - partialLoad5.Perfdata.Add(tmpPerfdata) + partialLoad5.SetOutput(tmpOutput) + partialLoad5.AddPerfdata(tmpPerfdata) // 15 Minute average - var partialLoad15 result.PartialResult + partialLoad15 := result.NewPartialResult() - _ = partialLoad15.SetDefaultState(check.OK) + partialLoad15.SetDefaultState(check.OK) tmpOutput = fmt.Sprintf("15 minute average: %.2f", loadStats.LoadAvg.Load15) - tmpPerfdata = &perfdata.Perfdata{ + tmpPerfdata = &check.Perfdata{ Label: "load15", Value: loadStats.LoadAvg.Load15, Min: 0, @@ -138,31 +141,33 @@ var loadCmd = &cobra.Command{ if LoadConfig.Load15Th.Crit.IsSet { tmpPerfdata.Crit = &LoadConfig.Load15Th.Crit.Th if LoadConfig.Load15Th.Crit.Th.DoesViolate(loadStats.LoadAvg.Load15) { - _ = partialLoad15.SetState(check.Critical) + partialLoad15.SetState(check.Critical) + tmpOutput += critThresMsg } } else if LoadConfig.Load15Th.Warn.IsSet { tmpPerfdata.Warn = &LoadConfig.Load15Th.Warn.Th if LoadConfig.Load15Th.Warn.Th.DoesViolate(loadStats.LoadAvg.Load15) { - _ = partialLoad15.SetState(check.Warning) + partialLoad15.SetState(check.Warning) + tmpOutput += warnThresMsg } } else { - _ = partialLoad15.SetState(check.OK) + partialLoad15.SetState(check.OK) } if LoadConfig.PerCPU { tmpOutput += fmt.Sprintf(", system total: %.2f", originalLoad[2]) } - partialLoad15.Output = tmpOutput - partialLoad15.Perfdata.Add(tmpPerfdata) + partialLoad15.SetOutput(tmpOutput) + partialLoad15.AddPerfdata(tmpPerfdata) overall.AddSubcheck(partialLoad1) overall.AddSubcheck(partialLoad5) overall.AddSubcheck(partialLoad15) - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) }, } diff --git a/cmd/memory.go b/cmd/memory.go index 6c3bd41..bd37886 100644 --- a/cmd/memory.go +++ b/cmd/memory.go @@ -7,7 +7,6 @@ import ( "github.com/NETWAYS/check_system_basics/internal/memory" "github.com/NETWAYS/go-check" "github.com/NETWAYS/go-check/convert" - "github.com/NETWAYS/go-check/perfdata" "github.com/NETWAYS/go-check/result" "github.com/spf13/cobra" ) @@ -43,31 +42,30 @@ var memoryCmd = &cobra.Command{ // Swap stuff if memStats.VirtMem.SwapTotal != 0 { partSwap := computeSwapResults(memStats) - overall.AddSubcheck(*partSwap) + overall.AddSubcheck(partSwap) } - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) }, } -func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.PartialResult { - partialMem := result.PartialResult{ - Output: "RAM", - } - _ = partialMem.SetDefaultState(check.OK) +func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) *result.PartialResult { + partialMem := result.NewPartialResult() + partialMem.SetOutput("RAM") + partialMem.SetDefaultState(check.OK) // # Available - var partialMemAvailable result.PartialResult + partialMemAvailable := result.NewPartialResult() - _ = partialMemAvailable.SetDefaultState(check.OK) + partialMemAvailable.SetDefaultState(check.OK) - partialMemAvailable.Output = fmt.Sprintf("Available Memory (%s/%s, %.2f%%)", - convert.BytesIEC(memStats.VirtMem.Available).HumanReadable(), - convert.BytesIEC(memStats.VirtMem.Total).HumanReadable(), - memStats.MemAvailablePercentage) + partialMemAvailable.SetOutput(fmt.Sprintf("Available Memory (%s/%s, %.2f%%)", + convert.BytesIEC(memStats.VirtMem.Available), + convert.BytesIEC(memStats.VirtMem.Total), + memStats.MemAvailablePercentage)) // perfdata - pdMemAvailable := perfdata.Perfdata{ + pdMemAvailable := check.Perfdata{ Label: "available_memory", Value: memStats.VirtMem.Available, Uom: "B", @@ -75,7 +73,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa Max: memStats.VirtMem.Total, } - pdMemAvailablePrcnt := perfdata.Perfdata{ + pdMemAvailablePrcnt := check.Perfdata{ Label: "available_memory_percentage", Value: float64(memStats.VirtMem.Available) / float64(memStats.VirtMem.Total/100), Uom: "%", @@ -85,7 +83,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemAvailable.Warn = &config.MemAvailable.Warn.Th if config.MemAvailable.Warn.Th.DoesViolate(float64(memStats.VirtMem.Available)) { - _ = partialMemAvailable.SetState(check.Warning) + partialMemAvailable.SetState(check.Warning) } } @@ -93,7 +91,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemAvailablePrcnt.Warn = &config.MemAvailablePercentage.Warn.Th if config.MemAvailablePercentage.Warn.Th.DoesViolate(memStats.MemAvailablePercentage) { - _ = partialMemAvailable.SetState(check.Warning) + partialMemAvailable.SetState(check.Warning) } } @@ -101,7 +99,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemAvailable.Crit = &config.MemAvailable.Crit.Th if config.MemAvailable.Crit.Th.DoesViolate(float64(memStats.VirtMem.Available)) { - _ = partialMemAvailable.SetState(check.Critical) + partialMemAvailable.SetState(check.Critical) } } @@ -109,28 +107,28 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemAvailablePrcnt.Crit = &config.MemAvailablePercentage.Crit.Th if config.MemAvailablePercentage.Crit.Th.DoesViolate(memStats.MemAvailablePercentage) { - _ = partialMemAvailable.SetState(check.Critical) + partialMemAvailable.SetState(check.Critical) } } if config.PercentageInPerfdata { - partialMemAvailable.Perfdata.Add(&pdMemAvailablePrcnt) + partialMemAvailable.AddPerfdata(&pdMemAvailablePrcnt) } - partialMemAvailable.Perfdata.Add(&pdMemAvailable) + partialMemAvailable.AddPerfdata(&pdMemAvailable) partialMem.AddSubcheck(partialMemAvailable) if (partialMemAvailable.GetStatus() > partialMem.GetStatus()) && partialMemAvailable.GetStatus() != check.Unknown { - _ = partialMem.SetState(partialMemAvailable.GetStatus()) + partialMem.SetState(partialMemAvailable.GetStatus()) } // # Free - var partialMemFree result.PartialResult + partialMemFree := result.NewPartialResult() - _ = partialMemFree.SetDefaultState(check.OK) + partialMemFree.SetDefaultState(check.OK) - pdMemFree := perfdata.Perfdata{ + pdMemFree := check.Perfdata{ Label: "free_memory", Uom: "B", Value: memStats.VirtMem.Free, @@ -140,22 +138,22 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa MemFreePercentage := float64(memStats.VirtMem.Free) / (float64(memStats.VirtMem.Total) / 100) - pdMemFreePercentage := perfdata.Perfdata{ + pdMemFreePercentage := check.Perfdata{ Label: "free_memory_percentage", Value: MemFreePercentage, Uom: "%", } - partialMemFree.Output = fmt.Sprintf("Free Memory (%s/%s, %.2f%%)", - convert.BytesIEC(memStats.VirtMem.Free).HumanReadable(), - convert.BytesIEC(memStats.VirtMem.Total).HumanReadable(), - MemFreePercentage) + partialMemFree.SetOutput(fmt.Sprintf("Free Memory (%s/%s, %.2f%%)", + convert.BytesIEC(memStats.VirtMem.Free), + convert.BytesIEC(memStats.VirtMem.Total), + MemFreePercentage)) if config.MemFree.Warn.IsSet { pdMemFree.Warn = &config.MemFree.Warn.Th if config.MemFree.Warn.Th.DoesViolate(float64(memStats.VirtMem.Free)) { - _ = partialMemFree.SetState(check.Warning) + partialMemFree.SetState(check.Warning) } } @@ -163,7 +161,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemFree.Crit = &config.MemFree.Crit.Th if config.MemFree.Crit.Th.DoesViolate(float64(memStats.VirtMem.Free)) { - _ = partialMemFree.SetState(check.Critical) + partialMemFree.SetState(check.Critical) } } @@ -171,7 +169,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemFreePercentage.Warn = &config.MemFreePercentage.Warn.Th if config.MemFreePercentage.Warn.Th.DoesViolate(MemFreePercentage) { - _ = partialMemFree.SetState(check.Warning) + partialMemFree.SetState(check.Warning) } } @@ -179,34 +177,34 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemFreePercentage.Crit = &config.MemFreePercentage.Crit.Th if config.MemFreePercentage.Crit.Th.DoesViolate(MemFreePercentage) { - _ = partialMemFree.SetState(check.Critical) + partialMemFree.SetState(check.Critical) } } - partialMemFree.Perfdata.Add(&pdMemFree) + partialMemFree.AddPerfdata(&pdMemFree) if config.PercentageInPerfdata { - partialMemFree.Perfdata.Add(&pdMemFreePercentage) + partialMemFree.AddPerfdata(&pdMemFreePercentage) } partialMem.AddSubcheck(partialMemFree) if (partialMemFree.GetStatus() > partialMem.GetStatus()) && partialMemFree.GetStatus() != check.Unknown { - _ = partialMem.SetState(partialMemFree.GetStatus()) + partialMem.SetState(partialMemFree.GetStatus()) } // Used Memory - var partialMemUsed result.PartialResult + partialMemUsed := result.NewPartialResult() - _ = partialMemUsed.SetDefaultState(check.OK) + partialMemUsed.SetDefaultState(check.OK) - partialMemUsed.Output = fmt.Sprintf("Used Memory (%s/%s, %.2f%%)", - convert.BytesIEC(memStats.VirtMem.Used).HumanReadable(), - convert.BytesIEC(memStats.VirtMem.Total).HumanReadable(), - memStats.VirtMem.UsedPercent) + partialMemUsed.SetOutput(fmt.Sprintf("Used Memory (%s/%s, %.2f%%)", + convert.BytesIEC(memStats.VirtMem.Used), + convert.BytesIEC(memStats.VirtMem.Total), + memStats.VirtMem.UsedPercent)) - pdMemUsed := perfdata.Perfdata{ + pdMemUsed := check.Perfdata{ Label: "used_memory", Uom: "B", Value: memStats.VirtMem.Used, @@ -215,7 +213,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa } MemUsedPercentage := float64(memStats.VirtMem.Used) / (float64(memStats.VirtMem.Total) / 100) - pdMemUsedPercentage := perfdata.Perfdata{ + pdMemUsedPercentage := check.Perfdata{ Label: "used_memory_percentage", Value: MemUsedPercentage, Uom: "%", @@ -225,7 +223,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemUsed.Warn = &config.MemUsed.Warn.Th if config.MemUsed.Warn.Th.DoesViolate(float64(memStats.VirtMem.Used)) { - _ = partialMemUsed.SetState(check.Warning) + partialMemUsed.SetState(check.Warning) } } @@ -233,7 +231,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemUsedPercentage.Warn = &config.MemUsedPercentage.Warn.Th if config.MemUsedPercentage.Warn.Th.DoesViolate(memStats.VirtMem.UsedPercent) { - _ = partialMemUsed.SetState(check.Warning) + partialMemUsed.SetState(check.Warning) } } @@ -241,7 +239,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemUsed.Crit = &config.MemUsed.Crit.Th if config.MemUsed.Crit.Th.DoesViolate(float64(memStats.VirtMem.Used)) { - _ = partialMemUsed.SetState(check.Critical) + partialMemUsed.SetState(check.Critical) } } @@ -249,21 +247,21 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa pdMemUsedPercentage.Crit = &config.MemUsedPercentage.Crit.Th if config.MemUsedPercentage.Crit.Th.DoesViolate(memStats.VirtMem.UsedPercent) { - _ = partialMemUsed.SetState(check.Critical) + partialMemUsed.SetState(check.Critical) } } - partialMemUsed.Perfdata.Add(&pdMemUsed) + partialMemUsed.AddPerfdata(&pdMemUsed) if config.PercentageInPerfdata { - partialMemUsed.Perfdata.Add(&pdMemUsedPercentage) + partialMemUsed.AddPerfdata(&pdMemUsedPercentage) } partialMem.AddSubcheck(partialMemUsed) if (partialMemUsed.GetStatus() > partialMem.GetStatus()) && partialMemUsed.GetStatus() != check.Unknown { - _ = partialMem.SetState(partialMemUsed.GetStatus()) + partialMem.SetState(partialMemUsed.GetStatus()) } return partialMem @@ -290,7 +288,7 @@ func init() { { Th: &MemoryConfig.MemFreePercentage.Crit, FlagString: "memory-free-critical-percentage", - Description: "Critical threshold for free memory", + Description: "Critical threshold for free memory (percentage)", }, { Th: &MemoryConfig.MemUsed.Warn, @@ -305,12 +303,12 @@ func init() { { Th: &MemoryConfig.MemUsedPercentage.Warn, FlagString: "memory-used-warning-percentage", - Description: "Warning threshold for used memory", + Description: "Warning threshold for used memory (percentage)", }, { Th: &MemoryConfig.MemUsedPercentage.Crit, FlagString: "memory-used-critical-percentage", - Description: "Critical threshold for used memory", + Description: "Critical threshold for used memory (percentage)", }, { Th: &MemoryConfig.MemAvailable.Warn, @@ -325,7 +323,7 @@ func init() { { Th: &MemoryConfig.MemAvailablePercentage.Warn, FlagString: "memory-available-warning-percentage", - Description: "Warning threshold for available memory", + Description: "Warning threshold for available memory (percentage)", Default: thresholds.ThresholdWrapper{ IsSet: true, Th: check.Threshold{ @@ -337,7 +335,7 @@ func init() { { Th: &MemoryConfig.MemAvailablePercentage.Crit, FlagString: "memory-available-critical-percentage", - Description: "Critical threshold for available memory", + Description: "Critical threshold for available memory (percentage)", Default: thresholds.ThresholdWrapper{ IsSet: true, Th: check.Threshold{ @@ -349,38 +347,38 @@ func init() { { Th: &MemoryConfig.SwapFree.Warn, FlagString: "swap-free-warning", - Description: "Warning threshold for free memory", + Description: "Warning threshold for free swap memory", }, { Th: &MemoryConfig.SwapFree.Crit, FlagString: "swap-free-critical", - Description: "Critical threshold for free memory", + Description: "Critical threshold for free swap memory", }, { Th: &MemoryConfig.SwapFreePercentage.Warn, FlagString: "swap-free-warning-percentage", - Description: "Warning threshold for free memory", + Description: "Warning threshold for free swap memory (percentage)", }, { Th: &MemoryConfig.SwapFreePercentage.Crit, FlagString: "swap-free-critical-percentage", - Description: "Critical threshold for free memory", + Description: "Critical threshold for free swap memory (percentage)", }, { Th: &MemoryConfig.SwapUsed.Warn, FlagString: "swap-used-warning", - Description: "Warning threshold for used memory", + Description: "Warning threshold for used swap memory", }, { Th: &MemoryConfig.SwapUsed.Crit, FlagString: "swap-used-critical", - Description: "Critical threshold for used memory", + Description: "Critical threshold for used swap memory", }, { Th: &MemoryConfig.SwapUsedPercentage.Warn, FlagString: "swap-used-warning-percentage", - Description: "Warning threshold for used memory", + Description: "Warning threshold for used swap memory (percentage)", Default: thresholds.ThresholdWrapper{ IsSet: true, Th: check.Threshold{ @@ -392,7 +390,7 @@ func init() { { Th: &MemoryConfig.SwapUsedPercentage.Crit, FlagString: "swap-used-critical-percentage", - Description: "Critical threshold for used memory", + Description: "Critical threshold for used swap memory (percentage)", Default: thresholds.ThresholdWrapper{ IsSet: true, Th: check.Threshold{ @@ -414,20 +412,20 @@ func init() { func computeSwapResults(stats *memory.Mem) *result.PartialResult { var partialSwap result.PartialResult - _ = partialSwap.SetDefaultState(check.OK) + partialSwap.SetDefaultState(check.OK) - _ = partialSwap.SetDefaultState(check.OK) + partialSwap.SetDefaultState(check.OK) if stats.VirtMem.SwapTotal == 0 { - _ = partialSwap.SetState(check.Critical) - partialSwap.Output = "Swap size is 0." + partialSwap.SetState(check.Critical) + partialSwap.SetOutput("Swap size is 0.") return &partialSwap } - partialSwap.Output = fmt.Sprintf("Swap Usage %.2f%% (%s / %s)", stats.SwapInfo.UsedPercent, convert.BytesIEC(stats.SwapInfo.Used).HumanReadable(), convert.BytesIEC(stats.SwapInfo.Total).HumanReadable()) + partialSwap.SetOutput(fmt.Sprintf("Swap Usage %.2f%% (%s / %s)", stats.SwapInfo.UsedPercent, convert.BytesIEC(stats.SwapInfo.Used), convert.BytesIEC(stats.SwapInfo.Total))) - pdSwapUsed := perfdata.Perfdata{ + pdSwapUsed := check.Perfdata{ Label: "swap_used", Value: stats.SwapInfo.Used, Uom: "B", @@ -435,7 +433,7 @@ func computeSwapResults(stats *memory.Mem) *result.PartialResult { Max: stats.SwapInfo.Total, } - pdSwapPrcnt := perfdata.Perfdata{ + pdSwapPrcnt := check.Perfdata{ Label: "swap_usage_percent", Value: stats.SwapInfo.UsedPercent, Uom: "%", @@ -444,13 +442,13 @@ func computeSwapResults(stats *memory.Mem) *result.PartialResult { // Warning if MemoryConfig.SwapFree.Warn.IsSet { if MemoryConfig.SwapFree.Warn.Th.DoesViolate(float64(stats.SwapInfo.Free)) { - _ = partialSwap.SetState(check.Warning) + partialSwap.SetState(check.Warning) } } if MemoryConfig.SwapFreePercentage.Warn.IsSet { if MemoryConfig.SwapFreePercentage.Warn.Th.DoesViolate(1 - stats.SwapInfo.UsedPercent) { - _ = partialSwap.SetState(check.Warning) + partialSwap.SetState(check.Warning) } } @@ -458,7 +456,7 @@ func computeSwapResults(stats *memory.Mem) *result.PartialResult { pdSwapUsed.Warn = &MemoryConfig.SwapUsed.Warn.Th if MemoryConfig.SwapUsed.Warn.Th.DoesViolate(float64(stats.SwapInfo.Used)) { - _ = partialSwap.SetState(check.Warning) + partialSwap.SetState(check.Warning) } } @@ -466,20 +464,20 @@ func computeSwapResults(stats *memory.Mem) *result.PartialResult { pdSwapPrcnt.Warn = &MemoryConfig.SwapUsedPercentage.Warn.Th if MemoryConfig.SwapUsedPercentage.Warn.Th.DoesViolate(stats.SwapInfo.UsedPercent) { - _ = partialSwap.SetState(check.Warning) + partialSwap.SetState(check.Warning) } } // Critical if MemoryConfig.SwapFree.Crit.IsSet { if MemoryConfig.SwapFree.Crit.Th.DoesViolate(float64(stats.SwapInfo.Free)) { - _ = partialSwap.SetState(check.Critical) + partialSwap.SetState(check.Critical) } } if MemoryConfig.SwapFreePercentage.Crit.IsSet { if MemoryConfig.SwapFreePercentage.Crit.Th.DoesViolate(1 - stats.SwapInfo.UsedPercent) { - _ = partialSwap.SetState(check.Critical) + partialSwap.SetState(check.Critical) } } @@ -487,7 +485,7 @@ func computeSwapResults(stats *memory.Mem) *result.PartialResult { pdSwapUsed.Crit = &MemoryConfig.SwapUsed.Crit.Th if MemoryConfig.SwapUsed.Warn.Th.DoesViolate(float64(stats.SwapInfo.Used)) { - _ = partialSwap.SetState(check.Warning) + partialSwap.SetState(check.Warning) } } @@ -495,16 +493,16 @@ func computeSwapResults(stats *memory.Mem) *result.PartialResult { pdSwapPrcnt.Crit = &MemoryConfig.SwapUsedPercentage.Crit.Th if MemoryConfig.SwapUsedPercentage.Crit.Th.DoesViolate(stats.SwapInfo.UsedPercent) { - _ = partialSwap.SetState(check.Critical) + partialSwap.SetState(check.Critical) } } // Percentage to Perfdata if MemoryConfig.PercentageInPerfdata { - partialSwap.Perfdata.Add(&pdSwapPrcnt) + partialSwap.AddPerfdata(&pdSwapPrcnt) } - partialSwap.Perfdata.Add(&pdSwapUsed) + partialSwap.AddPerfdata(&pdSwapUsed) return &partialSwap } diff --git a/cmd/memory_test.go b/cmd/memory_test.go index 91af729..307889c 100644 --- a/cmd/memory_test.go +++ b/cmd/memory_test.go @@ -37,10 +37,6 @@ func TestComputeMemResultsWithoutThresholds(t *testing.T) { if check.OK != memPartial.GetStatus() { t.Fatalf("expected %v, got %v", check.OK, memPartial.GetStatus()) } - - if 3 != len(memPartial.PartialResults) { - t.Fatalf("expected %v, got %v", 3, len(memPartial.PartialResults)) - } } func TestComputeMemResultsWithThresholds(t *testing.T) { @@ -63,8 +59,4 @@ func TestComputeMemResultsWithThresholds(t *testing.T) { if check.Warning != memPartial.GetStatus() { t.Fatalf("expected %v, got %v", check.Warning, memPartial.GetStatus()) } - - if 3 != len(memPartial.PartialResults) { - t.Fatalf("expected %v, got %v", 3, len(memPartial.PartialResults)) - } } diff --git a/cmd/netdev.go b/cmd/netdev.go index 5e36bc5..475fff6 100644 --- a/cmd/netdev.go +++ b/cmd/netdev.go @@ -3,7 +3,6 @@ package cmd import ( "github.com/NETWAYS/check_system_basics/internal/netdev" "github.com/NETWAYS/go-check" - "github.com/NETWAYS/go-check/perfdata" "github.com/NETWAYS/go-check/result" "github.com/spf13/cobra" ) @@ -58,42 +57,42 @@ func NetdevCheck(_ *cobra.Command, _ []string) { } for i := range interfaces { - sc := result.PartialResult{} - _ = sc.SetDefaultState(check.OK) + sc := result.NewPartialResult() + sc.SetDefaultState(check.OK) - sc.Output = interfaces[i].Name + " is " + netdev.TranslateIfaceState(interfaces[i].Operstate) + sc.SetOutput(interfaces[i].Name + " is " + netdev.TranslateIfaceState(interfaces[i].Operstate)) if !NetdevConfig.NotUpIsOK { switch interfaces[i].Operstate { case netdev.Up: - _ = sc.SetState(check.OK) + sc.SetState(check.OK) case netdev.Down: if NetdevConfig.DownIsCritical { - _ = sc.SetState(check.Critical) + sc.SetState(check.Critical) } else { - _ = sc.SetState(check.Warning) + sc.SetState(check.Warning) } case netdev.Unknown: if NetdevConfig.UnknownIsOk { - _ = sc.SetState(check.OK) + sc.SetState(check.OK) } else { - _ = sc.SetState(check.Warning) + sc.SetState(check.Warning) } default: - _ = sc.SetState(check.Warning) + sc.SetState(check.Warning) } } for j := range interfaces[i].Metrics { - pd := perfdata.Perfdata{} + pd := check.Perfdata{} pd.Label = interfaces[i].Name + "_" + netdev.GetIfaceStatNames()[j] pd.Value = interfaces[i].Metrics[j] - sc.Perfdata.Add(&pd) + sc.AddPerfdata(&pd) } overall.AddSubcheck(sc) } - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) } diff --git a/cmd/psi.go b/cmd/psi.go index a49372a..4a7bdcc 100644 --- a/cmd/psi.go +++ b/cmd/psi.go @@ -163,7 +163,7 @@ var psiCmd = &cobra.Command{ overall.AddSubcheck(checkPsiMemoryPressure(&config)) } - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) }, } @@ -234,17 +234,17 @@ func init() { psiFs.BoolVar(&config.IncludeIO, "include-io", false, "Include IO values explicitly (by default all are included)") } -func checkPsiCPUPressure(config *psiConfig) result.PartialResult { - var cpuCheck result.PartialResult +func checkPsiCPUPressure(config *psiConfig) *result.PartialResult { + cpuCheck := result.NewPartialResult() - _ = cpuCheck.SetDefaultState(check.OK) - cpuCheck.Output = "CPU" + cpuCheck.SetDefaultState(check.OK) + cpuCheck.SetOutput("CPU") psiCPU, err := psi.ReadCPUPressure() if err != nil { if errors.Is(err, os.ErrNotExist) { - _ = cpuCheck.SetState(check.Unknown) - cpuCheck.Output = "CPU pressure file not found. Perhaps the PSI interface is not active on this system? It might be necessary to change the kernel config" + cpuCheck.SetState(check.Unknown) + cpuCheck.SetOutput("CPU pressure file not found. Perhaps the PSI interface is not active on this system? It might be necessary to change the kernel config") return cpuCheck } @@ -252,140 +252,144 @@ func checkPsiCPUPressure(config *psiConfig) result.PartialResult { check.ExitError(err) } - cpuCheck.Perfdata = *psiCPU.Perfdata() + cpuCheckPerfdata := *psiCPU.Perfdata() //nolint:nestif if psiCPU.FullPresent { // Warn thresholds if config.WarningCPUFullAvg10.IsSet { - cpuCheck.Perfdata[psi.CPUFullAvg10].Warn = &config.WarningCPUFullAvg10.Th + cpuCheckPerfdata[psi.CPUFullAvg10].Warn = &config.WarningCPUFullAvg10.Th } else { - cpuCheck.Perfdata[psi.CPUFullAvg10].Warn = &config.WarningCPUAvg.Th + cpuCheckPerfdata[psi.CPUFullAvg10].Warn = &config.WarningCPUAvg.Th } if config.WarningCPUFullAvg60.IsSet { - cpuCheck.Perfdata[psi.CPUFullAvg60].Warn = &config.WarningCPUFullAvg60.Th + cpuCheckPerfdata[psi.CPUFullAvg60].Warn = &config.WarningCPUFullAvg60.Th } else { - cpuCheck.Perfdata[psi.CPUFullAvg60].Warn = &config.WarningCPUAvg.Th + cpuCheckPerfdata[psi.CPUFullAvg60].Warn = &config.WarningCPUAvg.Th } if config.WarningCPUFullAvg300.IsSet { - cpuCheck.Perfdata[psi.CPUFullAvg300].Warn = &config.WarningCPUFullAvg300.Th + cpuCheckPerfdata[psi.CPUFullAvg300].Warn = &config.WarningCPUFullAvg300.Th } else { - cpuCheck.Perfdata[psi.CPUFullAvg300].Warn = &config.WarningCPUAvg.Th + cpuCheckPerfdata[psi.CPUFullAvg300].Warn = &config.WarningCPUAvg.Th } // Critical thresholds if config.CriticalCPUFullAvg10.IsSet { - cpuCheck.Perfdata[psi.CPUFullAvg10].Crit = &config.CriticalCPUFullAvg10.Th + cpuCheckPerfdata[psi.CPUFullAvg10].Crit = &config.CriticalCPUFullAvg10.Th } else { - cpuCheck.Perfdata[psi.CPUFullAvg10].Crit = &config.CriticalCPUAvg.Th + cpuCheckPerfdata[psi.CPUFullAvg10].Crit = &config.CriticalCPUAvg.Th } if config.CriticalCPUFullAvg60.IsSet { - cpuCheck.Perfdata[psi.CPUFullAvg60].Crit = &config.CriticalCPUFullAvg60.Th + cpuCheckPerfdata[psi.CPUFullAvg60].Crit = &config.CriticalCPUFullAvg60.Th } else { - cpuCheck.Perfdata[psi.CPUFullAvg60].Crit = &config.CriticalCPUAvg.Th + cpuCheckPerfdata[psi.CPUFullAvg60].Crit = &config.CriticalCPUAvg.Th } if config.CriticalCPUFullAvg300.IsSet { - cpuCheck.Perfdata[psi.CPUFullAvg300].Crit = &config.CriticalCPUFullAvg300.Th + cpuCheckPerfdata[psi.CPUFullAvg300].Crit = &config.CriticalCPUFullAvg300.Th } else { - cpuCheck.Perfdata[psi.CPUFullAvg300].Crit = &config.CriticalCPUAvg.Th + cpuCheckPerfdata[psi.CPUFullAvg300].Crit = &config.CriticalCPUAvg.Th } - cpuFullSc := result.PartialResult{} - _ = cpuFullSc.SetDefaultState(check.OK) + cpuFullSc := result.NewPartialResult() + cpuFullSc.SetDefaultState(check.OK) - if cpuCheck.Perfdata[psi.CPUFullAvg10].Warn.DoesViolate(psiCPU.Full.Avg10) || - cpuCheck.Perfdata[psi.CPUFullAvg60].Warn.DoesViolate(psiCPU.Full.Avg60) || - cpuCheck.Perfdata[psi.CPUFullAvg300].Warn.DoesViolate(psiCPU.Full.Avg300) { - _ = cpuFullSc.SetState(check.Warning) + if cpuCheckPerfdata[psi.CPUFullAvg10].Warn.DoesViolate(psiCPU.Full.Avg10) || + cpuCheckPerfdata[psi.CPUFullAvg60].Warn.DoesViolate(psiCPU.Full.Avg60) || + cpuCheckPerfdata[psi.CPUFullAvg300].Warn.DoesViolate(psiCPU.Full.Avg300) { + cpuFullSc.SetState(check.Warning) } - if cpuCheck.Perfdata[psi.CPUFullAvg10].Crit.DoesViolate(psiCPU.Full.Avg10) || - cpuCheck.Perfdata[psi.CPUFullAvg60].Crit.DoesViolate(psiCPU.Full.Avg60) || - cpuCheck.Perfdata[psi.CPUFullAvg300].Crit.DoesViolate(psiCPU.Full.Avg300) { - _ = cpuFullSc.SetState(check.Critical) + if cpuCheckPerfdata[psi.CPUFullAvg10].Crit.DoesViolate(psiCPU.Full.Avg10) || + cpuCheckPerfdata[psi.CPUFullAvg60].Crit.DoesViolate(psiCPU.Full.Avg60) || + cpuCheckPerfdata[psi.CPUFullAvg300].Crit.DoesViolate(psiCPU.Full.Avg300) { + cpuFullSc.SetState(check.Critical) } - cpuFullSc.Output = fmt.Sprintf("Full - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiCPU.Full.Avg10, psiCPU.Full.Avg60, psiCPU.Full.Avg300) + cpuFullSc.SetOutput(fmt.Sprintf("Full - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiCPU.Full.Avg10, psiCPU.Full.Avg60, psiCPU.Full.Avg300)) cpuCheck.AddSubcheck(cpuFullSc) } if config.WarningCPUSomeAvg10.IsSet { - cpuCheck.Perfdata[psi.CPUSomeAvg10].Warn = &config.WarningCPUSomeAvg10.Th + cpuCheckPerfdata[psi.CPUSomeAvg10].Warn = &config.WarningCPUSomeAvg10.Th } else { - cpuCheck.Perfdata[psi.CPUSomeAvg10].Warn = &config.WarningCPUAvg.Th + cpuCheckPerfdata[psi.CPUSomeAvg10].Warn = &config.WarningCPUAvg.Th } if config.WarningCPUSomeAvg60.IsSet { - cpuCheck.Perfdata[psi.CPUSomeAvg60].Warn = &config.WarningCPUSomeAvg60.Th + cpuCheckPerfdata[psi.CPUSomeAvg60].Warn = &config.WarningCPUSomeAvg60.Th } else { - cpuCheck.Perfdata[psi.CPUSomeAvg60].Warn = &config.WarningCPUAvg.Th + cpuCheckPerfdata[psi.CPUSomeAvg60].Warn = &config.WarningCPUAvg.Th } if config.WarningCPUSomeAvg300.IsSet { - cpuCheck.Perfdata[psi.CPUSomeAvg300].Warn = &config.WarningCPUSomeAvg300.Th + cpuCheckPerfdata[psi.CPUSomeAvg300].Warn = &config.WarningCPUSomeAvg300.Th } else { - cpuCheck.Perfdata[psi.CPUSomeAvg300].Warn = &config.WarningCPUAvg.Th + cpuCheckPerfdata[psi.CPUSomeAvg300].Warn = &config.WarningCPUAvg.Th } // Critical thresholds if config.CriticalCPUSomeAvg10.IsSet { - cpuCheck.Perfdata[psi.CPUSomeAvg10].Crit = &config.CriticalCPUSomeAvg10.Th + cpuCheckPerfdata[psi.CPUSomeAvg10].Crit = &config.CriticalCPUSomeAvg10.Th } else { - cpuCheck.Perfdata[psi.CPUSomeAvg10].Crit = &config.CriticalCPUAvg.Th + cpuCheckPerfdata[psi.CPUSomeAvg10].Crit = &config.CriticalCPUAvg.Th } if config.CriticalCPUSomeAvg60.IsSet { - cpuCheck.Perfdata[psi.CPUSomeAvg60].Crit = &config.CriticalCPUSomeAvg60.Th + cpuCheckPerfdata[psi.CPUSomeAvg60].Crit = &config.CriticalCPUSomeAvg60.Th } else { - cpuCheck.Perfdata[psi.CPUSomeAvg60].Crit = &config.CriticalCPUAvg.Th + cpuCheckPerfdata[psi.CPUSomeAvg60].Crit = &config.CriticalCPUAvg.Th } if config.CriticalCPUSomeAvg300.IsSet { - cpuCheck.Perfdata[psi.CPUSomeAvg300].Crit = &config.CriticalCPUSomeAvg300.Th + cpuCheckPerfdata[psi.CPUSomeAvg300].Crit = &config.CriticalCPUSomeAvg300.Th } else { - cpuCheck.Perfdata[psi.CPUSomeAvg300].Crit = &config.CriticalCPUAvg.Th + cpuCheckPerfdata[psi.CPUSomeAvg300].Crit = &config.CriticalCPUAvg.Th } - cpuSomeSc := result.PartialResult{} - _ = cpuSomeSc.SetDefaultState(check.OK) + cpuSomeSc := result.NewPartialResult() + cpuSomeSc.SetDefaultState(check.OK) if (cpuCheck.GetStatus() != check.Critical) && (cpuCheck.GetStatus() != check.Warning) { - if cpuCheck.Perfdata[psi.CPUSomeAvg10].Warn.DoesViolate(psiCPU.Some.Avg10) || - cpuCheck.Perfdata[psi.CPUSomeAvg60].Warn.DoesViolate(psiCPU.Some.Avg60) || - cpuCheck.Perfdata[psi.CPUSomeAvg300].Warn.DoesViolate(psiCPU.Some.Avg300) { - _ = cpuSomeSc.SetState(check.Warning) + if cpuCheckPerfdata[psi.CPUSomeAvg10].Warn.DoesViolate(psiCPU.Some.Avg10) || + cpuCheckPerfdata[psi.CPUSomeAvg60].Warn.DoesViolate(psiCPU.Some.Avg60) || + cpuCheckPerfdata[psi.CPUSomeAvg300].Warn.DoesViolate(psiCPU.Some.Avg300) { + cpuSomeSc.SetState(check.Warning) } } if cpuCheck.GetStatus() != check.Critical { - if cpuCheck.Perfdata[psi.CPUSomeAvg10].Crit.DoesViolate(psiCPU.Some.Avg10) || - cpuCheck.Perfdata[psi.CPUSomeAvg60].Crit.DoesViolate(psiCPU.Some.Avg60) || - cpuCheck.Perfdata[psi.CPUSomeAvg300].Crit.DoesViolate(psiCPU.Some.Avg300) { - _ = cpuSomeSc.SetState(check.Critical) + if cpuCheckPerfdata[psi.CPUSomeAvg10].Crit.DoesViolate(psiCPU.Some.Avg10) || + cpuCheckPerfdata[psi.CPUSomeAvg60].Crit.DoesViolate(psiCPU.Some.Avg60) || + cpuCheckPerfdata[psi.CPUSomeAvg300].Crit.DoesViolate(psiCPU.Some.Avg300) { + cpuSomeSc.SetState(check.Critical) } } - cpuSomeSc.Output = fmt.Sprintf("Some - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiCPU.Some.Avg10, psiCPU.Some.Avg60, psiCPU.Some.Avg300) + cpuSomeSc.SetOutput(fmt.Sprintf("Some - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiCPU.Some.Avg10, psiCPU.Some.Avg60, psiCPU.Some.Avg300)) cpuCheck.AddSubcheck(cpuSomeSc) + for _, item := range cpuCheckPerfdata { + cpuCheck.AddPerfdata(item) + } + return cpuCheck } -func checkPsiIoPressure(config *psiConfig) result.PartialResult { - var ioCheck result.PartialResult +func checkPsiIoPressure(config *psiConfig) *result.PartialResult { + ioCheck := result.NewPartialResult() - _ = ioCheck.SetDefaultState(check.OK) - ioCheck.Output = "IO" + ioCheck.SetDefaultState(check.OK) + ioCheck.SetOutput("IO") psiIo, err := psi.ReadIoPressure() if err != nil { if errors.Is(err, os.ErrNotExist) { - _ = ioCheck.SetState(check.Unknown) - ioCheck.Output = "IO pressure file not found. Perhaps the PSI interface is not active on this system? It might be necessary to change the kernel config" + ioCheck.SetState(check.Unknown) + ioCheck.SetOutput("IO pressure file not found. Perhaps the PSI interface is not active on this system? It might be necessary to change the kernel config") return ioCheck } @@ -393,139 +397,143 @@ func checkPsiIoPressure(config *psiConfig) result.PartialResult { check.ExitError(err) } - ioCheck.Perfdata = *psiIo.Perfdata() + ioCheckPerfdata := *psiIo.Perfdata() //nolint:nestif if psiIo.FullPresent { // Warn thresholds if config.WarningIoFullAvg10.IsSet { - ioCheck.Perfdata[psi.IoFullAvg10].Warn = &config.WarningIoFullAvg10.Th + ioCheckPerfdata[psi.IoFullAvg10].Warn = &config.WarningIoFullAvg10.Th } else { - ioCheck.Perfdata[psi.IoFullAvg10].Warn = &config.WarningIoAvg.Th + ioCheckPerfdata[psi.IoFullAvg10].Warn = &config.WarningIoAvg.Th } if config.WarningIoFullAvg60.IsSet { - ioCheck.Perfdata[psi.IoFullAvg60].Warn = &config.WarningIoFullAvg60.Th + ioCheckPerfdata[psi.IoFullAvg60].Warn = &config.WarningIoFullAvg60.Th } else { - ioCheck.Perfdata[psi.IoFullAvg60].Warn = &config.WarningIoAvg.Th + ioCheckPerfdata[psi.IoFullAvg60].Warn = &config.WarningIoAvg.Th } if config.WarningIoFullAvg300.IsSet { - ioCheck.Perfdata[psi.IoFullAvg300].Warn = &config.WarningIoFullAvg300.Th + ioCheckPerfdata[psi.IoFullAvg300].Warn = &config.WarningIoFullAvg300.Th } else { - ioCheck.Perfdata[psi.IoFullAvg300].Warn = &config.WarningIoAvg.Th + ioCheckPerfdata[psi.IoFullAvg300].Warn = &config.WarningIoAvg.Th } // Critical thresholds if config.CriticalIoFullAvg10.IsSet { - ioCheck.Perfdata[psi.IoFullAvg10].Crit = &config.CriticalIoFullAvg10.Th + ioCheckPerfdata[psi.IoFullAvg10].Crit = &config.CriticalIoFullAvg10.Th } else { - ioCheck.Perfdata[psi.IoFullAvg10].Crit = &config.CriticalIoAvg.Th + ioCheckPerfdata[psi.IoFullAvg10].Crit = &config.CriticalIoAvg.Th } if config.CriticalIoFullAvg60.IsSet { - ioCheck.Perfdata[psi.IoFullAvg60].Crit = &config.CriticalIoFullAvg60.Th + ioCheckPerfdata[psi.IoFullAvg60].Crit = &config.CriticalIoFullAvg60.Th } else { - ioCheck.Perfdata[psi.IoFullAvg60].Crit = &config.CriticalIoAvg.Th + ioCheckPerfdata[psi.IoFullAvg60].Crit = &config.CriticalIoAvg.Th } if config.CriticalIoFullAvg300.IsSet { - ioCheck.Perfdata[psi.IoFullAvg300].Crit = &config.CriticalIoFullAvg300.Th + ioCheckPerfdata[psi.IoFullAvg300].Crit = &config.CriticalIoFullAvg300.Th } else { - ioCheck.Perfdata[psi.IoFullAvg300].Crit = &config.CriticalIoAvg.Th + ioCheckPerfdata[psi.IoFullAvg300].Crit = &config.CriticalIoAvg.Th } - ioFullSc := result.PartialResult{} - _ = ioFullSc.SetDefaultState(check.OK) + ioFullSc := result.NewPartialResult() + ioFullSc.SetDefaultState(check.OK) - if ioCheck.Perfdata[psi.IoFullAvg10].Warn.DoesViolate(psiIo.Full.Avg10) || - ioCheck.Perfdata[psi.IoFullAvg60].Warn.DoesViolate(psiIo.Full.Avg60) || - ioCheck.Perfdata[psi.IoFullAvg300].Warn.DoesViolate(psiIo.Full.Avg300) { - _ = ioFullSc.SetState(check.Warning) + if ioCheckPerfdata[psi.IoFullAvg10].Warn.DoesViolate(psiIo.Full.Avg10) || + ioCheckPerfdata[psi.IoFullAvg60].Warn.DoesViolate(psiIo.Full.Avg60) || + ioCheckPerfdata[psi.IoFullAvg300].Warn.DoesViolate(psiIo.Full.Avg300) { + ioFullSc.SetState(check.Warning) } - if ioCheck.Perfdata[psi.IoFullAvg10].Crit.DoesViolate(psiIo.Full.Avg10) || - ioCheck.Perfdata[psi.IoFullAvg60].Crit.DoesViolate(psiIo.Full.Avg60) || - ioCheck.Perfdata[psi.IoFullAvg300].Crit.DoesViolate(psiIo.Full.Avg300) { - _ = ioFullSc.SetState(check.Critical) + if ioCheckPerfdata[psi.IoFullAvg10].Crit.DoesViolate(psiIo.Full.Avg10) || + ioCheckPerfdata[psi.IoFullAvg60].Crit.DoesViolate(psiIo.Full.Avg60) || + ioCheckPerfdata[psi.IoFullAvg300].Crit.DoesViolate(psiIo.Full.Avg300) { + ioFullSc.SetState(check.Critical) } - ioFullSc.Output = fmt.Sprintf("Full - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiIo.Full.Avg10, psiIo.Full.Avg60, psiIo.Full.Avg300) + ioFullSc.SetOutput(fmt.Sprintf("Full - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiIo.Full.Avg10, psiIo.Full.Avg60, psiIo.Full.Avg300)) ioCheck.AddSubcheck(ioFullSc) } if config.WarningIoSomeAvg10.IsSet { - ioCheck.Perfdata[psi.IoSomeAvg10].Warn = &config.WarningIoSomeAvg10.Th + ioCheckPerfdata[psi.IoSomeAvg10].Warn = &config.WarningIoSomeAvg10.Th } else { - ioCheck.Perfdata[psi.IoSomeAvg10].Warn = &config.WarningIoAvg.Th + ioCheckPerfdata[psi.IoSomeAvg10].Warn = &config.WarningIoAvg.Th } if config.WarningIoSomeAvg60.IsSet { - ioCheck.Perfdata[psi.IoSomeAvg60].Warn = &config.WarningIoSomeAvg60.Th + ioCheckPerfdata[psi.IoSomeAvg60].Warn = &config.WarningIoSomeAvg60.Th } else { - ioCheck.Perfdata[psi.IoSomeAvg60].Warn = &config.WarningIoAvg.Th + ioCheckPerfdata[psi.IoSomeAvg60].Warn = &config.WarningIoAvg.Th } if config.WarningIoSomeAvg300.IsSet { - ioCheck.Perfdata[psi.IoSomeAvg300].Warn = &config.WarningIoSomeAvg300.Th + ioCheckPerfdata[psi.IoSomeAvg300].Warn = &config.WarningIoSomeAvg300.Th } else { - ioCheck.Perfdata[psi.IoSomeAvg300].Warn = &config.WarningIoAvg.Th + ioCheckPerfdata[psi.IoSomeAvg300].Warn = &config.WarningIoAvg.Th } if config.CriticalIoSomeAvg10.IsSet { - ioCheck.Perfdata[psi.IoSomeAvg10].Crit = &config.CriticalIoSomeAvg10.Th + ioCheckPerfdata[psi.IoSomeAvg10].Crit = &config.CriticalIoSomeAvg10.Th } else { - ioCheck.Perfdata[psi.IoSomeAvg10].Crit = &config.CriticalIoAvg.Th + ioCheckPerfdata[psi.IoSomeAvg10].Crit = &config.CriticalIoAvg.Th } if config.CriticalIoSomeAvg60.IsSet { - ioCheck.Perfdata[psi.IoSomeAvg60].Crit = &config.CriticalIoSomeAvg60.Th + ioCheckPerfdata[psi.IoSomeAvg60].Crit = &config.CriticalIoSomeAvg60.Th } else { - ioCheck.Perfdata[psi.IoSomeAvg60].Crit = &config.CriticalIoAvg.Th + ioCheckPerfdata[psi.IoSomeAvg60].Crit = &config.CriticalIoAvg.Th } if config.CriticalIoSomeAvg300.IsSet { - ioCheck.Perfdata[psi.IoSomeAvg300].Crit = &config.CriticalIoSomeAvg300.Th + ioCheckPerfdata[psi.IoSomeAvg300].Crit = &config.CriticalIoSomeAvg300.Th } else { - ioCheck.Perfdata[psi.IoSomeAvg300].Crit = &config.CriticalIoAvg.Th + ioCheckPerfdata[psi.IoSomeAvg300].Crit = &config.CriticalIoAvg.Th } - ioSomeSc := result.PartialResult{} - _ = ioSomeSc.SetDefaultState(check.OK) + ioSomeSc := result.NewPartialResult() + ioSomeSc.SetDefaultState(check.OK) if (ioCheck.GetStatus() != check.Critical) && (ioCheck.GetStatus() != check.Warning) { - if ioCheck.Perfdata[psi.IoSomeAvg10].Warn.DoesViolate(psiIo.Some.Avg10) || - ioCheck.Perfdata[psi.IoSomeAvg60].Warn.DoesViolate(psiIo.Some.Avg60) || - ioCheck.Perfdata[psi.IoSomeAvg300].Warn.DoesViolate(psiIo.Some.Avg300) { - _ = ioSomeSc.SetState(check.Warning) + if ioCheckPerfdata[psi.IoSomeAvg10].Warn.DoesViolate(psiIo.Some.Avg10) || + ioCheckPerfdata[psi.IoSomeAvg60].Warn.DoesViolate(psiIo.Some.Avg60) || + ioCheckPerfdata[psi.IoSomeAvg300].Warn.DoesViolate(psiIo.Some.Avg300) { + ioSomeSc.SetState(check.Warning) } } if ioCheck.GetStatus() != check.Critical { - if ioCheck.Perfdata[psi.IoSomeAvg10].Crit.DoesViolate(psiIo.Some.Avg10) || - ioCheck.Perfdata[psi.IoSomeAvg60].Crit.DoesViolate(psiIo.Some.Avg60) || - ioCheck.Perfdata[psi.IoSomeAvg300].Crit.DoesViolate(psiIo.Some.Avg300) { - _ = ioSomeSc.SetState(check.Critical) + if ioCheckPerfdata[psi.IoSomeAvg10].Crit.DoesViolate(psiIo.Some.Avg10) || + ioCheckPerfdata[psi.IoSomeAvg60].Crit.DoesViolate(psiIo.Some.Avg60) || + ioCheckPerfdata[psi.IoSomeAvg300].Crit.DoesViolate(psiIo.Some.Avg300) { + ioSomeSc.SetState(check.Critical) } } - ioSomeSc.Output = fmt.Sprintf("Some - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiIo.Some.Avg10, psiIo.Some.Avg60, psiIo.Some.Avg300) + ioSomeSc.SetOutput(fmt.Sprintf("Some - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiIo.Some.Avg10, psiIo.Some.Avg60, psiIo.Some.Avg300)) ioCheck.AddSubcheck(ioSomeSc) + for _, item := range ioCheckPerfdata { + ioCheck.AddPerfdata(item) + } + return ioCheck } -func checkPsiMemoryPressure(config *psiConfig) result.PartialResult { - var memoryCheck result.PartialResult +func checkPsiMemoryPressure(config *psiConfig) *result.PartialResult { + memoryCheck := result.NewPartialResult() - _ = memoryCheck.SetDefaultState(check.OK) - memoryCheck.Output = "Memory" + memoryCheck.SetDefaultState(check.OK) + memoryCheck.SetOutput("Memory") psiMemory, err := psi.ReadMemoryPressure() if err != nil { if errors.Is(err, os.ErrNotExist) { - _ = memoryCheck.SetState(check.Unknown) - memoryCheck.Output = "IO pressure file not found. Perhaps the PSI interface is not active on this system? It might be necessary to change the kernel config" + memoryCheck.SetState(check.Unknown) + memoryCheck.SetOutput("IO pressure file not found. Perhaps the PSI interface is not active on this system? It might be necessary to change the kernel config") return memoryCheck } @@ -533,125 +541,129 @@ func checkPsiMemoryPressure(config *psiConfig) result.PartialResult { check.ExitError(err) } - memoryCheck.Perfdata = *psiMemory.Perfdata() + memoryCheckPerfdata := *psiMemory.Perfdata() //nolint:nestif if psiMemory.FullPresent { // Warn thresholds if config.WarningMemoryFullAvg10.IsSet { - memoryCheck.Perfdata[psi.MemoryFullAvg10].Warn = &config.WarningMemoryFullAvg10.Th + memoryCheckPerfdata[psi.MemoryFullAvg10].Warn = &config.WarningMemoryFullAvg10.Th } else { - memoryCheck.Perfdata[psi.MemoryFullAvg10].Warn = &config.WarningMemoryAvg.Th + memoryCheckPerfdata[psi.MemoryFullAvg10].Warn = &config.WarningMemoryAvg.Th } if config.WarningMemoryFullAvg60.IsSet { - memoryCheck.Perfdata[psi.MemoryFullAvg60].Warn = &config.WarningMemoryFullAvg60.Th + memoryCheckPerfdata[psi.MemoryFullAvg60].Warn = &config.WarningMemoryFullAvg60.Th } else { - memoryCheck.Perfdata[psi.MemoryFullAvg60].Warn = &config.WarningMemoryAvg.Th + memoryCheckPerfdata[psi.MemoryFullAvg60].Warn = &config.WarningMemoryAvg.Th } if config.WarningMemoryFullAvg300.IsSet { - memoryCheck.Perfdata[psi.MemoryFullAvg300].Warn = &config.WarningMemoryFullAvg300.Th + memoryCheckPerfdata[psi.MemoryFullAvg300].Warn = &config.WarningMemoryFullAvg300.Th } else { - memoryCheck.Perfdata[psi.MemoryFullAvg300].Warn = &config.WarningMemoryAvg.Th + memoryCheckPerfdata[psi.MemoryFullAvg300].Warn = &config.WarningMemoryAvg.Th } // Critical thresholds if config.CriticalMemoryFullAvg10.IsSet { - memoryCheck.Perfdata[psi.MemoryFullAvg10].Crit = &config.CriticalMemoryFullAvg10.Th + memoryCheckPerfdata[psi.MemoryFullAvg10].Crit = &config.CriticalMemoryFullAvg10.Th } else { - memoryCheck.Perfdata[psi.MemoryFullAvg10].Crit = &config.CriticalMemoryAvg.Th + memoryCheckPerfdata[psi.MemoryFullAvg10].Crit = &config.CriticalMemoryAvg.Th } if config.CriticalMemoryFullAvg60.IsSet { - memoryCheck.Perfdata[psi.MemoryFullAvg60].Crit = &config.CriticalMemoryFullAvg60.Th + memoryCheckPerfdata[psi.MemoryFullAvg60].Crit = &config.CriticalMemoryFullAvg60.Th } else { - memoryCheck.Perfdata[psi.MemoryFullAvg60].Crit = &config.CriticalMemoryAvg.Th + memoryCheckPerfdata[psi.MemoryFullAvg60].Crit = &config.CriticalMemoryAvg.Th } if config.CriticalMemoryFullAvg300.IsSet { - memoryCheck.Perfdata[psi.MemoryFullAvg300].Crit = &config.CriticalMemoryFullAvg300.Th + memoryCheckPerfdata[psi.MemoryFullAvg300].Crit = &config.CriticalMemoryFullAvg300.Th } else { - memoryCheck.Perfdata[psi.MemoryFullAvg300].Crit = &config.CriticalMemoryAvg.Th + memoryCheckPerfdata[psi.MemoryFullAvg300].Crit = &config.CriticalMemoryAvg.Th } - memoryFullSc := result.PartialResult{} - _ = memoryFullSc.SetDefaultState(check.OK) + memoryFullSc := result.NewPartialResult() + memoryFullSc.SetDefaultState(check.OK) - if memoryCheck.Perfdata[psi.MemoryFullAvg10].Warn.DoesViolate(psiMemory.Full.Avg10) || - memoryCheck.Perfdata[psi.MemoryFullAvg60].Warn.DoesViolate(psiMemory.Full.Avg60) || - memoryCheck.Perfdata[psi.MemoryFullAvg300].Warn.DoesViolate(psiMemory.Full.Avg300) { - _ = memoryFullSc.SetState(check.Warning) + if memoryCheckPerfdata[psi.MemoryFullAvg10].Warn.DoesViolate(psiMemory.Full.Avg10) || + memoryCheckPerfdata[psi.MemoryFullAvg60].Warn.DoesViolate(psiMemory.Full.Avg60) || + memoryCheckPerfdata[psi.MemoryFullAvg300].Warn.DoesViolate(psiMemory.Full.Avg300) { + memoryFullSc.SetState(check.Warning) } - if memoryCheck.Perfdata[psi.MemoryFullAvg10].Crit.DoesViolate(psiMemory.Full.Avg10) || - memoryCheck.Perfdata[psi.MemoryFullAvg60].Crit.DoesViolate(psiMemory.Full.Avg60) || - memoryCheck.Perfdata[psi.MemoryFullAvg300].Crit.DoesViolate(psiMemory.Full.Avg300) { - _ = memoryFullSc.SetState(check.Critical) + if memoryCheckPerfdata[psi.MemoryFullAvg10].Crit.DoesViolate(psiMemory.Full.Avg10) || + memoryCheckPerfdata[psi.MemoryFullAvg60].Crit.DoesViolate(psiMemory.Full.Avg60) || + memoryCheckPerfdata[psi.MemoryFullAvg300].Crit.DoesViolate(psiMemory.Full.Avg300) { + memoryFullSc.SetState(check.Critical) } - memoryFullSc.Output = fmt.Sprintf("Full - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiMemory.Full.Avg10, psiMemory.Full.Avg60, psiMemory.Full.Avg300) + memoryFullSc.SetOutput(fmt.Sprintf("Full - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiMemory.Full.Avg10, psiMemory.Full.Avg60, psiMemory.Full.Avg300)) memoryCheck.AddSubcheck(memoryFullSc) } if config.WarningMemorySomeAvg10.IsSet { - memoryCheck.Perfdata[psi.MemorySomeAvg10].Warn = &config.WarningMemorySomeAvg10.Th + memoryCheckPerfdata[psi.MemorySomeAvg10].Warn = &config.WarningMemorySomeAvg10.Th } else { - memoryCheck.Perfdata[psi.MemorySomeAvg10].Warn = &config.WarningMemoryAvg.Th + memoryCheckPerfdata[psi.MemorySomeAvg10].Warn = &config.WarningMemoryAvg.Th } if config.WarningMemorySomeAvg60.IsSet { - memoryCheck.Perfdata[psi.MemorySomeAvg60].Warn = &config.WarningMemorySomeAvg60.Th + memoryCheckPerfdata[psi.MemorySomeAvg60].Warn = &config.WarningMemorySomeAvg60.Th } else { - memoryCheck.Perfdata[psi.MemorySomeAvg60].Warn = &config.WarningMemoryAvg.Th + memoryCheckPerfdata[psi.MemorySomeAvg60].Warn = &config.WarningMemoryAvg.Th } if config.WarningMemorySomeAvg300.IsSet { - memoryCheck.Perfdata[psi.MemorySomeAvg300].Warn = &config.WarningMemorySomeAvg300.Th + memoryCheckPerfdata[psi.MemorySomeAvg300].Warn = &config.WarningMemorySomeAvg300.Th } else { - memoryCheck.Perfdata[psi.MemorySomeAvg300].Warn = &config.WarningMemoryAvg.Th + memoryCheckPerfdata[psi.MemorySomeAvg300].Warn = &config.WarningMemoryAvg.Th } // Critical thresholds if config.CriticalMemorySomeAvg10.IsSet { - memoryCheck.Perfdata[psi.MemorySomeAvg10].Crit = &config.CriticalMemorySomeAvg10.Th + memoryCheckPerfdata[psi.MemorySomeAvg10].Crit = &config.CriticalMemorySomeAvg10.Th } else { - memoryCheck.Perfdata[psi.MemorySomeAvg10].Crit = &config.CriticalMemoryAvg.Th + memoryCheckPerfdata[psi.MemorySomeAvg10].Crit = &config.CriticalMemoryAvg.Th } if config.CriticalMemorySomeAvg60.IsSet { - memoryCheck.Perfdata[psi.MemorySomeAvg60].Crit = &config.CriticalMemorySomeAvg60.Th + memoryCheckPerfdata[psi.MemorySomeAvg60].Crit = &config.CriticalMemorySomeAvg60.Th } else { - memoryCheck.Perfdata[psi.MemorySomeAvg60].Crit = &config.CriticalMemoryAvg.Th + memoryCheckPerfdata[psi.MemorySomeAvg60].Crit = &config.CriticalMemoryAvg.Th } if config.CriticalMemorySomeAvg300.IsSet { - memoryCheck.Perfdata[psi.MemorySomeAvg300].Crit = &config.CriticalMemorySomeAvg300.Th + memoryCheckPerfdata[psi.MemorySomeAvg300].Crit = &config.CriticalMemorySomeAvg300.Th } else { - memoryCheck.Perfdata[psi.MemorySomeAvg300].Crit = &config.CriticalMemoryAvg.Th + memoryCheckPerfdata[psi.MemorySomeAvg300].Crit = &config.CriticalMemoryAvg.Th } - memorySomeSc := result.PartialResult{} - _ = memorySomeSc.SetDefaultState(check.OK) + memorySomeSc := result.NewPartialResult() + memorySomeSc.SetDefaultState(check.OK) if (memoryCheck.GetStatus() != check.Critical) && (memoryCheck.GetStatus() != check.Warning) { - if memoryCheck.Perfdata[psi.MemorySomeAvg10].Warn.DoesViolate(psiMemory.Some.Avg10) || - memoryCheck.Perfdata[psi.MemorySomeAvg60].Warn.DoesViolate(psiMemory.Some.Avg60) || - memoryCheck.Perfdata[psi.MemorySomeAvg300].Warn.DoesViolate(psiMemory.Some.Avg300) { - _ = memorySomeSc.SetState(check.Warning) + if memoryCheckPerfdata[psi.MemorySomeAvg10].Warn.DoesViolate(psiMemory.Some.Avg10) || + memoryCheckPerfdata[psi.MemorySomeAvg60].Warn.DoesViolate(psiMemory.Some.Avg60) || + memoryCheckPerfdata[psi.MemorySomeAvg300].Warn.DoesViolate(psiMemory.Some.Avg300) { + memorySomeSc.SetState(check.Warning) } } if memoryCheck.GetStatus() != check.Critical { - if memoryCheck.Perfdata[psi.MemorySomeAvg10].Crit.DoesViolate(psiMemory.Some.Avg10) || - memoryCheck.Perfdata[psi.MemorySomeAvg60].Crit.DoesViolate(psiMemory.Some.Avg60) || - memoryCheck.Perfdata[psi.MemorySomeAvg300].Crit.DoesViolate(psiMemory.Some.Avg300) { - _ = memorySomeSc.SetState(check.Critical) + if memoryCheckPerfdata[psi.MemorySomeAvg10].Crit.DoesViolate(psiMemory.Some.Avg10) || + memoryCheckPerfdata[psi.MemorySomeAvg60].Crit.DoesViolate(psiMemory.Some.Avg60) || + memoryCheckPerfdata[psi.MemorySomeAvg300].Crit.DoesViolate(psiMemory.Some.Avg300) { + memorySomeSc.SetState(check.Critical) } } - memorySomeSc.Output = fmt.Sprintf("Some - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiMemory.Some.Avg10, psiMemory.Some.Avg60, psiMemory.Some.Avg300) + memorySomeSc.SetOutput(fmt.Sprintf("Some - Avg10: %.2f, Avg60: %.2f, Avg300: %.2f", psiMemory.Some.Avg10, psiMemory.Some.Avg60, psiMemory.Some.Avg300)) memoryCheck.AddSubcheck(memorySomeSc) + for _, item := range memoryCheckPerfdata { + memoryCheck.AddPerfdata(item) + } + return memoryCheck } diff --git a/cmd/root.go b/cmd/root.go index cc58699..284e43b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -67,7 +67,7 @@ func RunFunction(cmd *cobra.Command, args []string) { if dumpConfig { ConfigDump(cmd, cmd.CommandPath()) - os.Exit(check.OK) + os.Exit(0) } Help(cmd, args) @@ -76,7 +76,7 @@ func RunFunction(cmd *cobra.Command, args []string) { func Help(cmd *cobra.Command, _ []string) { _ = cmd.Usage() - os.Exit(check.Unknown) + os.Exit(3) } func ConfigDump(cmd *cobra.Command, executableName string) { diff --git a/cmd/sensors.go b/cmd/sensors.go index 470986d..a2d2cf2 100644 --- a/cmd/sensors.go +++ b/cmd/sensors.go @@ -45,7 +45,7 @@ thresholds respecting the sensor type and the respective specialities`, if len(devices) == 0 { overall.Add(check.Unknown, "No devices found") - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) } var ( @@ -53,30 +53,35 @@ thresholds respecting the sensor type and the respective specialities`, ) for _, device := range devices { - var devicePartial result.PartialResult + devicePartial := result.NewPartialResult() - _ = devicePartial.SetDefaultState(check.OK) + devicePartial.SetDefaultState(check.OK) + + devicePartial.SetOutput(device.Name) - devicePartial.Output = device.Name for idx, sensor := range device.Sensors { - var ssc result.PartialResult + ssc := result.NewPartialResult() + + ssc.SetDefaultState(check.OK) - _ = ssc.SetDefaultState(check.OK) - ssc.Perfdata.Add(&(device.Sensors[idx]).Perfdata) + sensorPerfdata := &(device.Sensors[idx]).Perfdata + ssc.AddPerfdata(sensorPerfdata) + preliminaryOutput := "" if sensor.Alarm { - ssc.Output = "Alarm!" - _ = ssc.SetState(check.Critical) + preliminaryOutput = "Alarm!" + + ssc.SetState(check.Critical) + alarms++ } else { - ssc.Output = "Ok" - _ = ssc.SetState(check.OK) + preliminaryOutput = "Ok" + + ssc.SetState(check.OK) } // Add perfdata label (sensor name) to ouptput to make it more descriptive - if len(ssc.Perfdata) == 1 { - ssc.Output = fmt.Sprintf("%s: %s - %v%s", ssc.Perfdata[0].Label, ssc.Output, ssc.Perfdata[0].Value, ssc.Perfdata[0].Uom) - } + ssc.SetOutput(fmt.Sprintf("%s: %s - %v%s", sensorPerfdata.Label, preliminaryOutput, sensorPerfdata.Value, sensorPerfdata.Uom)) devicePartial.AddSubcheck(ssc) } @@ -84,7 +89,7 @@ thresholds respecting the sensor type and the respective specialities`, overall.AddSubcheck(devicePartial) } - check.ExitRaw(overall.GetStatus(), overall.GetOutput()) + check.Exit(overall.GetStatus(), overall.GetOutput()) }, } diff --git a/go.mod b/go.mod index 6acd313..abf70d2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/NETWAYS/check_system_basics go 1.25.0 require ( - github.com/NETWAYS/go-check v0.6.4 + github.com/NETWAYS/go-check v1.0.0 github.com/NETWAYS/go-icingadsl v0.1.2 github.com/shirou/gopsutil/v3 v3.24.5 github.com/spf13/cobra v1.10.2 diff --git a/go.sum b/go.sum index 17d8a2d..2aea79e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ github.com/NETWAYS/go-check v0.6.4 h1:4WETSVNZNEP0Yudcp5xlvxq6RGn920cmUKq4fz/P1GQ= github.com/NETWAYS/go-check v0.6.4/go.mod h1:8/GWnq8SirreAixgRmcp82JG16NnEl38rHq9phICy9s= +github.com/NETWAYS/go-check v1.0.0-rc4 h1:L+yS7wklUz/eUzkGndSKpi3DqCXmCAOSEQtBWkxF+7k= +github.com/NETWAYS/go-check v1.0.0-rc4/go.mod h1:8/GWnq8SirreAixgRmcp82JG16NnEl38rHq9phICy9s= +github.com/NETWAYS/go-check v1.0.0 h1:YkzTwFfGR+Z+mK3Wsqpnu8wibzsB30im19iPNfCOsMQ= +github.com/NETWAYS/go-check v1.0.0/go.mod h1:8/GWnq8SirreAixgRmcp82JG16NnEl38rHq9phICy9s= github.com/NETWAYS/go-icingadsl v0.1.2 h1:F25Y7HAw9VF8GC4eJFbhXzA4TBUwA/0R/n2vbTz6qsQ= github.com/NETWAYS/go-icingadsl v0.1.2/go.mod h1:CE74xcjOUHxYpltsooRTimANss5H2VZHFlme5138Cqw= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= diff --git a/internal/load/load.go b/internal/load/load.go index b40e29a..ec555b2 100644 --- a/internal/load/load.go +++ b/internal/load/load.go @@ -3,7 +3,7 @@ package load import ( "fmt" - "github.com/NETWAYS/go-check/perfdata" + "github.com/NETWAYS/go-check" "github.com/shirou/gopsutil/v3/load" ) @@ -34,8 +34,8 @@ func (l *Load) GetOutput() (output string) { return output } -func (l *Load) GetPerfData() perfdata.PerfdataList { - perfList := perfdata.PerfdataList{ +func (l *Load) GetPerfData() check.PerfdataList { + perfList := check.PerfdataList{ { Label: "load1", Value: l.LoadAvg.Load1, diff --git a/internal/psi/psi.go b/internal/psi/psi.go index 64fcd28..43a594f 100644 --- a/internal/psi/psi.go +++ b/internal/psi/psi.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "github.com/NETWAYS/go-check/perfdata" + "github.com/NETWAYS/go-check" ) const ( @@ -64,10 +64,10 @@ const ( io ) -func (p *PressureValue) Perfdata(prefix string) *perfdata.PerfdataList { - var ret perfdata.PerfdataList +func (p *PressureValue) Perfdata(prefix string) *check.PerfdataList { + var ret check.PerfdataList - avg10 := perfdata.Perfdata{} + avg10 := check.Perfdata{} avg10.Label = prefix + "avg10" avg10.Value = p.Avg10 avg10.Uom = "%" @@ -75,7 +75,7 @@ func (p *PressureValue) Perfdata(prefix string) *perfdata.PerfdataList { avg10.Max = 100 ret.Add(&avg10) - avg60 := perfdata.Perfdata{} + avg60 := check.Perfdata{} avg60.Label = prefix + "avg60" avg60.Value = p.Avg60 avg60.Uom = "%" @@ -83,7 +83,7 @@ func (p *PressureValue) Perfdata(prefix string) *perfdata.PerfdataList { avg60.Max = 100 ret.Add(&avg60) - avg300 := perfdata.Perfdata{} + avg300 := check.Perfdata{} avg300.Label = prefix + "avg300" avg300.Value = p.Avg300 avg300.Uom = "%" @@ -91,7 +91,7 @@ func (p *PressureValue) Perfdata(prefix string) *perfdata.PerfdataList { avg300.Max = 100 ret.Add(&avg300) - total := perfdata.Perfdata{} + total := check.Perfdata{} total.Label = prefix + "total" total.Value = p.Total total.Min = 0 @@ -101,7 +101,7 @@ func (p *PressureValue) Perfdata(prefix string) *perfdata.PerfdataList { return &ret } -func (p *PressureElement) Perfdata() *perfdata.PerfdataList { +func (p *PressureElement) Perfdata() *check.PerfdataList { switch p.Type { case cpu: tmp := *p.Some.Perfdata("cpu-some-") diff --git a/internal/psi/psi_test.go b/internal/psi/psi_test.go index 671c188..5a01a5c 100644 --- a/internal/psi/psi_test.go +++ b/internal/psi/psi_test.go @@ -4,7 +4,7 @@ import ( "reflect" "testing" - "github.com/NETWAYS/go-check/perfdata" + "github.com/NETWAYS/go-check" ) func TestPressureValueString(t *testing.T) { @@ -15,11 +15,11 @@ func TestPressureValueString(t *testing.T) { } // Expected - expected := perfdata.PerfdataList{} - expected.Add(&perfdata.Perfdata{Label: "preavg10", Value: 1.5, Min: 0, Max: 100, Uom: "%"}) - expected.Add(&perfdata.Perfdata{Label: "preavg60", Value: 2.5, Min: 0, Max: 100, Uom: "%"}) - expected.Add(&perfdata.Perfdata{Label: "preavg300", Value: 3.5, Min: 0, Max: 100, Uom: "%"}) - expected.Add(&perfdata.Perfdata{Label: "pretotal", Value: uint64(0), Min: 0, Uom: "c"}) + expected := check.PerfdataList{} + expected.Add(&check.Perfdata{Label: "preavg10", Value: 1.5, Min: 0, Max: 100, Uom: "%"}) + expected.Add(&check.Perfdata{Label: "preavg60", Value: 2.5, Min: 0, Max: 100, Uom: "%"}) + expected.Add(&check.Perfdata{Label: "preavg300", Value: 3.5, Min: 0, Max: 100, Uom: "%"}) + expected.Add(&check.Perfdata{Label: "pretotal", Value: uint64(0), Min: 0, Uom: "c"}) if !reflect.DeepEqual(&expected, pv.Perfdata("pre")) { t.Fatalf("expected %v, got %v", &expected, pv.Perfdata("pre")) @@ -35,15 +35,15 @@ func TestPressureElementString(t *testing.T) { } // Expected - pecpuexpected := perfdata.PerfdataList{} - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-some-avg10", Value: float64(0.1), Min: 0, Max: 100, Uom: "%"}) - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-some-avg60", Value: float64(0.6), Min: 0, Max: 100, Uom: "%"}) - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-some-avg300", Value: float64(0.3), Min: 0, Max: 100, Uom: "%"}) - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-some-total", Value: uint64(0), Min: 0, Uom: "c"}) - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-full-avg10", Value: float64(0.1), Min: 0, Max: 100, Uom: "%"}) - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-full-avg60", Value: float64(0.6), Min: 0, Max: 100, Uom: "%"}) - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-full-avg300", Value: float64(0.3), Min: 0, Max: 100, Uom: "%"}) - pecpuexpected.Add(&perfdata.Perfdata{Label: "cpu-full-total", Value: uint64(0), Min: 0, Uom: "c"}) + pecpuexpected := check.PerfdataList{} + pecpuexpected.Add(&check.Perfdata{Label: "cpu-some-avg10", Value: float64(0.1), Min: 0, Max: 100, Uom: "%"}) + pecpuexpected.Add(&check.Perfdata{Label: "cpu-some-avg60", Value: float64(0.6), Min: 0, Max: 100, Uom: "%"}) + pecpuexpected.Add(&check.Perfdata{Label: "cpu-some-avg300", Value: float64(0.3), Min: 0, Max: 100, Uom: "%"}) + pecpuexpected.Add(&check.Perfdata{Label: "cpu-some-total", Value: uint64(0), Min: 0, Uom: "c"}) + pecpuexpected.Add(&check.Perfdata{Label: "cpu-full-avg10", Value: float64(0.1), Min: 0, Max: 100, Uom: "%"}) + pecpuexpected.Add(&check.Perfdata{Label: "cpu-full-avg60", Value: float64(0.6), Min: 0, Max: 100, Uom: "%"}) + pecpuexpected.Add(&check.Perfdata{Label: "cpu-full-avg300", Value: float64(0.3), Min: 0, Max: 100, Uom: "%"}) + pecpuexpected.Add(&check.Perfdata{Label: "cpu-full-total", Value: uint64(0), Min: 0, Uom: "c"}) if !reflect.DeepEqual(&pecpuexpected, pecpu.Perfdata()) { t.Fatalf("expected %v, got %v", &pecpuexpected, pecpu.Perfdata()) @@ -57,11 +57,11 @@ func TestPressureElementString(t *testing.T) { } // Expected - peioexpected := perfdata.PerfdataList{} - peioexpected.Add(&perfdata.Perfdata{Label: "io-some-avg10", Value: float64(0.1), Min: 0, Max: 100, Uom: "%"}) - peioexpected.Add(&perfdata.Perfdata{Label: "io-some-avg60", Value: float64(0.6), Min: 0, Max: 100, Uom: "%"}) - peioexpected.Add(&perfdata.Perfdata{Label: "io-some-avg300", Value: float64(0.3), Min: 0, Max: 100, Uom: "%"}) - peioexpected.Add(&perfdata.Perfdata{Label: "io-some-total", Value: uint64(0), Min: 0, Uom: "c"}) + peioexpected := check.PerfdataList{} + peioexpected.Add(&check.Perfdata{Label: "io-some-avg10", Value: float64(0.1), Min: 0, Max: 100, Uom: "%"}) + peioexpected.Add(&check.Perfdata{Label: "io-some-avg60", Value: float64(0.6), Min: 0, Max: 100, Uom: "%"}) + peioexpected.Add(&check.Perfdata{Label: "io-some-avg300", Value: float64(0.3), Min: 0, Max: 100, Uom: "%"}) + peioexpected.Add(&check.Perfdata{Label: "io-some-total", Value: uint64(0), Min: 0, Uom: "c"}) if !reflect.DeepEqual(&peioexpected, peio.Perfdata()) { t.Fatalf("expected %v, got %v", &peioexpected, peio.Perfdata()) diff --git a/internal/sensors/sensors.go b/internal/sensors/sensors.go index e76d5f2..947bc43 100644 --- a/internal/sensors/sensors.go +++ b/internal/sensors/sensors.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/NETWAYS/go-check" - "github.com/NETWAYS/go-check/perfdata" ) /* @@ -21,7 +20,7 @@ type Sensor struct { Name string Path string Alarm bool - Perfdata perfdata.Perfdata + Perfdata check.Perfdata } type Device struct { diff --git a/internal/sensors/sensors_test.go b/internal/sensors/sensors_test.go index 8c7d1bc..eb3f2f7 100644 --- a/internal/sensors/sensors_test.go +++ b/internal/sensors/sensors_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/NETWAYS/go-check" - "github.com/NETWAYS/go-check/perfdata" ) func TestSensorAndDeviceString(t *testing.T) { @@ -12,7 +11,7 @@ func TestSensorAndDeviceString(t *testing.T) { Name: "testname", Path: "testpath", Alarm: false, - Perfdata: perfdata.Perfdata{ + Perfdata: check.Perfdata{ Label: "test", Value: 10.0, Uom: "%",