|
|
@@ -17,30 +17,67 @@ |
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
# EXAMPLE:
|
|
|
-# $ gluster peer status --xml | ./xml.py --connected <PEER1> <PEER2> <PEERn>
|
|
|
+# $ gluster peer status --xml | ./xml.py connected <PEER1> <PEER2> <PEERn>
|
|
|
# <BOOL>
|
|
|
|
|
|
# EXAMPLE:
|
|
|
-# $ gluster volume --xml info <VOLNAME> | ./xml.py --property <KEY>
|
|
|
+# $ gluster volume info --xml <VOLNAME> | ./xml.py property --key <KEY>
|
|
|
# <VALUE>
|
|
|
|
|
|
+# EXAMPLE:
|
|
|
+# $ gluster volume status --xml [<VOLNAME>] | ./xml.py port --volume <VOLUME> --host <HOST> --path <PATH>
|
|
|
+# <PORT>
|
|
|
+
|
|
|
+# EXAMPLE:
|
|
|
+# $ gluster volume status --xml [<VOLNAME>] | ./xml.py ports [--volume <VOLUME>] [--host <HOST>]
|
|
|
+# <PORT1>[,<PORT2>[,<PORTn>]]
|
|
|
+
|
|
|
import sys
|
|
|
+import argparse
|
|
|
import lxml.etree as etree
|
|
|
|
|
|
-argv = sys.argv
|
|
|
-argv.pop(0) # get rid of $0
|
|
|
+parser = argparse.ArgumentParser(description='gluster xml parsing tools')
|
|
|
+#parser.add_argument('--debug', dest='debug', action='store_true', default=False)
|
|
|
+subparsers = parser.add_subparsers(dest='mode')
|
|
|
+
|
|
|
+#
|
|
|
+# 'connected' parser
|
|
|
+#
|
|
|
+parser_connected = subparsers.add_parser('connected')
|
|
|
+parser_connected.add_argument('peers', type=str, nargs='*', action='store')
|
|
|
+
|
|
|
+#
|
|
|
+# 'property' parser
|
|
|
+#
|
|
|
+parser_property = subparsers.add_parser('property')
|
|
|
+parser_property.add_argument('--key', dest='key', action='store')
|
|
|
+
|
|
|
+#
|
|
|
+# 'port' parser
|
|
|
+#
|
|
|
+parser_port = subparsers.add_parser('port')
|
|
|
+parser_port.add_argument('--volume', dest='volume', action='store', required=True)
|
|
|
+parser_port.add_argument('--host', dest='host', action='store', required=True)
|
|
|
+parser_port.add_argument('--path', dest='path', action='store', required=True)
|
|
|
|
|
|
-if len(argv) < 1:
|
|
|
- sys.exit(3)
|
|
|
+#
|
|
|
+# 'ports' parser
|
|
|
+#
|
|
|
+parser_ports = subparsers.add_parser('ports')
|
|
|
+parser_ports.add_argument('--volume', dest='volume', action='store', required=False)
|
|
|
+parser_ports.add_argument('--host', dest='host', action='store', required=False)
|
|
|
|
|
|
-mode = argv.pop(0)
|
|
|
+#
|
|
|
+# final setup...
|
|
|
+#
|
|
|
+args = parser.parse_args()
|
|
|
tree = etree.parse(sys.stdin)
|
|
|
root = tree.getroot()
|
|
|
|
|
|
# are all the hostnames in argv connected ?
|
|
|
-if mode == '--connected':
|
|
|
+if args.mode == 'connected':
|
|
|
store = {}
|
|
|
- peers = [x for x in argv if x != '']
|
|
|
+ peers = args.peers
|
|
|
|
|
|
for i in root.findall('.//peerStatus'):
|
|
|
p = i.find('peer')
|
|
|
@@ -64,13 +101,10 @@ |
|
|
# must be good!
|
|
|
sys.exit(0)
|
|
|
|
|
|
-elif mode == '--property':
|
|
|
- if len(argv) != 1:
|
|
|
- sys.exit(3)
|
|
|
-
|
|
|
+elif args.mode == 'property':
|
|
|
store = []
|
|
|
for i in root.findall('.//option'):
|
|
|
- if i.find('name').text == str(argv[0]):
|
|
|
+ if str(i.find('name').text) == args.key:
|
|
|
store.append(i.find('value').text)
|
|
|
|
|
|
if len(store) == 1:
|
|
|
@@ -79,6 +113,66 @@ |
|
|
else: # more than one value found
|
|
|
sys.exit(1)
|
|
|
|
|
|
+elif args.mode == 'port':
|
|
|
+ port = 0
|
|
|
+ found = False
|
|
|
+ #print args.volume # volume
|
|
|
+ #print args.host # hostname
|
|
|
+ #print args.path # path
|
|
|
+ for i in root.findall('.//volumes'):
|
|
|
+ for j in i.findall('.//volume'):
|
|
|
+ v = str(j.find('volName').text)
|
|
|
+ #print v
|
|
|
+ for k in j.findall('.//node'):
|
|
|
+ h = str(k.find('hostname').text)
|
|
|
+ p = str(k.find('path').text)
|
|
|
+ #print h, p
|
|
|
+ #if v == args.volume and h == args.host and p == args.path:
|
|
|
+ if (v, h, p) == (args.volume, args.host, args.path):
|
|
|
+ if found:
|
|
|
+ # we have already found a match.
|
|
|
+ # there's a bug somewhere...
|
|
|
+ sys.exit(2)
|
|
|
+ found = True
|
|
|
+ port = int(k.find('port').text)
|
|
|
+
|
|
|
+ if found and port > 0:
|
|
|
+ print(port)
|
|
|
+ sys.exit(0)
|
|
|
+ else: # no value found
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
+# list all the ports used by one volume
|
|
|
+elif args.mode == 'ports':
|
|
|
+ ports = []
|
|
|
+ found = False
|
|
|
+ #print args.volume # volume (optional)
|
|
|
+ for i in root.findall('.//volumes'):
|
|
|
+ for j in i.findall('.//volume'):
|
|
|
+ v = str(j.find('volName').text)
|
|
|
+ #print v
|
|
|
+ # if no volume is specified, we use all of them...
|
|
|
+ if args.volume is None or args.volume == v:
|
|
|
+ for k in j.findall('.//node'):
|
|
|
+ h = str(k.find('hostname').text)
|
|
|
+ p = str(k.find('path').text)
|
|
|
+ #print h, p
|
|
|
+ if args.host is None or args.host == h:
|
|
|
+ try:
|
|
|
+ ports.append(int(k.find('port').text))
|
|
|
+ found = True
|
|
|
+ except ValueError, e:
|
|
|
+ pass
|
|
|
+
|
|
|
+ if found and len(ports) > 0:
|
|
|
+ # NOTE: you may get duplicates if you lookup multiple hosts...
|
|
|
+ # here we remove any duplicates and convert each int to strings
|
|
|
+ print(','.join([str(x) for x in list(set(ports))]))
|
|
|
+ sys.exit(0)
|
|
|
+ else: # no value found
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
# else:
|
|
|
sys.exit(3)
|
|
|
|
|
|
+# vim: ts=8
|
0 comments on commit
269be1c