From 8e176354f8655ed9e1d42c39dd4a760220830c71 Mon Sep 17 00:00:00 2001
From: "Matthias P. Walther" <matthiaspeterw@aol.com>
Date: Sun, 2 Jun 2019 03:00:36 +0200
Subject: [PATCH] gluon-mesh-vpn-tunneldigger add watchdog script

The logic is based on the previously used shell script implementation.

Co-Authored-By: Robin Weiligmann <admin@robwei.me>
---
 .../files/usr/bin/tunneldigger-watchdog       | 46 +++++++++++++++++++
 .../usr/lib/micron.d/tunneldigger-watchdog    |  1 +
 2 files changed, 47 insertions(+)
 create mode 100755 package/gluon-mesh-vpn-tunneldigger/files/usr/bin/tunneldigger-watchdog
 create mode 100644 package/gluon-mesh-vpn-tunneldigger/files/usr/lib/micron.d/tunneldigger-watchdog

diff --git a/package/gluon-mesh-vpn-tunneldigger/files/usr/bin/tunneldigger-watchdog b/package/gluon-mesh-vpn-tunneldigger/files/usr/bin/tunneldigger-watchdog
new file mode 100755
index 000000000..b5f7b4e47
--- /dev/null
+++ b/package/gluon-mesh-vpn-tunneldigger/files/usr/bin/tunneldigger-watchdog
@@ -0,0 +1,46 @@
+#!/usr/bin/lua
+
+local uci = require('simple-uci').cursor()
+
+function restart_tunneldigger()
+	os.execute('logger -t tunneldigger-watchdog "Restarting Tunneldigger."')
+	os.execute('/etc/init.d/tunneldigger restart')
+end
+
+function read_pid_file()
+	local pid_file = io.open('/var/run/tunneldigger.mesh-vpn.pid', 'r')
+	if not pid_file then
+		return nil
+	end
+	local pid = pid_file:read('*l') 
+	pid_file:close()
+	return pid
+end
+
+function has_mesh_vpn_neighbours()
+	local handle = io.popen('batctl o', 'r')
+	if not handle then
+		return false
+	end
+	for line in handle:lines() do
+		if line:find('mesh%-vpn') then
+			handle:close()
+			return true
+		end
+	end
+	handle:close()
+	return false
+end
+
+if uci:get_bool('tunneldigger', 'mesh_vpn', 'enabled') then
+	if io.popen('pgrep tunneldigger'):read('*l') ~= read_pid_file() then
+		os.execute('logger -t tunneldigger-watchdog "Process-Pid does not match with pid-File."')
+		restart_tunneldigger()
+		return
+	end
+	if not has_mesh_vpn_neighbours() then
+		os.execute('logger -t tunneldigger-watchdog "No vpn-mesh neighbours found."')
+		restart_tunneldigger()
+		return
+	end
+end
diff --git a/package/gluon-mesh-vpn-tunneldigger/files/usr/lib/micron.d/tunneldigger-watchdog b/package/gluon-mesh-vpn-tunneldigger/files/usr/lib/micron.d/tunneldigger-watchdog
new file mode 100644
index 000000000..c4ae3bc6c
--- /dev/null
+++ b/package/gluon-mesh-vpn-tunneldigger/files/usr/lib/micron.d/tunneldigger-watchdog
@@ -0,0 +1 @@
+*/5 * * * * /usr/bin/tunneldigger-watchdog
-- 
GitLab