-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·98 lines (87 loc) · 2.63 KB
/
main.go
File metadata and controls
executable file
·98 lines (87 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
"flag"
"fmt"
"math"
"syscall"
)
const VERSION = "v1.0.3 2024-01-13"
const GB = 1024 * MB
const MB = 1024 * 1024
const TERMINAL_COLOR_RED = "\033[41m"
const TERMINAL_COLOR_GREEN = "\033[42m"
const TERMINAL_COLOR_RESET = "\033[0m"
var pathToCheckFreespace = ""
var JSON_OUTPUT = false
var ERROR_IF_UNDER_PERCENT = 0.0
var ERROR_IF_UNDER_GB = 0.0
var NO_COLOR = false
type filesystemStats struct {
Filesystem string `json:"Filesystem"`
Total float64 `json:"TotalGb"`
Free float64 `json:"FreeGb"`
Percent float64 `json:"FreePercentage"`
Hostname string `json:"Hostname"`
Status string `json:"Status"`
Passed bool `json:"Passed"`
Path string `json:"Path"`
}
func main() {
parseCommandLineFlags()
fs := getFilesystemStats(pathToCheckFreespace)
if ERROR_IF_UNDER_GB > 0 && fs.Free < ERROR_IF_UNDER_GB {
fs.Passed = false
fs.Status = fmt.Sprintf("[FAIL] Free disk space under %.1fGB", ERROR_IF_UNDER_GB)
} else if ERROR_IF_UNDER_GB == 0 && fs.Percent < ERROR_IF_UNDER_PERCENT {
fs.Passed = false
fs.Status = fmt.Sprintf("[FAIL] Free disk space under %.1f%%", ERROR_IF_UNDER_PERCENT)
} else {
fs.Passed = true
fs.Status = "[PASS] Disk OK"
}
displayOutput(fs)
if !fs.Passed {
syscall.Exit(2)
}
}
func parseCommandLineFlags() {
showVersion := flag.Bool("version", false, VERSION)
flag.BoolVar(&JSON_OUTPUT, "json", false, "switch to json output")
flag.Float64Var(&ERROR_IF_UNDER_PERCENT, "percent", 10, "a number like 2.5 that will trigger an alert if free space is under 2.5 percent")
flag.Float64Var(&ERROR_IF_UNDER_GB, "gb", 0, "a number like 1.2 that will trigger an alert if free space is less than 1.2GB")
flag.StringVar(&pathToCheckFreespace, "path", "", "optionally sets the path to check for free space (ng /mnt/usbdrive or D: )")
flag.BoolVar(&NO_COLOR, "nocolor", false, "do not apply terminal color")
flag.Parse()
if *showVersion {
fmt.Println(VERSION)
syscall.Exit(0)
}
}
func displayOutput(fs filesystemStats) {
if JSON_OUTPUT {
jsonBytes, err := json.Marshal(fs)
if err != nil {
fmt.Printf("{'error':'%s'}\n", err)
}
fmt.Println(string(jsonBytes))
} else {
fmt.Println(fs.Path, "on", fs.Hostname)
fmt.Println(" Free: ", fs.Free, "GB /", fs.Total, "GB")
fmt.Printf(" Percent: %.2f%%\n", fs.Percent)
colorStart := ""
colorEnd := ""
if !NO_COLOR {
if fs.Passed {
colorStart = TERMINAL_COLOR_GREEN
} else {
colorStart = TERMINAL_COLOR_RED
}
colorEnd = TERMINAL_COLOR_RESET
}
fmt.Println(colorStart, fs.Status, colorEnd)
}
}
func roundOneDecimal(input float64) float64 {
return math.Round(input*10) / 10
}