nagopher

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: May 29, 2019 License: GPL-3.0 Imports: 11 Imported by: 3

README

nagopher

License Go Compatibility GitHub issues Coverage Status Copyright

Introduction

nagopher is a library written in Go which helps building Nagios plugins in a sane and efficient way. The development was heavily inspired by a Python library called "nagiosplugin" and follows the same concepts.

Copyright © 2018-2019 Pascal Mathis. All rights reserved.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bounds

type Bounds interface {
	fmt.Stringer
	ViolationHint() string

	ToNagiosRange() string
	Match(value float64) bool

	IsInverted() bool
	Lower() optional.Float64
	Upper() optional.Float64
}

Bounds support matching float64 numbers against a given range, optionally inverting the result

func NewBounds

func NewBounds(options ...BoundsOpt) Bounds

NewBounds instantiates Bounds with the given functional options

func NewBoundsFromNagiosRange

func NewBoundsFromNagiosRange(specifier string) (Bounds, error)

NewBoundsFromNagiosRange is a helper method, which constructs a new Bounds object from a Nagios range specifier

func OptionalBoundsPtr

func OptionalBoundsPtr(optionalBounds OptionalBounds) *Bounds

OptionalBoundsPtr takes an optional bounds value and either returns a pointer to it or nil. It is the callers duty to properly handle such nil values!

type BoundsOpt

type BoundsOpt func(*bounds)

BoundsOpt is a type alias for functional options used by NewBounds()

func InvertedBounds

func InvertedBounds(state bool) BoundsOpt

InvertedBounds is a functional option for NewBounds(), which inverts matching of the boundary (inside -> outside)

func LowerBound

func LowerBound(value float64) BoundsOpt

LowerBound is a functional option for NewBounds(), which sets the lower boundary

func NagiosRange

func NagiosRange(specifier string) ([]BoundsOpt, error)

NagiosRange is a functional option for NewBounds(), which parses a Nagios range specifier

func UpperBound

func UpperBound(value float64) BoundsOpt

UpperBound is a functional option for NewBounds(), which sets the upper boundary

type Check

type Check interface {
	Run(warnings WarningCollection)
	SetMeta(key string, value interface{})
	GetMeta(key string, defaultValue interface{}) interface{}
	AttachResources(resources ...Resource)
	AttachContexts(contexts ...Context)

	Name() string
	PerfData() []PerfData
	Contexts() []Context
	Resources() []Resource
	Results() ResultCollection
	State() State
	Summary() string
	VerboseSummary() []string
}

Check collects metrics (results) and performance data, which are being associated to one or more given contexts. Metrics are being collected by executing one or more resources using Resource.Probe(). Additional metadata without a specific type can be added using Check.SetMeta() / Check.GetMeta(). Collected results can be queried by using Check.State(), Check.Summary() or Check.VerboseSummary(), which use the configured summarizer instance.

func NewCheck

func NewCheck(name string, summarizer Summarizer) Check

NewCheck instantiates a new Check object with the given name and summarizer

type CheckResult

type CheckResult interface {
	ExitCode() int8
	Output() string
}

CheckResult contains the results of a Check together with an exit code to indicate the check state

func NewCheckResult

func NewCheckResult(exitCode int8, output string) CheckResult

NewCheckResult instantiates a new CheckResult with the given exit code and output string

type Context

type Context interface {
	Name() string
	Describe(Metric) string
	Evaluate(Metric, Resource) Result
	Performance(Metric, Resource) (OptionalPerfData, error)
}

Context provides methods for further processing a metric to generate results and/or performance data

func NewBaseContext added in v0.1.5

func NewBaseContext(name string, format string) Context

NewBaseContext instantiates a base Context which neither holds any information nor provides any kind of logic. It is meant to be used for developing custom Context types outside of nagopher.

func NewDeltaContext

func NewDeltaContext(name string, previousValue *float64, warningThreshold *Bounds, criticalThreshold *Bounds) Context

NewDeltaContext creates a new scalar Context object, which operates the same way as a ScalarContext, but instead of using the current absolute metric value, it will be compared to a previous measurement. It is the callers duty to provide a pointer to the previous metric value or nil, if not available.

func NewScalarContext

func NewScalarContext(name string, warningThreshold *Bounds, criticalThreshold *Bounds) Context

