summaryrefslogtreecommitdiffstats
path: root/files/xml.py
blob: d9473c21ef7faecca2ad04582c7250c76a8479c0 (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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# 	EXAMPLE:
#	$ gluster peer status --xml | ./xml.py connected <PEER1> <PEER2> <PEERn>
#	<BOOL>

# 	EXAMPLE:
#	$ 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

#	List of state codes:
#		<MESSAGE>					<CODE>
#	static char *glusterd_friend_sm_state_names[] = {	# glusterd-sm.c
#		"Establishing Connection",			# 0
#		"Probe Sent to Peer",				# 1
#		"Probe Received from Peer",			# 2
#		"Peer in Cluster",				# 3 (verified)
#		"Accepted peer request",			# 4
#		"Sent and Received peer request",		# 5
#		"Peer Rejected",				# 6 (verified)
#		"Peer detach in progress",			# 7
#		"Probe Received from peer",			# 8
#		"Connected to Peer",				# 9
#		"Peer is connected and Accepted",		# 10
#		"Invalid State"					# 11
#	};
valid_peered = ['3']

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)

#
#	'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)

#
#	final setup...
#
args = parser.parse_args()
tree = etree.parse(sys.stdin)
root = tree.getroot()

# are all the hostnames in argv connected ?
if args.mode == 'connected':
	store = {}
	peers = args.peers

	for i in root.findall('.//peerStatus'):
		p = i.find('peer')
		h = p.find('hostname').text
		c = (str(p.find('connected').text) == '1')	# connected...?
		s = (str(p.find('state').text) in valid_peered)	# valid peering
		store[h] = c and s	# save for later...

	# if no peers specified, assume we should check all...
	if len(peers) == 0:
		peers = store.keys()

	for i in peers:
		if i in store.keys():
			if not store[i]:
				# someone is unconnected
				sys.exit(1)
		else:
			# we're looking for a peer that isn't peered yet
			sys.exit(2)

	# must be good!
	sys.exit(0)

elif args.mode == 'property':
	store = []
	for i in root.findall('.//option'):
		if str(i.find('name').text) == args.key:
			store.append(i.find('value').text)

	if len(store) == 1:
		print(store[0])
		sys.exit(0)
	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