cmd

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2017 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyConfig           = errors.New("the configuration is completely empty (check config file)")
	ErrEmailNoSMTP           = errors.New("no email SMTP server")
	ErrEmailNoTo             = errors.New("no email to addresses")
	ErrEmailNoFrom           = errors.New("no email from addresses")
	ErrEmailNoPass           = errors.New("no email password")
	ErrEmailNoPort           = errors.New("no email port")
	ErrEmailNoSubject        = errors.New("no email subject")
	ErrSlackNoWebHookURL     = errors.New("no slack webhook url")
	ErrNoContainers          = errors.New("there were no containers found in the configuration file")
	ErrExistCheckFail        = errors.New("Existence check failure")
	ErrExistCheckRecovered   = errors.New("Existence check recovered")
	ErrRunningCheckFail      = errors.New("Running check failure")
	ErrRunningCheckRecovered = errors.New("Running check recovered")
	ErrCPUCheckFail          = errors.New("CPU check failure")
	ErrCPUCheckRecovered     = errors.New("CPU check recovered")
	ErrMemCheckFail          = errors.New("Memory check failure")
	ErrMemCheckRecovered     = errors.New("Memory check recovered")
	ErrMinPIDCheckFail       = errors.New("Min PID check Failure")
	ErrMinPIDCheckRecovered  = errors.New("Min PID check recovered")
	ErrMaxPIDCheckFail       = errors.New("Max PID check Failure")
	ErrMaxPIDCheckRecovered  = errors.New("Max PID check recovered")
	ErrUnknown               = errors.New("Received an unknown error")
	ErrPushoverAPIToken      = errors.New("no pushover api token")
	ErrPushoverUserKey       = errors.New("no pushover user key")
	ErrPushoverAPIURL        = errors.New("no pushover api url")
)

these errors are for the purpose of being able to compare them later

