Add date format functions
This commit is contained in:
parent
c02d64d674
commit
41a1dbeceb
20
README.md
20
README.md
@ -94,10 +94,28 @@ Also, when alerts are executed, they will be passed through Go's format function
|
|||||||
|`{{.AlertCount}}`|Number of times this monitor has alerted|
|
|`{{.AlertCount}}`|Number of times this monitor has alerted|
|
||||||
|`{{.FailureCount}}`|The total number of sequential failed checks for this monitor|
|
|`{{.FailureCount}}`|The total number of sequential failed checks for this monitor|
|
||||||
|`{{.LastCheckOutput}}`|The last returned value from the check command to either stderr or stdout|
|
|`{{.LastCheckOutput}}`|The last returned value from the check command to either stderr or stdout|
|
||||||
|`{{.LastSuccess}}`|The ISO datetime of the last successful check|
|
|`{{.LastSuccess}}`|The datetime of the last successful check as a go Time struct|
|
||||||
|`{{.MonitorName}}`|The name of the monitor that failed and triggered the alert|
|
|`{{.MonitorName}}`|The name of the monitor that failed and triggered the alert|
|
||||||
|`{{.IsUp}}`|Indicates if the monitor that is alerting is up or not. Can be used in a conditional message template|
|
|`{{.IsUp}}`|Indicates if the monitor that is alerting is up or not. Can be used in a conditional message template|
|
||||||
|
|
||||||
|
To provide flexible formatting, the following non-standard functions are available in templates:
|
||||||
|
|
||||||
|
|func|description|
|
||||||
|
|---|---|
|
||||||
|
|`ANSIC <Time>`|Formats provided time in ANSIC format|
|
||||||
|
|`UnixDate <Time>`|Formats provided time in UnixDate format|
|
||||||
|
|`RubyDate <Time>`|Formats provided time in RubyDate format|
|
||||||
|
|`RFC822Z <Time>`|Formats provided time in RFC822Z format|
|
||||||
|
|`RFC850 <Time>`|Formats provided time in RFC850 format|
|
||||||
|
|`RFC1123 <Time>`|Formats provided time in RFC1123 format|
|
||||||
|
|`RFC1123Z <Time>`|Formats provided time in RFC1123Z format|
|
||||||
|
|`RFC3339 <Time>`|Formats provided time in RFC3339 format|
|
||||||
|
|`RFC3339Nano <Time>`|Formats provided time in RFC3339Nano format|
|
||||||
|
|`FormatTime <Time> <string template>`|Formats provided time according to provided template|
|
||||||
|
|`InTZ <Time> <string timezone name>`|Converts provided time to parsed timezone from the provided name|
|
||||||
|
|
||||||
|
For more information, check out the [Go documentation for the time module](https://pkg.go.dev/time@go1.20.7#pkg-constants).
|
||||||
|
|
||||||
### Metrics
|
### Metrics
|
||||||
|
|
||||||
Minitor supports exporting metrics for [Prometheus](https://prometheus.io/). Prometheus is an open source tool for reading and querying metrics from different sources. Combined with another tool, [Grafana](https://grafana.com/), it allows building of charts and dashboards. You could also opt to just use Minitor to log check results, and instead do your alerting with Grafana.
|
Minitor supports exporting metrics for [Prometheus](https://prometheus.io/). Prometheus is an open source tool for reading and querying metrics from different sources. Combined with another tool, [Grafana](https://grafana.com/), it allows building of charts and dashboards. You could also opt to just use Minitor to log check results, and instead do your alerting with Grafana.
|
||||||
|
35
alert.go
35
alert.go
@ -57,6 +57,37 @@ func (alert *Alert) BuildTemplates() error {
|
|||||||
|
|
||||||
slog.Debugf("Building template for alert %s", alert.Name)
|
slog.Debugf("Building template for alert %s", alert.Name)
|
||||||
|
|
||||||
|
// Time format func factory
|
||||||
|
tff := func(formatString string) func(time.Time) string {
|
||||||
|
return func(t time.Time) string {
|
||||||
|
return t.Format(formatString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create some functions for formatting datetimes in popular formats
|
||||||
|
timeFormatFuncs := template.FuncMap{
|
||||||
|
"ANSIC": tff(time.ANSIC),
|
||||||
|
"UnixDate": tff(time.UnixDate),
|
||||||
|
"RubyDate": tff(time.RubyDate),
|
||||||
|
"RFC822Z": tff(time.RFC822Z),
|
||||||
|
"RFC850": tff(time.RFC850),
|
||||||
|
"RFC1123": tff(time.RFC1123),
|
||||||
|
"RFC1123Z": tff(time.RFC1123Z),
|
||||||
|
"RFC3339": tff(time.RFC3339),
|
||||||
|
"RFC3339Nano": tff(time.RFC3339Nano),
|
||||||
|
"FormatTime": func(t time.Time, timeFormat string) string {
|
||||||
|
return t.Format(timeFormat)
|
||||||
|
},
|
||||||
|
"InTZ": func(t time.Time, tzName string) (time.Time, error) {
|
||||||
|
tz, err := time.LoadLocation(tzName)
|
||||||
|
if err != nil {
|
||||||
|
return t, fmt.Errorf("failed to convert time to specified tz: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.In(tz), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case alert.commandTemplate == nil && alert.Command.Command != nil:
|
case alert.commandTemplate == nil && alert.Command.Command != nil:
|
||||||
alert.commandTemplate = []*template.Template{}
|
alert.commandTemplate = []*template.Template{}
|
||||||
@ -66,7 +97,7 @@ func (alert *Alert) BuildTemplates() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
alert.commandTemplate = append(alert.commandTemplate, template.Must(
|
alert.commandTemplate = append(alert.commandTemplate, template.Must(
|
||||||
template.New(alert.Name+fmt.Sprint(i)).Parse(cmdPart),
|
template.New(alert.Name+fmt.Sprint(i)).Funcs(timeFormatFuncs).Parse(cmdPart),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
case alert.commandShellTemplate == nil && alert.Command.ShellCommand != "":
|
case alert.commandShellTemplate == nil && alert.Command.ShellCommand != "":
|
||||||
@ -77,7 +108,7 @@ func (alert *Alert) BuildTemplates() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
alert.commandShellTemplate = template.Must(
|
alert.commandShellTemplate = template.Must(
|
||||||
template.New(alert.Name).Parse(shellCmd),
|
template.New(alert.Name).Funcs(timeFormatFuncs).Parse(shellCmd),
|
||||||
)
|
)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("No template provided for alert %s: %w", alert.Name, errNoTemplate)
|
return fmt.Errorf("No template provided for alert %s: %w", alert.Name, errNoTemplate)
|
||||||
|
Loading…
Reference in New Issue
Block a user