func (chk PHPConfig) Status() (int, string, error) { // getPHPVariable returns the value of a PHP configuration value as a string // or just "" if it doesn't exist getPHPVariable := func(name string) (val string) { quote := func(str string) string { return "\"" + str + "\"" } // php -r 'echo get_cfg_var("default_mimetype"); echo := fmt.Sprintf("echo get_cfg_var(%s);", quote(name)) cmd := exec.Command("php", "-r", echo) out, err := cmd.CombinedOutput() if err != nil { errutil.ExecError(cmd, string(out), err) } return string(out) } actualValue := getPHPVariable(chk.variable) if actualValue == chk.value { return errutil.Success() } else if actualValue == "" { msg := "PHP configuration variable not set" return errutil.GenericError(msg, chk.value, []string{actualValue}) } msg := "PHP variable did not match expected value" return errutil.GenericError(msg, chk.value, []string{actualValue}) }
func (chk Installed) Status() (int, string, error) { name := getManager() options := managers[name] cmd := exec.Command(name, options, chk.pkg) out, err := cmd.CombinedOutput() outstr := string(out) var msg string switch { case err == nil && (name == "rpm" || name == "pacman"): return errutil.Success() // failures due to mising package case name == "dpkg" && strings.Contains(outstr, "not installed"): case name == "pacman" && strings.Contains(outstr, "not found"): case name == "rpm" && strings.Contains(outstr, "not installed"): msg := "Package was not found:" msg += "\n\tPackage name: " + chk.pkg msg += "\n\tPackage manager: " + name msg += "\n\tCommand output: " + outstr return 1, msg, nil // failures that were not due to packages not being installed case err != nil: errutil.ExecError(cmd, outstr, err) default: return errutil.Success() } return 1, msg, nil }
// CommandOutput returns a string version of the ouput of a given command, // and reports errors effectively. func CommandOutput(cmd *exec.Cmd) string { out, err := cmd.CombinedOutput() outStr := string(out) if err != nil { errutil.ExecError(cmd, outStr, err) } return outStr }
func (chk CommandOutputMatches) Status() (int, string, error) { cmd := exec.Command("bash", "-c", chk.Command) out, err := cmd.CombinedOutput() if err != nil { errutil.ExecError(cmd, string(out), err) } if chk.re.Match(out) { return errutil.Success() } msg := "Command output did not match regexp" return errutil.GenericError(msg, chk.re.String(), []string{string(out)}) }
func (chk Temp) Status() (int, string, error) { // allCoreTemps returns the Temperature of each core allCoreTemps := func() (Temps []int) { cmd := exec.Command("sensors") out, err := cmd.CombinedOutput() outstr := string(out) errutil.ExecError(cmd, outstr, err) restr := `Core\s\d+:\s+[\+\-](?P<Temp>\d+)\.*\d*°C` re := regexp.MustCompile(restr) for _, line := range regexp.MustCompile(`\n+`).Split(outstr, -1) { if re.MatchString(line) { // submatch captures only the integer part of the Temperature matchDict := chkutil.SubmatchMap(re, line) if _, ok := matchDict["Temp"]; !ok { log.WithFields(log.Fields{ "regexp": re.String(), "matchDict": matchDict, "output": outstr, }).Fatal("Couldn't find any Temperatures in `sensors` output") } TempInt64, err := strconv.ParseInt(matchDict["Temp"], 10, 64) if err != nil { log.WithFields(log.Fields{ "regexp": re.String(), "matchDict": matchDict, "output": outstr, "error": err.Error(), }).Fatal("Couldn't parse integer from `sensors` output") } Temps = append(Temps, int(TempInt64)) } } return Temps } // getCoreTemp returns an integer Temperature for a certain core getCoreTemp := func(core int) (Temp int) { Temps := allCoreTemps() errutil.IndexError("No such core available", core, Temps) return Temps[core] } Temp := getCoreTemp(0) if Temp < int(chk.max) { return errutil.Success() } msg := "Core Temp exceeds defined maximum" return errutil.GenericError(msg, chk.max, []string{fmt.Sprint(Temp)}) }
func (chk KernelParameter) Status() (int, string, error) { // parameterValue returns the value of a kernel parameter parameterSet := func(name string) bool { cmd := exec.Command("/sbin/sysctl", "-q", "-n", name) out, err := cmd.CombinedOutput() // failed on incorrect module name if err != nil && strings.Contains(err.Error(), "255") { return false } else if err != nil { errutil.ExecError(cmd, string(out), err) } return true } if parameterSet(chk.name) { return errutil.Success() } return 1, "Kernel parameter not set: " + chk.name, nil }
func (chk Installed) Status() (int, string, error) { name := getManager() options := managers[name] cmd := exec.Command(name, options, chk.pkg) out, err := cmd.CombinedOutput() outstr := string(out) if strings.Contains(outstr, chk.pkg) { return errutil.Success() } else if outstr != "" && err != nil { // pacman - outstr == "", and exit code of 1 if it can't find the pkg errutil.ExecError(cmd, outstr, err) } msg := "Package was not found:" msg += "\n\tPackage name: " + chk.pkg msg += "\n\tPackage manager: " + name msg += "\n\tCommand output: " + outstr return 1, msg, nil }