Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ext filesystem capabilities #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions qs2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"strconv"
"strings"
"regexp"
)

type Quota struct {
Expand Down Expand Up @@ -105,7 +106,54 @@ func xfsQuota(name string, path string) Quota {

}

func utilizationBar(q *Quota) {
func extQuota(name string, path string) Quota{
var q Quota

cmd := exec.Command("sudo", "repquota", path)
out, _ := cmd.Output()
re := regexp.MustCompile( name + "[\\s-]+([0-9]+\\s+){6}")
sfs := re.FindString(string(out))
sf := strings.Fields(sfs)
if len(sf) < 7 {
// user probably doesn't have a quota. set everything to -1 and
// we'll deal with it later
q = Quota{-1, -1, -1, -1, -1, -1}
return q
}
var nolimits bool = false
sf3,_ := strconv.Atoi(sf[3])
sf2,_ := strconv.Atoi(sf[2])
sf5,_ := strconv.Atoi(sf[5])
sf6,_ := strconv.Atoi(sf[6])
if (sf3 == 0 || sf2 == 0) {
sf1, _ := strconv.Atoi(sf[1])
fmt.Printf("No size quota, current usage: %dM\n" , sf1*1024/1000/1000)
nolimits = true
}
if (sf5 == 0 || sf6 == 0){
sf4, _ := strconv.Atoi(sf[4])
fmt.Printf("No file quota, current usage: %d\n" , sf4)
nolimits = true
}
if nolimits {
return Quota{-1,-1,-1,-1,-1,-1}

}
kbytes, _ := strconv.Atoi(strings.Trim(sf[1], "*"))
kbsoft, _ := strconv.Atoi(sf[2])
kbhard, _ := strconv.Atoi(sf[3])
q.bytes = kbytes * 1024
q.bsoft = kbsoft * 1024
q.bhard = kbhard * 1024

q.files, _ = strconv.Atoi(strings.Trim(sf[4], "*"))
q.fsoft, _ = strconv.Atoi(sf[5])
q.fhard, _ = strconv.Atoi(sf[6])

return q
}

func utilizationBar(q *Quota, email string) {
// get some terminal info to make rad status bars
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
Expand All @@ -115,7 +163,7 @@ func utilizationBar(q *Quota) {

maxwidth := w / 4
if q.bytes == -1 || q.bsoft == -1 || q.bhard == -1 {
msg := "No quota information! Contact [email protected]"
msg := "No quota information! Contact " + email
fmt.Printf("[ %s", msg)
for i := 1; i <= (maxwidth - len(msg)); i++ {
fmt.Printf(" ")
Expand Down Expand Up @@ -149,9 +197,11 @@ func main() {

cephPathPtr := flag.String("c", "", "Path for CephFS filesystem NOT including username")
pathPtr := flag.String("n", "", "Path for XFS or NFS filesystem NOT including username")
extPtr := flag.String("e", "", "Path for EXT filesystem")
email := flag.String("s", "[email protected]" , "Support Email");
flag.Parse()

if (*cephPathPtr == "") && (*pathPtr == "") {
if (*cephPathPtr == "") && (*pathPtr == "") && (*extPtr == ""){
fmt.Println("Usage: ")
flag.PrintDefaults()
os.Exit(1)
Expand All @@ -161,12 +211,18 @@ func main() {
cq := cephQuota(username, *cephPathPtr)

fmt.Printf("%-10s: ", *cephPathPtr)
utilizationBar(&cq)
utilizationBar(&cq, *email)
}
if *pathPtr != "" {
xq := xfsQuota(username, *pathPtr)

fmt.Printf("%-10s: ", *pathPtr)
utilizationBar(&xq)
utilizationBar(&xq, *email)
}
if *extPtr != "" {
eq := extQuota(username, *extPtr)
fmt.Printf("%-10s: ", *extPtr)
utilizationBar(&eq, *email)

}
}