Skip to content
52 changes: 37 additions & 15 deletions pkg/snclient/check_drivesize_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,12 @@ func (l *CheckDrivesize) setDeviceInfo(drive map[string]string) {
}

// drivePath needs to be in form 'X:\'
drivePath := strings.ToUpper(drive["drive_or_id"])
drivePath := drive["drive_or_id"]
if matchingVolumePath, ok := drive["_matching_volume_path"]; matchingVolumePath != "" && ok {
drivePath = drive["_matching_volume_path"]
}
drivePath = strings.ToUpper(drivePath)

if !strings.HasSuffix(drivePath, "\\") {
drivePath += "\\"
}
Expand Down Expand Up @@ -706,45 +711,62 @@ func (l *CheckDrivesize) setCustomPath(path string, requiredDrives map[string]ma
// try to find closest matching volume
availVolumes := map[string]map[string]string{}
l.setVolumes(availVolumes)
log.Tracef("available volumes: %v", availVolumes)

testPath := strings.TrimSuffix(cleanedPath, "\\") + "\\"
// make first character uppercase because drives are uppercase in the volume list
if len(testPath) > 1 {
testPath = strings.ToUpper(testPath[0:1]) + testPath[1:]
}
volumeTestPath := strings.TrimSuffix(cleanedPath, "\\") + "\\"

var match *map[string]string

for i := range availVolumes {
volume := availVolumes[i]

// parent fallback means parent folders of a drive are valid as well
if parentFallback && volume["drive"] != "" &&
strings.HasPrefix(strings.ToUpper(testPath), strings.ToUpper(volume["drive"])) {
if match == nil || len((*match)["drive"]) < len(volume["drive"]) {
// if parentFallback argument is true, consider parent folders of a drive/mount as valid matches for the given custom search path
if parentFallback && volume["name"] != "" &&
strings.HasPrefix(strings.ToUpper(volumeTestPath), strings.ToUpper(volume["name"])) {
if match == nil || len((*match)["name"]) < len(volume["name"]) {
match = &volume
}
}

if strings.EqualFold(testPath, volume["drive"]) {
if strings.EqualFold(volumeTestPath, volume["name"]) {
match = &volume

break
}
}
if match != nil {
log.Tracef("found volume matching path: %q , volumeTestPath: %q , volume: %v", path, volumeTestPath, (*match))

requiredDrives[path] = utils.CloneStringMap(*match)
// "drive" and "name" attribute is set to custom search path
requiredDrives[path]["id"] = path
requiredDrives[path]["drive"] = path
requiredDrives[path]["name"] = path
requiredDrives[path]["drive_or_name"] = path
requiredDrives[path]["drive_or_name_or_id"] = path

// save this for the later GetVolumeInformation call
requiredDrives[path]["_matching_volume_path"] = (*match)["name"]

requiredDrives[path]["drive_or_name"] = requiredDrives[path]["drive"]
if requiredDrives[path]["drive_or_name"] == "" {
requiredDrives[path]["drive_or_name"] = requiredDrives[path]["name"]
}

requiredDrives[path]["drive_or_id"] = requiredDrives[path]["drive"]
if requiredDrives[path]["drive_or_id"] == "" {
requiredDrives[path]["drive_or_id"] = requiredDrives[path]["id"]
}

requiredDrives[path]["drive_or_name_or_id"] = requiredDrives[path]["drive_or_name"]
if requiredDrives[path]["drive_or_name_or_id"] == "" {
requiredDrives[path]["drive_or_name_or_id"] = requiredDrives[path]["id"]
}

return nil
}

// add anyway to generate an error later with more default values filled in
// if there is no match, add it anyway with an error, which will be printed as is
entry := l.driveEntry(path)
entry["_error"] = fmt.Sprintf("%s not mounted", path)
entry["_error"] = fmt.Sprintf("could not find a drive or volume matching path %q", path)
requiredDrives[path] = entry

return nil
Expand Down
15 changes: 13 additions & 2 deletions pkg/snclient/check_drivesize_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,24 @@ func TestCheckDrivesize(t *testing.T) {
// must not match
res = snc.RunCheck("check_drivesize", []string{"warn=used>100%", "crit=used>100%", "drive=c:\\Windows"})
assert.Equalf(t, CheckExitUnknown, res.State, "state UNKNOWN")
assert.Contains(t, string(res.BuildPluginOutput()), `not mounted`, "output matches")
assert.Contains(t, string(res.BuildPluginOutput()), `could not find a drive or volume matching path`, "output matches")

res = snc.RunCheck("check_drivesize", []string{"warn=used>100%", "crit=used>100%", "folder=c:\\Windows"})
StopTestAgent(t, snc)
}

func TestCheckDrivesizeFolder(t *testing.T) {
snc := StartTestAgent(t, "")

res := snc.RunCheck("check_drivesize", []string{"warn=used>100%", "crit=used>100%", "folder=c:\\Windows"})
assert.Equalf(t, CheckExitOK, res.State, "state OK")
assert.Contains(t, string(res.BuildPluginOutput()), `OK - All 1 drive`, "output matches")
assert.Contains(t, string(res.BuildPluginOutput()), `c:\Windows used %`, "output matches")

res = snc.RunCheck("check_drivesize", []string{"warn=used>100%", "crit=used>100%", "folder=C:\\Windows"})
assert.Equalf(t, CheckExitOK, res.State, "state OK")
assert.Contains(t, string(res.BuildPluginOutput()), `OK - All 1 drive`, "output matches")
assert.Contains(t, string(res.BuildPluginOutput()), `C:\Windows used %`, "output matches")

// check with forward slash
res = snc.RunCheck("check_drivesize", []string{"warn=used>100%", "crit=used>100%", "folder=c:/Windows"})
assert.Equalf(t, CheckExitOK, res.State, "state OK")
Expand Down
Loading