// getIPWorker(exitCode int, exitMessage string) is an abstraction of Ip4 and Ip6 func getIPWorker(name string, address string, version int) (exitCode int, exitMessage string) { ips := getInterfaceIPs(name, version) if tabular.StrIn(address, ips) { return 0, "" } return wrkutils.GenericError("Interface does not have IP", address, ips) }
// RoutingTableMatch asks: Is this value in this column of the routing table? func RoutingTableMatch(col string, str string) (int, string, error) { column := RoutingTableColumn(col) if tabular.StrIn(str, column) { return errutil.Success() } return errutil.GenericError("Not found in routing table", str, column) }
// routingTableMatch(exitCode int, exitMessage string) constructs a Worker that returns whether or not the // given string was found in the given column of the routing table. It is an // astraction of routingTableDestination, routingTableInterface, and // routingTableGateway func routingTableMatch(col string, str string) (exitCode int, exitMessage string) { column := routingTableColumn(col) if tabular.StrIn(str, column) { return 0, "" } return wrkutils.GenericError("Not found in routing table", str, column) }
// timers(exitCode int, exitMessage string) is pure DRY for systemctlTimer and systemctlTimerLoaded func timersWorker(unit string, all bool) (exitCode int, exitMessage string) { timers := getTimers(all) if tabular.StrIn(unit, timers) { return 0, "" } return wrkutils.GenericError("Timer not found", unit, timers) }
// timerCheck is pure DRY for SystemctlTimer and SystemctlTimerLoaded func timerCheck(unit string, all bool) (int, string, error) { timers, err := systemdstatus.Timers(all) if err != nil { return 1, "", err } else if tabular.StrIn(unit, timers) { return errutil.Success() } return errutil.GenericError("Timer not found", unit, timers) }
func (chk SystemctlSockListening) Status() (int, string, error) { listening, err := systemdstatus.ListeningSockets() if err != nil { return 1, "", err } if tabular.StrIn(chk.path, listening) { return errutil.Success() } return errutil.GenericError("Socket wasn't listening", chk.path, listening) }
func (chk DockerImage) Status() (int, string, error) { images, err := dockerstatus.DockerImageRepositories() if err != nil { return 1, "", err } if tabular.StrIn(chk.name, images) { return errutil.Success() } return errutil.GenericError("Docker image was not found", chk.name, images) }
func TestGetManager(t *testing.T) { t.Parallel() man := getManager() supported := []string{"pacman", "dpkg", "rpm"} if !tabular.StrIn(man, supported) { msg := "getManager returned an unsupported package manager" msg += "\n\tReturned: " + man msg += "\n\tSupported: " + fmt.Sprint(supported) t.Error(msg) } }
// UserInGroup asks whether or not the given user is a part of the given group. func UserInGroup(username, groupname string) (bool, error) { groups, err := Groups() if err != nil { return false, err } for _, group := range groups { if group.Name == groupname && tabular.StrIn(username, group.Users) { return true, nil } } return false, nil }
// systemctlSock is an abstraction of systemctlSockPath and systemctlSockUnit, // it reads from `systemctl list-sockets` and sees if the value is in the // appropriate column. func systemctlSock(value string, column string) (exitCode int, exitMessage string) { outstr := wrkutils.CommandOutput(exec.Command("systemctl", "list-sockets")) lines := tabular.Lines(outstr) msg := "systemctl list-sockers didn't output enough rows" wrkutils.IndexError(msg, len(lines)-4, lines) unlines := tabular.Unlines(lines[:len(lines)-4]) table := tabular.SeparateOnAlignment(unlines) values := tabular.GetColumnByHeader(column, table) if tabular.StrIn(value, values) { return 0, "" } return wrkutils.GenericError("Socket not found", value, values) }
func (chk Module) Status() (int, string, error) { // kernelModules returns a list of all Modules that are currently loaded // TODO just read from /proc/Modules kernelModules := func() (Modules []string) { cmd := exec.Command("/sbin/lsmod") return chkutil.CommandColumnNoHeader(0, cmd) } Modules := kernelModules() if tabular.StrIn(chk.name, Modules) { return errutil.Success() } return errutil.GenericError("Module is not loaded", chk.name, Modules) }
func (chk SystemctlUnitFileStatus) New(params []string) (chkutil.Check, error) { if len(params) != 2 { return chk, errutil.ParameterLengthError{2, params} } validStatuses := []string{"static", "enabled", "disabled"} if !tabular.StrIn(strings.ToLower(params[1]), validStatuses) { validStatusesStr := "static | enabled | disabled" return chk, errutil.ParameterTypeError{params[1], validStatusesStr} } chk.unit = params[0] chk.status = params[1] return chk, nil }
// userInGroup checks whether or not a given user is in a given group func userInGroup(parameters []string) (exitCode int, exitMessage string) { user := parameters[0] group := parameters[0] groups := getGroups() for _, g := range groups { if g.Name == group { if tabular.StrIn(user, g.Users) { return 0, "" } return wrkutils.GenericError("User not found in group", user, g.Users) } } return groupNotFound(group) }
// module checks to see if a kernel module is installed func module(parameters []string) (exitCode int, exitMessage string) { // kernelModules returns a list of all modules that are currently loaded // TODO just read from /proc/modules kernelModules := func() (modules []string) { cmd := exec.Command("/sbin/lsmod") return wrkutils.CommandColumnNoHeader(0, cmd) } name := parameters[0] modules := kernelModules() if tabular.StrIn(name, modules) { return 0, "" } return wrkutils.GenericError("Module is not loaded", name, modules) }
func (chk RepoExistsURI) New(params []string) (chkutil.Check, error) { if len(params) != 2 { return chk, errutil.ParameterLengthError{2, params} } re, err := regexp.Compile(params[1]) if err != nil { return chk, errutil.ParameterTypeError{params[1], "regexp"} } chk.re = re if !tabular.StrIn(params[0], keys) { return chk, errutil.ParameterTypeError{params[0], "package manager"} } chk.manager = params[0] return chk, nil }
func (chk PacmanIgnore) Status() (int, string, error) { path := "/etc/pacman.conf" data := chkutil.FileToString(path) re := regexp.MustCompile(`[^#]IgnorePkg\s+=\s+.+`) find := re.FindString(data) var packages []string if find != "" { spl := strings.Split(find, " ") errutil.IndexError("Not enough lines in "+path, 2, spl) packages = spl[2:] // first two are "IgnorePkg" and "=" if tabular.StrIn(chk.pkg, packages) { return errutil.Success() } } msg := "Couldn't find package in IgnorePkg" return errutil.GenericError(msg, chk.pkg, packages) }
func (chk Up) Status() (int, string, error) { // getUpInterfaces returns all the names of the interfaces that are up getUpInterfaces := func() (interfaceNames []string) { for _, iface := range netstatus.GetInterfaces() { if iface.Flags&net.FlagUp != 0 { interfaceNames = append(interfaceNames, iface.Name) } } return interfaceNames } upInterfaces := getUpInterfaces() if tabular.StrIn(chk.name, upInterfaces) { return errutil.Success() } return errutil.GenericError("Interface is not up", chk.name, upInterfaces) }
// pacmanIgnore checks to see whether a given package is in /etc/pacman.conf's // IgnorePkg setting func pacmanIgnore(parameters []string) (exitCode int, exitMessage string) { pkg := parameters[0] path := "/etc/pacman.conf" data := wrkutils.FileToString(path) re := regexp.MustCompile(`[^#]IgnorePkg\s+=\s+.+`) find := re.FindString(data) var packages []string if find != "" { spl := strings.Split(find, " ") wrkutils.IndexError("Not enough lines in "+path, 2, spl) packages = spl[2:] // first two are "IgnorePkg" and "=" if tabular.StrIn(pkg, packages) { return 0, "" } } msg := "Couldn't find package in IgnorePkg" return wrkutils.GenericError(msg, pkg, packages) }
func (chk Checksum) New(params []string) (chkutil.Check, error) { if len(params) != 3 { return chk, errutil.ParameterLengthError{3, params} } valid := []string{"MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512"} if !tabular.StrIn(strings.ToUpper(params[0]), valid) { return chk, errutil.ParameterTypeError{params[0], "algorithm"} } chk.algorithm = params[0] path := params[2] if _, err := os.Stat(path); err != nil { return chk, errutil.ParameterTypeError{path, "filepath"} } chk.path = path // TODO validate length of checksum string chk.expectedChksum = params[1] return chk, nil }
// up determines if a network interface is up and running or not func up(parameters []string) (exitCode int, exitMessage string) { // getUpInterfaces returns all the names of the interfaces that are up getUpInterfaces := func() (interfaceNames []string) { for _, iface := range getInterfaces() { if iface.Flags&net.FlagUp != 0 { interfaceNames = append(interfaceNames, iface.Name) } } return interfaceNames } name := parameters[0] upInterfaces := getUpInterfaces() if tabular.StrIn(name, upInterfaces) { return 0, "" } return wrkutils.GenericError("Interface is not up", name, upInterfaces) }
func (chk Running) Status() (int, string, error) { // getRunningCommands returns the entries in the "Command" column of `ps aux` getRunningCommands := func() (Commands []string) { cmd := exec.Command("ps", "aux") return chkutil.CommandColumnNoHeader(10, cmd) } // remove this process from consideration Commands := getRunningCommands() var filtered []string for _, cmd := range Commands { if !strings.Contains(cmd, "distributive") { filtered = append(filtered, cmd) } } if tabular.StrIn(chk.name, filtered) { return errutil.Success() } return errutil.GenericError("Process not Running", chk.name, filtered) }
// running checks if a process is running using `ps aux`, and searching for the // process name, excluding this process (in case the process name is in the JSON // file name) func running(parameters []string) (exitCode int, exitMessage string) { // getRunningCommands returns the entries in the "COMMAND" column of `ps aux` getRunningCommands := func() (commands []string) { cmd := exec.Command("ps", "aux") return wrkutils.CommandColumnNoHeader(10, cmd) } proc := parameters[0] // remove this process from consideration commands := getRunningCommands() var filtered []string for _, cmd := range commands { if !strings.Contains(cmd, "distributive") { filtered = append(filtered, cmd) } } if tabular.StrIn(proc, filtered) { return 0, "" } return wrkutils.GenericError("Process not running", proc, filtered) }