Skip to content

Commit 7ceb395

Browse files
sschnemuesli
authored andcommitted
add availability and usage threhold option
Signed-off-by: Simon Schneider <[email protected]>
1 parent 4e52b81 commit 7ceb395

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"strconv"
89
"strings"
910

1011
wildcard "git.iglou.eu/Imported/go-wildcard"
@@ -37,6 +38,9 @@ var (
3738
themeOpt = flag.String("theme", defaultThemeName(), "color themes: dark, light")
3839
styleOpt = flag.String("style", defaultStyleName(), "style: unicode, ascii")
3940

41+
availThreshold = flag.String("avail-threshold", "10G,1G", "specifies the coloring threshold (yellow, red) of the avail column, must be integer with optional SI prefixes")
42+
usageThreshold = flag.String("usage-threshold", "0.5,0.9", "specifies the coloring threshold (yellow, red) of the usage bars as a floating point number from 0 to 1")
43+
4044
inodes = flag.Bool("inodes", false, "list inode information instead of block usage")
4145
jsonOutput = flag.Bool("json", false, "output all devices in JSON format")
4246
warns = flag.Bool("warnings", false, "output all warnings to STDERR")
@@ -243,6 +247,34 @@ func main() {
243247
m = mounts
244248
}
245249

250+
// validate availability thresholds
251+
availbilityThresholds := strings.Split(*availThreshold, ",")
252+
if len(availbilityThresholds) != 2 {
253+
fmt.Fprintln(os.Stderr, fmt.Errorf("error parsing avail-threshold: invalid option '%s'", *availThreshold))
254+
os.Exit(1)
255+
}
256+
for _, thresold := range availbilityThresholds {
257+
_, err = stringToSize(thresold)
258+
if err != nil {
259+
fmt.Fprintln(os.Stderr, "error parsing avail-threshold:", err)
260+
os.Exit(1)
261+
}
262+
}
263+
264+
// validate usage thresholds
265+
usageThresholds := strings.Split(*usageThreshold, ",")
266+
if len(usageThresholds) != 2 {
267+
fmt.Fprintln(os.Stderr, fmt.Errorf("error parsing usage-threshold: invalid option '%s'", *usageThreshold))
268+
os.Exit(1)
269+
}
270+
for _, thresold := range usageThresholds {
271+
_, err = strconv.ParseFloat(thresold, 64)
272+
if err != nil {
273+
fmt.Fprintln(os.Stderr, "error parsing usage-threshold:", err)
274+
os.Exit(1)
275+
}
276+
}
277+
246278
// print out warnings
247279
if *warns {
248280
for _, warning := range warnings {

table.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"regexp"
7+
"strconv"
68
"strings"
79

810
"github.com/jedib0t/go-pretty/v6/table"
@@ -153,10 +155,12 @@ func spaceTransformer(val interface{}) string {
153155
free := val.(uint64)
154156

155157
var s = termenv.String(sizeToString(free))
158+
redAvail, _ := stringToSize(strings.Split(*availThreshold, ",")[1])
159+
yellowAvail, _ := stringToSize(strings.Split(*availThreshold, ",")[0])
156160
switch {
157-
case free < 1<<30:
161+
case free < redAvail:
158162
s = s.Foreground(theme.colorRed)
159-
case free < 10*1<<30:
163+
case free < yellowAvail:
160164
s = s.Foreground(theme.colorYellow)
161165
default:
162166
s = s.Foreground(theme.colorGreen)
@@ -183,10 +187,12 @@ func barTransformer(val interface{}) string {
183187
}
184188

185189
// apply color to progress-bar
190+
redUsage, _ := strconv.ParseFloat(strings.Split(*usageThreshold, ",")[1], 64)
191+
yellowUsage, _ := strconv.ParseFloat(strings.Split(*usageThreshold, ",")[0], 64)
186192
switch {
187-
case usage >= 0.9:
193+
case usage >= redUsage:
188194
s = s.Foreground(theme.colorRed)
189-
case usage >= 0.5:
195+
case usage >= yellowUsage:
190196
s = s.Foreground(theme.colorYellow)
191197
default:
192198
s = s.Foreground(theme.colorGreen)
@@ -260,6 +266,43 @@ func sizeToString(size uint64) (str string) {
260266
return
261267
}
262268

269+
// stringToSize transforms an SI size into a number.
270+
func stringToSize(s string) (size uint64, err error) {
271+
regex := regexp.MustCompile(`^(\d+)([KMGTPE]?)$`)
272+
matches := regex.FindStringSubmatch(s)
273+
if len(matches) == 0 {
274+
return 0, fmt.Errorf("'%s' is not valid, must have integer with optional SI prefix", s)
275+
}
276+
277+
num, err := strconv.ParseUint(matches[1], 10, 64)
278+
if err != nil {
279+
return 0, err
280+
}
281+
if matches[2] != "" {
282+
prefix := matches[2]
283+
switch {
284+
case prefix == "K":
285+
size = num << 10
286+
case prefix == "M":
287+
size = num << 20
288+
case prefix == "G":
289+
size = num << 30
290+
case prefix == "T":
291+
size = num << 40
292+
case prefix == "P":
293+
size = num << 50
294+
case prefix == "E":
295+
size = num << 60
296+
default:
297+
err = fmt.Errorf("prefix '%s' not allowed, valid prefixes are K, M, G, T, P, E", prefix)
298+
return
299+
}
300+
} else {
301+
size = num
302+
}
303+
return
304+
}
305+
263306
// stringToColumn converts a column name to its index.
264307
func stringToColumn(s string) (int, error) {
265308
s = strings.ToLower(s)

0 commit comments

Comments
 (0)