gochecks

package module
v0.0.0-...-ee372bf Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2021 License: MIT Imports: 19 Imported by: 0

README

gochecks

Build Status GoDoc

gochecks package provides utilities to check services health and publish events. It is written fully in Go.

It includes:

  • checks scheduler

  • add checks results

  • various checks:

    • Tcp port
    • ICMP/Ping
    • http
    • snmp get
    • rabbitmq queue len
    • Arris C4 CMTS temp
    • JunOS devices cpu usage and temp
    • MySQL connectivity
    • Jenkins jobs status
  • Publishers:

  • riemann

  • RabbitMQ / AMQP

Install

go get github.com/aleasoluciones/gochecks

Sample code

Create a Checks Engine with two publisher (riemman and log)

checkEngine := gochecks.NewCheckEngine([]gochecks.CheckPublisher{
    gochecks.NewRiemannPublisher("127.0.0.1:5555"),
    gochecks.NewLogPublisher(),
})

Add a periodic (20 seconds) http check with up to three retries, tagging the result as production and adding some attributes.

checkEngine.AddCheck(
    gochecks.NewHttpChecker("golang", "http", "http://www.golang.org", 200).
      Attributes(map[string]string{"version": "1", "network": "google"}).
      Tags("production").
      Retry(3, 1*time.Second),
    20 * time.Second)

Development

To pass the integration tests you need to execute a MySQL server, a Postgres server and a RabbitMQ Server and export the corresponding vars.

The directory dev/ offers two scripts and an environment file to achieve this easily:

source dev/env_develop
dev/start_gochecks_dependencies.sh
go test -tags integration -v ./...
dev/stop_gochecks_dependencies.sh

Todo

  • Metric for all the checks

Documentation

Overview

Package gochecks provide a monitoring checks engine to create or embbed small monitoring system that can publish the periodic check result to other monitoring plataforms

Index

Constants

This section is empty.

Variables

View Source
var DefaultSnmpCheckConf = SnmpCheckerConf{
	// contains filtered or unexported fields
}

DefaultSnmpCheckConf default values for snmp conection parameters

Functions

func Amqp2CheckEngine

func Amqp2CheckEngine(checkEngine *CheckEngine, brokerURI, exchange, topic, queue string, queueOptions *simpleamqp.QueueOptions)

Amqp2CheckEngine continously add check result from a queue (generated using RabbitMqPublisher) to a given CheckEngine

func CriticalIfError

func CriticalIfError(value float32, err error) (string, string)

CriticalIfError returns state critical when error, ok otherwise

Types

type CalculateStateFunction

type CalculateStateFunction func(float32, error) (string, string)

CalculateStateFunction function that given a metric and error generate the corresponding state value and description

type ChannelPublisher

type ChannelPublisher struct {
	Channel chan Event
}

ChannelPublisher object to publish to a channel each check result

func NewChannelPublisher

func NewChannelPublisher(c chan Event) ChannelPublisher

NewChannelPublisher return a new ChannelPublisher

func (ChannelPublisher) PublishCheckResult

func (p ChannelPublisher) PublishCheckResult(event Event)

PublishCheckResult send the event to the publisher channel

type CheckEngine

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

CheckEngine monitoring check engine to schedule periodics checks and publish the results

func NewCheckEngine

func NewCheckEngine(publishers []CheckPublisher) *CheckEngine

NewCheckEngine return a CheckEngine that publish the results of the periodic checks to the given publishers

func (*CheckEngine) AddCheck

func (ce *CheckEngine) AddCheck(check CheckFunction, period time.Duration)

AddCheck schedule a new check to be executed with the given period

func (*CheckEngine) AddMultiCheck

func (ce *CheckEngine) AddMultiCheck(check MultiCheckFunction, period time.Duration)

AddMultiCheck schedule a new multi check to be executed with the given period the muli check can return an array of events/results

func (*CheckEngine) AddResult

func (ce *CheckEngine) AddResult(event Event)

AddResult publish the given check result as if it was generated by a scheduled check

func (*CheckEngine) SetFilter

func (ce *CheckEngine) SetFilter(f EventFilterFunction)

type CheckFunction

type CheckFunction func() Event

CheckFunction type for a function that return a event

func NewC4CMTSTempChecker

func NewC4CMTSTempChecker(host, service, ip, community string, maxAllowedTemp int) CheckFunction