NewScalarContext creates a new scalar Context object, which handles metrics of the type NumericMetric and provides the ability to constraint these metric values to a given warning and/or critical threshold range

func NewStringInfoContext

func NewStringInfoContext(name string) Context

NewStringInfoContext instantiates a Context which only holds and returns a plain string without any further logic.

func NewStringMatchContext

func NewStringMatchContext(name string, problemState State, expectedValues []string) Context

NewStringMatchContext instantiates a Context which holds a string, which is being compared to a whitelist of acceptable values during the evaluation phase. Should the value not be accepted, a problem state gets returned.

type Metric

type Metric interface {
	ToNagiosValue() string

	Name() string
	ValueUnit() string
	ValueString() string
	ValueRange() OptionalBounds
	ContextName() string
}

Metric stores a value of a given type associated to a specific context, optionally restrained into a specific range. It can be converted to the according representation of that value in context of Nagios plugins.

type NumericMetric

type NumericMetric interface {
	Metric

	Value() float64
}

NumericMetric represents a Metric storing float64 values

func MustNewNumericMetric

func MustNewNumericMetric(name string, value float64, valueUnit string, valueRange *Bounds, contextName string) NumericMetric

MustNewNumericMetric calls MustNewNumericMetric and panics in case the creation of a metric instance fails

func NewNumericMetric

func NewNumericMetric(name string, value float64, valueUnit string, valueRange *Bounds, contextName string) (NumericMetric, error)

NewNumericMetric instantiates a new NumericMetric with the given parameters.

type OptionalBounds

type OptionalBounds struct {
	// contains filtered or unexported fields
}

OptionalBounds is an optional Bounds.

func NewOptionalBounds

func NewOptionalBounds(v Bounds) OptionalBounds

NewOptionalBounds creates an optional.OptionalBounds from a Bounds.

func (OptionalBounds) Get

func (o OptionalBounds) Get() (Bounds, error)

Get returns the Bounds value or an error if not present.

func (OptionalBounds) If

func (o OptionalBounds) If(fn func(Bounds))

If calls the function f with the value if the value is present.

func (OptionalBounds) OrElse

func (o OptionalBounds) OrElse(v Bounds) Bounds

OrElse returns the Bounds value or a default value if the value is not present.

func (OptionalBounds) Present

func (o OptionalBounds) Present() bool

Present returns whether or not the value is present.

func (*OptionalBounds) Set

func (o *OptionalBounds) Set(v Bounds)

Set sets the Bounds value.

type OptionalContext

type OptionalContext struct {
	// contains filtered or unexported fields
}

OptionalContext is an optional Context.

func NewOptionalContext

func NewOptionalContext(v Context) OptionalContext

NewOptionalContext creates an optional.OptionalContext from a Context.

func (OptionalContext) Get

func (o OptionalContext) Get() (Context, error)

Get returns the Context value or an error if not present.

func (OptionalContext) If

func (o OptionalContext) If(fn func(Context))

If calls the function f with the value if the value is present.

func (OptionalContext) OrElse

func (o OptionalContext) OrElse(v Context) Context

OrElse returns the Context value or a default value if the value is not present.

func (OptionalContext) Present

func (o OptionalContext) Present() bool

Present returns whether or not the value is present.

func (*OptionalContext) Set

func (o *OptionalContext) Set(v Context)

Set sets the Context value.

type OptionalMetric

type OptionalMetric struct {
	// contains filtered or unexported fields
}

OptionalMetric is an optional Metric.

func NewOptionalMetric

func NewOptionalMetric(v Metric) OptionalMetric

NewOptionalMetric creates an optional.OptionalMetric from a Metric.

func (OptionalMetric) Get

func (o OptionalMetric) Get() (Metric, error)

Get returns the Metric value or an error if not present.

func (OptionalMetric) If

func (o OptionalMetric) If(fn func(Metric))

If calls the function f with the value if the value is present.

func (OptionalMetric) OrElse

func (o OptionalMetric) OrElse(v Metric) Metric

OrElse returns the Metric value or a default value if the value is not present.

func (OptionalMetric) Present

func (o OptionalMetric) Present() bool

Present returns whether or not the value is present.

func (*OptionalMetric) Set

func (o *OptionalMetric) Set(v Metric)

Set sets the Metric value.

type OptionalPerfData

type OptionalPerfData struct {
	// contains filtered or unexported fields
}

OptionalPerfData is an optional PerfData.

