Skip to content

Commit 2182146

Browse files
committed
check_temperature: return sensors sorted
1 parent fb6266c commit 2182146

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

pkg/snclient/check_temperature.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (l *CheckTemperature) Build() *CheckData {
5858
{name: "max", description: "max value supplied from sensor"},
5959
{name: "min", description: "min value supplied from sensor"},
6060
},
61+
listSorted: []string{"label"},
6162
exampleDefault: `
6263
check_temperature
6364
OK - Package id 0: 65.0 °C, Core 0: 62.0 °C, Core 1: 61.0 °C, Core 2: 65.0 °C |...

pkg/snclient/checkdata.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ type CheckData struct {
124124
output string
125125
implemented Implemented
126126
attributes []CheckAttribute
127+
listSorted []string // sort result list by this keys
127128
exampleDefault string
128129
exampleArgs string
129130
timezone *time.Location // timezone used for date output set by --timezone
@@ -148,6 +149,7 @@ func (cd *CheckData) Finalize() (*CheckResult, error) {
148149

149150
// apply final filter
150151
cd.listData = cd.Filter(cd.filter, cd.listData)
152+
cd.listData = cd.sortList(cd.listData, cd.listSorted)
151153

152154
if cd.result == nil {
153155
cd.result = &CheckResult{}
@@ -550,6 +552,35 @@ func (cd *CheckData) Filter(conditions ConditionList, data []map[string]string)
550552
return result
551553
}
552554

555+
func (cd *CheckData) sortList(listData []map[string]string, keys []string) []map[string]string {
556+
if len(keys) == 0 {
557+
return listData
558+
}
559+
560+
slices.SortStableFunc(listData, func(a, b map[string]string) int {
561+
sortA := []string{}
562+
sortB := []string{}
563+
for _, k := range keys {
564+
sortA = append(sortA, a[k])
565+
sortB = append(sortB, b[k])
566+
}
567+
568+
strA := utils.ReplaceNumbersWithZeroPadded(strings.Join(sortA, ";"), 10)
569+
strB := utils.ReplaceNumbersWithZeroPadded(strings.Join(sortB, ";"), 10)
570+
571+
switch {
572+
case strA == strB:
573+
return 0
574+
case strA > strB:
575+
return 1
576+
default:
577+
return -1
578+
}
579+
})
580+
581+
return listData
582+
}
583+
553584
// parseStateString translates string naemon state to int64
554585
func (cd *CheckData) parseStateString(state string) int64 {
555586
switch strings.ToLower(state) {

pkg/utils/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,14 @@ func ReplaceCommonPasswordPattern(str string) string {
664664

665665
return str
666666
}
667+
668+
func ReplaceNumbersWithZeroPadded(s string, padding int) string {
669+
format := fmt.Sprintf("%%0%dd", padding)
670+
re := regexp.MustCompile(`\d+`)
671+
672+
return re.ReplaceAllStringFunc(s, func(numStr string) string {
673+
num, _ := strconv.Atoi(numStr)
674+
675+
return fmt.Sprintf(format, num)
676+
})
677+
}

0 commit comments

Comments
 (0)