From eb64fd4b46d334ddfda60a815e1655cb72bf68a2 Mon Sep 17 00:00:00 2001 From: Sahina Bose Date: Thu, 15 May 2014 18:04:22 +0530 Subject: server-plugins: Corrected cluster utilization when volume stopped Corrected the message returned by plugin when all volumes in cluster are stopped Change-Id: Ie5e0fbbb10fa7d2787a82e1caa7ca86e8c7010a3 Bug-Url: https://bugzilla.redhat.com/1096730 Signed-off-by: Sahina Bose Reviewed-on: http://review.gluster.org/7780 --- plugins/check_cluster_vol_usage.py | 39 +++++++++++------ tests/test_check_cluster_volusage.py | 83 ++++++++++++++++++++---------------- 2 files changed, 72 insertions(+), 50 deletions(-) diff --git a/plugins/check_cluster_vol_usage.py b/plugins/check_cluster_vol_usage.py index 21439b0..522a42e 100755 --- a/plugins/check_cluster_vol_usage.py +++ b/plugins/check_cluster_vol_usage.py @@ -30,6 +30,8 @@ import sys import re from argparse import ArgumentParser +import json + import livestatus from glusternagios import utils @@ -37,25 +39,31 @@ from glusternagios import utils def checkVolumePerfData(clusterName): # Write command to socket - cmd = "GET services\nColumns: description host_name " \ - "perf_data custom_variables\nFilter: " \ - "description ~~ %s" % 'Volume Utilization -' - table = livestatus.readLiveStatus(cmd) + cmd = ("GET services\nColumns: description host_name " + "perf_data custom_variables\n" + "Filter: host_name = %s\n" + "Filter: description ~~ %s\n" + % (clusterName, 'Volume Utilization -')) + + perf_data_output = livestatus.readLiveStatusAsJSON(cmd) + perf_data = json.loads(perf_data_output) + numvolumes = 0 totalUsed = 0.0 totalAvail = 0.0 - for row in table: + for row in perf_data: + numvolumes += 1 if len(row) <= 3: return 0.0, 0.0 - host = row[1] + perf_data = row[2] if len(perf_data) > 2: perf_arr = perf_data.split(' ') used = perf_arr[2].split('=')[1] avail = perf_arr[1].split('=')[1] - if host == clusterName: - totalUsed += float(re.match(r'\d*\.?\d+', used).group()) - totalAvail += float(re.match(r'\d*\.?\d+', avail).group()) - return totalUsed, totalAvail + + totalUsed += float(re.match(r'\d*\.?\d+', used).group()) + totalAvail += float(re.match(r'\d*\.?\d+', avail).group()) + return numvolumes, totalUsed, totalAvail # Main method if __name__ == "__main__": @@ -82,13 +90,18 @@ if __name__ == "__main__": required=True) args = parser.parse_args() # Check the various performance statuses for the host - used, avail = checkVolumePerfData(args.hostgroup) + numVolumes, used, avail = checkVolumePerfData(args.hostgroup) statusstr = utils.PluginStatus.OK exitstatus = utils.PluginStatusCode.OK - if used == 0 and avail == 0: + if numVolumes == 0: + statusstr = utils.PluginStatus.OK + exitstatus = utils.PluginStatusCode.OK + print ("%s - No volumes found|used=0;%s;%s;0;%s;" + % (statusstr, args.warn, args.crit, 100)) + elif numVolumes > 0 and used == 0 and avail == 0: statusstr = utils.PluginStatus.UNKNOWN exitstatus = utils.PluginStatusCode.UNKNOWN - print ("%s - No volumes found" % statusstr) + print ("%s - Volume utilization data could not be read" % statusstr) else: warn = int((args.warn * avail) / 100.0) crit = int((args.crit * avail) / 100.0) diff --git a/tests/test_check_cluster_volusage.py b/tests/test_check_cluster_volusage.py index 54e75c6..958a12b 100644 --- a/tests/test_check_cluster_volusage.py +++ b/tests/test_check_cluster_volusage.py @@ -19,6 +19,7 @@ # import mock +import json from testrunner import PluginsTestCase as TestCaseBase from plugins import check_cluster_vol_usage @@ -27,54 +28,62 @@ from plugins import check_cluster_vol_usage class TestClusterVolUsage(TestCaseBase): # Method to test volume perf data when no matching host method - @mock.patch('plugins.livestatus.readLiveStatus') + @mock.patch('plugins.livestatus.readLiveStatusAsJSON') def test_checkVolumePerfDataNoMatch(self, mock_readLiveStatus): - mock_readLiveStatus.return_value = _getTable() - testTotalUsed, testTotalAvail = (check_cluster_vol_usage - .checkVolumePerfData - ("dummy-cluster")) + mock_readLiveStatus.return_value = _getJsonNoData() + numVolumes, testTotalUsed, testTotalAvail = (check_cluster_vol_usage + .checkVolumePerfData + ("dummy-cluster")) + assert numVolumes == 0 assert testTotalUsed == 0 - # Method to test volume perf data when no matching host method - @mock.patch('plugins.livestatus.readLiveStatus') + # Method to test volume perf data + @mock.patch('plugins.livestatus.readLiveStatusAsJSON') def test_checkVolumePerfDataMatch(self, mock_readLiveStatus): - mock_readLiveStatus.return_value = _getTable() - testTotalUsed, testTotalAvail = (check_cluster_vol_usage - .checkVolumePerfData - ("test-cluster")) + mock_readLiveStatus.return_value = _getJson() + numVolumes, testTotalUsed, testTotalAvail = (check_cluster_vol_usage + .checkVolumePerfData + ("test-cluster")) print ("testTotal %s" % testTotalUsed) + assert numVolumes == 2 assert (testTotalUsed == 700) assert (testTotalAvail == 1134) - # Method to test volume perf data when no matching host method - @mock.patch('plugins.livestatus.readLiveStatus') + # Method to test volume perf data when no perfdata method + @mock.patch('plugins.livestatus.readLiveStatusAsJSON') def test_checkVolumePerfNoData(self, mock_readLiveStatus): - mock_readLiveStatus.return_value = _getTableWithoutCustomAndPerData() - testTotalUsed, testTotalAvail = (check_cluster_vol_usage - .checkVolumePerfData - ("test-cluster")) + mock_readLiveStatus.return_value = _getJsonWithoutCustomAndPerData() + numVolumes, testTotalUsed, testTotalAvail = (check_cluster_vol_usage + .checkVolumePerfData + ("test-cluster")) + assert numVolumes == 2 assert testTotalUsed == 0 -def _getTable(): - table = [["Volume Utilization - data-vol", - "test-cluster", - "utilization=4%;70;90 total=734 used=300 free=434", - "VOL_NAME;data-vol"], - ["Volume Utilization - dist-rep", - "test-cluster", - "utilization=100%;70;90 total=400 used=400 free=0", - "VOL_NAME;dist-rep"]] - return table +def _getJson(): + jOut = [["Volume Utilization - dist", + "test-cluster", + "utilization=4%;70;90 total=734 used=300 free=434", + {"VOL_NAME": "dist", "GLUSTER_ENTITY": "Service"}], + ["Volume Utilization - rep", + "test-cluster", + "utilization=100%;70;90 total=400 used=400 free=0", + {"VOL_NAME": "rep", "GLUSTER_ENTITY": "Service"}]] + return json.dumps(jOut) + + +def _getJsonNoData(): + jOut = [] + return json.dumps(jOut) -def _getTableWithoutCustomAndPerData(): - table = [["brick1", - "test-cluster", - "", - ""], - ["brick2", - "test-cluster", - "", - ""]] - return table +def _getJsonWithoutCustomAndPerData(): + jOut = [["Volume Utilization - dist", + "test-cluster", + "", + {}], + ["Volume Utilization - rep", + "test-cluster", + "", + {}]] + return json.dumps(jOut) -- cgit