scout

package module
v0.0.0-...-5da07f5 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2020 License: MIT Imports: 21 Imported by: 0

README

scout - Simple checking of http, tcp, udp, connections and icmp checks

GoDoc Build Status Coverage Status Go Report Card

Key Features

  • Ability to monitor multiple services
  • Ability to monitor tcp, udp, http, and icmp
  • Ability to add and remove services for monitoring
  • Ability to specify expected response content and codes
  • Ability to specify check interval and timeouts per service

Get Started

Installation
$ go get github.com/phenixrizen/scout
Example Usage

package main

import (
	"io/ioutil"
	"time"

	"github.com/ghodss/yaml"
	"github.com/sirupsen/logrus"

	"github.com/phenixrizen/scout"
)
[]
func main() {
	log := logrus.New()

	var servs []*scout.Service
	yb, err := ioutil.ReadFile("./services.yml")
	if err != nil {
		logrus.Fatal(err)
	}
	err = yaml.Unmarshal(yb, &servs)
	if err != nil {
		logrus.Fatal(err)
	}

	s := scout.NewScout(servs, log)

	go s.StartScoutingServices()
	go s.HandleResponses()

	for {
		time.Sleep(30 * time.Second)
		for _, serv := range s.Services {
			log.Infof("Service: %s, Address: %s, Type: %s, Online: %t, Last Online: %s, Last Status Code: %d, Latency: %.6fs, Ping Time: %.6fs", serv.Name, serv.Address, serv.Type, serv.Online, serv.LastOnline, serv.LastStatusCode, serv.Latency, serv.PingTime)
		}
	}
}
Example Services YAML
---
- id: 8b3c6416-2578-4418-8cbf-a8424e7ce04d
  name: Google
  address: https://google.com
  expected: ''
  expectedStatus: 200
  checkInterval: 5s
  type: http
  timeout: 5s
- id: 409455e9-c496-4907-8478-34cff2e7b131
  name: Netlify
  address: https://netlify.com
  expected: ''
  expectedStatus: 200
  checkInterval: 3.2s
  type: http
  timeout: 5s
- id: fe727692-bde3-4021-819b-1ceedad4aa27
  name: Netlify
  address: netlify.com
  checkInterval: 1.5s
  type: icmp
  timeout: 150ms
- id: 18f8bcce-ce1e-4f45-b209-c284593b5b94
  name: HP
  address: hp.com
  checkInterval: 1.5s
  type: icmp
  retry: true
  retryMinInterval: 1s
  retryMaxInterval: 10s
  retryMax: 10
  timeout: 150ms
  trace: true

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration time.Duration

Duration is a custom type to use for human readable durations in JSON/YAML

func (Duration) Duration

func (d Duration) Duration() time.Duration

Duration return a time.Duration

func (Duration) MarshalJSON

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

MarshalJSON marshals human redable durations

func (*Duration) UnmarshalJSON

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

UnmarshalJSON unmarshals human redable durations

type HTTPRequestMetrics

type HTTPRequestMetrics struct {
	GetConn              int64
	GotConn              int64
	GotFirstResponseByte int64
	DNSStart             int64
	DNSDone              int64
	ConnectStart         int64
	ConnectDone          int64
	TLSHandshakeStart    int64
	TLSHandshakeDone     int64
	WroteHeaderField     int64
	WroteHeaders         int64
	WroteRequest         int64
	GotResponse          int64
}

func HTTPRequest

func HTTPRequest(ctx context.Context, url, resolveTo, method string, contentType interface{}, headers http.Header, body io.Reader, timeout time.Duration, verifySSL bool) ([]byte, *http.Response, *HTTPRequestMetrics, error)

HTTPRequest is a global function to send a HTTP request

ctx - Context to be used in request
url - The URL for HTTP request
resolveTo - The ip:port of where to resolve to
method - GET, POST, DELETE, PATCH
contentType - The HTTP request content type (text/plain, application/json, or nil)
headers - Headers to be used for the request
body - The body or form data to send with HTTP request
timeout - Specific duration to timeout on. time.Duration(30 * time.Seconds)
verifySSL - verify the SSL certificate
You can use a HTTP Proxy if you HTTP_PROXY environment variable

func (*HTTPRequestMetrics) NetworkLatency

func (m *HTTPRequestMetrics) NetworkLatency() int64

NetworkLatency returns the network connection latency in ms

func (*HTTPRequestMetrics) NetworkLatencyDuration

func (m *HTTPRequestMetrics) NetworkLatencyDuration() time.Duration

NetworkLatencyDuration returns the network connection latency as a Duration

func (*HTTPRequestMetrics) RequestLatency

func (m *HTTPRequestMetrics) RequestLatency() int64

RequestLatency returns the request latency in ms

func (*HTTPRequestMetrics) RequestLatencyDuration

func (m *HTTPRequestMetrics) RequestLatencyDuration() time.Duration

RequestLatencyDuration returns the request latency as a Duration

type Scout

type Scout struct {
	Services  map[uuid.UUID]*Service
	Responses chan interface{}
	Running   bool
	Logger    logrus.FieldLogger
	// contains filtered or unexported fields
}

func NewScout

func NewScout(servs []*Service, log logrus.FieldLogger) *Scout

NewScout returns a scout

func (*Scout) AddService

func (s *Scout) AddService(serv *Service)

AddService adds a service to monitor

func (*Scout) DelService