func NewOptionalPerfData

func NewOptionalPerfData(v PerfData) OptionalPerfData

NewOptionalPerfData creates an optional.OptionalPerfData from a PerfData.

func (OptionalPerfData) Get

func (o OptionalPerfData) Get() (PerfData, error)

Get returns the PerfData value or an error if not present.

func (OptionalPerfData) If

func (o OptionalPerfData) If(fn func(PerfData))

If calls the function f with the value if the value is present.

func (OptionalPerfData) OrElse

func (o OptionalPerfData) OrElse(v PerfData) PerfData

OrElse returns the PerfData value or a default value if the value is not present.

func (OptionalPerfData) Present

func (o OptionalPerfData) Present() bool

Present returns whether or not the value is present.

func (*OptionalPerfData) Set

func (o *OptionalPerfData) Set(v PerfData)

Set sets the PerfData value.

type OptionalResource

type OptionalResource struct {
	// contains filtered or unexported fields
}

OptionalResource is an optional Resource.

func NewOptionalResource

func NewOptionalResource(v Resource) OptionalResource

NewOptionalResource creates an optional.OptionalResource from a Resource.

func (OptionalResource) Get

func (o OptionalResource) Get() (Resource, error)

Get returns the Resource value or an error if not present.

func (OptionalResource) If

func (o OptionalResource) If(fn func(Resource))

If calls the function f with the value if the value is present.

func (OptionalResource) OrElse

func (o OptionalResource) OrElse(v Resource) Resource

OrElse returns the Resource value or a default value if the value is not present.

func (OptionalResource) Present

func (o OptionalResource) Present() bool

Present returns whether or not the value is present.

func (*OptionalResource) Set

func (o *OptionalResource) Set(v Resource)

Set sets the Resource value.

type OptionalResult

type OptionalResult struct {
	// contains filtered or unexported fields
}

OptionalResult is an optional Result.

func NewOptionalResult

func NewOptionalResult(v Result) OptionalResult

NewOptionalResult creates an optional.OptionalResult from a Result.

func (OptionalResult) Get

func (o OptionalResult) Get() (Result, error)

Get returns the Result value or an error if not present.

func (OptionalResult) If

func (o OptionalResult) If(fn func(Result))

If calls the function f with the value if the value is present.

func (OptionalResult) OrElse

func (o OptionalResult) OrElse(v Result) Result

OrElse returns the Result value or a default value if the value is not present.

func (OptionalResult) Present

func (o OptionalResult) Present() bool

Present returns whether or not the value is present.

func (*OptionalResult) Set

func (o *OptionalResult) Set(v Result)

Set sets the Result value.

type OptionalState

type OptionalState struct {
	// contains filtered or unexported fields
}

OptionalState is an optional State.

func NewOptionalState

func NewOptionalState(v State) OptionalState

NewOptionalState creates an optional.OptionalState from a State.

func (OptionalState) Get

func (o OptionalState) Get() (State, error)

Get returns the State value or an error if not present.

func (OptionalState) If

func (o OptionalState) If(fn func(State))

If calls the function f with the value if the value is present.

func (OptionalState) OrElse

func (o OptionalState) OrElse(v State) State

OrElse returns the State value or a default value if the value is not present.

func (OptionalState) Present

func (o OptionalState) Present() bool

Present returns whether or not the value is present.

func (*OptionalState) Set

func (o *OptionalState) Set(v State)

Set sets the State value.

type PerfData

type PerfData interface {
	ToNagiosPerfData() string
	Metric() Metric
}

PerfData holds a Metric instance and allows transformation into Nagios performance data

func NewNumericPerfData

func NewNumericPerfData(name string, value float64, valueUnit string, valueRange *Bounds,
	warningThreshold *Bounds, criticalThreshold *Bounds) (PerfData, error)

NewNumericPerfData instantiates new PerfData. The parameters are being used to create a new NumericMetric instance, which is then passed along with the optional thresholds to NewPerfData().

func NewPerfData

func NewPerfData(metric Metric, warningThreshold *Bounds, criticalThreshold *Bounds) (PerfData, error)

NewPerfData instantiates a new PerfData with the given metric and optional thresholds

type Resource

type Resource interface {
	Setup(WarningCollection) error
	Probe(WarningCollection) ([]Metric, error)
	Teardown(WarningCollection) error
}

Resource offers a method for collecting one or more metrics

