diff options
author | Eriks Sneiders <eriks.sneiders@zabbix.com> | 2022-03-22 23:02:27 +0300 |
---|---|---|
committer | Eriks Sneiders <eriks.sneiders@zabbix.com> | 2022-03-31 17:45:17 +0300 |
commit | ee756799e70bbcc17e8708013e9975cd2df991dd (patch) | |
tree | 34a254580e11e61269696e772df4538550cc1a59 /src/go/plugins | |
parent | ac58eb3cda53ca9456f3c5a10e8e27fb252b7ae7 (diff) |
...G...... [ZBXNEXT-7559] code clean up
Diffstat (limited to 'src/go/plugins')
-rw-r--r-- | src/go/plugins/smart/smart.go | 2 | ||||
-rw-r--r-- | src/go/plugins/smart/smart_test.go | 10 | ||||
-rw-r--r-- | src/go/plugins/smart/smartfs.go | 171 |
3 files changed, 89 insertions, 94 deletions
diff --git a/src/go/plugins/smart/smart.go b/src/go/plugins/smart/smart.go index b7b406b3fcf..b3cd927ce52 100644 --- a/src/go/plugins/smart/smart.go +++ b/src/go/plugins/smart/smart.go @@ -224,6 +224,8 @@ func (p *Plugin) attributeDiscovery() (jsonArray []byte, err error) { return } +// setSingleDiskFields goes through provided device json data and sets required output fields. +// It returns an error if there is an issue with unmarshal for the provided input JSON map. func setSingleDiskFields(dev []byte) (out map[string]interface{}, err error) { attr := make(map[string]interface{}) if err = json.Unmarshal(dev, &attr); err != nil { diff --git a/src/go/plugins/smart/smart_test.go b/src/go/plugins/smart/smart_test.go index 6677119ec65..8f9a9c5a94b 100644 --- a/src/go/plugins/smart/smart_test.go +++ b/src/go/plugins/smart/smart_test.go @@ -85,17 +85,7 @@ const ( "id": 2071267458 }, "firmware_version": "CV26", - "user_capacity": { - "blocks": 1953525168, - "bytes": 1000204886016 - }, - "logical_block_size": 512, - "physical_block_size": 4096, "rotation_rate": 7200, - "form_factor": { - "ata_value": 2, - "name": "3.5 inches" - }, "ata_smart_data": { "self_test": { "status": { diff --git a/src/go/plugins/smart/smartfs.go b/src/go/plugins/smart/smartfs.go index 0e139b2deb4..15ea202e494 100644 --- a/src/go/plugins/smart/smartfs.go +++ b/src/go/plugins/smart/smartfs.go @@ -245,6 +245,7 @@ func (p *Plugin) execute(jsonRunner bool) (*runner, error) { return r, err } +// executeSingle returns device data for single device from smartctl based on provided path. func (p *Plugin) executeSingle(path string) (device []byte, err error) { device, err = p.executeSmartctl(fmt.Sprintf("-a %s -j", path), false) if err != nil { @@ -254,90 +255,7 @@ func (p *Plugin) executeSingle(path string) (device []byte, err error) { return } -// getDevices returns a parsed slices of all devices returned by smartctl scan. -// Returns a separate slice for both normal and raid devices. -// It returns an error if there is an issue with getting or parsing results from smartctl. -func (p *Plugin) getDevices() (basic, raid, megaraid []deviceInfo, err error) { - basicTmp, err := p.scanDevices("--scan -j") - if err != nil { - return nil, nil, nil, fmt.Errorf("Failed to scan for devices: %s.", err.Error()) - } - - raidTmp, err := p.scanDevices("--scan -d sat -j") - if err != nil { - return nil, nil, nil, fmt.Errorf("Failed to scan for sat devices: %s.", err.Error()) - } - - basic, raid, megaraid = formatDeviceOutput(basicTmp, raidTmp) - - return -} - -func formatDeviceOutput(basic, raid []deviceInfo) (basicDev, raidDev, megaraidDev []deviceInfo) { -loop: - for _, tmp := range basic { - for _, r := range raid { - if tmp.Name == r.Name { - continue loop - } - } - - basicDev = append(basicDev, tmp) - } - - for _, r := range raid { - if strings.Contains(r.DevType, "megaraid") { - megaraidDev = append(megaraidDev, r) - - continue - } - - raidDev = append(raidDev, r) - } - - return -} - -// scanDevices executes smartctl. -// It parses the smartctl data into a slice with deviceInfo. -// The data is sorted based on device name in alphabet order. -// It returns an error if there is an issue with getting or parsing results from smartctl. -func (p *Plugin) scanDevices(args string) ([]deviceInfo, error) { - var d devices - - devices, err := p.executeSmartctl(args, false) - if err != nil { - return nil, err - } - - if err = json.Unmarshal(devices, &d); err != nil { - return nil, zbxerr.ErrorCannotUnmarshalJSON.Wrap(err) - } - - var names []string - for _, info := range d.Info { - names = append(names, info.InfoName) - } - - sort.Strings(names) - - var out []deviceInfo - -names: - for _, name := range names { - for _, info := range d.Info { - if name == info.InfoName { - out = append(out, info) - - continue names - } - } - } - - return out, nil -} - -//executeBase executed runners for basic devices retrived from smartctl +//executeBase executed runners for basic devices retrieved from smartctl. func (r *runner) executeBase(basicDev []deviceInfo, jsonRunner bool) error { r.startBasicRunners(jsonRunner) @@ -766,6 +684,91 @@ func (dp *deviceParser) checkErr() (err error) { return } +// getDevices returns a parsed slices of all devices returned by smartctl scan. +// Returns a separate slice for both normal and raid devices. +// It returns an error if there is an issue with getting or parsing results from smartctl. +func (p *Plugin) getDevices() (basic, raid, megaraid []deviceInfo, err error) { + basicTmp, err := p.scanDevices("--scan -j") + if err != nil { + return nil, nil, nil, fmt.Errorf("Failed to scan for devices: %s.", err.Error()) + } + + raidTmp, err := p.scanDevices("--scan -d sat -j") + if err != nil { + return nil, nil, nil, fmt.Errorf("Failed to scan for sat devices: %s.", err.Error()) + } + + basic, raid, megaraid = formatDeviceOutput(basicTmp, raidTmp) + + return +} + +// formatDeviceOutput removes raid devices from basic device list and separates megaraid devices from the rest of raid +// devices. +func formatDeviceOutput(basic, raid []deviceInfo) (basicDev, raidDev, megaraidDev []deviceInfo) { +loop: + for _, tmp := range basic { + for _, r := range raid { + if tmp.Name == r.Name { + continue loop + } + } + + basicDev = append(basicDev, tmp) + } + + for _, r := range raid { + if strings.Contains(r.DevType, "megaraid") { + megaraidDev = append(megaraidDev, r) + + continue + } + + raidDev = append(raidDev, r) + } + + return +} + +// scanDevices executes smartctl. +// It parses the smartctl data into a slice with deviceInfo. +// The data is sorted based on device name in alphabet order. +// It returns an error if there is an issue with getting or parsing results from smartctl. +func (p *Plugin) scanDevices(args string) ([]deviceInfo, error) { + var d devices + + devices, err := p.executeSmartctl(args, false) + if err != nil { + return nil, err + } + + if err = json.Unmarshal(devices, &d); err != nil { + return nil, zbxerr.ErrorCannotUnmarshalJSON.Wrap(err) + } + + var names []string + for _, info := range d.Info { + names = append(names, info.InfoName) + } + + sort.Strings(names) + + var out []deviceInfo + +names: + for _, name := range names { + for _, info := range d.Info { + if name == info.InfoName { + out = append(out, info) + + continue names + } + } + } + + return out, nil +} + func init() { cpuCount = runtime.NumCPU() if cpuCount < 1 { |