check

package module
v0.0.0-...-38296d8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2020 License: GPL-2.0 Imports: 12 Imported by: 0

README

go-check

GoDoc Test Status GitHub

go-check is a library to help with development of monitoring plugins for tools like Icinga.

Usage

import "github.com/NETWAYS/go-check"

License

Copyright (c) 2020 NETWAYS GmbH

This library is distributed under the GPL-2.0 or newer license found in the COPYING file.

Documentation

Index

Examples

Constants

View Source
const (
	// OK means everything is fine
	OK = 0
	// Warning means there is a problem the admin should review
	Warning = 1
	// Critical means there is a problem that requires immediate action
	Critical = 2
	// Unknown means the status can not be determined, probably due to an error or something missing
	Unknown = 3
)

Variables

View Source
var AllowExit = true

AllowExit lets you disable the call to os.Exit() in ExitXxx() functions of this package.

This should be used carefully and most likely only for testing.

View Source
var PrintStack = true

PrintStack prints the error stack when recovering from a panic with CatchPanic()

Functions

func BaseExit

func BaseExit(rc int)

func CatchPanic

func CatchPanic()

CatchPanic is a general function for defer, to capture any panic that occurred during runtime of a check

The function will recover from the condition and exit with a proper UNKNOWN status, while showing error and the call stack.

Example
defer CatchPanic()

panic("something bad happened")
Output:

UNKNOWN - Golang encountered a panic: something bad happened
would exit with code 3

func DumpBenchmark

func DumpBenchmark()

Dump the Benchmark to stdout

func DumpBenchmarkWhen

func DumpBenchmarkWhen(criteria bool)

Dump the Benchmark when criteria is met, to be used with defer and a boolean variable

func Exit

func Exit(rc int, output string, args ...interface{})

Exit prints the plugin output and exits the program

Example
Exit(OK, "Everything is fine")
Output:

OK - Everything is fine
would exit with code 0

func ExitError

func ExitError(err error)

ExitError exists with an Unknown state while reporting the error

TODO: more information about the error

Example
err := fmt.Errorf("connection to %s has been timed out", "localhost:12345")
ExitError(err)
Output:

UNKNOWN - connection to localhost:12345 has been timed out
would exit with code 3

func HandleTimeout

func HandleTimeout(timeout int)

Helper for a goroutine, to wait for signals and timeout, and exit with a proper code

func InitBenchmark

func InitBenchmark(message string)

Initialize a Benchmark for the running program as static instance

Example

noinspection GoBoolExpressions

debug := true /* flags.Debug */
if debug {
	InitBenchmark("Start plugin")
	defer DumpBenchmarkWhen(debug /* flags.Debug */)
}

time.Sleep(1 * time.Second)
RecordBenchmark("Connecting to service")
time.Sleep(2 * time.Second)
RecordBenchmark("Connection open")
Output:

func RecordBenchmark

func RecordBenchmark(message string)

Recording timing and memory for the current program

func StatusText

func StatusText(status int) string

StatusText returns the string corresponding to a state

Example
fmt.Println(StatusText(OK), StatusText(Warning), StatusText(Critical), StatusText(Unknown))
Output:

OK WARNING CRITICAL UNKNOWN

Types

type Benchmark

type Benchmark struct {
	Events []*BenchmarkEvent
}

Benchmark records multiple events and provides functionality to dump the recorded events

var ActiveBenchmark *Benchmark

func NewBenchmark

func NewBenchmark(message string) *Benchmark

NewBenchmark starts a new benchmark and records the message as event

Example
bench := NewBenchmark("Start plugin")
defer bench.DumpWhen(true /* flags.Debug */)

time.Sleep(1 * time.Second)
bench.Record("Connecting to service")
time.Sleep(2 * time.Second)
bench.Record("Connection open")
Output:

func (*Benchmark) Dump

func (b *Benchmark) Dump()

Dump the benchmark to stdout

func (*Benchmark) DumpWhen

func (b *Benchmark) DumpWhen(criteria bool)

Dump the benchmark when a criteria is met

Makes it simple to defer dumping by adding a boolean reference from a variable.

func (*Benchmark) Record

func (b *Benchmark) Record(message string)

Record the current step of execution

Use a short description, so that one knows at what stage the program is at. Time and offset will be calculated automatically.

type BenchmarkEvent

type BenchmarkEvent struct {
	Time       *time.Time
	Offset     *time.Duration
	Message    string
	TotalAlloc uint64
	HeapAlloc  uint64
}

BenchmarkEvent represents a single event during a benchmark with the time it occurred

type Config

type Config struct {
	Name          string
	Readme        string
	Version       string
	Timeout       int
	Verbose       bool
	Debug         bool
	PrintVersion  bool
	DefaultFlags  bool
	DefaultHelper bool
	FlagSet       *flag.FlagSet
}
Example
config := NewConfig()
config.Name = "check_test"
config.Readme = `Test Plugin`
config.Version = "1.0.0"

_ = config.FlagSet.StringP("hostname", "H", "localhost", "Hostname to check")

os.Args = []string{"check_example", "--help"}
config.ParseArguments()

log.Info("test")
Output:

Usage of check_test

Test Plugin

Arguments:
  -H, --hostname string   Hostname to check (default "localhost")
  -t, --timeout int       Abort the check after n seconds (default 30)
  -d, --debug             Enable debug mode
  -v, --verbose           Enable verbose mode
  -V, --version           Print version and exit
would exit with code 3

func NewConfig

func NewConfig() *Config

func (*Config) EnableTimeoutHandler

func (c *Config) EnableTimeoutHandler()

Start the timeout and signal handler in a goroutine

func (*Config) ParseArguments

func (c *Config) ParseArguments()

func (*Config) ParseArray

func (c *Config) ParseArray(arguments []string)

func (*Config) SetupLogging

func (c *Config) SetupLogging()

Directories

Path Synopsis
cmd
result tries to
result tries to

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL