-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapter.go
198 lines (179 loc) · 5.87 KB
/
adapter.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
197
198
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
type adInfo struct {
ControllerStatus string `json:"controller status"`
ChannelDescription string `json:"channel description"`
ControllerModel string `json:"controller model"`
ControllerSerialNumber string `json:"controller serial number"`
ControllerWorldWideName string `json:"controller world wide name"`
ControllerAlarm string `json:"controller alarm"`
Temperature string `json:"temperature"`
InstalledMemory string `json:"installed memory"`
GlobalTaskPriority string `json:"global task priority"`
PerformanceMode string `json:"performance mode"`
StayawakePeriod string `json:"stayawake period"`
DefunctDiskDriveCount int `json:"defunct disk drive count"`
LogicalDevicesFailed int `json:"logical devices failed"`
LogicalDevicesTotal int `json:"logical devices total"`
LogicalDevicesDegraded int `json:"logical devices degraded"`
NCQStatus string `json:"ncq status"`
Copyback string `json:"copyback"`
AutomaticFailover string `json:"automatic failover"`
BackgroundConsistencyCheck string `json:"background consistency check"`
BIOS string `json:"bios"`
Firmware string `json:"firmware"`
Driver string `json:"driver"`
Status string `json:"status"`
BatteryPresent string `json:"battery present"`
}
func adDiscovery() {
deviceType := "AD"
adapters := []discoveryDevice{}
controllers, err := controllersCount()
if err != nil {
fmt.Printf("Cannot check lspci adaptec controllers\n - %v", err)
os.Exit(1)
}
if controllers > 0 {
bin, binErr := getBin("arcconf")
if binErr != nil {
fmt.Printf("arcconf - not found in system PATH\n - %v", err)
os.Exit(0)
}
for controller := 1; controller <= controllers; controller++ {
args := []string{"getconfig", strconv.Itoa(controller), deviceType, "nologs"}
cmd := exec.Command(bin, args...)
out, err := cmd.Output()
if err != nil {
fmt.Printf("Error %v", err)
}
ad := discoveryDevice{}
for _, adstat := range strings.Split(string(out), "\n") {
ad.adDiscoveryParser(adstat, controller)
}
adapters = append(adapters, ad)
}
}
data := data{Data: adapters}
//r, _ := json.MarshalIndent(data, "", " ")
r, _ := json.Marshal(data)
fmt.Print(string(r))
}
func adStats(adController string) {
deviceType := "AD"
controllers, cErr := controllersCount()
if cErr != nil {
fmt.Printf("Cannot check lspci adaptec controllers: %v", cErr)
os.Exit(1)
}
bin, binErr := getBin("arcconf")
if binErr != nil {
fmt.Println("Arcconf not found:", binErr)
os.Exit(0)
}
ads := map[string]adInfo{}
for controller := 1; controller <= controllers; controller++ {
args := []string{"getconfig", strconv.Itoa(controller), deviceType, "nologs"}
cmd := exec.Command(bin, args...)
out, err := cmd.Output()
if err != nil {
fmt.Printf("Error %v", err)
}
ad := adInfo{Status: "NotPresent"}
for _, adstat := range strings.Split(string(out), "\n") {
ad.adParserInfo(adstat)
}
ads[strconv.Itoa(controller)] = ad
}
if _, ok := ads[adController]; ok {
//r, _ := json.MarshalIndent(devices[ldName], "", " ")
r, _ := json.Marshal(ads[adController])
fmt.Print(string(r))
} else {
fmt.Printf("AD not exist %v", adController)
os.Exit(1)
}
}
func (d *discoveryDevice) adDiscoveryParser(line string, controller int) error {
split := strings.Split(line, " : ")
match := strings.ToLower(strings.TrimSpace(split[0]))
if match == "controller model" {
model := strings.TrimSpace(split[1])
d.DeviceAlias = model
}
d.DeviceID = strconv.Itoa(controller)
d.Present = "Present"
d.DeviceType = "AD"
return nil
}
func (ad *adInfo) adParserInfo(line string) error {
split := strings.Split(line, " : ")
match := strings.ToLower(strings.TrimSpace(split[0]))
switch match {
case "controller status":
ad.ControllerStatus = strings.TrimSpace(split[1])
case "channel description":
ad.ChannelDescription = strings.TrimSpace(split[1])
case "controller model":
ad.ControllerModel = strings.TrimSpace(split[1])
case "controller serial number":
ad.ControllerSerialNumber = strings.TrimSpace(split[1])
case "controller world wide name":
ad.ControllerWorldWideName = strings.TrimSpace(split[1])
case "controller alarm":
ad.ControllerAlarm = strings.TrimSpace(split[1])
case "temperature":
ad.Temperature = strings.TrimSpace(split[1])
case "installed memory":
ad.InstalledMemory = strings.TrimSpace(split[1])
case "global task priority":
ad.GlobalTaskPriority = strings.TrimSpace(split[1])
case "performance mode":
ad.PerformanceMode = strings.TrimSpace(split[1])
case "stayawake period":
ad.StayawakePeriod = strings.TrimSpace(split[1])
case "defunct disk drive count":
ad.DefunctDiskDriveCount = 0
case "logical devices/failed/degraded":
device := strings.Split(split[1], "/")
var r []int
for _, i := range device {
d, err := strconv.Atoi(i)
if err != nil {
return err
}
r = append(r, d)
}
ad.LogicalDevicesTotal = r[0]
ad.LogicalDevicesFailed = r[1]
ad.LogicalDevicesDegraded = r[2]
case "ncq status":
ad.NCQStatus = strings.TrimSpace(split[1])
case "copyback":
ad.Copyback = strings.TrimSpace(split[1])
case "automatic failover":
ad.AutomaticFailover = strings.TrimSpace(split[1])
case "background consistency check":
ad.BackgroundConsistencyCheck = strings.TrimSpace(split[1])
case "bios":
ad.BIOS = strings.TrimSpace(split[1])
case "firmware":
ad.Firmware = strings.TrimSpace(split[1])
case "driver":
ad.Driver = strings.TrimSpace(split[1])
case "status":
ad.Status = strings.TrimSpace(split[1])
case "controller zmm information":
ad.BatteryPresent = "True"
case "battery":
ad.BatteryPresent = "True"
}
return nil
}