check

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check struct {
	// Id should be any unique value for this check.
	Id string

	// Schedule determines when this Check is due to be executed.
	Schedule Schedule

	// Command is the command this check runs against the service or host.
	// Examples are snmp, ping, dns, or http commands
	Command Command

	// Meta is a key/value store of any extra data you want to live with the
	// check.
	Meta map[string]any

	// Incident needs to be the current active incident for this check
	// or else nil.
	Incident *Incident

	// SuppressIncidents set to true means when this Check executes and
	// produces an Incident, it discards it.
	SuppressIncidents bool

	// Handlers is a slice of handlers to execute after the Check's Command runs.
	// A Handler has a Mutate() and Process() method for mutating a Check's data
	// and processing it, respectfully.  Mutate() methods are called first in
	// sequential order as specified in this slice.  Then Process() methods are
	// called asynchronously.
	Handlers []Handler

	// LastCheck is a time.Time of the last time this Check executed. This will
	// be updated automatically by Execute(), but be sure it's set to the
	// correct time when loading a check from an external database.
	LastCheck *time.Time

	// LastResult is a Result from the last time this Check executed (or nil).
	// This will be updated automatically by Execute(), but be sure it's set to
	// the correct Result when loading a check from an external database.
	//
	// LastResult could be used by the Command to determine value deltas.  If
	// you are certain this is not required in your case, then this could always
	// be nil.
	LastResult *Result

	// Executed is true when the Check has had Execute() called on it.  You should
	// set this back to false prior to queueing it again.
	Executed bool
	// contains filtered or unexported fields
}

Check defines a service or a host to be checked with a given Command and at a given Schedule.

func New

func New(id string, options ...Option) *Check

New creates a new Check with the provided Options.

func (*Check) Debugf added in v0.0.19

func (c *Check) Debugf(format string, args ...any)

Debugf should be used liberally by Commands and Handlers to provide debugging information.

func (*Check) DueAt

func (c *Check) DueAt() time.Time

DueAt returns the time when check is due (could be past or future)

func (*Check) Execute

func (c *Check) Execute() error

Execute executes a Check's Command followed by its Handlers. It then sets the Incident (if there is one), LastCheck and LastResult fields on the Check.

func (*Check) IsDue

func (c *Check) IsDue() bool

IsDue returns true if the check is due for execution

func (*Check) SetDebugLogger added in v0.0.18

func (c *Check) SetDebugLogger(logger debugLogger)

type Command

type Command interface {
	Run(*Check) (*Result, error)
}

Command is a simple interface with a Run(Check) method that returns a Result and error.

type Handler

type Handler interface {
	// Mutate allows the handler to mutate any data in the Check, Result or
	// Incident prior to Process()ing it.  Mutate() is called sequentially in
	// the order the Handlers are defined on the Check.
	Mutate(check *Check, newResult *Result, newIncident *Incident)

	// Process executes asynchronously and should not mutate data.
	Process(check *Check, newResult *Result, newIncident *Incident) error
}

Handler mutates and/or processes a Check after it has executed. Mutate() is called first and sequentially in the order defined in the Check. This allows the second mutation to see the first mutations, etc. Process() is called asynchronously and should never mutate data.

type Incident

type Incident struct {
	Id           uuid.UUID
	FromState    ResultState
	ToState      ResultState
	ReasonCode   string
	Time         time.Time
	Resolved     *time.Time
	Acknowledged *time.Time
}

Incident defines a Check that has undergone a non-OK state change.

func MakeIncidentFromResults

func MakeIncidentFromResults(lastResult *Result, currentResult *Result) *Incident

MakeIncidentFromResults creates a new Incident based on a Check last Result, and it's current Result.

func (*Incident) Acknowledge

func (i *Incident) Acknowledge()

Acknowledge sets the Incident to acknowledged at the current time.

func (*Incident) IsAcknowledged

func (i *Incident) IsAcknowledged() bool

IsAcknowledged returns true if incident has been acknowledged.

func (*Incident) IsResolved

func (i *Incident) IsResolved() bool

IsResolved returns true if incident has been resolved.