View Source
var RootCmd = &cobra.Command{
	Use:   "docker-alertd",
	Short: "docker-alertd: alert daemon for docker engine",
	Long: `docker-alerts parses a configuration file and then monitors containers through
the docker api.`,

	Run: func(cmd *cobra.Command, args []string) {

		err := Config.Validate()
		if err != nil {
			log.Println(err)
			os.Exit(1)
		}

		Start(&Config)
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func CheckContainers

func CheckContainers(cnt []AlertdContainer, cli *client.Client, a *Alert)

CheckContainers goes through and checks all the containers in a loop

func ContainerInspect

func ContainerInspect(a *AlertdContainer, c *client.Client) (*types.ContainerJSON, error)

ContainerInspect returns the information which can decide if the container is current;y running or not.

func ErrContainsErr

func ErrContainsErr(e, b error) bool

ErrContainsErr returns true if the error string contains the message

func Execute

func Execute()

Execute adds all child commands to the root command sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

func GetStats

func GetStats(a *AlertdContainer, c *client.Client) (*types.Stats, error)

GetStats just uses the docker API and an already tested Unmarshal function, no testing needed.

func Monitor

func Monitor(c *Conf, a *Alert)

Monitor contains all the calls for the main loop of the monitor

func Start

func Start(c *Conf)

Start the main monitor loop for a set amount of iterations

Types

type Alert

type Alert struct {
	Messages         []error
	SubjectAddendums []string
}

Alert is the struct that stores information about alerts and its methods satisfy the Alerter interface

func (*Alert) Add

func (a *Alert) Add(e1, e2 error, s, subAddendum string)

Add should take in an error and wrap it

func (*Alert) Clear

func (a *Alert) Clear()

Clear will reset the alert to an empty string

func (*Alert) Concat

func (a *Alert) Concat(b ...*Alert)

Concat will concat different alerts from containers together into one

func (*Alert) Dump

func (a *Alert) Dump() string

Dump takes the slice of alerts and dumps them to a single string

func (*Alert) DumpEmail added in v0.4.4

func (a *Alert) DumpEmail() (s string)

DumpEmail behaves like dump, but formats them for email by splitting on ":" and adding \n\t (newline and tab) for the first two segments and joining the last segment. This should result in an email that is formatted as follows... [containerName]:

[alertName]:
Error: [errString]

func (*Alert) Evaluate

func (a *Alert) Evaluate()

Evaluate will check if error should be sent and then trigger it if necessary

func (*Alert) Len

func (a *Alert) Len() int

Len returns the length of the alert message strings

func (*Alert) Log

func (a *Alert) Log()

Log prints the alert to the log

func (*Alert) Send

func (a *Alert) Send(b []Alerter)

Send is for sending out alerts to syslog and to alerts that are active in conf

func (*Alert) ShouldSend

func (a *Alert) ShouldSend() bool

ShouldSend returns true if there is an alert message to be sent

type AlertdContainer

type AlertdContainer struct {
	Name     string `json:"name"`
	Alert    *Alert
	CPUCheck *MetricCheck
	MemCheck *MetricCheck
	PIDCheck *MetricCheck

	// static checks only below...
	ExistenceCheck *StaticCheck
	RunningCheck   *StaticCheck
}

AlertdContainer has the name of the container and the StaticChecks, and MetricChecks which are to be run on the container.

func InitCheckers

func InitCheckers(c *Conf) []AlertdContainer

InitCheckers returns a slice of containers with all the info needed to run a check on the container. Active is for whether or not the alert is active, not the check

func (*AlertdContainer) CheckCPUUsage

func (c *AlertdContainer) CheckCPUUsage(s *types.Stats)

CheckCPUUsage takes care of sending the alerts if they are needed

func (*AlertdContainer) CheckExists

func (c *AlertdContainer) CheckExists(e error)

CheckExists checks that the container exists, running or not

func (*AlertdContainer) CheckMemory

func (c *AlertdContainer) CheckMemory(s *types.Stats)

CheckMemory checks the memory used by the container in MB, returns true if an error should be sent as well as the actual memory usage

func (*AlertdContainer) CheckMetrics

func (c *AlertdContainer) CheckMetrics(s *types.Stats, e error)

CheckMetrics checks everything where the Limit is not 0, there is no return because the checks modify the error in AlertdContainer

func (*AlertdContainer) CheckMinPids

func (c *AlertdContainer) CheckMinPids(s *types.Stats)

CheckMinPids uses the min pids setting and check the number of PIDS in the container returns true if alerts should be sent, and also returns the amount of running pids.

func (*AlertdContainer) CheckRunning

func (c *AlertdContainer) CheckRunning(j *types.ContainerJSON)

CheckRunning will check to see if the container is currently running or not

func (*AlertdContainer) CheckStatics

func (c *AlertdContainer) CheckStatics(j *types.ContainerJSON, e error)

CheckStatics will run all of the static checks that are listed for a container.

func (*AlertdContainer) ChecksShouldStop

func (c *AlertdContainer) ChecksShouldStop() bool

ChecksShouldStop returns whether the checks should stop after the static checks or continue onto the metric checks.

func (*AlertdContainer) HasBecomeKnown

func (c *AlertdContainer) HasBecomeKnown(e error) bool

HasBecomeKnown returns true if there is an active alert and error is nil, which means that the container check was successful and the 0 index check (existence check) cannot be active

func (*AlertdContainer) HasErrored

func (c *AlertdContainer) HasErrored(e error) bool

HasErrored returns true when the error is something other than `isUnknownContainer` which means that docker-alertd probably crashed.

func (*AlertdContainer) IsUnknown

func (c *AlertdContainer) IsUnknown(err error) bool

IsUnknown is a check that takes the error when the docker API is polled, it mathes part of the error string that is returned.

func (*AlertdContainer) MemUsageMB

func (c *AlertdContainer) MemUsageMB(s *types.Stats) uint64

MemUsageMB returns the memory usage in MB

func (*AlertdContainer) RealCPUUsage

func (c *AlertdContainer) RealCPUUsage(s *types.Stats) uint64

RealCPUUsage calculates the CPU usage based on the ContainerJSON info

func (*AlertdContainer) ShouldAlertCPU

func (c *AlertdContainer) ShouldAlertCPU(u uint64) bool

ShouldAlertCPU returns true if the limit is breached

func (*AlertdContainer) ShouldAlertMemory

func (c *AlertdContainer) ShouldAlertMemory(s *types.Stats) bool

ShouldAlertMemory returns whether the memory limit has been exceeded

func (*AlertdContainer) ShouldAlertMinPIDS

func (c *AlertdContainer) ShouldAlertMinPIDS(s *types.Stats) bool

ShouldAlertMinPIDS returns true if the minPID check fails

func (*AlertdContainer) ShouldAlertRunning

func (c *AlertdContainer) ShouldAlertRunning(j *types.ContainerJSON) bool

ShouldAlertRunning returns whether the running state is as expected

type Alerter

type Alerter interface {
	Valid() error
	Alert(a *Alert) error
}

Alerter is the interface which will handle alerting via different methods such as email and twitter/slack

type AlerterStub added in v0.3.2

type AlerterStub struct {
	ShouldPrint bool
	Bytes       []byte
}

AlerterStub stores the bytes needed for the alerter stub in the config file as well as a boolean value which is evaluated only if the user has supplied specific alerter stubs to include

type Checker

type Checker interface {
	CPUCheck(s *types.Stats)
	MemCheck(s *types.Stats)
	PIDCheck(s *types.Stats)
	ExistenceCheck(a *types.ContainerJSON, e error)
	RunningCheck(a *types.ContainerJSON, e error)
}

Checker interface has all of the methods necessary to check a container

type Conf

type Conf struct {
	Containers []Container
	Email      Email
	Slack      Slack
	Pushover   Pushover
	Iterations uint64
	Duration   uint64
	Alerters   []Alerter
}

Conf struct that combines containers and email settings structs

var (

	// Config is the configuration object defined in conf-types file
	Config Conf
)

func (*Conf) Validate

func (c *Conf) Validate() error

Validate validates the configuration that was passed in

func (*Conf) ValidateEmailSettings

func (c *Conf) ValidateEmailSettings() error

ValidateEmailSettings calls valid on the Email settings and adds them to the alerters if everything is ok

func (*Conf) ValidatePushoverSettings added in v0.3.2

func (c *Conf) ValidatePushoverSettings() error

ValidatePushoverSettings validates pushover settings and adds it to the alerters

func (*Conf) ValidateSlackSettings

func (c *Conf) ValidateSlackSettings() error

ValidateSlackSettings validates slack settings and adds it to the alerters

type Container

type Container struct {
	Name            string
	MaxCPU          *uint64
	MaxMem          *uint64
	MinProcs        *uint64
	ExpectedRunning *bool
}

Container gets data from the Unmarshaling of the configuration file JSON and stores the data throughout the course of the monitor.

type Email

type Email struct {
	SMTP     string
	Password string
	Port     string
	From     string
	To       []string
	Subject  string
}

Email implements the Alerter interface and sends emails

func (Email) Alert

func (e Email) Alert(a *Alert) error

Alert sends an email alert

func (Email) Valid

func (e Email) Valid() error

Valid returns true if the email settings are complete

type Evaluator

type Evaluator interface {
	Send(a []Alerter)
	Evaluate()
}

Evaluator evaluates a set of alerts and decides if they need to be sent

type MetricCheck

type MetricCheck struct {
	AlertActive bool
	Limit       *uint64
}

MetricCheck stores the name of the alert, a function, and a active boolean

func (*MetricCheck) ToggleAlertActive

func (c *MetricCheck) ToggleAlertActive()

ToggleAlertActive changes the state of the alert

type Pushover added in v0.3.2

type Pushover struct {
	APIToken string
	UserKey  string
	APIURL   string
}

Pushover contains all info needed to push a notification to Pushover api

func (Pushover) Alert added in v0.3.2

func (p Pushover) Alert(a *Alert) error

Alert sends the alert to Pushover API

func (Pushover) Valid added in v0.3.2

func (p Pushover) Valid() error

Valid returns an error if pushover settings are invalid

type Slack

type Slack struct {
	WebhookURL string
}

Slack contains all the info needed to connect to a slack channel

func (Slack) Alert

func (s Slack) Alert(a *Alert) error

Alert sends the alert to a slack channel

func (Slack) Valid

func (s Slack) Valid() error

Valid returns an error if slack settings are invalid

type StaticCheck

type StaticCheck struct {
	AlertActive bool
	Expected    *bool
}

StaticCheck checks the container for some static thing that is not based on usage statistics, like its existence, whether it is running or not, etc.

func (*StaticCheck) ToggleAlertActive

func (c *StaticCheck) ToggleAlertActive()

ToggleAlertActive changes the state of the alert

Jump to

Keyboard shortcuts

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