Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67c2375bba | ||
|
|
aad9eaa32f | ||
|
|
5dc5ba5257 | ||
|
|
4aff294739 | ||
|
|
0684b15a44 | ||
|
|
d3826dacde | ||
|
|
f8e40c643c | ||
|
|
cffbbd734a |
+5
-1
@@ -6,6 +6,8 @@ steps:
|
|||||||
|
|
||||||
- name: test
|
- name: test
|
||||||
image: golang:1.12
|
image: golang:1.12
|
||||||
|
environment:
|
||||||
|
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
|
||||||
commands:
|
commands:
|
||||||
- make build
|
- make build
|
||||||
- make test
|
- make test
|
||||||
@@ -47,8 +49,10 @@ trigger:
|
|||||||
steps:
|
steps:
|
||||||
- name: build all binaries
|
- name: build all binaries
|
||||||
image: golang:1.12
|
image: golang:1.12
|
||||||
|
environment:
|
||||||
|
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
|
||||||
commands:
|
commands:
|
||||||
- make all
|
- make all-linux
|
||||||
|
|
||||||
- name: push image - arm
|
- name: push image - arm
|
||||||
image: plugins/docker
|
image: plugins/docker
|
||||||
|
|||||||
Vendored
+1
-2
@@ -17,5 +17,4 @@ config.yml
|
|||||||
|
|
||||||
# Output binary
|
# Output binary
|
||||||
minitor
|
minitor
|
||||||
minitor-linux-*
|
dist/
|
||||||
minitor-darwin-amd64
|
|
||||||
|
|||||||
@@ -17,3 +17,8 @@ repos:
|
|||||||
- id: go-imports
|
- id: go-imports
|
||||||
# - id: gometalinter
|
# - id: gometalinter
|
||||||
# - id: golangci-lint
|
# - 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
|
# Copy minitor in
|
||||||
ARG ARCH=amd64
|
ARG ARCH=amd64
|
||||||
COPY ./minitor-linux-${ARCH} ./minitor
|
COPY ./dist/minitor-linux-${ARCH} ./minitor
|
||||||
|
|
||||||
# Drop to non-root user
|
# Drop to non-root user
|
||||||
USER minitor
|
USER minitor
|
||||||
|
|||||||
@@ -1,36 +1,43 @@
|
|||||||
DOCKER_TAG ?= minitor-go-${USER}
|
DOCKER_TAG ?= minitor-go-${USER}
|
||||||
GIT_TAG_NAME := $(shell git tag -l --contains HEAD)
|
VERSION ?= $(shell git describe --tags --dirty)
|
||||||
GIT_SHA := $(shell git rev-parse HEAD)
|
GOFILES = *.go
|
||||||
VERSION := $(if $(GIT_TAG_NAME),$(GIT_TAG_NAME),$(GIT_SHA))
|
# 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
|
.PHONY: all
|
||||||
all: minitor-linux-amd64 minitor-linux-arm minitor-linux-arm64
|
all: $(TARGETS)
|
||||||
|
|
||||||
.PHONY: default
|
# Build all static Linux Minitor binaries. Used in Docker images
|
||||||
default: test
|
.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
|
.PHONY: build
|
||||||
build: minitor
|
build: minitor
|
||||||
|
|
||||||
minitor:
|
# Run minitor for the current machine
|
||||||
@echo Version: $(VERSION)
|
|
||||||
go build -ldflags '-X "main.version=${VERSION}"' -o minitor
|
|
||||||
|
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: minitor build
|
run: minitor
|
||||||
./minitor -debug
|
./minitor -debug
|
||||||
|
|
||||||
.PHONY: run-metrics
|
.PHONY: run-metrics
|
||||||
run-metrics: minitor build
|
run-metrics: minitor
|
||||||
./minitor -debug -metrics
|
./minitor -debug -metrics
|
||||||
|
|
||||||
|
# Run all tests
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test:
|
test:
|
||||||
go test -coverprofile=coverage.out
|
go test -coverprofile=coverage.out
|
||||||
@echo
|
|
||||||
go tool cover -func=coverage.out
|
go tool cover -func=coverage.out
|
||||||
@echo
|
|
||||||
@# Check min coverage percentage
|
|
||||||
@go tool cover -func=coverage.out | awk -v target=80.0% \
|
@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; } }'
|
'/^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
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -f ./minitor
|
rm -f ./minitor
|
||||||
rm -f ./minitor-linux-*
|
|
||||||
rm -f ./minitor-darwin-amd64
|
|
||||||
rm -f ./coverage.out
|
rm -f ./coverage.out
|
||||||
|
rm -fr ./dist
|
||||||
|
|
||||||
.PHONY: docker-build
|
.PHONY: docker-build
|
||||||
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)
|
docker run --rm -v $(shell pwd)/config.yml:/root/config.yml $(DOCKER_TAG)
|
||||||
|
|
||||||
## Multi-arch targets
|
## Multi-arch targets
|
||||||
|
$(TARGETS): $(GOFILES)
|
||||||
# Arch specific go build targets
|
mkdir -p ./dist
|
||||||
minitor-darwin-amd64:
|
GOOS=$(word 2, $(subst -, ,$(@))) GOARCH=$(word 3, $(subst -, ,$(@))) CGO_ENABLED=0 \
|
||||||
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 \
|
|
||||||
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
||||||
-o minitor-darwin-amd64
|
-o $@
|
||||||
|
|
||||||
minitor-linux-amd64:
|
.PHONY: $(TARGET_ALIAS)
|
||||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
|
$(TARGET_ALIAS):
|
||||||
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
|
$(MAKE) $(addprefix dist/,$@)
|
||||||
-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
|
|
||||||
|
|
||||||
# Arch specific docker build targets
|
# Arch specific docker build targets
|
||||||
.PHONY: docker-build-arm
|
.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
|
docker build --build-arg REPO=arm32v7 --build-arg ARCH=arm . -t ${DOCKER_TAG}-linux-arm
|
||||||
|
|
||||||
.PHONY: docker-build-arm
|
.PHONY: docker-build-arm64
|
||||||
docker-build-arm64: minitor-linux-arm64
|
docker-build-arm64: dist/minitor-linux-arm64
|
||||||
docker build --build-arg REPO=arm64v8 --build-arg ARCH=arm64 . -t ${DOCKER_TAG}-linux-arm64
|
docker build --build-arg REPO=arm64v8 --build-arg ARCH=arm64 . -t ${DOCKER_TAG}-linux-arm64
|
||||||
|
|
||||||
# Cross run on host architechture
|
# 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|
|
|`{{.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 ISO datetime of the last successful check|
|
||||||
|`{{.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|
|
||||||
|
|
||||||
### Metrics
|
### Metrics
|
||||||
|
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ func NewLogAlert() *Alert {
|
|||||||
Command: CommandOrShell{
|
Command: CommandOrShell{
|
||||||
Command: []string{
|
Command: []string{
|
||||||
"echo",
|
"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",
|
"Command shell with legacy template",
|
||||||
true,
|
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 {
|
for _, c := range cases {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func checkMonitors(config *Config) error {
|
|||||||
hasAlert := alertNotice != nil
|
hasAlert := alertNotice != nil
|
||||||
|
|
||||||
// Track status metrics
|
// Track status metrics
|
||||||
Metrics.SetMonitorStatus(monitor.Name, success)
|
Metrics.SetMonitorStatus(monitor.Name, monitor.IsUp())
|
||||||
Metrics.CountCheck(monitor.Name, success, hasAlert)
|
Metrics.CountCheck(monitor.Name, success, hasAlert)
|
||||||
|
|
||||||
// Should probably consider refactoring everything below here
|
// Should probably consider refactoring everything below here
|
||||||
|
|||||||
+3
-2
@@ -85,12 +85,13 @@ func (monitor *Monitor) Check() (bool, *AlertNotice) {
|
|||||||
return isSuccess, 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
|
return monitor.alertCount == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *Monitor) success() (notice *AlertNotice) {
|
func (monitor *Monitor) success() (notice *AlertNotice) {
|
||||||
if !monitor.isUp() {
|
if !monitor.IsUp() {
|
||||||
// Alert that we have recovered
|
// Alert that we have recovered
|
||||||
notice = monitor.createAlertNotice(true)
|
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) {
|
func TestMonitorIsUp(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
monitor Monitor
|
monitor Monitor
|
||||||
@@ -71,9 +71,9 @@ func TestMonitorIsUp(t *testing.T) {
|
|||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
log.Printf("Testing case %s", c.name)
|
log.Printf("Testing case %s", c.name)
|
||||||
actual := c.monitor.isUp()
|
actual := c.monitor.IsUp()
|
||||||
if actual != c.expected {
|
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.Printf("Case failed: %s", c.name)
|
||||||
}
|
}
|
||||||
log.Println("-----")
|
log.Println("-----")
|
||||||
|
|||||||
Reference in New Issue
Block a user