81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package check
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
//DfChecker df command implementation of AdvancedChecker
|
|
type DfChecker struct {
|
|
CliChecker
|
|
pourcentRegexMatcher *regexp.Regexp
|
|
}
|
|
|
|
type DfOut []DfOutLine
|
|
type DfOutLine struct {
|
|
Filesystem,
|
|
Size,
|
|
Used,
|
|
Available string
|
|
PourcentUsed int
|
|
Mounted string
|
|
}
|
|
|
|
//NewDfChecker build new checker for df command
|
|
func NewDfChecker(alertingMessage string, command ...string) AdvancedChecker {
|
|
|
|
return &DfChecker{
|
|
CliChecker: CliChecker{
|
|
alertingMessage: alertingMessage,
|
|
command: command,
|
|
},
|
|
pourcentRegexMatcher: regexp.MustCompile(`(?P<value>.*)%`),
|
|
}
|
|
}
|
|
|
|
//DfParser parse df stdout
|
|
func (dc *DfChecker) Parse(stdout string) (interface{}, error) {
|
|
var dfOut DfOut
|
|
|
|
for _, line := range strings.Split(stdout, "\n")[1:] {
|
|
if strings.TrimSpace(line) == "" {
|
|
continue
|
|
}
|
|
|
|
var cleanedColumns []string
|
|
for _, col := range strings.Split(line, " ") {
|
|
if v := strings.TrimSpace(col); v != "" {
|
|
cleanedColumns = append(cleanedColumns, v)
|
|
}
|
|
}
|
|
|
|
valueIndex := dc.pourcentRegexMatcher.SubexpIndex("value")
|
|
v, err := strconv.Atoi(dc.pourcentRegexMatcher.FindStringSubmatch(cleanedColumns[4])[valueIndex])
|
|
if err != nil {
|
|
return dfOut, err
|
|
}
|
|
// extract pourcent used from columns
|
|
dfOut = append(dfOut, DfOutLine{
|
|
Filesystem: cleanedColumns[0],
|
|
Size: cleanedColumns[1],
|
|
Used: cleanedColumns[2],
|
|
Available: cleanedColumns[3],
|
|
PourcentUsed: v,
|
|
Mounted: cleanedColumns[len(cleanedColumns)-1],
|
|
})
|
|
}
|
|
return dfOut, nil
|
|
}
|
|
|
|
func FileSystemChecker(fileSystemName string, criticalPourcent int, dfOut DfOut) bool {
|
|
for _, line := range dfOut {
|
|
if line.Filesystem == fileSystemName {
|
|
if line.PourcentUsed > criticalPourcent {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|