Add Monitor.Alerts
continuous-integration/drone/push Build is passing Details

Add the ability to pass a single list of alerts to a monitor that will
be executed on either an up or down alert.

Right now this can be used with a Go template that changes the alert
content depending on the type of alert. Later, I may add a way to pass
two commands to an Alert where one can be for down and the other for
up.
This commit is contained in:
IamTheFij 2020-06-19 22:37:14 -07:00
parent cffbbd734a
commit 89f2849912
3 changed files with 9 additions and 6 deletions

View File

@ -68,6 +68,7 @@ Each monitor allows the following configuration:
|---|---|
|`name`|Name of the monitor running. This will show up in messages and logs.|
|`command`|Specifies the command that should be executed, either in exec or shell form. This command's exit value will determine whether the check is successful|
|`alerts`|A list of Alerts to be triggered when the monitor changes states to either "up" or "down". This is most useful if using a conditional template|
|`alert_down`|A list of Alerts to be triggered when the monitor is in a "down" state|
|`alert_up`|A list of Alerts to be triggered when the monitor moves to an "up" state|
|`check_interval`|The interval at which this monitor should be checked. This must be greater than the global `check_interval` value|
@ -76,15 +77,15 @@ Each monitor allows the following configuration:
### Alerts
Alerts exist as objects keyed under `alerts`. Their key should be the name of the Alert. This is used in your monitor setup in `alert_down` and `alert_up`.
Alerts exist as objects keyed under `alerts`. Their key should be the name of the Alert. This is used in your monitor setup in `alerts`, `alert_down` and `alert_up`.
Eachy alert allows the following configuration:
Each alert allows the following configuration:
|key|value|
|---|---|
|`command`|Specifies the command that should be executed, either in exec or shell form. This is the command that will be run when the alert is executed. This can be templated with environment variables or the variables shown in the table below|
Also, when alerts are executed, they will be passed through Go's format function with arguments for some attributes of the Monitor. The following monitor specific variables can be referenced using Go formatting syntax:
Also, when alerts are executed, they will be passed through Go's templating engine with arguments for some attributes of the Monitor. The following monitor specific variables can be referenced using Go formatting syntax:
|token|value|
|---|---|

View File

@ -12,6 +12,7 @@ type Monitor struct {
// Config values
Name string
Command CommandOrShell
Alerts []string
AlertDown []string `yaml:"alert_down"`
AlertUp []string `yaml:"alert_up"`
CheckInterval float64 `yaml:"check_interval"`
@ -30,7 +31,7 @@ type Monitor struct {
func (monitor Monitor) IsValid() bool {
return (!monitor.Command.Empty() &&
monitor.getAlertAfter() > 0 &&
monitor.AlertDown != nil)
(monitor.Alerts != nil || monitor.AlertDown != nil))
}
// ShouldCheck returns a boolean indicating if the Monitor is ready to be
@ -158,9 +159,9 @@ func (monitor Monitor) getAlertAfter() int16 {
// GetAlertNames gives a list of alert names for a given monitor status
func (monitor Monitor) GetAlertNames(up bool) []string {
if up {
return monitor.AlertUp
return append(monitor.Alerts, monitor.AlertUp...)
}
return monitor.AlertDown
return append(monitor.Alerts, monitor.AlertDown...)
}
func (monitor Monitor) createAlertNotice(isUp bool) *AlertNotice {

View File

@ -18,6 +18,7 @@ func TestMonitorIsValid(t *testing.T) {
{Monitor{Command: CommandOrShell{Command: []string{"echo", "test"}}}, false, "No AlertDown"},
{Monitor{AlertDown: []string{"log"}}, false, "No commands"},
{Monitor{Command: CommandOrShell{Command: []string{"echo", "test"}}, AlertDown: []string{"log"}, AlertAfter: -1}, false, "Invalid alert threshold, -1"},
{Monitor{Command: CommandOrShell{Command: []string{"echo", "test"}}, Alerts: []string{"log"}}, true, "Using non-up/down specific alerts"},
}
for _, c := range cases {