From f0987ccb44fd2b7575c0f2b3e33df07d366d49b9 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Thu, 19 Aug 2021 20:03:24 +0200 Subject: [PATCH] bundles/unbound: automatically restart unbound if dns resolution fails for more than five minutes --- bundles/unbound/files/unbound-auto-restart | 25 ++++++++++++++++++++++ bundles/unbound/items.py | 3 +++ bundles/unbound/metadata.py | 1 + 3 files changed, 29 insertions(+) create mode 100644 bundles/unbound/files/unbound-auto-restart diff --git a/bundles/unbound/files/unbound-auto-restart b/bundles/unbound/files/unbound-auto-restart new file mode 100644 index 0000000..b2a82d2 --- /dev/null +++ b/bundles/unbound/files/unbound-auto-restart @@ -0,0 +1,25 @@ +#!/bin/bash + +now="$(date +%s)" + +resolved="$(dig +short kunbox.net)" + +if [[ -n "$resolved" ]] +then + echo "$now" > /var/tmp/unbound_last_resolved + exit 0 +fi + +last_resolved="$(cat /var/tmp/unbound_last_resolved)" +five_min_ago="$(expr $now - 300)" + +if [[ "$last_resolved" -lt "$five_min_ago" ]] +then + systemctl restart unbound + + # Write "in 25 minutes" timestamp to /var/tmp/unbound_last_resolved. + # If resolving gets fixed by the restart, this will get re-written + # on the next run of this script. If not, this script will restart + # unbound again in half an hour (25min plus 5min grace time). + echo "$(expr $now + 1500)" > /var/tmp/unbound_last_resolved +fi diff --git a/bundles/unbound/items.py b/bundles/unbound/items.py index fdd1a18..519b811 100644 --- a/bundles/unbound/items.py +++ b/bundles/unbound/items.py @@ -6,6 +6,9 @@ files = { 'svc_systemd:unbound:restart', }, }, + '/usr/local/sbin/unbound-auto-restart': { + 'mode': '0755', + }, } actions = { diff --git a/bundles/unbound/metadata.py b/bundles/unbound/metadata.py index caf3d04..4a27851 100644 --- a/bundles/unbound/metadata.py +++ b/bundles/unbound/metadata.py @@ -13,6 +13,7 @@ defaults = { node.magic_number%24, node.magic_number%7, ), + 'unbound-auto-restart': '* * * * * root /usr/local/sbin/unbound-auto-restart', }, 'nameservers': { '127.0.0.1',