-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskquota.go
196 lines (170 loc) · 4.29 KB
/
diskquota.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
package wlbdqm
import (
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
const (
scriptKeyDiskquota = "WLBDQM_DISKQUOTA_SCRIPT"
scriptKeyBash = "WLBDQM_SHELL_BASH"
)
// Filesystem type blocks quota limit in_doubt grace | files quota limit in_doubt grace Remarks
const (
dqFieldFilesystemIdx = iota
dqFieldTypeIdx
dqFieldDBlocksIdx
dqFieldDQuotaIdx
dqFieldDLimitIdx
dqFieldDInDoubtIdx
dqFieldDGraceIdx
dqFieldUnused
dqFieldFFilesIdx
dqFieldFQuotaIdx
dqFieldFLimitIdx
dqFieldFInDoubtIdx
dqFieldFGraceIdx
dqFieldRemarksIdx
)
var dqHeader = "Filesystem\ttype\tblocks\tquota\tlimit\tin_doubt\tgrace\t|\tfiles\tquota\tlimit\tin_doubt\tgrace\tRemarks"
func RunDiskQuota() (string, error) {
bash := "/bin/bash"
if os.Getenv(scriptKeyBash) != "" {
bash = os.Getenv(scriptKeyBash)
}
DebugPrintln("env bash:", os.Getenv(scriptKeyBash)!="")
DebugPrintln("env diskquota:", os.Getenv(scriptKeyDiskquota))
DebugPrintln("bash:", bash)
cmd := exec.Command(bash, os.Getenv(scriptKeyDiskquota))
output, err := cmd.Output()
DebugPrintln("command output:", string(output))
if err != nil {
return "", err
}
return string(output), nil
}
// DQOutput
// Filesystem type blocks quota limit in_doubt grace
// files quota limit in_doubt grace Remarks
type DQOutput struct {
Filesystem string
Type string
DBlocks string
DQuota string
DLimit string
DInDoubt string
DGrace string
FFiles string
FQuota string
FLimit string
FInDoubt string
FGrace string
Remarks string
}
func (dq DQOutput) ByteOfBlocks() float64 {
f, err := ParseSizeToByte(dq.DBlocks)
if err != nil {
panic(err)
}
return f
}
func (dq DQOutput) ByteOfDQuota() float64 {
f, err := ParseSizeToByte(dq.DQuota)
if err != nil {
panic(err)
}
return f
}
func (dq DQOutput) NumOfFFiles() int64 {
DebugPrintln("dq.FFiles", dq.FFiles)
i, err := strconv.ParseInt(dq.FFiles, 10, 64)
if err != nil {
panic(err)
}
DebugPrintln("dq.FFiles returns", i)
return i
}
func (dq DQOutput) NumOfFQuota() int64 {
DebugPrintln("dq.FQuota", dq.FQuota)
i, err := strconv.ParseInt(dq.FQuota, 10, 64)
if err != nil {
panic(err)
}
DebugPrintln("dq.FQuota returns", i)
return i
}
func (dq DQOutput) String() string {
s1 := dqHeader
s2 := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s", dq.Filesystem, dq.Type, dq.DBlocks,
dq.DQuota, dq.DLimit, dq.DInDoubt, dq.DGrace, "|", dq.FFiles, dq.FQuota, dq.FLimit, dq.FInDoubt, dq.FGrace, dq.Remarks)
return s1 + "\n" + s2
}
// Filesystem type blocks quota limit in_doubt grace
// files quota limit in_doubt grace Remarks
func (dq DQOutput) HTMLTable() string {
return `<table>
<tr>
<th>Filesystem</th>
<th>type</th>
<th>blocks</th>
<th>quota</th>
<th>limit</th>
<th>in_doubt</th>
<th>grace</th>
<th>|</th>
<th>files</th>
<th>quota</th>
<th>limit</th>
<th>in_doubt</th>
<th>grace</th>
<th>Remarks</th>
</tr>
<tr>
<td>` + dq.Filesystem + `</td>
<td>` + dq.Type + `</td>
<td>` + dq.DBlocks + `</td>
<td>` + dq.DQuota + `</td>
<td>` + dq.DLimit + `</td>
<td>` + dq.DInDoubt + `</td>
<td>` + dq.DGrace + `</td>
<td>|</td>
<td>` + dq.FFiles + `</td>
<td>` + dq.FQuota + `</td>
<td>` + dq.FLimit + `</td>
<td>` + dq.FInDoubt + `</td>
<td>` + dq.FGrace + `</td>
<td>` + dq.Remarks + `</td>
</tr>
</table>`
}
func ParseDiskQuotaOutput(output string) (*DQOutput, error) {
lines := strings.Split(output, "\n")
DebugPrintln("output line number", len(lines))
if len(lines) < 2 {
return nil, errors.New("output line number error")
}
fieldValues := strings.Split(lines[1], "\t")
DebugPrintln(fieldValues)
remarks := ""
if len(fieldValues) > 13 {
remarks = fieldValues[dqFieldRemarksIdx]
}
dqOutput := &DQOutput{
Filesystem: fieldValues[dqFieldFilesystemIdx],
Type: fieldValues[dqFieldTypeIdx],
DBlocks: fieldValues[dqFieldDBlocksIdx],
DQuota: fieldValues[dqFieldDQuotaIdx],
DLimit: fieldValues[dqFieldDLimitIdx],
DInDoubt: fieldValues[dqFieldDInDoubtIdx],
DGrace: fieldValues[dqFieldDGraceIdx],
FFiles: fieldValues[dqFieldFFilesIdx],
FQuota: fieldValues[dqFieldFQuotaIdx],
FLimit: fieldValues[dqFieldFLimitIdx],
FInDoubt: fieldValues[dqFieldFInDoubtIdx],
FGrace: fieldValues[dqFieldFGraceIdx],
Remarks: remarks,
}
return dqOutput, nil
}