monitor

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxConcurrentChecks = 5

DefaultMaxConcurrentChecks specifies the default maximum number of parallel checks

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration struct {
	time.Duration
}

Duration datatype. Equivalent to time.Duration, but allows us to marshal/unmarshal Entry data structure to/from json

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() (out []byte, err error)

MarshalJSON marshals Duration to bytes

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON unmarshals bytes to a Duration

type Entry

type Entry struct {
	// Spec contains the site's specification. See SiteSpec
	Spec SiteSpec `json:"spec"`
	// State contains the site's state. See SiteState
	State *SiteState `json:"state,omitempty"`
}

The Entry structure holds all information on one host to be checked

type Monitor

type Monitor struct {
	// The Register channel is used to add a host to the monitor
	Register chan SiteSpec
	// The Unregister channel is used to remove a host from the monitor
	Unregister chan SiteSpec
	// HTTPClient is the http.Client that will be used to check sites.
	// Under normal circumstances, this can be left blank and Monitor will create the required client.
	HTTPClient *http.Client
	// MaxConcurrentChecks limits the number of sites that are checked in parallel. Default: DefaultMaxConcurrentChecks
	MaxConcurrentChecks int64
	// contains filtered or unexported fields
}

A Monitor checks a list of website, either on a continuous basis through the Run() function, or on demand via the CheckSites method. See the Entry structure for attributes of a site that are checked.

func New

func New(hosts []string) (monitor *Monitor)

New creates a new Monitor instance for the specified list of sites

func (*Monitor) CheckSites

func (monitor *Monitor) CheckSites(ctx context.Context)

CheckSites checks each site. The site's status isn't reported here, but is kept internally to be scraped by Prometheus using the Collect function.

func (*Monitor) Collect

func (monitor *Monitor) Collect(ch chan<- prometheus.Metric)

Collect implements the prometheus collector Collect interface

func (*Monitor) Describe

func (monitor *Monitor) Describe(ch chan<- *prometheus.Desc)

Describe implements the prometheus collector Describe interface

func (*Monitor) GetEntry

func (monitor *Monitor) GetEntry(url string) (entry Entry, ok bool)

GetEntry returns the monitor's entry for the specified site URL. Should only be used for testing purposes

func (*Monitor) Health

func (monitor *Monitor) Health(w http.ResponseWriter, _ *http.Request)

Health reports back all configured sites and their state

func (*Monitor) Run

func (monitor *Monitor) Run(ctx context.Context, interval time.Duration) (err error)

Run calls CheckSites on a recurring basis, based on the specified interval. To be able to stop this function, call it with a context obtained by context.WithCancel() and then call cancel() when required.

type SiteSpec added in v0.3.4

type SiteSpec struct {
	// URL of the site
	URL string `json:"url"`
	// Name of the site
	Name string `json:"name,omitempty"`
}

A SiteSpec to monitor

type SiteState added in v0.3.4

type SiteState struct {
	// Up indicates if the site is up or down
	Up bool `json:"up"`
	// LastError is the last error received when checking the site
	LastError string `json:"last_error,omitempty"`
	// HTTPCode is the last HTTP Code received when checking the site
	HTTPCode int `json:"http_code,omitempty"`
	// CertificateAge contains the number of days that the site's TLS certificate is still valid
	// IsTLS indicates the site is using encryption (i.e. TLS)
	IsTLS bool `json:"is_tls"`
	// For HTTP sites, this will be zero.
	CertificateAge float64 `json:"certificate_age,omitempty"`
	// Latency contains the time it took to check the site
	Latency Duration `json:"latency,omitempty"`
	// LastCheck is the timestamp the site was last checked. Before there first check, this is zero
	LastCheck time.Time `json:"last_check,omitempty"`
}

The SiteState structure holds the attributes that will be checked

Jump to

Keyboard shortcuts

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