Commit 1b4f4640 authored by nimrod's avatar nimrod
Browse files

More work on checks.

Not tested so really WIP.
parent e4015042
Loading
Loading
Loading
Loading

pkg/nagios/check.go

0 → 100644
+76 −0
Original line number Diff line number Diff line
/*
Deal with Nagios plugin checks. Mainly running the command and checking the exit
code.
*/
package nagios

import (
	"errors"
	"os/exec"
)

/*
Although exit codes can't be a negative, int is used to match ExitCode in
os.ProcessState.
*/
type ReturnCode int

/*
The agreed upon exit codes that a plugin uses to pass the state of the check.
*/
const (
	OK       ReturnCode = 0
	WARNING             = 1
	CRITICAL            = 2
	UNKNOWN             = 3
)

/*
Represents a check that is going to run or has run.
*/
type Check struct {
	Command string
	Args    []string
	Code    int
	Result  ReturnCode
}

/*
Returns a new Check struct, one that hasn't run yet.
*/
func New(command string, args []string) (*Check, error) {
	if command == "" {
		return nil, errors.New("Empty command.")
	}
	check := new(Check)
	check.Command = command
	check.Args = args
	check.Code = -1
	check.Result = -1
	return check, nil
}

/*
Run a check that hasn't been run yet.
*/
func Run(check Check) error {
	if check.Command == "" {
		return errors.New("Empty command.")
	}
	if check.Code != -1 {
		return errors.New("Command has already run.")
	}
	cmd := exec.Command(check.Command, check.Args...)
	err := cmd.Run()
	if err == nil {
		check.Code = 0
		check.Result = OK
		return nil
	}
	check.Code = cmd.ProcessState.ExitCode()
	if check.Code == WARNING || check.Code == CRITICAL || check.Code == UNKNOWN {
		check.Result = ReturnCode(check.Code)
		return nil
	}
	return err
}

pkg/nagios/nagios.go

deleted100644 → 0
+0 −12
Original line number Diff line number Diff line
package nagios

// Although exit codes can't be a negative, int is used to match
// ExitCode in *os.ProcessState.
type ReturnCode int

const (
	OK       ReturnCode = 0
	WARNING             = 1
	CRITICAL            = 2
	UNKNOWN             = 3
)