forked from rouben/zfswatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zparse.go
434 lines (403 loc) · 12.2 KB
/
zparse.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//
// zparse.go
//
// Copyright © 2012-2013 Damicon Kraa Oy
//
// This file is part of zfswatcher.
//
// Zfswatcher is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Zfswatcher is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with zfswatcher. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"errors"
"zfswatcher/notifier"
"io"
"runtime"
"strings"
)
// ZFS pool disk usage.
type PoolUsageType struct {
Name string
Avail int64
Used int64
Usedsnap int64
Usedds int64
Usedrefreserv int64
Usedchild int64
Refer int64
Mountpoint string
Logicalused int64
Compressratio string
}
// Methods for calculating the percentage to make sure that the calculation
// is made the same way everywhere. We use integers for simplicity.
func (u *PoolUsageType) GetUsedPercent() int {
return int(float64(u.Used)*100/float64(u.Avail+u.Used) + 0.5)
}
func (u *PoolUsageType) GetAvailPercent() int {
return int(float64(u.Avail)*100/float64(u.Avail+u.Used) + 0.5)
}
// Parse "zfs list -H -o name,avail,used,logicalused,usedsnap,usedds,usedrefreserv,usedchild,refer,mountpoint,compressratio" command output.
func parseZfsList(str string) map[string]*PoolUsageType {
usagemap := make(map[string]*PoolUsageType)
for lineno, line := range strings.Split(str, "\n") {
if line == "" {
continue
}
f := strings.Split(line, "\t")
if len(f) != 11 {
notify.Printf(notifier.CRIT, "invalid line %d in ZFS usage output: %s",
lineno+1, line)
notify.Attach(notifier.CRIT, str)
continue
}
usagemap[f[0]] = &PoolUsageType{
Name: f[0],
Avail: unniceNumber(f[1]),
Used: unniceNumber(f[2]),
Logicalused: unniceNumber(f[3]),
Usedsnap: unniceNumber(f[4]),
Usedds: unniceNumber(f[5]),
Usedrefreserv: unniceNumber(f[6]),
Usedchild: unniceNumber(f[7]),
Refer: unniceNumber(f[8]),
Mountpoint: f[9],
Compressratio: f[10],
}
}
return usagemap
}
// This represents ZFS disk/container/whatever.
type DevEntry struct {
name string
state string
read int64
write int64
cksum int64
rest string
subDevs []int
parentDev int
}
// Parse ZFS zpool status config section and return a tree of the volumes/containers/devices.
func parseConfstr(confstr string) (devs []*DevEntry, err error) {
if confstr == "The configuration cannot be determined." {
return nil, errors.New("configuration can not be determined")
}
var prevIndent int
var devStack []int
for _, line := range strings.Split(confstr, "\n") {
if line == "" {
continue
}
origlen := len(line)
line = strings.TrimLeft(line, " ")
indent := (origlen - len(line)) / 2
f := strings.Fields(line)
if f[0] == "NAME" && f[1] == "STATE" && f[2] == "READ" && f[3] == "WRITE" && f[4] == "CKSUM" {
continue
}
// make a new dev entry
var dev DevEntry
dev.name = f[0]
// set up defaults
dev.read = -1
dev.write = -1
dev.cksum = -1
if len(f) > 1 {
dev.state = f[1]
}
if len(f) > 2 {
dev.read = unniceNumber(f[2])
}
if len(f) > 3 {
dev.write = unniceNumber(f[3])
}
if len(f) > 4 {
dev.cksum = unniceNumber(f[4])
}
if len(f) > 5 {
dev.rest = strings.Join(f[5:], " ")
}
switch {
case indent == 0: // root level entry
dev.parentDev = -1
devs = append(devs, &dev)
thisDev := len(devs) - 1
devStack = []int{thisDev} // reset + push
case indent > prevIndent: // subdev of the previous entry
dev.parentDev = devStack[len(devStack)-1]
devs = append(devs, &dev)
thisDev := len(devs) - 1
devs[dev.parentDev].subDevs = append(devs[dev.parentDev].subDevs, thisDev)
devStack = append(devStack, thisDev) // push
case indent == prevIndent: // same level as the previous entry
devStack = devStack[:len(devStack)-1] // pop
dev.parentDev = devStack[len(devStack)-1]
devs = append(devs, &dev)
thisDev := len(devs) - 1
devs[dev.parentDev].subDevs = append(devs[dev.parentDev].subDevs, thisDev)
devStack = append(devStack, thisDev) // push
case indent < prevIndent: // dedent
devStack = devStack[:len(devStack)-1-(prevIndent-indent)] // pop x N
dev.parentDev = devStack[len(devStack)-1]
devs = append(devs, &dev)
thisDev := len(devs) - 1
devs[dev.parentDev].subDevs = append(devs[dev.parentDev].subDevs, thisDev)
devStack = append(devStack, thisDev) // push
}
prevIndent = indent
}
return devs, nil
}
// A single ZFS pool.
type PoolType struct {
name string
state string
status string
action string
see string
scan string
devs []*DevEntry
errors string
infostr string
}
// Internal parser state for parseZpoolStatus() function.
type zpoolStatusParserState int
const (
stSTART zpoolStatusParserState = iota
stPOOL
stSTATE
stSTATUS
stACTION
stSEE
stSCAN
stCONFIG
stERRORS
)
// Parse "zpool status" output.
func parseZpoolStatus(zpoolStatusOutput string) (pools []*PoolType, err error) {
// catch a panic which might occur during parsing if we get something unexpected:
defer func() {
if p := recover(); p != nil {
// get the panic location:
buf := make([]byte, 4096)
length := runtime.Stack(buf, false)
str := string(buf[:length]) // truncate trailing garbage
notify.Printf(notifier.CRIT, "panic parsing status output: %v", p)
notify.Attach(notifier.CRIT, str+"\n"+zpoolStatusOutput)
// force the return value err to be true:
err = errors.New("panic parsing status output")
}
}()
var curpool *PoolType
var confstr string
var poolinfostr string
var s zpoolStatusParserState = stSTART
for lineno, line := range strings.Split(zpoolStatusOutput, "\n") {
poolinfostr += line + "\n"
// this state machine implements a parser:
switch {
case s == stSTART && line == "no pools available":
// pools will be empty slice
return pools, nil
case s == stSTART && len(line) >= 8 && line[:8] == " pool: ":
curpool = &PoolType{name: line[8:]}
s = stPOOL
case s == stPOOL && len(line) >= 8 && line[:8] == " state: ":
curpool.state = line[8:]
s = stSTATE
case s == stSTATE && len(line) >= 8 && line[:8] == "status: ":
curpool.status = line[8:]
s = stSTATUS
case s == stSTATUS && len(line) >= 1 && line[:1] == "\t":
curpool.status += "\n" + line[1:]
case s == stSTATUS && len(line) >= 8 && line[:8] == "action: ":
curpool.action = line[8:]
s = stACTION
case s == stACTION && len(line) >= 1 && line[:1] == "\t":
curpool.action += "\n" + line[1:]
case (s == stSTATE || s == stACTION) && len(line) >= 8 &&
line[:8] == " see: ":
curpool.see = line[8:]
s = stSEE
case (s == stSTATE || s == stACTION || s == stSEE) &&
len(line) >= 7 && line[:7] == " scan: ":
curpool.scan = line[7:]
s = stSCAN
// fix for 240245896aad46d0d41b0f9f257ff2abd09cb29b
// released in zfs-0.6.0-rc14
case (s == stSTATE || s == stACTION || s == stSEE) &&
len(line) >= 8 && line[:8] == " scan: ":
curpool.scan = line[8:]
s = stSCAN
case s == stSCAN && len(line) >= 1 && line[:1] == "\t":
curpool.scan += "\n" + line[1:]
case s == stSCAN && len(line) >= 4 && line[:4] == " ":
curpool.scan += "\n" + line[4:]
case (s == stSCAN || s == stSTATE || s == stACTION || s == stSEE) &&
len(line) >= 7 && line[:7] == "config:":
s = stCONFIG
if line[7:] != "" {
confstr = line[7:]
}
case s == stCONFIG && line == "":
// skip
case s == stCONFIG && len(line) >= 1 && line[:1] == "\t":
confstr += "\n" + line[1:]
case s == stCONFIG && len(line) >= 8 && line[:8] == "errors: ":
curpool.errors = line[8:]
s = stERRORS
case s == stERRORS && line == "":
// this is the end of a pool!
curpool.devs, err = parseConfstr(confstr)
if err != nil {
notify.Print(notifier.ERR, "device configuration parse error: %s", err)
notify.Attach(notifier.ERR, confstr)
}
confstr = ""
curpool.infostr = poolinfostr
poolinfostr = ""
pools = append(pools, curpool)
s = stSTART
default:
notify.Printf(notifier.CRIT, "invalid line %d in status output: %s", lineno+1, line)
notify.Attach(notifier.CRIT, zpoolStatusOutput)
return pools, errors.New("parser error")
}
}
return pools, nil
}
/*
capacity operations bandwidth
pool alloc free read write read write
------------------------ ----- ----- ----- ----- ----- -----
tank 1.72T 8.03T 18 72 303K 6.43M
raidz2 586G 2.68T 7 24 102K 2.14M
sdi - - 2 7 16.8K 550K
sdj - - 0 7 14.8K 550K
raidz2 586G 2.68T 3 24 98.2K 2.15M
sdk - - 0 7 23.2K 550K
sdv - - 0 7 13.0K 550K
logs - - - - - -
sdc 84K 7.37G 0 0 12 3
cache - - - - - -
sdx 31.3G 87.9G 0 2 2.21K 282K
sdy 31.2G 88.0G 0 2 2.35K 281K
sdz 31.3G 87.9G 0 2 2.40K 282K
sdd 31.5G 87.7G 0 2 2.14K 284K
------------------------ ----- ----- ----- ----- ----- -----
vmstore 17.5G 50.5G 0 0 14 1
scsi-3500000e0158fea80 17.5G 50.5G 0 0 14 1
------------------------ ----- ----- ----- ----- ----- -----
*/
type ZpoolIostatRow struct {
Dev string
CapacityAlloc int64
CapacityFree int64
OperationsRead int64
OperationsWrite int64
BandwidthRead int64
BandwidthWrite int64
}
type ZpoolIostatEntry map[string]*ZpoolIostatRow
type ZpoolIostatTable map[string]ZpoolIostatEntry
func zpoolIostatParseRow(str string) *ZpoolIostatRow {
f := strings.Fields(str)
if len(f) != 7 {
return nil
}
return &ZpoolIostatRow{
Dev: f[0],
CapacityAlloc: unniceNumber(f[1]),
CapacityFree: unniceNumber(f[2]),
OperationsRead: unniceNumber(f[3]),
OperationsWrite: unniceNumber(f[4]),
BandwidthRead: unniceNumber(f[5]),
BandwidthWrite: unniceNumber(f[6]),
}
}
type zpoolIostatParserState int
const (
ioSTART zpoolIostatParserState = iota
ioPOOL
ioDEV
)
func ZpoolIostatParser(str string) *ZpoolIostatTable {
table := make(ZpoolIostatTable)
var currpool string
st := ioSTART
for _, row := range strings.Split(str, "\n") {
if row == "" {
break
}
switch st {
case ioSTART:
// just wait for first separator
if len(row) > 1 && row[0:1] == "-" {
st = ioPOOL
}
case ioPOOL:
if len(row) > 1 && row[0:1] == "-" {
st = ioPOOL
continue
}
// first row has the pool name
f := zpoolIostatParseRow(row)
if f == nil {
return nil // error
}
currpool = f.Dev
table[currpool] = make(ZpoolIostatEntry)
table[currpool][currpool] = f
st = ioDEV
case ioDEV:
if len(row) > 1 && row[0:1] == "-" {
st = ioPOOL
continue
}
// other device rows
f := zpoolIostatParseRow(row)
if f == nil {
return nil // error
}
table[currpool][f.Dev] = f
}
}
return &table
}
func ZpoolIostatStreamReader(ch chan *ZpoolIostatTable, r io.Reader) {
readbuf := make([]byte, 4096)
var collectbuf string
for {
n, err := r.Read(readbuf)
if n > 0 {
collectbuf = collectbuf + string(readbuf[:n])
// two consecutive newlines (an empty line) separates two iostat entries:
if pos := strings.Index(collectbuf, "\n\n"); pos != -1 {
ch <- ZpoolIostatParser(collectbuf[:pos+1]) // include 1 newline
collectbuf = collectbuf[pos+2:] // skip both newlines
}
}
if err != nil && err != io.EOF {
// unexpected error XXX
ch <- nil
}
if err != nil || n == 0 {
// end of input or error
close(ch)
return
}
}
}
// eof