summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/profile_ops.py
blob: 76286aa3f64860678834a2575561ef309a6da3c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
#  Copyright (C) 2019 Red Hat, Inc. <http://www.redhat.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""
    Description: Library for volume profile operations.
"""
from glusto.core import Glusto as g
from pprint import pformat
try:
    import xml.etree.cElementTree as etree
except ImportError:
    import xml.etree.ElementTree as etree


def profile_start(mnode, volname):
    """Start profile on the specified volume.

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): Volume on which profile has to started.

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        profile_start(mnode, "testvol")
    """
    cmd = "gluster volume profile %s start" % volname
    return g.run(mnode, cmd)


def profile_info(mnode, volname, options=''):
    """Run profile info on the specified volume.

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): Volume for which profile info has to be retrived.

    Kwargs:
        options (str): Options can be
        [peek|incremental [peek]|cumulative|clear].If not given the
        function returns the output of gluster volume profile <volname> info.

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.
        None: If invalid option is given.

    Example:
        profile_info(mnode, "testvol")
    """
    if not check_profile_options(options):
        return None
    cmd = "gluster volume profile %s info %s" % (volname, options)
    return g.run(mnode, cmd)


def profile_stop(mnode, volname):
    """Stop profile on the specified volume.

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): Volume on which profile has to be stopped.

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        profile_stop(mnode, "testvol")
    """
    cmd = "gluster volume profile %s stop" % volname
    return g.run(mnode, cmd)


def check_profile_options(options):
    """Helper function to valid if profile options.

    Args:
        options (str): Options can be nothing or
        [peek|incremental [peek]|cumulative|clear].

    Returns:
        True: If valid option is given.
        False: If invalid option is given
    """

    list_of_options = ['peek', 'incremental', 'incremental peek',
                       'cumulative', 'clear', '']
    if options not in list_of_options:
        g.log.error("Invalid profile info option given.")
        return False
    return True


def get_profile_info(mnode, volname, options=''):
    """Fetches the volume profile information as displayed in the volume
        profile info.
        Uses xml output of volume profile info and parses the
        into to a dict

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): Volume for which profile info has to be retrived.

    Kwargs:
        options (str): Options can be
        [peek|incremental [peek]|cumulative|clear].If not given the
        function returns the output of gluster volume profile <volname> info.

    Returns:
        NoneType: If there are errors.
        dict: Volume profile info in dict of dicts

    Example:
        get_profile_info(mnode, "testvol")
    """

    if not check_profile_options(options):
        return None

    cmd = "gluster volume profile %s info %s --xml" % (volname, options)
    ret, out, err = g.run(mnode, cmd, log_level='DEBUG')
    if ret:
        g.log.error("Profile not running on volume.")
        return None

    # Iterating through the XML and creating dict
    root = etree.XML(out)
    volprofileinfo = {}
    volume = root.find("volProfile")
    brick_counter = 0
    for elem in volume.getchildren():
        if elem.tag == "volname":
            volname = elem.text
            volprofileinfo[volname] = {}
        elif elem.tag == "brick":
            brick_counter += 1
            volprofileinfo[volname][elem.tag+str(brick_counter)] = {}
            brick_dict = volprofileinfo[volname][elem.tag+str(brick_counter)]
            for brick_tag in elem.getchildren():
                if 'cumulativeStats' == brick_tag.tag:
                    brick_dict["cumulativeStats"] = {}
                    for el in brick_tag.getchildren():
                        if el.tag == 'duration':
                            brick_dict["cumulativeStats"][el.tag] = el.text
                        elif el.tag == 'totalWrite' or el.tag == 'totalRead':
                            brick_dict["cumulativeStats"][el.tag] = el.text
                        elif el.tag == 'blockStats':
                            brick_dict["cumulativeStats"][el.tag] = {}
                            block_dict = brick_dict["cumulativeStats"][el.tag]
                            counter = 0
                            for block in el.getchildren():
                                counter += 1
                                block_dict[block.tag+str(counter)] = {}
                                elm_dict = block_dict[block.tag+str(counter)]
                                for block_elm in block.getchildren():
                                    elm_dict[block_elm.tag] = block_elm.text
                        elif el.tag == 'fopStats':
                            brick_dict["cumulativeStats"][el.tag] = {}
                            fop_dict = brick_dict["cumulativeStats"][el.tag]
                            fop_count = 0
                            for fops in el.getchildren():
                                fop_dict['fop'+str(fop_count)] = {}
                                fop_param = fop_dict['fop'+str(fop_count)]
                                for fop in fops.getchildren():
                                    fop_param[fop.tag] = fop.text
                                fop_count += 1
                elif 'intervalStats' == brick_tag.tag:
                    brick_dict["intervalStats"] = {}
                    for el in brick_tag.getchildren():
                        if el.tag == 'duration':
                            brick_dict["intervalStats"][el.tag] = el.text
                        elif el.tag == 'totalWrite' or el.tag == 'totalRead':
                            brick_dict["intervalStats"][el.tag] = el.text
                        elif el.tag == 'blockStats':
                            brick_dict["intervalStats"][el.tag] = {}
                            block_dict = brick_dict["intervalStats"][el.tag]
                            counter = 0
                            for block in el.getchildren():
                                counter += 1
                                block_dict[block.tag+str(counter)] = {}
                                elm_dict = block_dict[block.tag+str(counter)]
                                for block_elm in block.getchildren():
                                    elm_dict[block_elm.tag] = block_elm.text
                        elif el.tag == 'fopStats':
                            brick_dict["intervalStats"][el.tag] = {}
                            fop_dict = brick_dict["intervalStats"][el.tag]
                            fop_count = 0
                            for fops in el.getchildren():
                                fop_dict['fop'+str(fop_count)] = {}
                                fop_param = fop_dict['fop'+str(fop_count)]
                                for fop in fops.getchildren():
                                    fop_param[fop.tag] = fop.text
                                fop_count += 1
                else:
                    brick_dict[brick_tag.tag] = brick_tag.text
        else:
            volprofileinfo[elem.tag] = elem.text

    g.log.debug("Volume profile info output: %s"
                % pformat(volprofileinfo, indent=10))
    return volprofileinfo


def profile_info_clear(mnode, volname):
    """Run profile info clear on the specified volume.

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): Volume for which profile info has to be retrived.

    Returns:
        tuple: Tuple containing three elements (ret, out, err).

            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

    Example:
        profile_info_clear(mnode, "testvol")
    """
    cmd = "gluster volume profile %s info clear" % (volname)
    return g.run(mnode, cmd)