monitor

package
v0.2.11 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package monitor provides the tools to monitor a set of system properties, for example, services

Index

Constants

View Source
const CheckMaxStartTries = 5

CheckMaxStartTries defines a global default value for how many failed start attempts are made before automatically unmonitoring a check

Variables

This section is empty.

Functions

func CheckOnce

func CheckOnce(c interface {
	Checkable
}, opts Opts) bool

CheckOnce calls the provided check Perform operation once, ignoring the call if a previous call is still in process

func NewClient

func NewClient(socket string) interface {
	ChecksManager
}

NewClient returns a new monitor Client instance using the provided socket to connect to a previously running daemon

Types

type Checkable

type Checkable interface {
	GetID() string
	GetUniqueID() string
	GetTimeout() time.Duration
	Perform()
	SetMonitored(bool)
	IsMonitored() bool
	Parse(data string)
	String() string
	Initialize(Opts)
	SummaryText() string
}

Checkable defines the interface the every Check must provide

type CheckableProcess

type CheckableProcess interface {
	Checkable
	Start() error
	Stop() error
	Restart() error
	Status() string
	IsRunning() bool
	IsNotRunning() bool
	Uptime() time.Duration
	Pid() int
}

CheckableProcess defines the interface the every process Check (type service) must provide

type ChecksDatabase

type ChecksDatabase struct {
	database.Storer
}

ChecksDatabase defines a storage for saving process check information

func NewDatabase

func NewDatabase(file string) (*ChecksDatabase, error)

NewDatabase returns a new database storage for checks information

func (*ChecksDatabase) AddEntry

func (cd *ChecksDatabase) AddEntry(id string) *ChecksDatabaseEntry

AddEntry adds a check entry to the database

func (*ChecksDatabase) GetEntry

func (cd *ChecksDatabase) GetEntry(id string) *ChecksDatabaseEntry

GetEntry returns an entry from the database

type ChecksDatabaseEntry

type ChecksDatabaseEntry struct {
	ID              string
	Monitored       bool
	DataCollectedAt time.Time
	Uptime          time.Duration
	// contains filtered or unexported fields
}

ChecksDatabaseEntry defines a check entry in the monitor database

type ChecksManager

type ChecksManager interface {
	//	Uptime() time.Duration
	// 	UpdateDatabase() error
	//	Reload() error
	Monitor(id string) error
	Unmonitor(id string) error
	Start(id string) error
	Stop(id string) error
	Restart(id string) error
	MonitorAll() []error
	UnmonitorAll() []error
	StartAll() []error
	StopAll() []error
	RestartAll() []error
	SummaryText(args ...string) string
	StatusText(args ...string) string
}

ChecksManager defines the interface provided by objects being able to manipulate checks

type Client

type Client struct {
	Socket string

	Error error
	// contains filtered or unexported fields
}

Client allows connection to an existing monitor via a UNIX socket and use it through the same API as when directly using the monitor The main difference is that service management call don't block

func (*Client) Monitor

func (c *Client) Monitor(id string) error

Monitor looks for the Check with the provide id and set its monitored status to true

func (*Client) MonitorAll

func (c *Client) MonitorAll() (errors []error)

MonitorAll set all checks monitored status to true

func (*Client) Restart

func (c *Client) Restart(id string) error

Restart allows restarting a process check by ID

func (*Client) RestartAll

func (c *Client) RestartAll() (errors []error)

RestartAll allows restarting all process checks

func (*Client) Start

func (c *Client) Start(id string) error

Start allows starting a process check by ID

func (*Client) StartAll

func (c *Client) StartAll() (errors []error)

StartAll allows starting all process checks

func (*Client) StatusText

func (c *Client) StatusText(args ...string) string

StatusText returns a string containing a long description of all checks and Monitor attributes

func (*Client) Stop

func (c *Client) Stop(id string) error

Stop allows stopping a process check by ID

func (*Client) StopAll

func (c *Client) StopAll() (errors []error)

StopAll allows stopping all process checks

func (*Client) SummaryText

func (c *Client) SummaryText(args ...string) string

SummaryText returns a string containing a short status summary for every check registered