func (*Incident) Resolve

func (i *Incident) Resolve()

Resolve sets the Incident to resolved at the current time.

type Option

type Option func(*Check)

func WithCommand

func WithCommand(cmd Command) Option

func WithDebugLogger added in v0.0.17

func WithDebugLogger(logger debugLogger) Option

func WithHandlers

func WithHandlers(handlers []Handler) Option

func WithMeta

func WithMeta(meta map[string]any) Option

func WithPeriodicSchedule

func WithPeriodicSchedule(intervalSeconds int) Option

func WithSchedule

func WithSchedule(schedule Schedule) Option

func WithSuppressedIncidents

func WithSuppressedIncidents() Option

type PeriodicSchedule

type PeriodicSchedule struct {
	IntervalSeconds int
}

PeriodicSchedule is a simple Scheduler that is due every IntervalSeconds seconds

func (PeriodicSchedule) DueAt

func (s PeriodicSchedule) DueAt(check *Check) time.Time

type Queue

type Queue interface {
	Enqueue(chk *Check)
	Dequeue() *Check
	Count() uint64
	Flush()
}

Queue is used by a server.Server to feed it work (Checks to execute).

type Result

type Result struct {
	Id         uuid.UUID
	State      ResultState
	ReasonCode string
	Metrics    []ResultMetric
	Time       time.Time
}

Result contains the state, reason, metrics and time of a check.Command.

func MakeUnknownResult

func MakeUnknownResult(reasonCode string) *Result

MakeUnknownResult creates a new unknown Result with the given reason code. Unknown states are common during Command errors where a definitive state cannot be determined.

func NewResult

func NewResult(state ResultState, reasonCode string, metrics []ResultMetric) *Result

NewResult creates a new Result with the provided attributes and the time set to now.

type ResultMetric

type ResultMetric struct {
	// Label is an identifier for the metric (ex. avg_rtt_ms, temperature_f).
	Label string

	// Value is the metric's numeric value.  It is a string so that it can hold
	// any number, and it is up to the user to convert this into whatever
	// numerical type they need during processing.
	Value string

	// Type is the type of metric Value is.  Can be ResultMetricCounter or
	// ResultMetricGauge.
	Type ResultMetricType
}

ResultMetric is a metric that lives in a Result and was produced by a Command. For example, an HTTP check may have a "resp_time" Gauge metric that measured how long it took to get the HTTP response from an endpoint. Another example is an SNMP check that has an "ifHCInOctets" Counter metric that defines the inbound bandwidth utilization of an interface.

type ResultMetricType

type ResultMetricType uint8

ResultMetricType represents a type of metric. Currently, it can be either a counter or a gauge.

const (
	// ResultMetricCounter is an ever-incrementing integer that rolls over at
	// its inherent integer size and restarts to 0.
	ResultMetricCounter ResultMetricType = 1
	// ResultMetricGauge is any numeric value from some point in time.
	ResultMetricGauge ResultMetricType = 2
)

type ResultState

type ResultState uint8

ResultState represents the state that a Check result is.

const (
	StateOk      ResultState = 0
	StateWarn    ResultState = 1
	StateCrit    ResultState = 2
	StateUnknown ResultState = 3
)

func NewResultStateFromString

func NewResultStateFromString(state string) ResultState

func (ResultState) Overrides added in v0.1.10

func (s ResultState) Overrides(z ResultState) bool

Overrides returns true if s should take the place of z. For example, if s is WARN and z is CRIT, then z is a "worse" result and thus should take its place.

func (ResultState) String

func (s ResultState) String() string

type Schedule

type Schedule interface {
	// DueAt returns a time.Time of the exact point in time the Check will
	// be next due.
	DueAt(*Check) time.Time
}

Schedule is used by a Check to provide its execution schedule.

Directories

Path Synopsis
command
dns
junsubpool
Package junsubpool provides a Check command that monitors the IP pool usage on a Juniper BNG.
Package junsubpool provides a Check command that monitors the IP pool usage on a Juniper BNG.
handler

Jump to

Keyboard shortcuts

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