-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
133 lines (108 loc) · 2.74 KB
/
discovery.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os/exec"
"regexp"
"strings"
hierr "github.com/reconquest/hierr-go"
)
func discovery() error {
discoveryData := make(map[string][]map[string]string)
out, err := exec.Command(sudo, smartctl, "--scan-open").CombinedOutput()
if err != nil {
return hierr.Errorf(
out,
"can't run %s %s --scan-open",
sudo,
smartctl,
)
}
disks, err := getDisks(string(out))
if err != nil {
return hierr.Errorf(err, "can't discovery disks")
}
for _, diskData := range disks {
discoveryData["data"] = append(discoveryData["data"], diskData)
}
out, err = json.Marshal(discoveryData)
if err != nil {
return hierr.Errorf(err, "can't create json data")
}
fmt.Printf("%s\n", out)
return nil
}
func getDisks(rawOut string) ([]map[string]string, error) {
var disks []map[string]string
regexpDiskName := regexp.MustCompile(`(\/(.+?))\s(-d [A-Za-z0-9,\+]+)`)
scanner := bufio.NewScanner(strings.NewReader(rawOut))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Permission denied") {
return nil, fmt.Errorf("can't get disks. Permission denied")
}
if strings.HasPrefix(line, "#") {
continue
}
diskName := regexpDiskName.FindString(line)
diskData, err := getDiskData(diskName)
if err != nil {
return nil, hierr.Errorf(
err,
"can't get data for %s disk",
diskName,
)
}
// skip append data if duplicate #SERIAL
duplicate := bool(false)
for _, disk := range disks {
if diskData["{#SERIAL}"] == disk["{#SERIAL}"] {
duplicate = true
continue
}
}
if duplicate {
continue
}
disks = append(disks, diskData)
}
return disks, nil
}
func getDiskData(diskName string) (map[string]string, error) {
diskData := make(map[string]string)
diskData["{#DISK_NAME}"] = diskName
diskData["{#SMART_ENABLED}"] = "Disabled"
diskData["{#INTERFACE_TYPE}"] = SAS
args := []string{smartctl, "-i"}
args = append(args, strings.Split(diskName, " ")...)
rawDiskInfo, err := exec.Command(sudo, args...).CombinedOutput()
if err != nil {
return diskData, hierr.Errorf(
rawDiskInfo,
"can't run %s %s -i %s",
sudo,
smartctl,
diskName,
)
}
diskInfo := string(rawDiskInfo)
s := regexp.MustCompile(`Serial (N|n)umber:.+`)
serialLines := s.FindAllString(diskInfo, -1)
for _, serialLine := range serialLines {
diskData["{#SERIAL}"] = strings.TrimSpace(
strings.Split(serialLine, ":")[1],
)
}
r := regexp.MustCompile(`SMART.+?: +(.+)`)
smartStats := r.FindAllString(diskInfo, -1)
for _, smartStat := range smartStats {
if strings.Contains(smartStat, "Enabled") {
diskData["{#SMART_ENABLED}"] = "Enabled"
}
}
if strings.Contains(diskInfo, SATA) {
diskData["{#INTERFACE_TYPE}"] = SATA
}
return diskData, nil
}