func (s *Scout) DelService(id uuid.UUID)

DelService adds a service to monitor

func (*Scout) GetResponseChannel

func (s *Scout) GetResponseChannel() chan interface{}

GetResponseChannel returns a interface channel that has either ServiceSuccess or ServiceFailure responses

func (*Scout) GetService

func (s *Scout) GetService(id uuid.UUID) *Service

GetService returns a service

func (*Scout) GetServices

func (s *Scout) GetServices() []*Service

GetServices returns all services

func (*Scout) HandleResponses

func (s *Scout) HandleResponses()

HandleResponses simply logs current responses, this is not intended to be used, but demonatrates scouts usage

func (*Scout) StartScoutingServices

func (s *Scout) StartScoutingServices()

StartScoutingServices will start the checking go routine for each service

func (*Scout) StopScoutingServices

func (s *Scout) StopScoutingServices()

StopScoutingServices will start the checking go routine for each service

type Service

type Service struct {
	ID               uuid.UUID              `json:"id"`
	Name             string                 `json:"name"`
	Address          string                 `json:"address"`
	ResolveTo        string                 `json:"resolveTo"`
	Expected         string                 `json:"expected"`
	ExpectedStatus   int                    `json:"expectedStatus"`
	Interval         Duration               `json:"checkInterval"`
	Type             string                 `json:"type"`
	Method           string                 `json:"method"`
	PostData         string                 `json:"postData"`
	Port             int                    `json:"port"`
	Timeout          Duration               `json:"timeout"`
	VerifySSL        bool                   `json:"verifySSL"`
	Headers          http.Header            `json:"headers"`
	CreatedAt        time.Time              `json:"createdAt"`
	UpdatedAt        time.Time              `json:"updatedAt"`
	Online           bool                   `json:"online"`
	DNSResolve       int64                  `json:"dnsResolve"`
	RequestLatency   int64                  `json:"requestLatency"`
	NetworkLatency   int64                  `json:"networkLatency"`
	Trace            bool                   `json:"trace"`
	TraceData        []traceroute.TraceData `json:"traceData,omitempty"`
	Retry            bool                   `json:"retry"`
	RetryMinInterval Duration               `json:"retryMinInterval"`
	RetryMaxInterval Duration               `json:"retryMaxInterval"`
	RetryMax         int                    `json:"retryMax"`
	RetryAttempts    int                    `json:"-" bson:"-"`
	Running          chan bool              `json:"-" bson:"-"`
	Checkpoint       time.Time              `json:"-" bson:"-"`
	SleepDuration    Duration               `json:"-" bson:"-"`
	LastResponse     string                 `json:"lastResponse"`
	DownText         string                 `json:"downText"`
	LastStatusCode   int                    `json:"statusCode"`
	LastOnline       time.Time              `json:"lastSuccess"`
	Logger           logrus.FieldLogger     `json:"-" bson:"-"`
	Responses        chan interface{}       `json:"-" bson:"-"`
}

Service is the main struct for Services

func (*Service) Check

func (s *Service) Check()

Check will run checkHttp for HTTP services and checkTcp for TCP services

func (*Service) CheckHTTP

func (s *Service) CheckHTTP()

CheckHTTP will check a HTTP service

func (*Service) CheckICMP

func (s *Service) CheckICMP()

CheckICMP will send a ICMP ping packet to the service

func (*Service) CheckNet

func (s *Service) CheckNet()

CheckNet will check a TCP/UDP service

func (*Service) DNSCheck

func (s *Service) DNSCheck() (int64, error)

DNSCheck will check the domain name and return a int64 representing the milliseconds it took to resolve DNS

func (*Service) Failure

func (s *Service) Failure(issue string)

Failure will create a new 'ServiceFailure' record on the Response Channel

func (*Service) Initialize

func (s *Service) Initialize()

Initialize a Service

func (*Service) IsRunning

func (s *Service) IsRunning() bool

IsRunning returns true if the service go routine is running

func (*Service) LinearJitterBackoff

func (s *Service) LinearJitterBackoff()

LinearJitterBackoff will perform linear backoff based on the attempt number and with jitter to prevent a thundering herd. Min and max here are NOT absolute values. The number to be multipled by the attempt number will be chosen at random from between them, thus they are bounding the jitter.

func (*Service) Scout

func (s *Service) Scout()

Scout is the main go routine for checking a service

func (*Service) Start

func (s *Service) Start()

Start will create a channel for use to stop the service checking go routine

func (*Service) Stop

func (s *Service) Stop()

Stop will stop the go routine that is checking if service is online or not

func (*Service) Success

func (s *Service) Success()

Success will create a new 'ServiceSuccess' record on the Response Channel

type ServiceFailure

type ServiceFailure struct {
	Service          uuid.UUID              `json:"service"`
	Issue            string                 `json:"issue"`
	NetworkLatency   int64                  `json:"networkLatency"`
	TraceData        []traceroute.TraceData `json:"traceData,omitempty"`
	RetriesExhausted bool                   `json:"retiresExhausted,omitempty"`
	CreatedAt        time.Time              `json:"createdAt"`
	ErrorCode        int                    `json:"errorCode,omitempty"`
}

type ServiceSuccess

type ServiceSuccess struct {
	Service        uuid.UUID `json:"service"`
	RequestLatency int64     `json:"requestLatency"`
	NetworkLatency int64     `json:"networkLatency"`
	CreatedAt      time.Time `json:"createdAt"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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