func (*Client) Unmonitor

func (c *Client) Unmonitor(id string) error

Unmonitor looks for the Check with the provide id and set its monitored status to false

func (*Client) UnmonitorAll

func (c *Client) UnmonitorAll() (errors []error)

UnmonitorAll set all checks monitored status to false

type Command

type Command struct {
	// Cmd contains the actual commad to call
	Cmd string
	// Timeout defines the time to wait for the command to trigger
	// a state change in the check (for example, running to stopped after calling stop)
	Timeout time.Duration
	// contains filtered or unexported fields
}

Command defines a check command to execute

func (*Command) Exec

func (c *Command) Exec()

Exec performs the actual command execution

type Config

type Config struct {
	ControlFile     string
	Verbose         bool
	ShouldDaemonize bool
	CheckInterval   time.Duration
	PidFile         string
	SocketFile      string
	StateFile       string
	LogFile         string
}

Config reprosents the basic configuration settings supported by the monitor

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	MDebugf(format string, args ...interface{})
}

Logger defines the required interface to support to be able to log messages

type Monitor

type Monitor struct {
	// Pid is the Pid of the running monitor process
	Pid int
	// PidFile contains the path in which the Monitor PID is stored
	PidFile string
	// LogFile constains the path to the Monitor log file
	LogFile string
	// StartTime is the moment in which the monitor was started
	StartTime time.Time
	// ControlFile points to the configuration file used to load the checks to perform
	ControlFile string
	// CheckInterval configures the interval between checks
	CheckInterval time.Duration
	// SocketFile contains the path to he listening Unix domain socket when the HTTP server is enabled
	SocketFile string
	// contains filtered or unexported fields
}

Monitor represents an instance of the monitor application

func New

func New(c Config) (*Monitor, error)

New returns a new Monitor instance

func (*Monitor) AddCheck

func (m *Monitor) AddCheck(c interface {
	Checkable
}) error

AddCheck registers a new Check in the Monitor. It will return an error if an existing check is already registered with the same id

func (*Monitor) FindCheck

func (m *Monitor) FindCheck(id string) interface {
	Checkable
}

FindCheck looks for a registered Check by id

func (*Monitor) HTTPServerSupported

func (m *Monitor) HTTPServerSupported() bool

HTTPServerSupported return wether the HTTP interface can be enabled or not

func (*Monitor) LastCheck

func (m *Monitor) LastCheck() time.Time

LastCheck return the time in which the last monitor check was performed

func (*Monitor) LoopForever

func (m *Monitor) LoopForever(finish chan bool)

LoopForever allows performing all registerd checks in a loop

func (*Monitor) Monitor

func (m *Monitor) Monitor(id string) error

Monitor looks for the Check with the provide id and set its monitored status to true

func (*Monitor) MonitorAll

func (m *Monitor) MonitorAll() (errors []error)

MonitorAll set all checks monitored status to true

func (*Monitor) Perform

func (m *Monitor) Perform()

Perform calls the Perform method for all managed checks currently monitored

func (*Monitor) Reload

func (m *Monitor) Reload() error

Reload makes the running app re-parse the configuration file, updating the set of monitored checks

func (*Monitor) Restart

func (m *Monitor) Restart(id string) error

Restart allows restarting a process check by ID

func (*Monitor) RestartAll

func (m *Monitor) RestartAll() []error

RestartAll allows restarting all process checks

func (*Monitor) RuntimeDebugStats

func (m *Monitor) RuntimeDebugStats() string

RuntimeDebugStats returns a summary text with currently running Go Routines and memory consume

func (*Monitor) Start

func (m *Monitor) Start(id string) error

Start allows starting a process check by ID

func (*Monitor) StartAll

func (m *Monitor) StartAll() []error

StartAll allows starting all process checks

func (*Monitor) StartServer

func (m *Monitor) StartServer() error

StartServer starts the HTTP intterface

func (*Monitor) StatusText

func (m *Monitor) StatusText(args ...string) string

StatusText returns a string containing a long description of all checks and Monitor attributes

func (*Monitor) Stop

func (m *Monitor) Stop(id string) error

