monitor

package
v0.0.0-...-9c1f1a2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2017 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Monitor

type Monitor struct {
	// The scheduler object used to perform regular availability checks.
	Scheduler *Scheduler
	// Interface to the status storage. By default Monitor will use in-memory
	// internal storage (SimpleStore), but you might want to use some other
	// backend like Redis with RedisStore.
	StatusStore StatusStore
	// Expiration time for status values in the store.
	ExpirationTime time.Duration
	// If this flag describes how the monitor reacts to new statuses.
	// A new status is a status of a target, which has not been checked out into
	// status store yet.
	// If this flag is set to false, monitor will NOT send new OK statuses
	// into Updates chanel, but will send new error statuses.
	// If this flag is set to true, monitor will send any new status to Updates.
	NotifyFirstOK bool
	// Channel by which the monitor will send all status changes.
	// Whenever a type of a target's status (Status.Type) changes, monitor will
	// send the target and its _new_ status into this channel.
	// If the caller ignores this channel and does not read values from it,
	// the monitor (and its scheduler) will clobber and stop doing status checks.
	Updates chan TargetStatus
	// contains filtered or unexported fields
}

Monitor is a wrapper around Scheduler, which saves statuses into a StatusStore and sends status changes into Updates channel.

func New

func New(targets TargetsGetter) *Monitor

New creates a Monitor with given targets getter and default field values.

func (*Monitor) Errors

func (m *Monitor) Errors() chan error

Errors returns a channel through which the monitor will send all errors, appearing in the process of its work. The caller may ignore this channel, its optional. It may be useful to log the monitor errors.

func (*Monitor) Run

func (m *Monitor) Run(ctx context.Context)

Run starts the monitoring in the foreground. Call this method in another goroutine if you want to do background monitoring. If `ctx` is not nil, the monitor will listen to ctx.Done() and stop monitoring when it recieves the signal.

type Poller

type Poller struct {
	// Timeout of network request.
	Timeout time.Duration
	// How many times should poller repeat the request if all previous ones
	// ended in timeout.
	TimeoutRetries int
}

Poller makes HTTP request to some URL to return its availability status.

func NewPoller

func NewPoller() *Poller

NewPoller constructs a new Poller with default fields.

func (*Poller) PollService

func (p *Poller) PollService(url string) Status

PollService makes HTTP GET request to the URL and returns its availability status. If there was an error during request, the returned Status structure will contain information about the error.

type RedisOptions

type RedisOptions struct {
	Host     string
	Port     uint
	Password string
	DB       int
}

RedisOptions contains options for connecting to a redis instance.

type RedisStore

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

RedisStore is an implementation of StatusStore, which uses Redis as storage backend.

func NewRedisStore

func NewRedisStore(opt RedisOptions) *RedisStore

NewRedisStore constructs a new RedisStore, which connect to the redis instance, pointed by `opt`.

func (*RedisStore) GetStatus

func (rs *RedisStore) GetStatus(t Target) (Status, bool, error)

GetStatus attempts to retrieve target's status from redis. If status is not set, it will return ok=false, err=nil.

func (*RedisStore) Ping

func (rs *RedisStore) Ping() error

Ping checks redis instance for availability by issuing `PING` command. If redis is available, it returns nil.

func (*RedisStore) Scan

func (rs *RedisStore) Scan() ([]TargetStatus, error)

Scan searches redis db for stored statuses using `SCAN` command and returns a list of all found items.

func (*RedisStore) SetStatus

func (rs *RedisStore) SetStatus(t Target, s Status, exp time.Duration) error

SetStatus stores the target's status into redis with given expiration time.

type Scheduler

type Scheduler struct {
	// The Poller object which will be used to do polling.
	Poller *Poller
	// Source of lists of targets.
	Targets TargetsGetter
	// Time interval between targets polling.
	Interval time.Duration
	// Maximum number of parallel http requests.
	ParallelPolls uint
	// The channel into which the scheduler will write the polling results.
	Statuses chan TargetStatus
	// contains filtered or unexported fields
}

Scheduler is an object which performs availability polling once every interval and writes the availability statuses into a channel.

func NewScheduler

func NewScheduler(targets TargetsGetter) *Scheduler

NewScheduler constructs a new Scheduler with given TargetsGetter and default fields values.

func (*Scheduler) Errors

func (s *Scheduler) Errors() chan error

Errors retruns a channel through which the Scheduler will send all errors, appearing in the process.

func (*Scheduler) PollTargets