func NewResource

func NewResource() Resource

NewResource instantiates a new Resource.

type Result

type Result interface {
	fmt.Stringer

	Hint() string
	State() OptionalState
	Metric() OptionalMetric
	Context() OptionalContext
	Resource() OptionalResource
}

Result represents the output of a context, after the evaluation of an associated metric

func NewResult

func NewResult(options ...ResultOpt) Result

NewResult instantiates a new Result with the given functional options

type ResultCollection

type ResultCollection interface {
	Add(results ...Result)
	Get() []Result
	Count() int
	MostSignificantResult() OptionalResult
	MostSignificantState() OptionalState

	GetByMetricName(name string) OptionalResult
	GetMetricByName(name string) OptionalMetric
	GetNumericMetricValue(name string) optional.Float64
	GetStringMetricValue(name string) optional.String
}

ResultCollection contains an arbitrary amount of Result instances and methods to sort them by relevance

func NewResultCollection

func NewResultCollection() ResultCollection

NewResultCollection instantiates a new ResultCollection object without any items

type ResultOpt

type ResultOpt func(*result)

ResultOpt is a type alias for functional options used by NewResult()

func ResultContext

func ResultContext(value Context) ResultOpt

ResultContext is a functional option for NewResult(), which stores the responsible context of the result

func ResultHint

func ResultHint(value string) ResultOpt

ResultHint is a functional option for NewResult(), which stores the hint of the result

func ResultMetric

func ResultMetric(value Metric) ResultOpt

ResultMetric is a functional option for NewResult(), which stores the responsible metric of the result

func ResultResource

func ResultResource(value Resource) ResultOpt

ResultResource is a functional option for NewResult(), which stores the responsible resource of the result

func ResultState

func ResultState(value State) ResultOpt

ResultState is a functional option for NewResult(), which stores the state of the result

type Runtime

type Runtime interface {
	Execute(Check) CheckResult
	ExecuteAndExit(check Check)
}

Runtime executes a specific Check instance and prints or outputs the results according to the Nagios plugin specs

func NewRuntime

func NewRuntime(verboseOutput bool) Runtime

NewRuntime instantiates a new Runtime, optionally enabling verbose output

type State

type State interface {
	ExitCode() int8
	Description() string
}

State represents a Nagios plugin state, which consists of an exit code and description

func StateCritical

func StateCritical() State

StateCritical returns an "CRITICAL" state according to Nagios plugin standards

func StateInfo added in v0.1.6

func StateInfo() State

StateInfo returns an "INFO" state, which is used for verbose output and otherwise behaves as StateOk()

func StateOk

func StateOk() State

StateOk returns an "OK" state according to Nagios plugin standards

func StateUnknown

func StateUnknown() State

StateUnknown returns an "UNKNOWN" state according to Nagios plugin standards

func StateWarning

func StateWarning() State

StateWarning returns an "WARNING" state according to Nagios plugin standards

type StringMetric

type StringMetric interface {
	Metric

	Value() string
}

StringMetric represents a Metric storing string values

func MustNewStringMetric

func MustNewStringMetric(name string, value string, contextName string) StringMetric

MustNewStringMetric calls NewStringMetric and panics in case the creation of a metric instance fails

func NewStringMetric

func NewStringMetric(name string, value string, contextName string) (StringMetric, error)

NewStringMetric instantiates a new StringMetric with the given parameters.

type Summarizer

type Summarizer interface {
	Ok(Check) string
	Problem(Check) string
	Verbose(Check) []string
	Empty() string
}

Summarizer provides methods for displaying a human-readable

func NewSummarizer

func NewSummarizer() Summarizer

NewSummarizer instantiates a new Summarizer.

type Warning

type Warning interface {
	Warning() string
}

Warning represents a single warning cont

func NewWarning

func NewWarning(format string, values ...interface{}) Warning

NewWarning instantiates a new warning, passing the format string and an arbitrary amount of arguments to fmt.Sprintf() for building the string.

type WarningCollection

type WarningCollection interface {
	Add(warnings ...Warning)
	Get() []Warning
	GetWarningStrings() []string
}

WarningCollection collects an arbitrary amount of warnings, which can happen during runtime execution.

func NewWarningCollection

func NewWarningCollection() WarningCollection

NewWarningCollection instantiates a new WarningCollection without any items.

Jump to

Keyboard shortcuts

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