Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67c2375bba | ||
|
|
aad9eaa32f | ||
|
|
5dc5ba5257 | ||
|
|
4aff294739 | ||
|
|
0684b15a44 | ||
|
|
d3826dacde | ||
|
|
f8e40c643c | ||
|
|
cffbbd734a |
+5
-1
@@ -6,6 +6,8 @@ steps:
|
||||
|
||||
- name: test
|
||||
image: golang:1.12
|
||||
environment:
|
||||
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
|
||||
commands:
|
||||
- make build
|
||||
- make test
|
||||
@@ -47,8 +49,10 @@ trigger:
|
||||
steps:
|
||||
- name: build all binaries
|
||||
image: golang:1.12
|
||||
environment:
|
||||
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
|
||||
commands:
|
||||
- make all
|
||||
- make all-linux
|
||||
|
||||
- name: push image - arm
|
||||
image: plugins/docker
|
||||
|
||||
Vendored
+1
-2
@@ -17,5 +17,4 @@ config.yml
|
||||
|
||||
# Output binary
|
||||
minitor
|
||||
minitor-linux-*
|
||||
minitor-darwin-amd64
|
||||
dist/
|
||||
|
||||
@@ -17,3 +17,8 @@ repos:
|
||||
- id: go-imports
|
||||
# - id: gometalinter
|
||||
# - id: golangci-lint
|
||||
# - repo: https://github.com/IamTheFij/docker-pre-commit
|
||||
# rev: v2.0.0
|
||||
# hooks:
|
||||
# - id: docker-compose-check
|
||||
# - id: hadolint
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ RUN chmod -R 755 /app/scripts
|
||||
|
||||
# Copy minitor in
|
||||
ARG ARCH=amd64
|
||||
COPY ./minitor-linux-${ARCH} ./minitor
|
||||
COPY ./dist/minitor-linux-${ARCH} ./minitor
|
||||
|
||||
# Drop to non-root user
|
||||
USER minitor
|
||||
|
||||
@@ -1,36 +1,43 @@
|
||||
DOCKER_TAG ?= minitor-go-${USER}
|
||||
GIT_TAG_NAME := $(shell git tag -l --contains HEAD)
|
||||
GIT_SHA := $(shell git rev-parse HEAD)
|
||||
VERSION := $(if $(GIT_TAG_NAME),$(GIT_TAG_NAME),$(GIT_SHA))
|
||||
VERSION ?= $(shell git describe --tags --dirty)
|
||||
GOFILES = *.go
|
||||
# Multi-arch targets are generated from this
|
||||
TARGET_ALIAS = minitor-linux-amd64 minitor-linux-arm minitor-linux-arm64 minitor-darwin-amd64
|
||||
TARGETS = $(addprefix dist/,$(TARGET_ALIAS))
|
||||
#
|
||||
# Default make target will run tests
|
||||
.DEFAULT_GOAL = test
|
||||
|
||||
# Build all static Minitor binaries
|
||||
.PHONY: all
|
||||
all: minitor-linux-amd64 minitor-linux-arm minitor-linux-arm64
|
||||
all: $(TARGETS)
|
||||
|
||||
.PHONY: default
|
||||
default: test
|
||||
# Build all static Linux Minitor binaries. Used in Docker images
|
||||
.PHONY: all-linux
|
||||
all-linux: $(filter dist/minitor-linux-%,$(TARGETS))
|
||||
|
||||
# Build minitor for the current machine
|
||||
minitor: $(GOFILES)
|
||||
@echo Version: $(VERSION)
|
||||
go build -ldflags '-X "main.version=${VERSION}"' -o minitor
|
||||
|
||||
.PHONY: build
|
||||
build: minitor
|
||||
|
||||
minitor:
|
||||
@echo Version: $(VERSION)
|
||||
go build -ldflags '-X "main.version=${VERSION}"' -o minitor
|
||||
|
||||
# Run minitor for the current machine
|
||||
.PHONY: run
|
||||
run: minitor build
|
||||
run: minitor
|
||||
./minitor -debug
|
||||
|
||||
.PHONY: run-metrics
|
||||
run-metrics: minitor build
|
||||
run-metrics: minitor
|
||||
./minitor -debug -metrics
|
||||
|
||||
# Run all tests
|
||||
.PHONY: test
|
||||
test:
|
||||
go test -coverprofile=coverage.out
|
||||
@echo
|
||||
go tool cover -func=coverage.out
|
||||
@echo
|
||||
@# Check min coverage percentage
|
||||
@go tool cover -func=coverage.out | awk -v target=80.0% \
|
||||
'/^total:/ { print "Total coverage: " $$3 " Minimum coverage: " target; if ($$3+0.0 >= target+0.0) print "ok"; else { print "fail"; exit 1; } }'
|
||||
|
||||
@@ -47,9 +54,8 @@ check:
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f ./minitor
|
||||
rm -f ./minitor-linux-*
|
||||
rm -f ./minitor-darwin-amd64
|
||||
rm -f ./coverage.out
|
||||
rm -fr ./dist
|
||||
|
||||
.PHONY: docker-build
|
||||
docker-build:
|
||||
@@ -60,35 +66,23 @@ docker-run: docker-build
|
||||
docker run --rm -v $(shell pwd)/config.yml:/root/config.yml $(DOCKER_TAG)
|
||||
|
||||
## Multi-arch targets
|
||||
$(TARGETS): $(GOFILES)
|
||||
mkdir -p ./dist
|
||||
GOOS=$(word 2, $(subst -, ,$(@))) GOARCH=$(word 3, $(subst -, ,$(@))) CGO_ENABLED=0 \
|
||||
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
||||
-o $@
|
||||
|
||||
# Arch specific go build targets
|
||||
minitor-darwin-amd64:
|
||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 \
|
||||
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
||||
-o minitor-darwin-amd64
|
||||
|
||||
minitor-linux-amd64:
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
|
||||
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
||||
-o minitor-linux-amd64
|
||||
|
||||
minitor-linux-arm:
|
||||
GOOS=linux GOARCH=arm CGO_ENABLED=0 \
|
||||
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
||||
-o minitor-linux-arm
|
||||
|
||||
minitor-linux-arm64:
|
||||
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 \
|
||||
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
||||
-o minitor-linux-arm64
|
||||
.PHONY: $(TARGET_ALIAS)
|
||||
$(TARGET_ALIAS):
|
||||
$(MAKE) $(addprefix dist/,$@)
|
||||
|
||||
# Arch specific docker build targets
|
||||
.PHONY: docker-build-arm
|
||||
docker-build-arm: minitor-linux-arm
|
||||
docker-build-arm: dist/minitor-linux-arm
|
||||
docker build --build-arg REPO=arm32v7 --build-arg ARCH=arm . -t ${DOCKER_TAG}-linux-arm
|
||||
|
||||
.PHONY: docker-build-arm
|
||||
docker-build-arm64: minitor-linux-arm64
|
||||
.PHONY: docker-build-arm64
|
||||
docker-build-arm64: dist/minitor-linux-arm64
|
||||
docker build --build-arg REPO=arm64v8 --build-arg ARCH=arm64 . -t ${DOCKER_TAG}-linux-arm64
|
||||
|
||||
# Cross run on host architechture
|
||||
|
||||
@@ -93,6 +93,7 @@ Also, when alerts are executed, they will be passed through Go's format function
|
||||
|`{{.LastCheckOutput}}`|The last returned value from the check command to either stderr or stdout|
|
||||
|`{{.LastSuccess}}`|The ISO datetime of the last successful check|
|
||||
|`{{.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|
|
||||
|
||||
### Metrics
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ func NewLogAlert() *Alert {
|
||||
Command: CommandOrShell{
|
||||
Command: []string{
|
||||
"echo",
|
||||
"{{.MonitorName}} check has failed {{.FailureCount}} times",
|
||||
"{{.MonitorName}} {{if .IsUp}}has recovered{{else}}check has failed {{.FailureCount}} times{{end}}",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -76,6 +76,24 @@ func TestAlertSend(t *testing.T) {
|
||||
"Command shell with legacy template",
|
||||
true,
|
||||
},
|
||||
// Test default log alert down
|
||||
{
|
||||
*NewLogAlert(),
|
||||
AlertNotice{MonitorName: "Test", FailureCount: 1, IsUp: false},
|
||||
"Test check has failed 1 times\n",
|
||||
false,
|
||||
"Default log alert down",
|
||||
false,
|
||||
},
|
||||
// Test default log alert up
|
||||
{
|
||||
*NewLogAlert(),
|
||||
AlertNotice{MonitorName: "Test", IsUp: true},
|
||||
"Test has recovered\n",
|
||||
false,
|
||||
"Default log alert up",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
|
||||
@@ -33,7 +33,7 @@ func checkMonitors(config *Config) error {
|
||||
hasAlert := alertNotice != nil
|
||||
|
||||
// Track status metrics
|
||||
Metrics.SetMonitorStatus(monitor.Name, success)
|
||||
Metrics.SetMonitorStatus(monitor.Name, monitor.IsUp())
|
||||
Metrics.CountCheck(monitor.Name, success, hasAlert)
|
||||
|
||||
// Should probably consider refactoring everything below here
|
||||
|
||||
+3
-2
@@ -85,12 +85,13 @@ func (monitor *Monitor) Check() (bool, *AlertNotice) {
|
||||
return isSuccess, alertNotice
|
||||
}
|
||||
|
||||
func (monitor Monitor) isUp() bool {
|
||||
// IsUp returns the status of the current monitor
|
||||
func (monitor Monitor) IsUp() bool {
|
||||
return monitor.alertCount == 0
|
||||
}
|
||||
|
||||
func (monitor *Monitor) success() (notice *AlertNotice) {
|
||||
if !monitor.isUp() {
|
||||
if !monitor.IsUp() {
|
||||
// Alert that we have recovered
|
||||
notice = monitor.createAlertNotice(true)
|
||||
}
|
||||
|
||||
+3
-3
@@ -56,7 +56,7 @@ func TestMonitorShouldCheck(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestMonitorIsUp tests the Monitor.isUp()
|
||||
// TestMonitorIsUp tests the Monitor.IsUp()
|
||||
func TestMonitorIsUp(t *testing.T) {
|
||||
cases := []struct {
|
||||
monitor Monitor
|
||||
@@ -71,9 +71,9 @@ func TestMonitorIsUp(t *testing.T) {
|
||||
|
||||
for _, c := range cases {
|
||||
log.Printf("Testing case %s", c.name)
|
||||
actual := c.monitor.isUp()
|
||||
actual := c.monitor.IsUp()
|
||||
if actual != c.expected {
|
||||
t.Errorf("isUp(%v), expected=%t actual=%t", c.name, c.expected, actual)
|
||||
t.Errorf("IsUp(%v), expected=%t actual=%t", c.name, c.expected, actual)
|
||||
log.Printf("Case failed: %s", c.name)
|
||||
}
|
||||
log.Println("-----")
|
||||
|
||||
Reference in New Issue
Block a user