-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsping.go
executable file
·123 lines (110 loc) · 2.47 KB
/
fsping.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
flag "github.com/ogier/pflag"
"os"
"strings"
"syscall"
"time"
)
var verbose = flag.BoolP("verbose", "v", false, "verbose")
var timeout = flag.DurationP("timeout", "T", 5*time.Second, "timeout (ex: 100ms or 3s)")
var printpath = flag.BoolP("path", "p", true, "show path")
var printdev = flag.BoolP("dev", "d", false, "show device")
var quiet = flag.BoolP("quiet", "q", false, "quiet mode, only set exit status")
var checktype = flag.StringP("type", "t", "nfs", "filesystem type to check")
var checkall = flag.BoolP("all", "a", false, "check all filesystems types")
type fs_t struct {
path string
dev string
resptime time.Duration
done bool
err error
}
type fsmap_t map[string]fs_t
func (fs fs_t) String() (s string) {
switch {
case fs.err != nil:
if strings.Index(fs.err.Error(), "stale") >= 0 {
s += fmt.Sprintf("%-9s", "STALE")
} else {
s += fmt.Sprintf("%-9s", "ERROR")
}
case !fs.done:
s += fmt.Sprintf("%-9s", "TIMEOUT")
default:
s += fmt.Sprintf("%-9s", fs.resptime)
}
if *printpath {
s += fmt.Sprintf(" %-20s", fs.path)
}
if *printdev {
s += fmt.Sprintf(" %-30s", fs.dev)
}
if fs.err != nil {
s += fmt.Sprintf(" %s", fs.err)
}
return s
}
func statit(fs fs_t, ch chan fs_t) {
var buf syscall.Statfs_t
start := time.Now()
fs.err = syscall.Statfs(fs.path, &buf)
fs.resptime = time.Since(start)
fs.done = true
ch <- fs
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "fsping: check responsiveness of mounted filesystems\n\n")
flag.PrintDefaults()
}
flag.Parse()
fsnum := 0
fsch := make(chan fs_t)
fsmap := readmounts()
args := flag.Args()
// if extra args were specified keep only filesystems listed
if len(args) > 0 {
L:
for fs, _ := range fsmap {
for _, a := range args {
if fs == a {
continue L
}
}
delete(fsmap, fs)
}
}
// fire them up
for _, fs := range fsmap {
fsnum += 1
// fire off a goroutine for each mounted nfs
// filesystem. fsch is used for completion
// notifications
go statit(fs, fsch)
}
// timeout ticker
tick := time.Tick(*timeout)
// collect results
for fsnum > 0 {
select {
case fs := <-fsch:
if *verbose || fs.err != nil {
fmt.Println(fs)
}
fsmap[fs.path] = fs
fsnum -= 1
case <-tick:
// ticker has kicked in, print timeout
// messages for filesystems that haven't
// finished
for _, fs := range fsmap {
if !*quiet && !fs.done {
fmt.Println(fs)
}
}
os.Exit(1)
}
}
}