From 7cac6c94d7fc8961a9e8fbb45fd044e27b7714a7 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Fri, 15 Nov 2024 12:05:17 -0800 Subject: [PATCH] Update readme and update some test files to be better examples --- Makefile | 2 +- README.md | 96 +++++++++++++------------------- config_test.go | 93 +++++++++++++++---------------- sample-config.hcl | 50 +++++++++-------- sample-config.yml | 42 -------------- test/valid-config.hcl | 10 ++-- test/valid-verify-multi-line.hcl | 12 ++-- 7 files changed, 125 insertions(+), 180 deletions(-) delete mode 100644 sample-config.yml diff --git a/Makefile b/Makefile index b9423e7..29d9856 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,7 @@ docker-build: .PHONY: docker-run docker-run: docker-build - docker run --rm -v $(shell pwd)/config.yml:/root/config.yml $(DOCKER_TAG) + docker run --rm -v $(shell pwd)/sample-config.hcl:/root/config.hcl $(DOCKER_TAG) ## Multi-arch targets $(TARGETS): $(GOFILES) diff --git a/README.md b/README.md index a53e80c..9a39203 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A minimal monitoring system ## What does it do? -Minitor accepts a YAML configuration file with a set of commands to run and a set of alerts to execute when those commands fail. It is designed to be as simple as possible and relies on other command line tools to do checks and issue alerts. +Minitor accepts an HCL configuration file with a set of commands to run and a set of alerts to execute when those commands fail. Minitor has a narow feature set and instead follows a principle to outsource to other command line tools when possible. Thus, it relies on other command line tools to do checks and issue alerts. To make getting started a bit easier, Minitor includes a few scripts to help with common tasks. ## But why? @@ -17,7 +17,7 @@ I'm running a few small services and found Sensu, Consul, Nagios, etc. to all be Install and execute with: ```bash -go get github.com/iamthefij/minitor-go +go install github.com/iamthefij/minitor-go@latest minitor ``` @@ -27,7 +27,7 @@ If locally developing you can use: make run ``` -It will read the contents of `config.yml` and begin its loop. You could also run it directly and provide a new config file via the `-config` argument. +It will read the contents of `sample-config.hcl` and begin its loop. You could also run it directly and provide a new config file via the `-config` argument. #### Docker @@ -38,19 +38,19 @@ You can pull this repository directly from Docker: docker pull iamthefij/minitor-go:latest ``` -The Docker image uses a default `config.yml` that is copied from `sample-config.yml`. This won't really do anything for you, so when you run the Docker image, you should supply your own `config.yml` file: +The Docker image uses a default `config.hcl` copied from `sample-config.hcl`. This won't really do anything for you, so when you run the Docker image, you should supply your own `config.hcl` file: ```bash -docker run -v $PWD/config.yml:/app/config.yml iamthefij/minitor-go:latest +docker run -v $PWD/sample-config.hcl:/app/config.hcl iamthefij/minitor-go:latest ``` Images are provided for `amd64`, `arm`, and `arm64` architechtures. -Timezone configuration for the container is set by passing the `TZ` env variable. Eg. `TZ=America/Los_Angeles`. +You can configure the timezone for the container by passing a `TZ` env variable. Eg. `TZ=America/Los_Angeles`. ## Configuring -In this repo, you can explore the `sample-config.yml` file for an example, but the general structure is as follows. It should be noted that environment variable interpolation happens on load of the YAML file. +In this repo, you can explore the `sample-config.hcl` file for an example, but the general structure is as follows. It should be noted that environment variable interpolation happens on load of the HCL file. The global configurations are: @@ -58,21 +58,34 @@ The global configurations are: |---|---| |`check_interval`|Maximum frequency to run checks for each monitor as duration, eg. 1m2s.| |`default_alert_after`|A default value used as an `alert_after` value for a monitor if not specified or 0.| +|`default_alert_every`|A default value used as an `alert_every` value for a monitor if not specified.| |`default_alert_down`|Default down alerts to used by a monitor in case none are provided.| |`default_alert_up`|Default up alerts to used by a monitor in case none are provided.| -|`monitors`|List of all monitors. Detailed description below| -|`alerts`|List of all alerts. Detailed description below| +|`monitor`|block listing monitors. Detailed description below| +|`alert`|List of all alerts. Detailed description below| ### Monitors -All monitors should be listed under `monitors`. +Represent your monitors as blocks with a label indicating the name of the monitor. + +```hcl +monitor "example" { + command = ["echo", "Hello, World!"] + alert_down = ["log"] + alert_up = ["log"] + check_interval = "1m" + alert_after = 1 + alert_every = 0 +} +``` Each monitor allows the following configuration: |key|value| |---|---| |`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| +|`command`|A list of strings representing a command to be executed. This command's exit value will determine whether the check is successful. This value is mutually exclusive to `shell_command`| +|`shell_command`|A single string that represents a shell command to be executed. This command's exit value will determine whether the check is successful. This value is mutually exclusive to `command`| |`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| @@ -81,13 +94,25 @@ 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`. +Represent your alerts as blocks with a lable indicating the name of the alert. The name will be used in your monitor setup in `alert_down` and `alert_up`. -Eachy alert allows the following configuration: +```hcl +monitor "example" { + command = ["false"] + alert_down = ["log"] +} + +alert "log" { + shell_command = "echo '{{.MonitorName}} is down!'" +} +``` + +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| +|`command`|Specifies the command that should be executed in exec 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. This value is mutually exclusive to `shell_command`| +|`shell_command`|Specifies a shell command as a single string. 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. This value is mutually exclusive to `command`| 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: @@ -125,7 +150,7 @@ It's not the best feeling to find out your alerts are broken when you're expecti Eg. ```bash -minitor -startup-alerts=log_down,log_up -config ./config.yml +minitor -startup-alerts=log_down,log_up -config ./config.hcl ``` ### Metrics @@ -157,44 +182,3 @@ This is a reimplementation of [Minitor](https://git.iamthefij.com/iamthefij/mini Minitor is already a minimal monitoring tool. Python 3 was a quick way to get something live, but Python itself comes with a large footprint. Thus Go feels like a better fit for the project, longer term. Initial target is meant to be roughly compatible requiring only minor changes to configuration. Future iterations may diverge to take advantage of Go specific features. - -### Differences from Python version - -Templating for Alert messages has been updated. In the Python version, `str.format(...)` was used with certain keys passed in that could be used to format messages. In the Go version, we use a struct, `AlertNotice` defined in `alert.go` and the built in Go templating format. Eg. - -minitor-py: -```yaml -alerts: - log: - command: 'echo {monitor_name}' -``` - -minitor-go: -```yaml -alerts: - log: - command: 'echo {{.MonitorName}}' -``` - -Interval durations have changed from being an integer number of seconds to a duration string supported by Go, for example: - -minitor-py: -```yaml -check_interval: 90 -``` - -minitor-go: -```yaml -check_interval: 1m30s -``` - -The `-py-compat` flag has been removed. Any existing Python oriented configuration needs to be migrated to the new templates. - -## Future - -Future, potentially breaking changes - - - [ ] Consider value of templating vs injecting values into Env variables - - [ ] Async checking - - [ ] Revisit metrics and see if they all make sense - - [ ] Consider dropping `alert_up` and `alert_down` in favor of using Go templates that offer more control of messaging (Breaking) diff --git a/config_test.go b/config_test.go index 66726c6..dc72bad 100644 --- a/config_test.go +++ b/config_test.go @@ -45,60 +45,59 @@ func TestMultiLineConfig(t *testing.T) { t.Fatalf("TestMultiLineConfig(load), expected=no_error actual=%v", err) } - expected := "echo 'Some string with stuff';\necho \"\";\nexit 1\n" - actual := config.Monitors[0].ShellCommand + t.Run("Test Monitor with Indented Multi-Line String", func(t *testing.T) { + // Verify indented heredoc is as expected + expected := "echo 'Some string with stuff'\necho \"\"\nexit 1\n" + actual := config.Monitors[0].ShellCommand - if expected != actual { - t.Errorf("TestMultiLineConfig(>) failed") - t.Logf("string expected=`%v`", expected) - t.Logf("string actual =`%v`", actual) - t.Logf("bytes expected=%v", []byte(expected)) - t.Logf("bytes actual =%v", []byte(actual)) - } + if expected != actual { + t.Error("Heredoc mismatch") + t.Errorf("string expected=`%v`", expected) + t.Errorf("string actual =`%v`", actual) + } - _, notice := config.Monitors[0].Check() - if notice == nil { - t.Fatal("Did not receive an alert notice") - } + // Run the monitor and verify the output + _, notice := config.Monitors[0].Check() + if notice == nil { + t.Fatal("Did not receive an alert notice and should have") + } - expected = "Some string with stuff\n\n" - actual = notice.LastCheckOutput + // Verify the output of the monitor is as expected + expected = "Some string with stuff\n\n" + actual = notice.LastCheckOutput - if expected != actual { - t.Errorf("TestMultiLineConfig(execute > string) check failed") - t.Logf("string expected=`%v`", expected) - t.Logf("string actual =`%v`", actual) - t.Logf("bytes expected=%v", []byte(expected)) - t.Logf("bytes actual =%v", []byte(actual)) - } + if expected != actual { + t.Error("Output mismatch") + t.Errorf("string expected=`%v`", expected) + t.Errorf("string actual =`%v`", actual) + } + }) - expected = "echo 'Some string with stuff'\necho ''\n" + t.Run("Test Alert with Multi-Line String", func(t *testing.T) { + alert, ok := config.GetAlert("log_shell") + if !ok { + t.Fatal("Could not find expected alert 'log_shell'") + } - alert, ok := config.GetAlert("log_shell") - if !ok { - t.Fatal("Could not find expected alert 'log_shell'") - } + expected := " echo 'Some string with stuff'\n echo ''\n" + actual := alert.ShellCommand - actual = alert.ShellCommand - if expected != actual { - t.Errorf("TestMultiLineConfig(|) failed") - t.Logf("string expected=`%v`", expected) - t.Logf("string actual =`%v`", actual) - t.Logf("bytes expected=%v", []byte(expected)) - t.Logf("bytes actual =%v", []byte(actual)) - } + if expected != actual { + t.Error("Heredoc mismatch") + t.Errorf("string expected=`%v`", expected) + t.Errorf("string actual =`%v`", actual) + } - actual, err = alert.Send(m.AlertNotice{}) - if err != nil { - t.Errorf("Execution of alert failed") - } + actual, err = alert.Send(m.AlertNotice{}) + if err != nil { + t.Fatal("Execution of alert failed") + } - expected = "Some string with stuff\n\n" - if expected != actual { - t.Errorf("TestMultiLineConfig(execute | string) check failed") - t.Logf("string expected=`%v`", expected) - t.Logf("string actual =`%v`", actual) - t.Logf("bytes expected=%v", []byte(expected)) - t.Logf("bytes actual =%v", []byte(actual)) - } + expected = "Some string with stuff\n\n" + if expected != actual { + t.Error("Output mismatch") + t.Errorf("string expected=`%v`", expected) + t.Errorf("string actual =`%v`", actual) + } + }) } diff --git a/sample-config.hcl b/sample-config.hcl index b54e2e3..e102dd9 100644 --- a/sample-config.hcl +++ b/sample-config.hcl @@ -19,30 +19,34 @@ monitor "Real Website" { } alert "log_down" { - command = ["echo", "Minitor failure for {{.MonitorName}}"] - } + command = ["echo", "Minitor failure for {{.MonitorName}}"] +} + alert "log_up" { - command = ["echo", "Minitor recovery for {{.MonitorName}}"] - } + command = ["echo", "Minitor recovery for {{.MonitorName}}"] +} + alert "email_up" { - command = ["sendmail", "me@minitor.mon", "Recovered: {monitor_name}", "We're back!"] - } + command = ["sendmail", "me@minitor.mon", "Recovered: {monitor_name}", "We're back!"] +} + alert "mailgun_down" { - shell_command = < - curl -s -X POST - -F subject="Alert! {{.MonitorName}} failed" - -F from="Minitor " - -F to=me@minitor.mon - -F text="Our monitor failed" - https://api.mailgun.net/v3/minitor.mon/messages - -u "api:${MAILGUN_API_KEY}" - sms_down: - command: > - curl -s -X POST -F "Body=Failure! {{.MonitorName}} has failed" - -F "From=${AVAILABLE_NUMBER}" -F "To=${MY_PHONE}" - "https://api.twilio.com/2010-04-01/Accounts/${ACCOUNT_SID}/Messages" - -u "${ACCOUNT_SID}:${AUTH_TOKEN}" diff --git a/test/valid-config.hcl b/test/valid-config.hcl index cfd4a06..5b64931 100644 --- a/test/valid-config.hcl +++ b/test/valid-config.hcl @@ -16,11 +16,11 @@ monitor "Command" { } monitor "Shell" { - shell_command = <"; -exit 1 + shell_command = <<-EOF + echo 'Some string with stuff' + echo "" + exit 1 EOF alert_down = ["log_shell"] alert_after = 1 @@ -13,7 +13,7 @@ exit 1 alert "log_shell" { shell_command = <' + echo 'Some string with stuff' + echo '' EOF }