From f6039b69b3189d63c6bbd78c2a5a4731be5db736 Mon Sep 17 00:00:00 2001
From: Simon Michnowicz <simon.michnowicz@monash.edu>
Date: Tue, 13 Apr 2021 15:11:44 +1000
Subject: [PATCH] First checkin of python testing script

---
 .../templates/detect-gluster-problems.py.j2   | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 roles/gluster-monitor/templates/detect-gluster-problems.py.j2

diff --git a/roles/gluster-monitor/templates/detect-gluster-problems.py.j2 b/roles/gluster-monitor/templates/detect-gluster-problems.py.j2
new file mode 100644
index 00000000..e0e2ae7b
--- /dev/null
+++ b/roles/gluster-monitor/templates/detect-gluster-problems.py.j2
@@ -0,0 +1,87 @@
+# detect-gluster-problems.py
+# Authors simon michnowicz 13 April 2021
+
+COMMAND="sudo gluster volume heal gv info"
+#COMMAND="sudo gluster volume heal gv info split-brain"
+#also gluster volume heal gv info split-brain
+
+#myOutput=`$COMMAND`
+#myOutput=" a split brain that heals"
+#echo "Output is $myOutput"
+#
+#
+#sudo gluster volume heal gv info split-brain
+#Brick 172.16.227.169:/gbrick/brick
+#Status: Connected
+#Number of entries in split-brain: 
+
+import os
+import sys
+import subprocess
+
+def error(e):
+	'''
+	we have an error. Send an email
+	e=error string
+	'''
+	hostname = os.uname()[1]
+        #print("Error is:\n{}".format(e))
+	HEADER="Error Message from {}".format(hostname)
+	MAILTO="{{ EMAIL_DEST }}"
+    command="echo -e \"{}\" | mail -s \"{}\" \"{}\" ".format(e,HEADER,MAILTO)
+	#print("Command is:\n{}".format(command))
+	os.system(command)
+
+def Test1():
+	'''
+	This tests looks for the word "split brain" in a general query
+	'''
+	COMMAND="sudo gluster volume heal gv info"
+	try:
+        	result = subprocess.check_output(COMMAND.split()).decode('UTF-8')
+        	#print("Test1 Output is {}".format(result))
+        	outList=result.splitlines()
+		for line in outList:
+			#print("Test1 line is {}".format(line))
+			if 'split' in line:
+				error(COMMAND+"\n"+outlist)
+    	except subprocess.CalledProcessError as error:
+        	error("Test1:  \nPlease contact mcc-help@monash.edu \n error code", error.returncode, error.output)
+        	sys.exit(1)				
+def Test2():
+	'''
+        This test checks for number of split brain entries
+
+	sudo gluster volume heal gv info split-brain
+	Brick 172.16.227.169:/gbrick/brick
+	Status: Connected
+	Number of entries in split-brain:  0
+	'''
+	COMMAND="sudo gluster volume heal gv info split-brain"
+	try:
+                result = subprocess.check_output(COMMAND.split()).decode('UTF-8')
+                #print("Test2 Output is {}".format(result))
+                outList=result.splitlines()
+                for line in outList:
+                        #print("Line is {}".format(line))
+                        if 'Number of entries in split-brain' in line:
+				split=line.split(':')
+				if len(split)!=2:
+					error("Logic error in Test2: split is {}".format(split))
+					sys.exit(1)
+				NoOfSplitBrains=int(split[1])
+				#print("Number of Split Brains is {}".format(NoOfSplitBrains))
+				if (NoOfSplitBrains!=0):
+					error("Number of Split Brains is {}".format(NoOfSplitBrains))
+        except subprocess.CalledProcessError as error:
+                error("Test1:  \nPlease contact mcc-help@monash.edu \n error code", error.returncode, error.output)
+                sys.exit(1)
+
+def main():
+	#error("Simon test")
+	#sys.exit(0)
+	Test1()
+	Test2()
+
+if __name__ == "__main__":
+    main()
-- 
GitLab