This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
gluster_client.go
176 lines (157 loc) · 5.22 KB
/
gluster_client.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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
"time"
"github.com/ofesseler/gluster_exporter/structs"
"github.com/prometheus/common/log"
)
func execGlusterCommand(arg ...string) (*bytes.Buffer, error) {
stdoutBuffer := &bytes.Buffer{}
argXML := append(arg, "--xml")
glusterExec := exec.Command(GlusterCmd, argXML...)
glusterExec.Stdout = stdoutBuffer
err := glusterExec.Run()
if err != nil {
log.Errorf("tried to execute %v and got error: %v", arg, err)
return stdoutBuffer, err
}
return stdoutBuffer, nil
}
func execMountCheck() (*bytes.Buffer, error) {
stdoutBuffer := &bytes.Buffer{}
mountCmd := exec.Command("mount", "-t", "fuse.glusterfs")
mountCmd.Stdout = stdoutBuffer
return stdoutBuffer, mountCmd.Run()
}
func execTouchOnVolumes(mountpoint string) (bool, error) {
testFileName := fmt.Sprintf("%v/%v_%v", mountpoint, "gluster_mount.test", time.Now())
_, createErr := os.Create(testFileName)
if createErr != nil {
return false, createErr
}
removeErr := os.Remove(testFileName)
if removeErr != nil {
return false, removeErr
}
return true, nil
}
// ExecVolumeInfo executes "gluster volume info" at the local machine and
// returns VolumeInfoXML struct and error
func ExecVolumeInfo() (structs.VolumeInfoXML, error) {
args := []string{"volume", "info"}
bytesBuffer, cmdErr := execGlusterCommand(args...)
if cmdErr != nil {
return structs.VolumeInfoXML{}, cmdErr
}
volumeInfo, err := structs.VolumeInfoXMLUnmarshall(bytesBuffer)
if err != nil {
log.Errorf("Something went wrong while unmarshalling xml: %v", err)
return volumeInfo, err
}
return volumeInfo, nil
}
// ExecVolumeList executes "gluster volume info" at the local machine and
// returns VolumeList struct and error
func ExecVolumeList() (structs.VolList, error) {
args := []string{"volume", "list"}
bytesBuffer, cmdErr := execGlusterCommand(args...)
if cmdErr != nil {
return structs.VolList{}, cmdErr
}
volumeList, err := structs.VolumeListXMLUnmarshall(bytesBuffer)
if err != nil {
log.Errorf("Something went wrong while unmarshalling xml: %v", err)
return volumeList.VolList, err
}
return volumeList.VolList, nil
}
// ExecPeerStatus executes "gluster peer status" at the local machine and
// returns PeerStatus struct and error
func ExecPeerStatus() (structs.PeerStatus, error) {
args := []string{"peer", "status"}
bytesBuffer, cmdErr := execGlusterCommand(args...)
if cmdErr != nil {
return structs.PeerStatus{}, cmdErr
}
peerStatus, err := structs.PeerStatusXMLUnmarshall(bytesBuffer)
if err != nil {
log.Errorf("Something went wrong while unmarshalling xml: %v", err)
return peerStatus.PeerStatus, err
}
return peerStatus.PeerStatus, nil
}
// ExecVolumeProfileGvInfoCumulative executes "gluster volume {volume] profile info cumulative" at the local machine and
// returns VolumeInfoXML struct and error
func ExecVolumeProfileGvInfoCumulative(volumeName string) (structs.VolProfile, error) {
args := []string{"volume", "profile", volumeName, "info", "cumulative"}
bytesBuffer, cmdErr := execGlusterCommand(args...)
if cmdErr != nil {
return structs.VolProfile{}, cmdErr
}
volumeProfile, err := structs.VolumeProfileGvInfoCumulativeXMLUnmarshall(bytesBuffer)
if err != nil {
log.Errorf("Something went wrong while unmarshalling xml: %v", err)
return volumeProfile.VolProfile, err
}
return volumeProfile.VolProfile, nil
}
// ExecVolumeStatusAllDetail executes "gluster volume status all detail" at the local machine
// returns VolumeStatusXML struct and error
func ExecVolumeStatusAllDetail() (structs.VolumeStatusXML, error) {
args := []string{"volume", "status", "all", "detail"}
bytesBuffer, cmdErr := execGlusterCommand(args...)
if cmdErr != nil {
return structs.VolumeStatusXML{}, cmdErr
}
volumeStatus, err := structs.VolumeStatusAllDetailXMLUnmarshall(bytesBuffer)
if err != nil {
log.Errorf("Something went wrong while unmarshalling xml: %v", err)
return volumeStatus, err
}
return volumeStatus, nil
}
// ExecVolumeHealInfo executes volume heal info on host system and processes input
// returns (int) number of unsynced files
func ExecVolumeHealInfo(volumeName string) (int, error) {
args := []string{"volume", "heal", volumeName, "info"}
entriesOutOfSync := 0
bytesBuffer, cmdErr := execGlusterCommand(args...)
if cmdErr != nil {
return -1, cmdErr
}
healInfo, err := structs.VolumeHealInfoXMLUnmarshall(bytesBuffer)
if err != nil {
log.Error(err)
return -1, err
}
for _, brick := range healInfo.HealInfo.Bricks.Brick {
var count int
var err error
count, err = strconv.Atoi(brick.NumberOfEntries)
if err != nil {
log.Error(err)
return -1, err
}
entriesOutOfSync += count
}
return entriesOutOfSync, nil
}
// ExecVolumeQuotaList executes volume quota list on host system and processes input
// returns QuotaList structs and errors
func ExecVolumeQuotaList(volumeName string) (structs.VolumeQuotaXML, error) {
args := []string{"volume", "quota", volumeName, "list"}
bytesBuffer, cmdErr := execGlusterCommand(args...)
if cmdErr != nil {
return structs.VolumeQuotaXML{}, cmdErr
}
volumeQuota, err := structs.VolumeQuotaListXMLUnmarshall(bytesBuffer)
if err != nil {
log.Errorf("Something went wrong while unmarshalling xml: %v", err)
return volumeQuota, err
}
return volumeQuota, nil
}