Add make target for generating mock API data from alertmanager

This will allow moving away from manually captured/edited mock API responses and start generating them by sending alerts & silences to a live Alertmanager instance
This commit is contained in:
Łukasz Mierzwa
2017-05-01 22:23:08 +01:00
parent 5ae8f43227
commit f4ab8428d8
3 changed files with 218 additions and 0 deletions

29
mock/Makefile Normal file
View File

@@ -0,0 +1,29 @@
DOCKER_NAME := alertmanager-unsee-mock
DOCKER_IMAGE := prom/alertmanager
DOCKER_ARGS := --name $(DOCKER_NAME) --rm -d -p 9093:9093 -v $(CURDIR)/alertmanager.yml:/etc/alertmanager/config.yml
# list of Alertmanager versions to generate mock files for
VERSIONS := 0.4.0 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.6.1
%/.ok:
$(eval VERSION := $(word 1, $(subst /, ,$@)))
@echo "Generating mock files for Alertmanager $(VERSION)"
@docker pull $(DOCKER_IMAGE):v$(VERSION) > /dev/null
@docker rm -f $(DOCKER_NAME) > /dev/null 2>&1 || true
@echo "Starting Alertmanager"
@docker run $(DOCKER_ARGS) $(DOCKER_IMAGE):v$(VERSION)
@sleep 15
@echo "Sending mock alerts and silences"
@python livemock.py
@mkdir -p $(CURDIR)/$(VERSION)/api/v1 $(CURDIR)/$(VERSION)/api/v1/alerts
@echo "Collecting API responses"
@curl --fail -s localhost:9093/api/v1/status | python -m json.tool > $(CURDIR)/$(VERSION)/api/v1/status
@curl --fail -s localhost:9093/api/v1/silences | python -m json.tool > $(CURDIR)/$(VERSION)/api/v1/silences
@curl --fail -s localhost:9093/api/v1/alerts/groups | python -m json.tool > $(CURDIR)/$(VERSION)/api/v1/alerts/groups
@touch $(VERSION)/.ok
@echo "Done"
.PHONY: all
all: $(foreach version, $(VERSIONS), $(version)/.ok)
.DEFAULT_GOAL := all

17
mock/alertmanager.yml Normal file
View File

@@ -0,0 +1,17 @@
route:
group_by: ['alertname', 'cluster', 'service']
group_wait: 15s
group_interval: 35s
repeat_interval: 999h
receiver: default
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
# Apply inhibition if the alertname is the same.
equal: ['alertname', 'cluster', 'service']
receivers:
- name: 'default'

172
mock/livemock.py Normal file
View File

@@ -0,0 +1,172 @@
#!/usr/bin/env python
import os
import time
import json
import requests
requests.post('http://localhost:9093/api/v1/silences',
json={
"matchers": [
{
"name": "instance",
"value": "web1",
"isRegex": False
}
],
"startsAt": "2017-02-18T01:34:34Z",
"endsAt": "2063-01-01T00:00:00Z",
"createdBy": "john@example.com",
"comment": "Silenced instance"
})
requests.post('http://localhost:9093/api/v1/silences',
json={
"matchers": [
{
"name": "alertname",
"value": "Host_Down",
"isRegex": False
},
{
"name": "cluster",
"value": "dev",
"isRegex": False
}
],
"startsAt": "2017-02-18T01:34:34Z",
"endsAt": "2063-01-01T00:00:00Z",
"createdBy": "john@example.com",
"comment": "Silenced Host_Down alerts in the dev cluster"
})
for i in xrange(0, 5):
requests.post('http://localhost:9093/api/v1/alerts',
json=[{
"labels": {
"alertname": "HTTP_Probe_Failed",
"instance": "web1",
"job": "node_exporter",
"cluster": "dev"
},
"annotations": {
"help": "Example help annotation",
"summary": "Example summary",
"url": "http://localhost/example.html"
}
}, {
"labels": {
"alertname": "HTTP_Probe_Failed",
"instance": "web2",
"job": "node_exporter",
"cluster": "dev"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server1",
"job": "node_ping",
"cluster": "prod"
},
"annotations": {
"summary": "Example summary",
"url": "http://localhost/example.html"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server2",
"job": "node_ping",
"cluster": "prod"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server3",
"job": "node_ping",
"cluster": "staging"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server4",
"job": "node_ping",
"cluster": "staging"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server5",
"job": "node_ping",
"cluster": "staging"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server6",
"job": "node_ping",
"cluster": "dev"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server7",
"job": "node_ping",
"cluster": "dev"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Host_Down",
"instance": "server8",
"job": "node_ping",
"cluster": "dev"
},
"annotations": {
"summary": "Example summary"
}
}, {
"labels": {
"alertname": "Memory_Usage_Too_High",
"instance": "server2",
"job": "node_exporter",
"cluster": "prod"
},
"annotations": {
"alert": "Memory usage exceeding threshold",
"dashboard": "http://localhost/dashboard.html"
}
}, {
"labels": {
"alertname": "Free_Disk_Space_Too_Low",
"instance": "server5",
"job": "node_exporter",
"cluster": "staging"
},
"annotations": {
"alert": "Less than 10% disk space is free",
"dashboard": "http://localhost/dashboard.html"
}
}]
)
time.sleep(10)