From 896389e4b529367021dcd90e7e3a1d1a2b1f006a Mon Sep 17 00:00:00 2001 From: hari gowtham Date: Wed, 31 Jan 2018 10:15:13 +0530 Subject: quota: lib to check if hard and soft limit are exceed. This is a library to check if quota' hard and soft limit are exceeded from the output of the quota list xml command. Change-Id: Ie02ab9fcbf2aa2d248e0cb6385ab3d3f0554dec0 Signed-off-by: hari gowtham --- .../glustolibs/gluster/exceptions.py | 14 ++++ glustolibs-gluster/glustolibs/gluster/quota_ops.py | 88 ++++++++++++++++++++++ 2 files changed, 102 insertions(+) (limited to 'glustolibs-gluster/glustolibs/gluster') diff --git a/glustolibs-gluster/glustolibs/gluster/exceptions.py b/glustolibs-gluster/glustolibs/gluster/exceptions.py index 71591bf7b..42ee48441 100644 --- a/glustolibs-gluster/glustolibs/gluster/exceptions.py +++ b/glustolibs-gluster/glustolibs/gluster/exceptions.py @@ -15,3 +15,17 @@ class ExecutionError(Exception): ''' pass + + +class ExecutionParseError(Exception): + ''' + Custom exception thrown when parsing a command executed by Glusto + results in an unexpected error. + + For example, the output of a command when has to be parsed, can have three + states. First, the output was as expected. Second, didn't get the expected + ouput after the parsing result and Third, didn't get the expected result as + the command itself failed. + + ''' + pass diff --git a/glustolibs-gluster/glustolibs/gluster/quota_ops.py b/glustolibs-gluster/glustolibs/gluster/quota_ops.py index 5a7f50fcb..3a6ca89c7 100644 --- a/glustolibs-gluster/glustolibs/gluster/quota_ops.py +++ b/glustolibs-gluster/glustolibs/gluster/quota_ops.py @@ -20,6 +20,7 @@ """ from glusto.core import Glusto as g +from glustolibs.gluster.exceptions import ExecutionError, ExecutionParseError try: import xml.etree.cElementTree as etree @@ -224,6 +225,93 @@ def get_quota_list(mnode, volname, path=None): return quotalist +def is_hard_limit_exceeded(mnode, volname, path=None): + """Parse the output of 'gluster quota list' command. + + Args: + mnode (str): Node on which command has to be executed. + volname (str): volume name + + Kwargs: + path (str): Quota path + + Returns: + boolean: True if exceeded, False if not. + + Examples: + >>> get_quota_list('abc.lab.eng.xyz.com', "testvol") + {'/': {'used_space': '0', 'hl_exceeded': 'No', 'soft_limit_percent': + '60%', 'avail_space': '2147483648', 'soft_limit_value': '1288490188', + 'sl_exceeded': 'No', 'hard_limit': '2147483648'}} + """ + if not path: + path = '' + + cmd = "gluster volume quota %s list %s --xml" % (volname, path) + ret, out, _ = g.run(mnode, cmd) + if ret != 0: + g.log.error("Failed to execute 'quota list' on node %s. " + "Hence failed to get the quota list." % + mnode) + raise ExecutionError("Quota list --xml command failed") + else: + try: + root = etree.XML(out) + except etree.ParseError: + raise ExecutionParseError("Failed to parse the gluster quota " + "list xml output.") + else: + for path in root.findall("volQuota/limit"): + for elem in path.getchildren(): + if elem.tag == 'hl_exceeded': + if elem.text == 'Yes': + return True + return False + + +def is_soft_limit_exceeded(mnode, volname, path=None): + """Parse the output of 'gluster quota list' command. + + Args: + mnode (str): Node on which command has to be executed. + volname (str): volume name + + Kwargs: + path (str): Quota path + + Returns: + boolean: True if exceeded, False if not. + Examples: + >>> get_quota_list('abc.lab.eng.xyz.com', "testvol") + {'/': {'used_space': '0', 'hl_exceeded': 'No', 'soft_limit_percent': + '60%', 'avail_space': '2147483648', 'soft_limit_value': '1288490188', + 'sl_exceeded': 'No', 'hard_limit': '2147483648'}} + """ + if not path: + path = '' + + cmd = "gluster volume quota %s list %s --xml" % (volname, path) + ret, out, _ = g.run(mnode, cmd) + if ret != 0: + g.log.error("Failed to execute 'quota list' on node %s. " + "Hence failed to get the quota list." % + mnode) + raise ExecutionError("Quota list --xml command failed") + if ret == 0: + try: + root = etree.XML(out) + except etree.ParseError: + raise ExecutionParseError("Failed to parse the gluster quota " + "list xml output.") + else: + for path in root.findall("volQuota/limit"): + for elem in path.getchildren(): + if elem.tag == 'sl_exceeded': + if elem.text == 'Yes': + return True + return False + + def set_quota_limit_objects(mnode, volname, path='/', limit='10', soft_limit=''): """Sets limit-objects on the path of the specified volume to -- cgit