From 076ff6605e618e3bf0fdb0bd9f9f6df56f7b35e4 Mon Sep 17 00:00:00 2001
From: MegaBrutal <code+git@megabrutal.com>
Date: Sat, 5 Nov 2016 23:20:40 +0100
Subject: [PATCH] check-nfs-hang.sh: Added script to monitor NFS mount health

The check-nfs-hang.sh script alerts when the monitored network file system
is not mounted or hanging. Similarly to check-fs-usage.sh, it may be
scheduled with cron, and sends e-mail when an e-mail address is supplied.

By the way, check-fs-usage.sh has been moved to the more appropriate
"fs" directory.

	renamed:    host/check-fs-usage.sh -> fs/check-fs-usage.sh
	new file:   fs/check-nfs-hang.sh
---
 {host => fs}/check-fs-usage.sh |  0
 fs/check-nfs-hang.sh           | 52 ++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
 rename {host => fs}/check-fs-usage.sh (100%)
 create mode 100755 fs/check-nfs-hang.sh

diff --git a/host/check-fs-usage.sh b/fs/check-fs-usage.sh
similarity index 100%
rename from host/check-fs-usage.sh
rename to fs/check-fs-usage.sh
diff --git a/fs/check-nfs-hang.sh b/fs/check-nfs-hang.sh
new file mode 100755
index 0000000..f50cc85
--- /dev/null
+++ b/fs/check-nfs-hang.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+if [ $# -lt 1 ]
+then
+	echo "Usage: $0 <fs> [email]"
+	echo "Where"
+	echo "	fs		- file system to check"
+	echo "	email		- send warning to specified e-mail address (print to stdout, if omitted)"
+	echo "Example: $0 /srv/remoteshare admin@cutekittens.tld"
+	exit 1
+fi
+
+make_command()
+{
+	if [ -z "$1" ]
+	then
+		echo "cat"
+	else
+		echo "mail -s \"$subject\" $1"
+	fi
+}
+
+if ! mount | grep "on $1 type nfs" > /dev/null
+then
+	subject="WARNING! Network file system ($1) on $(hostname) is not mounted!"
+
+	[ -z "$2" ] && echo "${subject}"
+	CMD=$(make_command "$2")
+	eval ${CMD} << EOT
+This is to inform you that no NFS is mounted at $1.
+Please investigate immediately!
+EOT
+exit 2
+
+elif ! timeout 15 stat "$1" > /dev/null
+then
+	subject="WARNING! Network file system ($1) on $(hostname) is hanging/unreachable!"
+
+	[ -z "$2" ] && echo "${subject}"
+	CMD=$(make_command "$2")
+	eval ${CMD} << EOT
+This is to inform you that network share $1 is hanging/unreachable.
+
+Mount details:
+
+$(mount | grep "$1")
+
+Please investigate immediately!
+EOT
+exit 3
+
+fi
-- 
2.43.0