NewC4CMTSTempChecker returns a check function that check if any of the slot of a Arris C4 CMTS have a temperature above a given max

func NewGenericCheck

func NewGenericCheck(host, service string, metricFunc ObtainMetricFunction, stateFunc CalculateStateFunction) CheckFunction

NewGenericCheck returns a check function that invoke a given function to obtain a metric (metricFunc) and invoke another function (stateFunc) to calculate the resulting state and description from this metric value

func NewGenericHTTPChecker

func NewGenericHTTPChecker(host, service, url string, validationFunc ValidateHTTPResponseFunction) CheckFunction

NewGenericHTTPChecker returns a check function that can check the returned http response of a http get with a given validation function

func NewHTTPChecker

func NewHTTPChecker(host, service, url string, expectedStatusCode int) CheckFunction

NewHTTPChecker returns a check function that get a given url and validate if the return code is the expected one

func NewHeartbeatCheck

func NewHeartbeatCheck(host, service string) CheckFunction

NewHeartbeatCheck returns a check function which returns a Event

func NewJenkinsJobsChecker

func NewJenkinsJobsChecker(host, service, jenkinsBaseURL string, jobRegExp string) CheckFunction

NewJenkinsJobsChecker returns a check function that validate that the jenkins api is accesible and all the jenkins jobs matching the given jobRegExp are ok (blue color). When one or more of these jobs are in error the event is critical and the jobs names are included at the event description

func NewJuniperCPUChecker

func NewJuniperCPUChecker(host, service, ip, community string, maxAllowedCPUPercent uint) CheckFunction

NewJuniperCPUChecker returns a check function that check if a Juniper device (router, switch, etc) have a any cpu usage above a given percent

func NewJuniperTempChecker

func NewJuniperTempChecker(host, service, ip, community string, maxAllowedTemp uint) CheckFunction

NewJuniperTempChecker returns a check function that check if a Juniper device (router, switch, etc) have a temperature above a given max

func NewMysqlConnectionCheck

func NewMysqlConnectionCheck(host, service, mysqluri string) CheckFunction

NewMysqlConnectionCheck returns a check function to detect connection/credentials problems to connect to mysql

func NewPingChecker

func NewPingChecker(host, service, ip string) CheckFunction

NewPingChecker returns a check function that can check if a host answer to a ICMP Ping

func NewPostgresConnectionCheck

func NewPostgresConnectionCheck(host, service, postgresuri string) CheckFunction

NewPostgresConnectionCheck returns a check function to detect connection/credentials problems to connect to postgres

func NewRabbitMQQueueLenCheck

func NewRabbitMQQueueLenCheck(host, service, amqpuri, queue string, max int) CheckFunction

NewRabbitMQQueueLenCheck returns a check function that check if queue have more pending messages than a given limit

func NewRabbitMQQueueListLenCheck

func NewRabbitMQQueueListLenCheck(host, service, amqpuri string, queues []string, max int) CheckFunction

func NewSentryUnresolvedIssuesChecker

func NewSentryUnresolvedIssuesChecker(host, service, sentryBaseUrl, projectName string) CheckFunction

func NewSnmpChecker

func NewSnmpChecker(host, service, ip, community string, conf SnmpCheckerConf) CheckFunction

NewSnmpChecker returns a check function that check if a host respond to a snmp get query

func NewTCPPortChecker

func NewTCPPortChecker(host, service, ip string, port int, timeout time.Duration) CheckFunction

NewTCPPortChecker returns a check function that can check if a host have a tcp port open

func (CheckFunction) Attributes

func (f CheckFunction) Attributes(attributes map[string]string) CheckFunction

Attributes returns a new check function that adds the attributes map to the result generated by the initial check function

func (CheckFunction) CriticalIfGreaterThan

func (f CheckFunction) CriticalIfGreaterThan(threshold float32) CheckFunction

CriticalIfGreaterThan returns a new check function that change the state to "critical" when the resulting metric is greater than a threadshold and is not already "critical"

func (CheckFunction) CriticalIfLessThan

func (f CheckFunction) CriticalIfLessThan(threshold float32) CheckFunction

CriticalIfLessThan returns a new check function that change the state to "critical" when the resulting metric is less than a threadshold and is not already "critical"

func (CheckFunction) Retry

func (f CheckFunction) Retry(times int, sleep time.Duration) CheckFunction

