Add options for free rate
@@ -14,11 +14,14 @@ type diskStatus struct {
Used uint64 `json:"used"`
Free uint64 `json:"free"`
Avail uint64 `json:"available"`
+ FreeRate uint64 `json:"free_rate"`
}
var opts struct {
- Warning *float64 `short:"w" long:"warning" value-name:"N" description:"Exit with WARNING status if less than N GB of disk are free"`
- Critical *float64 `short:"c" long:"critical" value-name:"N" description:"Exit with CRITICAL status if less than N GB of disk are free"`
+ Warning *float64 `short:"W" long:"warning" value-name:"N" description:"Exit with WARNING status if less than N GB of disk are free"`
+ Critical *float64 `short:"C" long:"critical" value-name:"N" description:"Exit with CRITICAL status if less than N GB of disk are free"`
+ WarningRate *float64 `short:"w" long:"warning-rate" value-name:"N" description:"Exit with WARNING status if less than N % of disk are free"`
+ CriticalRate *float64 `short:"c" long:"critical-rate" value-name:"N" description:"Exit with CRITICAL status if less than N % of disk are free"`
Path *string `short:"p" long:"path" value-name:"PATH" description:"Mount point or block device as emitted by the mount(8) command"`
}
@@ -41,6 +44,7 @@ func getDiskUsage(path string) (*diskStatus, error) {
disk.Free = fs.Bfree * uint64(fs.Bsize)
disk.Used = disk.All - disk.Free
disk.Avail = fs.Bavail * uint64(fs.Bsize)
+ disk.FreeRate = (disk.Free / disk.All) * 100
return disk, nil
}
@@ -69,20 +73,27 @@ func run(args []string) *checkers.Checker {
return checkers.Unknown(fmt.Sprintf("Faild to fetch disk usage: %s", err))
}
+ all := float64(disk.All) / float64(GB)
+ used := float64(disk.Used) / float64(GB)
+ free := float64(disk.Free) / float64(GB)
+ avail := float64(disk.Avail) / float64(GB)
+ freeRate := float64(disk.FreeRate)
+
checkSt := checkers.OK
- if opts.Warning != nil && *opts.Warning > float64(disk.Free)/float64(GB) {
+ if opts.Warning != nil && *opts.Warning > free {
checkSt = checkers.WARNING
}
- if opts.Critical != nil && *opts.Critical > float64(disk.Free)/float64(GB) {
+ if opts.Critical != nil && *opts.Critical > free {
+ checkSt = checkers.CRITICAL
+ }
+ if opts.WarningRate != nil && *opts.WarningRate > freeRate {
+ checkSt = checkers.WARNING
+ }
+ if opts.CriticalRate != nil && *opts.CriticalRate > freeRate {
checkSt = checkers.CRITICAL
}
- all := float64(disk.All) / float64(GB)
- used := float64(disk.Used) / float64(GB)
- free := float64(disk.Free) / float64(GB)
- avail := float64(disk.Avail) / float64(GB)
-
- msg := fmt.Sprintf("All: %.2f GB, Used: %.2f GB, Free: %.2f GB, Available: %.2f GB\n", all, used, free, avail)
+ msg := fmt.Sprintf("All: %.2f GB, Used: %.2f GB, Free: %.2f GB, Available: %.2f GB, Free percentage: %.2f\n", all, used, free, avail, freeRate)
return checkers.NewChecker(checkSt, msg)
}
