Skip to content

Commit

Permalink
...G...... [DEV-2701] fixed regexp and check executable existance
Browse files Browse the repository at this point in the history
  • Loading branch information
Eriks Sneiders committed Oct 27, 2023
1 parent 225c46f commit 09fa80b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/go/plugins/smart/smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ type Plugin struct {

var impl Plugin

// Allow only alphanumeric, hyphens and underscores, but dis-allow hyphens and underscores at the end of the string
var cleanRegex = regexp.MustCompile(`[^a-zA-Z0-9 -_]+`)
var cleanRegex = regexp.MustCompile(`^[a-zA-Z0-9 -_/\\]*$`)

// Configure -
func (p *Plugin) Configure(global *plugin.GlobalOptions, options interface{}) {
Expand Down Expand Up @@ -145,12 +144,12 @@ func (p *Plugin) diskDiscovery() (jsonArray []byte, err error) {
}

func (p *Plugin) diskGet(params []string) ([]byte, error) {
fmt.Println("p", params)

for k, v := range params {
params[k] = clearString(v)
for _, v := range params {
err := clearString(v)
if err != nil {
return nil, err
}
}
fmt.Println("p clean", params)

switch len(params) {
case twoParameters:
Expand Down Expand Up @@ -438,8 +437,12 @@ func getTypeByRateAndAttr(rate int, tables []table) string {
return ssdType
}

func clearString(str string) string {
return cleanRegex.ReplaceAllString(str, "")
func clearString(str string) error {
if !cleanRegex.MatchString(str) {
return zbxerr.New(fmt.Sprintf(" '%s' string contain dis-allowed characters", str))
}

return nil
}

func init() {
Expand Down
6 changes: 6 additions & 0 deletions src/go/plugins/smart/smart_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package smart

import (
"fmt"
"os/exec"
"time"

"zabbix.com/pkg/zbxcmd"
Expand All @@ -39,6 +40,11 @@ func (p *Plugin) executeSmartctl(args string, strict bool) ([]byte, error) {
var out string
var err error

_, err = exec.LookPath(path)
if err != nil {
return nil, err
}

executable := fmt.Sprintf("sudo -n %s %s", path, args)

p.Tracef("executing smartctl command: %s", executable)
Expand Down
6 changes: 6 additions & 0 deletions src/go/plugins/smart/smart_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package smart

import (
"fmt"
"os/exec"
"time"

"zabbix.com/pkg/zbxcmd"
Expand All @@ -40,6 +41,11 @@ func (p *Plugin) executeSmartctl(args string, strict bool) ([]byte, error) {

var err error

_, err = exec.LookPath(path)
if err != nil {
return nil, err
}

executable := fmt.Sprintf("%s %s", path, args)

p.Tracef("executing smartctl command: %s", executable)
Expand Down
32 changes: 29 additions & 3 deletions src/go/plugins/smart/smartfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,13 @@ func (r *runner) getBasicDevices(jsonRunner bool) {
defer r.wg.Done()

for name := range r.names {
devices, err := r.plugin.executeSmartctl(fmt.Sprintf("-a %s -j", clearString(name)), false)
err := clearString(name)
if err != nil {
r.err <- zbxerr.ErrorCannotFetchData.Wrap(err)
return
}

devices, err := r.plugin.executeSmartctl(fmt.Sprintf("-a %s -j", name), false)
if err != nil {
r.err <- fmt.Errorf("Failed to execute smartctl: %s.", err.Error())
return
Expand Down Expand Up @@ -510,7 +516,17 @@ runner:
name = fmt.Sprintf("%s -d %s,%d", raid.name, raid.rType, i)
}

device, err := r.plugin.executeSmartctl(fmt.Sprintf("-a %s -j ", clearString(name)), false)
err := clearString(name)
if err != nil {
r.plugin.Tracef(
"stopped looking for RAID devices of %s type, err: %s",
raid.rType, fmt.Errorf("failed to parse RAID disk data from smartctl: %s", err.Error()),
)

continue runner
}

device, err := r.plugin.executeSmartctl(fmt.Sprintf("-a %s -j ", name), false)
if err != nil {
r.plugin.Tracef(
"stopped looking for RAID devices of %s type, err: %s",
Expand Down Expand Up @@ -581,7 +597,17 @@ func (r *runner) getMegaRaidDevices(jsonRunner bool) {

name := fmt.Sprintf("%s -d %s", raid.name, raid.rType)

device, err := r.plugin.executeSmartctl(fmt.Sprintf("-a %s -j ", clearString(name)), false)
err := clearString(name)
if err != nil {
r.plugin.Tracef(
"stopped looking for RAID devices of %s type, err: %s",
raid.rType, fmt.Errorf("failed to parse RAID disk data from smartctl: %s", err.Error()),
)

continue
}

device, err := r.plugin.executeSmartctl(fmt.Sprintf("-a %s -j ", name), false)
if err != nil {
r.plugin.Tracef(
"failed to get megaraid device with name %s, %s", name, err.Error(),
Expand Down

0 comments on commit 09fa80b

Please sign in to comment.