From 473ee10b186c458b0216d697df916f2700430e9b Mon Sep 17 00:00:00 2001 From: Ahmet Oeztuerk Date: Fri, 8 May 2026 16:33:21 +0200 Subject: [PATCH 1/2] support per-drive crit/warning thresholds by adding metrics when needed By default, snclient does not add unnecessary metrics if they do not occur in a condition. This is done by checking the operand of conditions using check.HasThreshold() function Adding used_pct adds metrics per drive: ' used' and ' used %' , but if it is not present these metrics will be missing. ``` ./snclient -vvv --logfile stdout run check_drivesize "drive=/" "warn=used_pct gt 50" show-all OK - / 428.610 GiB/935.929 GiB (45.8%) |'/ used'=460216111104B;502473211904;904451781427;0;1004946423808 '/ used %'=45.8%;50;90;0;100 ``` But adding a bare warn='used_pct gt 90' would affect all drives. To check multiple drives while specifying different thresholds for each drive, we need to add the percentage usage metrics. Metrics are also checked when building finalizing the check, and can influence the final state. ``` ./snclient -vvv --logfile stdout run check_drivesize "drive=/" "drive=/tmp" "warn='/ used %' gt 30" "crit='/tmp used %' gt 66" show-all WARNING - / 428.867 GiB/935.929 GiB (45.8%), /tmp 961.945 MiB/31.127 GiB (3.0%) |'/ used'=460492066816B;;;0;1004946423808 '/ used %'=45.8%;30;;0;100 '/tmp used'=1008672768B;;;0;33422544896 '/tmp used %'=3%;;66;0;100 ``` Detect conditions where the operand is named ' used %', if there is a condition using that as operator, add usage metrics for that drive as well. This only works on that drive, and since the operand ' used %' is different for each drive, it wont effect other drives perfdata. --- pkg/snclient/check_drivesize.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/snclient/check_drivesize.go b/pkg/snclient/check_drivesize.go index 0bc0d1d3..fa782664 100644 --- a/pkg/snclient/check_drivesize.go +++ b/pkg/snclient/check_drivesize.go @@ -304,12 +304,18 @@ func (l *CheckDrivesize) addMetrics(drive string, check *CheckData, usage *disk. total = usage.Used + usage.Free // use this total instead of usage.Total to account in the root reserved space } + // need to detect conditions where the operand is named ' used %', this is the default way snclient names percent usage metrics. + // if there is a condition using that as an operand, add usage metrics for that drive as well. these metrics will be added for that drive only. + // this helps to check usage metrics specific to drives. + percentageUsageMetric := fmt.Sprintf("%s used %%", drive) + if check.HasThreshold("free") || check.HasThreshold("free_pct") || check.HasThreshold("free_bytes") { check.warnThreshold = check.TransformMultipleKeywords([]string{"free_pct", "free_bytes"}, "free", check.warnThreshold) check.critThreshold = check.TransformMultipleKeywords([]string{"free_pct", "free_bytes"}, "free", check.critThreshold) check.AddBytePercentMetrics("free", drive+" free", magic*float64(usage.Free), magic*float64(total)) } - if check.HasThreshold("used") || check.HasThreshold("used_pct") || check.HasThreshold("used_bytes") { + + if check.HasThreshold(percentageUsageMetric) || check.HasThreshold("used") || check.HasThreshold("used_pct") || check.HasThreshold("used_bytes") { check.warnThreshold = check.TransformMultipleKeywords([]string{"used_pct", "used_bytes"}, "used", check.warnThreshold) check.critThreshold = check.TransformMultipleKeywords([]string{"used_pct", "used_bytes"}, "used", check.critThreshold) check.AddBytePercentMetrics("used", drive+" used", magic*float64(usage.Used), magic*float64(total)) From 876b2a6fc506dcf6e40f682930dd723ab626e98f Mon Sep 17 00:00:00 2001 From: Ahmet Oeztuerk Date: Mon, 11 May 2026 10:48:04 +0200 Subject: [PATCH 2/2] add support for ' used_pct' way for adding threshold conditions these conditions have their keyword transformed to ' used %' so that the metric name matches the condition name in condition.String() , check if the keyword is in the original, if it isnt, its likely changed. print it out separately. --- pkg/snclient/check_drivesize.go | 18 ++++++++++++------ pkg/snclient/condition.go | 7 ++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/snclient/check_drivesize.go b/pkg/snclient/check_drivesize.go index fa782664..2f41d9e8 100644 --- a/pkg/snclient/check_drivesize.go +++ b/pkg/snclient/check_drivesize.go @@ -304,18 +304,24 @@ func (l *CheckDrivesize) addMetrics(drive string, check *CheckData, usage *disk. total = usage.Used + usage.Free // use this total instead of usage.Total to account in the root reserved space } - // need to detect conditions where the operand is named ' used %', this is the default way snclient names percent usage metrics. - // if there is a condition using that as an operand, add usage metrics for that drive as well. these metrics will be added for that drive only. - // this helps to check usage metrics specific to drives. - percentageUsageMetric := fmt.Sprintf("%s used %%", drive) - if check.HasThreshold("free") || check.HasThreshold("free_pct") || check.HasThreshold("free_bytes") { check.warnThreshold = check.TransformMultipleKeywords([]string{"free_pct", "free_bytes"}, "free", check.warnThreshold) check.critThreshold = check.TransformMultipleKeywords([]string{"free_pct", "free_bytes"}, "free", check.critThreshold) check.AddBytePercentMetrics("free", drive+" free", magic*float64(usage.Free), magic*float64(total)) } - if check.HasThreshold(percentageUsageMetric) || check.HasThreshold("used") || check.HasThreshold("used_pct") || check.HasThreshold("used_bytes") { + // convert ' used_pct' keywords in conditions to ' used %' as that matches the metric name + convertDriveUsagePctMetric1 := fmt.Sprintf("%s used_pct", drive) + // metrics are normally added if the operand is simply 'used' , 'used_pct' , 'used_bytes' etc. and do not have a drive prefix + // detect conditions where the operand is named ' used %', this is the default way snclient names percent usage metrics. + // if there is a condition using that as an operand, add usage metrics for that drive as well. during the metrics condition checking, they will take effect. + // this helps to check usage metrics specific to drives. + driveUsagePctMetric := fmt.Sprintf("%s used %%", drive) + + check.warnThreshold = check.TransformMultipleKeywords([]string{convertDriveUsagePctMetric1}, driveUsagePctMetric, check.warnThreshold) + check.critThreshold = check.TransformMultipleKeywords([]string{convertDriveUsagePctMetric1}, driveUsagePctMetric, check.critThreshold) + + if check.HasThreshold(driveUsagePctMetric) || check.HasThreshold("used") || check.HasThreshold("used_pct") || check.HasThreshold("used_bytes") { check.warnThreshold = check.TransformMultipleKeywords([]string{"used_pct", "used_bytes"}, "used", check.warnThreshold) check.critThreshold = check.TransformMultipleKeywords([]string{"used_pct", "used_bytes"}, "used", check.critThreshold) check.AddBytePercentMetrics("used", drive+" used", magic*float64(usage.Used), magic*float64(total)) diff --git a/pkg/snclient/condition.go b/pkg/snclient/condition.go index 8e1c7052..239c532f 100644 --- a/pkg/snclient/condition.go +++ b/pkg/snclient/condition.go @@ -229,7 +229,12 @@ func NewCondition(input string, attr *[]CheckAttribute) (*Condition, error) { func (c *Condition) String() string { if c.original != "" { - return c.original + // keyword might have been changed by a transform function, print it out separately if that is the case + if strings.Contains(c.original, c.keyword) { + return c.original + } + + return fmt.Sprintf("(original: %s | keyword: %s)", c.original, c.keyword) } if len(c.group) > 0 {