From f1a6ae81345c4e236cb72ee6c550616ff1ae5f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Mon, 20 Apr 2026 12:13:29 +0200 Subject: [PATCH 1/2] apply go fix --- cmd/filesystem.go | 8 +++----- internal/sensors/sensors.go | 11 ++++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cmd/filesystem.go b/cmd/filesystem.go index ee2998a..8b1074c 100644 --- a/cmd/filesystem.go +++ b/cmd/filesystem.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "slices" "strconv" "time" @@ -521,11 +522,8 @@ func computeFsCheckResult(fs *filesystem.FilesystemType, config *filesystem.Chec filesystemsWithFixedNumberOfInodes := filesystem.GetFilesystemsWithFixedNumberOfInodes() - for i := range filesystemsWithFixedNumberOfInodes { - if fs.PartStats.Fstype == filesystemsWithFixedNumberOfInodes[i] { - returnResult.AddSubcheck(computeFsCheckResultInodes(fs, config)) - break - } + if slices.Contains(filesystemsWithFixedNumberOfInodes, fs.PartStats.Fstype) { + returnResult.AddSubcheck(computeFsCheckResultInodes(fs, config)) } return returnResult diff --git a/internal/sensors/sensors.go b/internal/sensors/sensors.go index b40f6fc..33a32dc 100644 --- a/internal/sensors/sensors.go +++ b/internal/sensors/sensors.go @@ -38,17 +38,18 @@ const ( ) func (d *Device) String() string { - result := d.Name - result += ": " + var result strings.Builder + result.WriteString(d.Name) + result.WriteString(": ") for idx := range d.Sensors { - result += d.Sensors[idx].String() + result.WriteString(d.Sensors[idx].String()) if idx < len(d.Sensors) { - result += ";" + result.WriteString(";") } } - return result + return result.String() } func (s *Sensor) String() string { From eb587b58275eabbcbf93e0ca3b38f371365d1921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Mon, 20 Apr 2026 12:28:55 +0200 Subject: [PATCH 2/2] Fix linter issues --- cmd/filesystem.go | 4 +--- cmd/load.go | 9 +++++++++ cmd/memory.go | 5 ++++- cmd/netdev.go | 2 -- cmd/psi.go | 3 +++ cmd/root.go | 3 ++- cmd/sensors.go | 5 +++++ internal/common/filter/filter.go | 3 +-- internal/filesystem/filesystem.go | 3 ++- internal/netdev/netdev.go | 7 +------ internal/psi/psi.go | 1 - internal/sensors/sensors.go | 21 +++------------------ 12 files changed, 31 insertions(+), 35 deletions(-) diff --git a/cmd/filesystem.go b/cmd/filesystem.go index 8b1074c..59b977f 100644 --- a/cmd/filesystem.go +++ b/cmd/filesystem.go @@ -71,7 +71,6 @@ var diskCmd = &cobra.Command{ \_ [OK] Percentage of used inodes: 5.23% |/_inodes_free_percentage=82.688% /_space_free=33146855424B;@40;@20;0;62669000704 /_space_used=26305536000B;40;20;0;62669000704 /_inodes_free=3231100;;@200;0;3907584 /_inodes_used=676484;;@400;0;3907584 /_space_free_percentage=55.754%;60;30 /_space_used_percentage=44.246%;@20:40;@20 /_inodes_used_percentage=17.312%;99;98 /boot_inodes_free_percentage=99.718% /boot_space_free=173091840B;@40;@20;0;493201408 /boot_space_used=294524928B;40;20;0;493201408 /boot_inodes_free=124576;;@200;0;124928 /boot_inodes_used=352;;@400;0;124928 /boot_space_free_percentage=37.016%;60;30 /boot_space_used_percentage=62.984%;@20:40;@20 /boot_inodes_used_percentage=0.282%;99;98 /var_inodes_free_percentage=92.832% /var_space_free=141826428928B;@40;@20;0;250843787264 /var_space_used=96200613888B;40;20;0;250843787264 /var_inodes_free=14510026;;@200;0;15630336 /var_inodes_used=1120310;;@400;0;15630336 /var_space_free_percentage=59.584%;60;30 /var_space_used_percentage=40.416%;@20:40;@20 /var_inodes_used_percentage=7.168%;99;98 /home_inodes_free_percentage=94.768% /home_space_free=247921197056B;@40;@20;0;502813065216 /home_space_used=229275156480B;40;20;0;502813065216 /home_inodes_free=29617311;;@200;0;31252480 /home_inodes_used=1635169;;@400;0;31252480 /home_space_free_percentage=51.954%;60;30 /home_space_used_percentage=48.046%;@20:40;@20 /home_inodes_used_percentage=5.232%;99;98`, Run: func(_ *cobra.Command, _ []string) { - overall := result.Overall{} err := validateOptions(&FsConfig) @@ -130,7 +129,6 @@ var diskCmd = &cobra.Command{ } overall.AddSubcheck(countResult) - } else if len(filesystemList) == 0 { nullResult := result.PartialResult{} _ = nullResult.SetState(check.OK) @@ -155,12 +153,12 @@ var diskCmd = &cobra.Command{ 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) } + overall.AddSubcheck(sc) } // Output and Exit check.ExitRaw(overall.GetStatus(), overall.GetOutput()) - }, } diff --git a/cmd/load.go b/cmd/load.go index c3f7d3a..60ecdcc 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -51,6 +51,7 @@ var loadCmd = &cobra.Command{ // 1 Minute average var partialLoad1 result.PartialResult + _ = partialLoad1.SetDefaultState(check.OK) // TODO Use strings.Builder @@ -77,14 +78,17 @@ var loadCmd = &cobra.Command{ } else { _ = partialLoad1.SetState(check.OK) } + if LoadConfig.PerCPU { tmpOutput += fmt.Sprintf(", system total: %.2f", originalLoad[0]) } + partialLoad1.Output = tmpOutput partialLoad1.Perfdata.Add(tmpPerfdata) // 5 Minute average var partialLoad5 result.PartialResult + _ = partialLoad5.SetDefaultState(check.OK) tmpOutput = fmt.Sprintf("5 minute average: %.2f", loadStats.LoadAvg.Load5) @@ -110,14 +114,17 @@ var loadCmd = &cobra.Command{ } else { _ = partialLoad5.SetState(check.OK) } + if LoadConfig.PerCPU { tmpOutput += fmt.Sprintf(", system total: %.2f", originalLoad[1]) } + partialLoad5.Output = tmpOutput partialLoad5.Perfdata.Add(tmpPerfdata) // 15 Minute average var partialLoad15 result.PartialResult + _ = partialLoad15.SetDefaultState(check.OK) tmpOutput = fmt.Sprintf("15 minute average: %.2f", loadStats.LoadAvg.Load15) @@ -143,9 +150,11 @@ var loadCmd = &cobra.Command{ } else { _ = partialLoad15.SetState(check.OK) } + if LoadConfig.PerCPU { tmpOutput += fmt.Sprintf(", system total: %.2f", originalLoad[2]) } + partialLoad15.Output = tmpOutput partialLoad15.Perfdata.Add(tmpPerfdata) diff --git a/cmd/memory.go b/cmd/memory.go index fe7378c..df7b9a5 100644 --- a/cmd/memory.go +++ b/cmd/memory.go @@ -27,7 +27,6 @@ var memoryCmd = &cobra.Command{ |available_memory_percentage=74.36%;15:100;5:100 available_memory=24856633344B;10:20;;0;33427595264 free_memory=16696102912B;;;0;33427595264 free_memory_percentage=49.947%;;50:80 used_memory=6542696448B;@10;;0;33427595264 free_memory_percentage=19.573% swap_usage_percent=0%;20;85 swap_used=0B;;;0;38654701568 `, Run: func(_ *cobra.Command, _ []string) { - var overall result.Overall // ## RAM stuff @@ -59,6 +58,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa // # Available var partialMemAvailable result.PartialResult + _ = partialMemAvailable.SetDefaultState(check.OK) partialMemAvailable.Output = fmt.Sprintf("Available Memory (%s/%s, %.2f%%)", @@ -127,6 +127,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa // # Free var partialMemFree result.PartialResult + _ = partialMemFree.SetDefaultState(check.OK) pdMemFree := perfdata.Perfdata{ @@ -197,6 +198,7 @@ func computeMemResults(config *memory.MemConfig, memStats *memory.Mem) result.Pa // Used Memory var partialMemUsed result.PartialResult + _ = partialMemUsed.SetDefaultState(check.OK) partialMemUsed.Output = fmt.Sprintf("Used Memory (%s/%s, %.2f%%)", @@ -411,6 +413,7 @@ func init() { func computeSwapResults(stats *memory.Mem) *result.PartialResult { var partialSwap result.PartialResult + _ = partialSwap.SetDefaultState(check.OK) _ = partialSwap.SetDefaultState(check.OK) diff --git a/cmd/netdev.go b/cmd/netdev.go index f9d0a12..5e36bc5 100644 --- a/cmd/netdev.go +++ b/cmd/netdev.go @@ -48,13 +48,11 @@ func NetdevCheck(_ *cobra.Command, _ []string) { overall := result.Overall{} interfaces, err := netdev.GetAllInterfaces() - if err != nil { check.ExitError(err) } interfaces, err = netdev.FilterInterfaces(&interfaces, &NetdevConfig.Filters) - if err != nil { check.ExitError(err) } diff --git a/cmd/psi.go b/cmd/psi.go index 720e8c7..a49372a 100644 --- a/cmd/psi.go +++ b/cmd/psi.go @@ -236,6 +236,7 @@ func init() { func checkPsiCPUPressure(config *psiConfig) result.PartialResult { var cpuCheck result.PartialResult + _ = cpuCheck.SetDefaultState(check.OK) cpuCheck.Output = "CPU" @@ -376,6 +377,7 @@ func checkPsiCPUPressure(config *psiConfig) result.PartialResult { func checkPsiIoPressure(config *psiConfig) result.PartialResult { var ioCheck result.PartialResult + _ = ioCheck.SetDefaultState(check.OK) ioCheck.Output = "IO" @@ -515,6 +517,7 @@ func checkPsiIoPressure(config *psiConfig) result.PartialResult { func checkPsiMemoryPressure(config *psiConfig) result.PartialResult { var memoryCheck result.PartialResult + _ = memoryCheck.SetDefaultState(check.OK) memoryCheck.Output = "Memory" diff --git a/cmd/root.go b/cmd/root.go index 6f9f16c..cc58699 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,7 +29,8 @@ var rootCmd = &cobra.Command{ func Execute() { defer check.CatchPanic() - if err := rootCmd.Execute(); err != nil { + err := rootCmd.Execute() + if err != nil { check.ExitError(err) } } diff --git a/cmd/sensors.go b/cmd/sensors.go index 0e5cb8c..470986d 100644 --- a/cmd/sensors.go +++ b/cmd/sensors.go @@ -47,18 +47,23 @@ thresholds respecting the sensor type and the respective specialities`, overall.Add(check.Unknown, "No devices found") check.ExitRaw(overall.GetStatus(), overall.GetOutput()) } + var ( alarms uint = 0 ) for _, device := range devices { var devicePartial result.PartialResult + _ = devicePartial.SetDefaultState(check.OK) + devicePartial.Output = device.Name for idx, sensor := range device.Sensors { var ssc result.PartialResult + _ = ssc.SetDefaultState(check.OK) ssc.Perfdata.Add(&(device.Sensors[idx]).Perfdata) + if sensor.Alarm { ssc.Output = "Alarm!" _ = ssc.SetState(check.Critical) diff --git a/internal/common/filter/filter.go b/internal/common/filter/filter.go index 3dea414..177288e 100644 --- a/internal/common/filter/filter.go +++ b/internal/common/filter/filter.go @@ -14,7 +14,7 @@ type Options struct { RegexpMatching bool } -// Filters on Value with a list of filters +// Filter filters the input with a list of filters // @return, true if value should be included, false otherwise func Filter[I Filterable](input []I, filterList *[]string, valueIdentifier uint, opts Options) ([]I, error) { if len(*filterList) == 0 { @@ -40,7 +40,6 @@ func Filter[I Filterable](input []I, filterList *[]string, valueIdentifier uint, (*filterList)[j], input[i].GetFilterableValue(valueIdentifier), ) - if err != nil { return []I{}, err } diff --git a/internal/filesystem/filesystem.go b/internal/filesystem/filesystem.go index 92ebeb3..144924f 100644 --- a/internal/filesystem/filesystem.go +++ b/internal/filesystem/filesystem.go @@ -9,6 +9,7 @@ import ( "github.com/shirou/gopsutil/v3/disk" ) +// FilesystemType describes the retrieved metadata of a filesystem // nolint: revive type FilesystemType struct { PartStats disk.PartitionStat @@ -29,8 +30,8 @@ func GetDiskUsageSingle(ctx context.Context, timeout time.Duration, fs *Filesyst go func() { tmp := tmpFileSystemWrapper{} - usageStats, err := disk.Usage(fs.PartStats.Mountpoint) + usageStats, err := disk.Usage(fs.PartStats.Mountpoint) if err == nil { tmp.usage = *usageStats } diff --git a/internal/netdev/netdev.go b/internal/netdev/netdev.go index c2951c3..d6a178f 100644 --- a/internal/netdev/netdev.go +++ b/internal/netdev/netdev.go @@ -115,7 +115,6 @@ func (iface IfaceData) GetFilterableValue(ident uint) string { func GetAllInterfaces() ([]IfaceData, error) { interfaces, err := listInterfaces() - if err != nil { return []IfaceData{}, err } @@ -130,7 +129,6 @@ func GetAllInterfaces() ([]IfaceData, error) { result[i].Name = interfaces[i] err = getInterfaceState(&result[i]) - if err != nil { return result, err } @@ -216,14 +214,13 @@ func getInfacesStatistics(data *IfaceData) error { for idx, stat := range GetIfaceStatNames() { numberBytes, err := os.ReadFile(path.Join(basePath, stat)) - if err != nil { return err } numberString := string(numberBytes) - val, err = strconv.ParseUint(numberString[:len(numberString)-1], 10, 64) + val, err = strconv.ParseUint(numberString[:len(numberString)-1], 10, 64) if err != nil { return err } @@ -244,7 +241,6 @@ func FilterInterfaces(interfaces *[]IfaceData, filters *Filter) ([]IfaceData, er EmptyFilterNoMatch: false, }, ) - if err != nil { return []IfaceData{}, err } @@ -258,7 +254,6 @@ func FilterInterfaces(interfaces *[]IfaceData, filters *Filter) ([]IfaceData, er EmptyFilterNoMatch: false, }, ) - if err != nil { return []IfaceData{}, err } diff --git a/internal/psi/psi.go b/internal/psi/psi.go index 9d8bf65..64fcd28 100644 --- a/internal/psi/psi.go +++ b/internal/psi/psi.go @@ -175,7 +175,6 @@ func parsePressureValue(val string) (PressureValue, error) { func readPressureFile(pressurePath string) (*PressureElement, error) { readFile, err := os.Open(pressurePath) - if err != nil { return nil, err } diff --git a/internal/sensors/sensors.go b/internal/sensors/sensors.go index 33a32dc..db05e77 100644 --- a/internal/sensors/sensors.go +++ b/internal/sensors/sensors.go @@ -39,11 +39,13 @@ const ( func (d *Device) String() string { var result strings.Builder + result.WriteString(d.Name) result.WriteString(": ") for idx := range d.Sensors { result.WriteString(d.Sensors[idx].String()) + if idx < len(d.Sensors) { result.WriteString(";") } @@ -66,7 +68,6 @@ func GetDevices(hwmonPath string) ([]Device, error) { * hwmon0 hwmon1 hwmon2 ... */ files, err := filepath.Glob(hwmonPath + "/*") - if err != nil { return []Device{}, err } @@ -273,7 +274,6 @@ func readHumiditySensor(devicePath string, index int) (Sensor, error) { // Look for input (the actual value) value, err := readIntFromFile(basePath + inputFileSuffix) - if err != nil { return sensor, err } @@ -513,7 +513,6 @@ func readPowerSensor(_, devicePath string, index int) (Sensor, error) { // Look for input (the actual value) value, err := readIntFromFile(basePath + inputFileSuffix) - if err != nil { return sensor, err } @@ -584,7 +583,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { // Look for input (the actual value) value, err := readIntFromFile(basePath + inputFileSuffix) - if err != nil { return sensor, err } @@ -601,7 +599,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { warnPresent := false // Is there a tempN_max file? If yes, use that as warning value, err = readIntFromFile(basePath + maxValueFileSuffix) - if err == nil { tmpWarn.Upper = float64(value / 1000) warnPresent = true @@ -621,7 +618,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { critPresent := false // Is there a tempN_crit file? If yes, use that as critical value, err = readIntFromFile(basePath + critThresholdFileSuffix) - if err == nil { tmpCrit.Upper = float64(value / 1000) critPresent = true @@ -629,7 +625,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { // Is there a tempN_emergency file? If yes, use that instead of crit value, err = readIntFromFile(basePath + "_emergency") - if err == nil { tmpCrit.Upper = float64(value / 1000) critPresent = true @@ -637,7 +632,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { // Is there a tempN_min file? If yes, use that as lower boundary value, err = readIntFromFile(basePath + "_min") - if err == nil { tmpCrit.Lower = float64(value / 1000) critPresent = true @@ -650,7 +644,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { // == Min // Is there a tempN_lowest file? Use it for min value value, err = readIntFromFile(basePath + lowestValueFileSuffix) - if err == nil { sensor.Perfdata.Min = value } @@ -658,7 +651,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { // == Max // Is there a tempN_highest file? Use it for max value value, err = readIntFromFile(basePath + highestValueFileSuffix) - if err == nil { sensor.Perfdata.Max = value } @@ -670,7 +662,6 @@ func readTempSensor(_, devicePath string, index int) (Sensor, error) { func readStringFromFile(fp string) (string, error) { tmp, err := os.ReadFile(fp) - if err != nil { return "", err } @@ -680,13 +671,11 @@ func readStringFromFile(fp string) (string, error) { func readIntFromFile(fp string) (int, error) { tmp, err := os.ReadFile(fp) - if err != nil { return 0, err } value, err := strconv.ParseInt(strings.TrimSpace(string(tmp)), 10, 64) - if err != nil { return 0, err } @@ -696,13 +685,11 @@ func readIntFromFile(fp string) (int, error) { func readBoolFromFile(fp string) (bool, error) { tmp, err := os.ReadFile(fp) - if err != nil { return false, err } value, err := strconv.ParseInt(strings.TrimSpace(string(tmp)), 10, 64) - if err != nil { return false, err } @@ -719,7 +706,6 @@ func readBoolFromFile(fp string) (bool, error) { func readSensorLabel(sensorBasePath string) string { // See if Sensor is alarmed, use that for the status label, err := readStringFromFile(sensorBasePath + "_label") - if err == nil { return label } @@ -727,8 +713,8 @@ func readSensorLabel(sensorBasePath string) string { // Try the "name" of the device and add the sensor type devicePath := path.Dir(sensorBasePath) sensorBaseName := path.Base(sensorBasePath) - label, err = readStringFromFile(devicePath + "/name") + label, err = readStringFromFile(devicePath + "/name") if err == nil { return label + "_" + sensorBaseName } @@ -743,7 +729,6 @@ func readSensorLabel(sensorBasePath string) string { func readSensorAlarm(sensorBasePath string) bool { // See if Sensor is alarmed, use that for the status alarmFiles, err := filepath.Glob(sensorBasePath + "*_alarm") - if err != nil { return false }