From 89f28499122719a6713ba509f2606fa48c98a91f Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Fri, 19 Jun 2020 22:37:14 -0700 Subject: [PATCH] Add Monitor.Alerts 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. --- README.md | 7 ++++--- monitor.go | 7 ++++--- monitor_test.go | 1 + 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index aac11d0..f4d1aa1 100644 --- a/README.md +++ b/README.md @@ -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| |---|---| diff --git a/monitor.go b/monitor.go index 764b788..3fb8fac 100644 --- a/monitor.go +++ b/monitor.go @@ -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 { diff --git a/monitor_test.go b/monitor_test.go index d5a1b1b..9aa26c9 100644 --- a/monitor_test.go +++ b/monitor_test.go @@ -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 {