func (s *Scheduler) PollTargets()

PollTargets does single cycle of targets polling in foreground, which includes: - getting the targets list from s.Targets; - polling each target with s.Poller; - writing results into s.Statuses channel. If the s.Targets returns an error, which method will no perform polling and will attempt to send the error to s.Errors channel, if it's not nil.

func (*Scheduler) Run

func (s *Scheduler) Run(context context.Context)

Run start infinite looop of targets polling in foreground. Call this method in a goroutine to do polling in background. If the context argument is not nil, the scheduler will stop the loop when it receives a signal from context.Done().

type SimpleStore

type SimpleStore map[uint]simpleStoreRecord

SimpleStore is the basic implementation of StatusStore which uses a map as a storage backend.

func (SimpleStore) GetStatus

func (ss SimpleStore) GetStatus(t Target) (Status, bool, error)

GetStatus returns status of a target if it's set and not expired.

func (SimpleStore) SetStatus

func (ss SimpleStore) SetStatus(t Target, s Status, exp time.Duration) error

SetStatus saves the status of a target and makes it expire after `exp` amount of time.

type Status

type Status struct {
	Type StatusType
	// The exact error value, if Type is not StatusOK.
	// If Type is StatusOK it is nil.
	Err error
	// Time spent for the request.
	ResponseTime time.Duration
	// If HTTP response was not received, it's set to 0.
	HTTPStatusCode int
}

Status contains information about service availability.

func (Status) ExpandedString

func (s Status) ExpandedString() string

ExpandedString returns a multi-line string, describing contents of the status object.

func (Status) String

func (s Status) String() string

type StatusStore

type StatusStore interface {
	GetStatus(t Target) (Status, bool, error)
	SetStatus(t Target, s Status, exp time.Duration) error
}

StatusStore is an interface of storage of availability statuses. Standart implementation of this interface (RedisStore) uses Redis as backend, but some user may reimplement interface to store data in a RDB or in memory.

type StatusType

type StatusType uint

StatusType describes availability status as one enumerable value.

const (
	// StatusOK - the service is available.
	StatusOK StatusType = iota
	// StatusGenericError - some generic network error.
	StatusGenericError
	// StatusTimeout - HTTP request timed out.
	StatusTimeout
	// StatusURLParsingError - request URL is invalid.
	StatusURLParsingError
	// StatusDNSLookupError - could not resolve domain name.
	StatusDNSLookupError
	// StatusHTTPError - the service returned non-successfull HTTP status code.
	StatusHTTPError
)

func ScanStatusType

func ScanStatusType(s string) (StatusType, bool)

ScanStatusType tries to parse StatusType by strictly comparing it to the string representation of each StatusType variant.

func ScanStatusTypeSoft

func ScanStatusTypeSoft(s string) (StatusType, bool)

ScanStatusTypeSoft is similar to ScanStatusType, but on comparing it converts both strings to lower case and does not check for strict equiality, but checks if StatusType's string starts with input string. This method is more error-tolerant and is intended to use with user input.

func (StatusType) String

func (st StatusType) String() string

type Target

type Target struct {
	// Unique identifier of this target. Targets' IDs cannot intercept. Target's
	// ID must be constant between GetTargets() calls.
	ID uint
	// User-supplied target title, used purely for display.
	Title string
	// The HTTP URL to poll.
	URL string
}

Target is a URL, which has to be polled for availability.

func (Target) String

func (t Target) String() string

type TargetStatus

type TargetStatus struct {
	Target Target
	Status Status
}

TargetStatus simply connects target and its status in one structure.

func (TargetStatus) String

func (ts TargetStatus) String() string

type TargetsGetter

type TargetsGetter interface {
	GetTargets() ([]Target, error)
}

TargetsGetter is an interface of targets source. Monitor uses it to retrieve list targets on every polling iteration. External frontend may implement this interface to store targets in a DB or in a configuration file.

type TargetsSlice

type TargetsSlice []Target

TargetsSlice - the most basic implementation on TargetsGetter.

func NewTargetsSliceFromUrls

func NewTargetsSliceFromUrls(urls []string) TargetsSlice

NewTargetsSliceFromUrls constructs TargetsSlice from a list of urls. Each target is given an ID equal to the url's index, and the title of format "Target N".

func (TargetsSlice) GetTargets

func (ts TargetsSlice) GetTargets() ([]Target, error)

GetTargets implements TargetsGetter for TargetsSlice.

Directories

Path Synopsis
bin

Jump to

Keyboard shortcuts

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