Retry returns a new check function that execute the given function up to a given retry times or until the first execution that returns a ok (whichever comes first). The new function will return the event of the last execution

func (CheckFunction) TTL

func (f CheckFunction) TTL(ttl float32) CheckFunction

TTL returns a new check function that adds the given TTL time (in seconds) to the result generated by the initial check function

func (CheckFunction) Tags

func (f CheckFunction) Tags(tags ...string) CheckFunction

Tags returns a new check function that adds the given tags to the result generated by the initial check function

func (CheckFunction) WarningIfGreaterThan

func (f CheckFunction) WarningIfGreaterThan(threshold float32) CheckFunction

WarningIfGreaterThan returns a new check function that change the state to "warning" when the resulting metric is greater than a threadshold and is not already "critical"

func (CheckFunction) WarningIfLessThan

func (f CheckFunction) WarningIfLessThan(threshold float32) CheckFunction

WarningIfLessThan returns a new check function that change the state to "warning" when the resulting metric is less than a threadshold and is not already "critical"

type CheckPublisher

type CheckPublisher interface {
	PublishCheckResult(Event)
}

CheckPublisher define the check result Publisher

type Event

type Event struct {
	Host        string
	Service     string
	State       string
	Metric      interface{}
	Description string
	Tags        []string
	Attributes  map[string]string
	TTL         float32
}

Event is the check result, very inspired and compatible with Riemann events

func NoopEventFilter

func NoopEventFilter(event Event) (bool, Event)

type EventFilterFunction

type EventFilterFunction func(event Event) (bool, Event)

type JobStatus

type JobStatus struct {
	Name  string `json:"name"`
	Color string `json:"color"`
}

JobStatus jenkins job status info

type JobsMessage

type JobsMessage struct {
	Jobs []JobStatus `json:"jobs"`
}

JobsMessage jenkins jobs statuses

type LogPublisher

type LogPublisher struct{}

LogPublisher object to log each check result

func NewLogPublisher

func NewLogPublisher() LogPublisher

NewLogPublisher return a new LogPublisher

func (LogPublisher) PublishCheckResult

func (p LogPublisher) PublishCheckResult(event Event)

PublishCheckResult log the event

type MultiCheckFunction

type MultiCheckFunction func() []Event

MultiCheckFunction type for a function that return an array of events

type ObtainMetricFunction

type ObtainMetricFunction func() (float32, error)

ObtainMetricFunction function that return a metric value or error

type RabbitMqPublisher

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

RabbitMqPublisher object to publish the event to a rabbitmq exchange

func NewRabbitMqPublisher

func NewRabbitMqPublisher(amqpuri, exchange string) RabbitMqPublisher

NewRabbitMqPublisher return a publisher to send to RabbitMq exchange

func (RabbitMqPublisher) PublishCheckResult

func (p RabbitMqPublisher) PublishCheckResult(event Event)

PublishCheckResult publish the event to a configured rabbitmq exchange

type RiemannPublisher

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

RiemannPublisher object to publish events to a riemann server

func NewRiemannPublisher

func NewRiemannPublisher(addr string) RiemannPublisher

NewRiemannPublisher return a publisher to send directly to a riemann instance

func (RiemannPublisher) PublishCheckResult

func (p RiemannPublisher) PublishCheckResult(event Event)

PublishCheckResult return a publisher to send directly to a riemann instance

type SnmpCheckerConf

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

SnmpCheckerConf snmp connection parameters to use for the check

type ValidateContentFunction

type ValidateContentFunction func(content string) (state, description string)

ValidateContentFunctio function type that should validate a content (usualy a http body) and return the state (ok, critical, warning) and error description for a check.

type ValidateHTTPResponseFunction

type ValidateHTTPResponseFunction func(resp *http.Response) (state, description string)

ValidateHTTPResponseFunction function type that should validate a http response and return the state (ok, critical, warning) and error description for a check. (Used with NewGenericHTTPChecker)

func BodyGreaterThan

func BodyGreaterThan(minLength int) ValidateHTTPResponseFunction

BodyGreaterThan return a function that given a http response return true if the body is greater than a given number of bytes

func BodyValidation

func BodyValidation(bodyValidationFunc ValidateContentFunction) ValidateHTTPResponseFunction

BodyValidation return a ValidationHTTPResponseFunction that check the body of a http response using the given bodyValidationFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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