-
Notifications
You must be signed in to change notification settings - Fork 9
/
monitor.sh.j2
56 lines (47 loc) · 1.56 KB
/
monitor.sh.j2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env bash
# vim: set ft=sh:
CONSUL_SERVICE_FILE='/etc/consul/service_{{ nim_waku_consul_config_name }}.json'
function date_iso8691(){
date -u +"%Y-%m-%dT%H:%M:%SZ"
}
function event_loop () {
while read EVENT; do
echo "${EVENT}" | jq -c \
'{ time, commit: .Actor.Attributes.commit, Action }'
update_consul && { update_service_timestamp; reload_consul; }
done
}
function get_meta_attr() {
echo "${EVENT}" | jq -r ".Actor.Attributes.${1} // \"unknown\""
}
function update_service_json() {
# Uglier but MUCH less effort than using the API.
sed "${CONSUL_SERVICE_FILE}" \
{% for field in nim_waku_monitor_meta_fields %}
-e 's#"{{ field }}": ".*"#"{{ field }}": "'$(get_meta_attr {{ field }})'"#' \
{% endfor %}
| sponge "${CONSUL_SERVICE_FILE}"
}
function update_service_timestamp() {
sed "${CONSUL_SERVICE_FILE}" \
-e 's#"timestamp": ".*"#"timestamp": "'$(date_iso8691)'"#' \
| sponge "${CONSUL_SERVICE_FILE}"
}
function update_consul () {
BEFORE=$(md5sum "${CONSUL_SERVICE_FILE}")
update_service_json
AFTER=$(md5sum "${CONSUL_SERVICE_FILE}")
# Avoid unnecessary reloads.
[[ "${BEFORE}" == "${AFTER}" ]] && return 1 || return 0
}
function reload_consul() {
echo "Changes detected, reloading Consul."
systemctl reload consul
}
docker system events \
--format='{% raw %}{{ json . }}{% endraw %}' \
--filter='container={{ nim_waku_cont_name }}' \
{% for event in nim_waku_monitor_docker_events %}
--filter='event={{ event }}' \
{% endfor %}
| event_loop