Compare commits
4 Commits
master
...
remove-py-
Author | SHA1 | Date | |
---|---|---|---|
cea16606ba | |||
a03f430d0e | |||
f3f7c215a7 | |||
c75302bdb8 |
@ -1,11 +1,11 @@
|
||||
ARG REPO=library
|
||||
FROM ${REPO}/alpine:3.12
|
||||
FROM ${REPO}/alpine:3.17
|
||||
|
||||
RUN mkdir /app
|
||||
WORKDIR /app/
|
||||
|
||||
# Add common checking tools
|
||||
RUN apk --no-cache add bash=~5.0 curl=~7.79 jq=~1.6
|
||||
RUN apk --no-cache add bash=~5 curl=~8 jq=~1.6 bind-tools~=9
|
||||
|
||||
# Add minitor user for running as non-root
|
||||
RUN addgroup -S minitor && adduser -S minitor -G minitor
|
||||
|
@ -14,7 +14,7 @@ ARG VERSION=dev
|
||||
ENV CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH}
|
||||
RUN go build -ldflags "-X main.version=${VERSION}" -a -installsuffix nocgo -o minitor .
|
||||
|
||||
FROM ${REPO}/alpine:3.12
|
||||
FROM ${REPO}/alpine:3.17
|
||||
RUN mkdir /app
|
||||
WORKDIR /app/
|
||||
|
||||
@ -22,7 +22,7 @@ WORKDIR /app/
|
||||
COPY --from=builder /app/minitor .
|
||||
|
||||
# Add common checking tools
|
||||
RUN apk --no-cache add bash=~5.0 curl=~7.79 jq=~1.6
|
||||
RUN apk --no-cache add bash=~5 curl=~8 jq=~1.6 bind-tools~=9
|
||||
|
||||
# Add minitor user for running as non-root
|
||||
RUN addgroup -S minitor && adduser -S minitor -G minitor
|
||||
|
@ -158,7 +158,7 @@ minitor-go:
|
||||
check_interval: 1m30s
|
||||
```
|
||||
|
||||
For the time being, legacy configs for the Python version of Minitor should be compatible if you apply the `-py-compat` flag when running Minitor. Eventually, this flag will go away when later breaking changes are introduced.
|
||||
The `-py-compat` flag has been removed. Any existing Python oriented configuration needs to be migrated to the new templates.
|
||||
|
||||
## Future
|
||||
|
||||
|
19
alert.go
19
alert.go
@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
@ -45,26 +44,12 @@ func (alert Alert) IsValid() bool {
|
||||
|
||||
// BuildTemplates compiles command templates for the Alert
|
||||
func (alert *Alert) BuildTemplates() error {
|
||||
// TODO: Remove legacy template support later after 1.0
|
||||
legacy := strings.NewReplacer(
|
||||
"{alert_count}", "{{.AlertCount}}",
|
||||
"{alert_message}", "{{.MonitorName}} check has failed {{.FailureCount}} times",
|
||||
"{failure_count}", "{{.FailureCount}}",
|
||||
"{last_output}", "{{.LastCheckOutput}}",
|
||||
"{last_success}", "{{.LastSuccess}}",
|
||||
"{monitor_name}", "{{.MonitorName}}",
|
||||
)
|
||||
|
||||
slog.Debugf("Building template for alert %s", alert.Name)
|
||||
|
||||
switch {
|
||||
case alert.commandTemplate == nil && alert.Command.Command != nil:
|
||||
alert.commandTemplate = []*template.Template{}
|
||||
for i, cmdPart := range alert.Command.Command {
|
||||
if PyCompat {
|
||||
cmdPart = legacy.Replace(cmdPart)
|
||||
}
|
||||
|
||||
alert.commandTemplate = append(alert.commandTemplate, template.Must(
|
||||
template.New(alert.Name+fmt.Sprint(i)).Parse(cmdPart),
|
||||
))
|
||||
@ -72,10 +57,6 @@ func (alert *Alert) BuildTemplates() error {
|
||||
case alert.commandShellTemplate == nil && alert.Command.ShellCommand != "":
|
||||
shellCmd := alert.Command.ShellCommand
|
||||
|
||||
if PyCompat {
|
||||
shellCmd = legacy.Replace(shellCmd)
|
||||
}
|
||||
|
||||
alert.commandShellTemplate = template.Must(
|
||||
template.New(alert.Name).Parse(shellCmd),
|
||||
)
|
||||
|
@ -70,14 +70,6 @@ func TestAlertSend(t *testing.T) {
|
||||
"Command shell with bad template",
|
||||
false,
|
||||
},
|
||||
{
|
||||
Alert{Command: CommandOrShell{ShellCommand: "echo {alert_message}"}},
|
||||
AlertNotice{MonitorName: "test", FailureCount: 1},
|
||||
"test check has failed 1 times\n",
|
||||
false,
|
||||
"Command shell with legacy template",
|
||||
true,
|
||||
},
|
||||
// Test default log alert down
|
||||
{
|
||||
*NewLogAlert(),
|
||||
@ -100,8 +92,6 @@ func TestAlertSend(t *testing.T) {
|
||||
|
||||
for _, c := range cases {
|
||||
log.Printf("Testing case %s", c.name)
|
||||
// Set PyCompat to value of compat flag
|
||||
PyCompat = c.pyCompat
|
||||
|
||||
err := c.alert.BuildTemplates()
|
||||
if err != nil {
|
||||
@ -121,9 +111,6 @@ func TestAlertSend(t *testing.T) {
|
||||
log.Printf("Case failed: %s", c.name)
|
||||
}
|
||||
|
||||
// Set PyCompat back to default value
|
||||
PyCompat = false
|
||||
|
||||
log.Println("-----")
|
||||
}
|
||||
}
|
||||
|
50
config.go
50
config.go
@ -13,11 +13,11 @@ var errInvalidConfig = errors.New("Invalid configuration")
|
||||
|
||||
// Config type is contains all provided user configuration
|
||||
type Config struct {
|
||||
CheckInterval SecondsOrDuration `yaml:"check_interval"`
|
||||
DefaultAlertAfter int16 `yaml:"default_alert_after"`
|
||||
DefaultAlertEvery *int16 `yaml:"default_alert_every"`
|
||||
DefaultAlertDown []string `yaml:"default_alert_down"`
|
||||
DefaultAlertUp []string `yaml:"default_alert_up"`
|
||||
CheckInterval time.Duration `yaml:"check_interval"`
|
||||
DefaultAlertAfter int16 `yaml:"default_alert_after"`
|
||||
DefaultAlertEvery *int16 `yaml:"default_alert_every"`
|
||||
DefaultAlertDown []string `yaml:"default_alert_down"`
|
||||
DefaultAlertUp []string `yaml:"default_alert_up"`
|
||||
Monitors []*Monitor
|
||||
Alerts map[string]*Alert
|
||||
}
|
||||
@ -56,34 +56,6 @@ func (cos *CommandOrShell) UnmarshalYAML(unmarshal func(interface{}) error) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// SecondsOrDuration wraps a duration value for parsing a duration or seconds from YAML
|
||||
// NOTE: This should be removed in favor of only parsing durations once compatibility is broken
|
||||
type SecondsOrDuration struct {
|
||||
value time.Duration
|
||||
}
|
||||
|
||||
// Value returns a duration value
|
||||
func (sod SecondsOrDuration) Value() time.Duration {
|
||||
return sod.value
|
||||
}
|
||||
|
||||
// UnmarshalYAML allows unmarshalling a duration value or seconds if an int was provided
|
||||
func (sod *SecondsOrDuration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var seconds int64
|
||||
err := unmarshal(&seconds)
|
||||
|
||||
if err == nil {
|
||||
sod.value = time.Second * time.Duration(seconds)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Error indicates that we don't have an int
|
||||
err = unmarshal(&sod.value)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// IsValid checks config validity and returns true if valid
|
||||
func (config Config) IsValid() (isValid bool) {
|
||||
isValid = true
|
||||
@ -182,18 +154,6 @@ func LoadConfig(filePath string) (config Config, err error) {
|
||||
|
||||
slog.Debugf("Config values:\n%v\n", config)
|
||||
|
||||
// Add log alert if not present
|
||||
if PyCompat {
|
||||
// Initialize alerts list if not present
|
||||
if config.Alerts == nil {
|
||||
config.Alerts = map[string]*Alert{}
|
||||
}
|
||||
|
||||
if _, ok := config.Alerts["log"]; !ok {
|
||||
config.Alerts["log"] = NewLogAlert()
|
||||
}
|
||||
}
|
||||
|
||||
// Finish initializing configuration
|
||||
if err = config.Init(); err != nil {
|
||||
return
|
||||
|
@ -15,7 +15,6 @@ func TestLoadConfig(t *testing.T) {
|
||||
}{
|
||||
{"./test/valid-config.yml", false, "Valid config file", false},
|
||||
{"./test/valid-config-default-values.yml", false, "Valid config file with default values", false},
|
||||
{"./test/valid-default-log-alert.yml", false, "Valid config file with default log alert PyCompat", true},
|
||||
{"./test/valid-default-log-alert.yml", true, "Invalid config file no log alert", false},
|
||||
{"./test/does-not-exist", true, "Invalid config path", false},
|
||||
{"./test/invalid-config-type.yml", true, "Invalid config type for key", false},
|
||||
@ -25,8 +24,6 @@ func TestLoadConfig(t *testing.T) {
|
||||
|
||||
for _, c := range cases {
|
||||
log.Printf("Testing case %s", c.name)
|
||||
// Set PyCompat based on compatibility mode
|
||||
PyCompat = c.pyCompat
|
||||
_, err := LoadConfig(c.configPath)
|
||||
hasErr := (err != nil)
|
||||
|
||||
@ -34,9 +31,6 @@ func TestLoadConfig(t *testing.T) {
|
||||
t.Errorf("LoadConfig(%v), expected_error=%v actual=%v", c.name, c.expectErr, err)
|
||||
log.Printf("Case failed: %s", c.name)
|
||||
}
|
||||
|
||||
// Set PyCompat to default value
|
||||
PyCompat = false
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,15 +47,15 @@ func TestIntervalParsing(t *testing.T) {
|
||||
oneMinute := time.Minute
|
||||
|
||||
// validate top level interval seconds represented as an int
|
||||
if config.CheckInterval.Value() != oneSecond {
|
||||
if config.CheckInterval != oneSecond {
|
||||
t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
||||
}
|
||||
|
||||
if config.Monitors[0].CheckInterval.Value() != tenSeconds {
|
||||
if config.Monitors[0].CheckInterval != tenSeconds {
|
||||
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
||||
}
|
||||
|
||||
if config.Monitors[1].CheckInterval.Value() != oneMinute {
|
||||
if config.Monitors[1].CheckInterval != oneMinute {
|
||||
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
||||
}
|
||||
|
||||
|
6
main.go
6
main.go
@ -17,9 +17,6 @@ var (
|
||||
// Metrics contains all active metrics
|
||||
Metrics = NewMetrics()
|
||||
|
||||
// PyCompat enables support for legacy Python templates
|
||||
PyCompat = false
|
||||
|
||||
// version of minitor being run
|
||||
version = "dev"
|
||||
|
||||
@ -97,7 +94,6 @@ func main() {
|
||||
|
||||
flag.BoolVar(&slog.DebugLevel, "debug", false, "Enables debug logs (default: false)")
|
||||
flag.BoolVar(&ExportMetrics, "metrics", false, "Enables prometheus metrics exporting (default: false)")
|
||||
flag.BoolVar(&PyCompat, "py-compat", false, "Enables support for legacy Python Minitor config. Will eventually be removed. (default: false)")
|
||||
flag.IntVar(&MetricsPort, "metrics-port", MetricsPort, "The port that Prometheus metrics should be exported on, if enabled. (default: 8080)")
|
||||
flag.Parse()
|
||||
|
||||
@ -124,6 +120,6 @@ func main() {
|
||||
err = checkMonitors(&config)
|
||||
slog.OnErrPanicf(err, "Error checking monitors")
|
||||
|
||||
time.Sleep(config.CheckInterval.Value())
|
||||
time.Sleep(config.CheckInterval)
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import (
|
||||
// Monitor represents a particular periodic check of a command
|
||||
type Monitor struct { //nolint:maligned
|
||||
// Config values
|
||||
AlertAfter int16 `yaml:"alert_after"`
|
||||
AlertEvery *int16 `yaml:"alert_every"`
|
||||
CheckInterval SecondsOrDuration `yaml:"check_interval"`
|
||||
AlertAfter int16 `yaml:"alert_after"`
|
||||
AlertEvery *int16 `yaml:"alert_every"`
|
||||
CheckInterval time.Duration `yaml:"check_interval"`
|
||||
Name string
|
||||
AlertDown []string `yaml:"alert_down"`
|
||||
AlertUp []string `yaml:"alert_up"`
|
||||
@ -45,7 +45,7 @@ func (monitor Monitor) ShouldCheck() bool {
|
||||
|
||||
sinceLastCheck := time.Since(monitor.lastCheck)
|
||||
|
||||
return sinceLastCheck >= monitor.CheckInterval.Value()
|
||||
return sinceLastCheck >= monitor.CheckInterval
|
||||
}
|
||||
|
||||
// Check will run the command configured by the Monitor and return a status
|
||||
|
@ -45,9 +45,9 @@ func TestMonitorShouldCheck(t *testing.T) {
|
||||
name string
|
||||
}{
|
||||
{Monitor{}, true, "Empty"},
|
||||
{Monitor{lastCheck: timeNow, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "Just checked"},
|
||||
{Monitor{lastCheck: timeTenSecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "-10s"},
|
||||
{Monitor{lastCheck: timeTwentySecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, true, "-20s"},
|
||||
{Monitor{lastCheck: timeNow, CheckInterval: time.Second * 15}, false, "Just checked"},
|
||||
{Monitor{lastCheck: timeTenSecAgo, CheckInterval: time.Second * 15}, false, "-10s"},
|
||||
{Monitor{lastCheck: timeTwentySecAgo, CheckInterval: time.Second * 15}, true, "-20s"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
|
@ -1,5 +1,5 @@
|
||||
---
|
||||
check_interval: 1
|
||||
check_interval: 1s
|
||||
|
||||
monitors:
|
||||
- name: Command
|
||||
|
Loading…
Reference in New Issue
Block a user