Stop allows stopping a process check by ID

func (*Monitor) StopAll

func (m *Monitor) StopAll() []error

StopAll allows stopping all process checks

func (*Monitor) SummaryText

func (m *Monitor) SummaryText(args ...string) string

SummaryText returns a string containing a short status summary for every check registered

func (*Monitor) Terminate

func (m *Monitor) Terminate() (err error)

Terminate allows cleaning quitting a monitor (ie. stopping the HTTP server)

func (*Monitor) Unmonitor

func (m *Monitor) Unmonitor(id string) error

Unmonitor looks for the Check with the provide id and set its monitored status to false

func (*Monitor) UnmonitorAll

func (m *Monitor) UnmonitorAll() (errors []error)

UnmonitorAll set all checks monitored status to false

func (*Monitor) UpdateDatabase

func (m *Monitor) UpdateDatabase() error

UpdateDatabase updates the file database with the in-memory state

func (*Monitor) Uptime

func (m *Monitor) Uptime() time.Duration

Uptime returns for how long the monitor have been running

type Opts

type Opts struct {
	// Logger allows customizing the logger to used
	Logger Logger
}

Opts defines a set of common configuration options for many of the package functions

type ProcessCheck

type ProcessCheck struct {
	Group        string
	PidFile      string
	StartProgram *Command
	StopProgram  *Command
	// contains filtered or unexported fields
}

ProcessCheck defines a service type check

func (ProcessCheck) GetID

func (c ProcessCheck) GetID() string

GetID returns the check ID

func (ProcessCheck) GetTimeout

func (c ProcessCheck) GetTimeout() time.Duration

GetTimeout returns the check Timeout

func (ProcessCheck) GetUniqueID

func (c ProcessCheck) GetUniqueID() string

GetUniqueID returns a globally unique ide for the check. Two instances of the same check with the same ID will have different UniqueId

func (*ProcessCheck) Initialize

func (c *ProcessCheck) Initialize(opts Opts)

Initialize fills up any unconfigured process attributes, for example, the logger

func (ProcessCheck) IsMonitored

func (c ProcessCheck) IsMonitored() bool

func (*ProcessCheck) IsNotRunning

func (c *ProcessCheck) IsNotRunning() bool

IsNotRunning returns true if the process is not running

func (*ProcessCheck) IsRunning

func (c *ProcessCheck) IsRunning() bool

IsRunning returns true if the process is running

func (*ProcessCheck) Parse

func (c *ProcessCheck) Parse(data string)

Parse reads a string containing a monit-like process configuration text and loads the specified settings

func (*ProcessCheck) Perform

func (c *ProcessCheck) Perform()

Perform makes the process check execute its default task. In case of process type checks, calling its start command and waiting for the status to change to "running"

func (*ProcessCheck) Pid

func (c *ProcessCheck) Pid() int

Pid returns the pid of the process by reading its pid file. It will return -1 in case of no pid file found or it is malformed

func (*ProcessCheck) Restart

func (c *ProcessCheck) Restart() (err error)

Restart restarts tge service by calling its stop and restart commands and waiting for the checck to be in running status

func (ProcessCheck) SetMonitored

func (c ProcessCheck) SetMonitored(monitored bool)

func (*ProcessCheck) Start

func (c *ProcessCheck) Start() error

Start starts the process by calling its start command and waiting for the checck to be in running status

func (*ProcessCheck) Status

func (c *ProcessCheck) Status() (str string)

Status returns a string specifying the process status ("running" or "stopped")

func (*ProcessCheck) Stop

func (c *ProcessCheck) Stop() error

Stop stops the process by calling its stop command and waiting for the checck to be in stopped status

func (*ProcessCheck) String

func (c *ProcessCheck) String() string

String returns a string representation for the process check

func (*ProcessCheck) SummaryText

func (c *ProcessCheck) SummaryText() string

SummaryText returns a string the a short summary of the check status: Process id monitored

func (*ProcessCheck) Uptime

func (c *ProcessCheck) Uptime() time.Duration

Uptime returns for how long the process have been running

Jump to

Keyboard shortcuts

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