forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iops_unix.go
70 lines (65 loc) · 2.04 KB
/
iops_unix.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
// +build linux freebsd solaris openbsd darwin
package hostInfo
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"io/ioutil"
"os"
"strconv"
)
func (i IopsCollector) getIopsData(readOrWrite string) (map[string]interface{}, error) {
file, err := os.Open("/var/lib/rancher/state/" + readOrWrite + ".json")
if err != nil {
return map[string]interface{}{}, err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return map[string]interface{}{}, err
}
var result map[string]interface{}
err = json.Unmarshal(data, result)
if err != nil {
return map[string]interface{}{}, err
}
return result, nil
}
func (i IopsCollector) parseIopsData() (map[string]interface{}, error) {
data := map[string]interface{}{}
readJSONData, err := i.getIopsData("read")
if err != nil && !os.IsNotExist(err) {
return data, errors.Wrap(err, constants.ParseIopsDataError+"failed to read iops file")
} else if err != nil && os.IsNotExist(err) {
return data, nil
}
writeJSONData, err := i.getIopsData("write")
if err != nil && !os.IsNotExist(err) {
return data, errors.Wrap(err, constants.ParseIopsDataError+"failed to read iops file")
} else if err != nil && os.IsNotExist(err) {
return data, nil
}
readIops, _ := utils.GetFieldsIfExist(readJSONData["jobs"].([]map[string]interface{})[0], "read", "iops")
writeIops, _ := utils.GetFieldsIfExist(writeJSONData["jobs"].([]map[string]interface{})[0], "write", "iops")
device, _ := utils.GetFieldsIfExist(readJSONData["disk_util"].([]map[string]interface{})[0], "name")
key := "/dev" + strconv.QuoteToASCII(device.(string))
data[key] = map[string]interface{}{
"read": readIops,
"write": writeIops,
}
return data, nil
}
func (i IopsCollector) getDefaultDisk() (string, error) {
data, err := i.GetData()
if err != nil {
return "", errors.Wrap(err, constants.GetDefaultDiskError+"failed to get data")
}
if len(data) == 0 {
return "", nil
}
for key := range data {
return key, nil
}
return "", nil
}