summaryrefslogtreecommitdiff
path: root/server/status.py
blob: 382626af845c61daf92a4847ab5db3d608f75f81 (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
#!/usr/bin/python
import sys, re, os, itertools


class Machine:
	"""
	Represents the current state of a machine. Possible values are:
		TESTING     currently running a test
		REBOOTING   currently rebooting
		BROKEN      busted somehow (e.g. reboot timed out)
		OTHER       none of the above

	The implementation is basically that of a state machine. From an
	external point of view the only relevant attributes are:
	        details     text description of the current status
		test_count  number of tests run
	"""
	def __init__(self):
		self.state = "OTHER"
		self.details = "Running"
		self.test_name = ""
		self.test_count = 0


	def process_line(self, line):
		self.handlers[self.state](self, line)


	def _OTHER_handler(self, line):
		match = self.job_start.match(line)
		if match and match.group(2) != "----":
			self.state = "TESTING"
			self.tab_level = len(match.group(1))
			self.test_name = match.group(2)
			self.test_status = "GOOD"
			self.details = "Running %s" % self.test_name
			return

		match = self.reboot_start.match(line)
		if match:
			self.boot_status = match.group(1)
			if self.worse_status("GOOD", self.boot_status) == "GOOD":
				self.state = "REBOOTING"
				self.details = "Rebooting"
			else:
				self.state = "BROKEN"
				self.details = "Reboot failed - machine broken"
			return


	def _TESTING_handler(self, line):
		match = self.job_status.match(line)
		if match:
			if len(match.group(1)) != self.tab_level + 1:
				return   # we don't care about subgroups
			if self.test_name != match.group(3):
				return   # we don't care about other tests
			self.test_status = self.worse_status(self.test_status,
							     match.group(2))
			self.details = "Running %s: %s" % (self.test_name,
							    match.group(4))
			return

		match = self.job_end.match(line)
		if match:
			if len(match.group(1)) != self.tab_level:
				return   # we don't care about subgroups
			if self.test_name != match.group(3):
				raise ValueError('Group START and END name mismatch')
			self.state = "OTHER"
			self.test_status = self.worse_status(self.test_status,
							     match.group(2))
			self.test_name = ""
			del self.test_status
			self.details = "Running"
			self.test_count += 1
			return


	def _REBOOTING_handler(self, line):
		match = self.reboot_done.match(line)
		if match:
			status = self.worse_status(self.boot_status,
						   match.group(1))
			del self.boot_status
			if status == "GOOD":
				self.state = "OTHER"
				self.details = "Running"
			else:
				self.state = "BROKEN"
				self.details = "Reboot failed - machine broken"
			return


	def _BROKEN_handler(self, line):
		pass    # just do nothing - we're broken and staying broken


	handlers = {"OTHER": _OTHER_handler,
		    "TESTING": _TESTING_handler,
		    "REBOOTING": _REBOOTING_handler,
		    "BROKEN": _BROKEN_handler}


	status_list = ["GOOD", "WARN", "FAIL", "ABORT", "ERROR"]
	order_dict = {None: -1}
	order_dict.update((status, i)
			  for i, status in enumerate(status_list))


	job_start = re.compile(r"^(\t*)START\t----\t([^\t]+).*$")
	job_status = re.compile(r"^(\t*)(%s)\t([^\t]+)\t(?:[^\t]+).*\t([^\t]+)$" %
				"|".join(status_list))
	job_end = re.compile(r"^(\t*)END (%s)\t----\t([^\t]+).*$" %
			     "|".join(status_list))
	reboot_start = re.compile(r"^\t?(%s)\t[^\t]+\treboot\.start.*$" %
				  "|".join(status_list))
	reboot_done = re.compile(r"^\t?(%s)\t[^\t]+\treboot\.verify.*$" %
				 "|".join(status_list))

	@classmethod
	def worse_status(cls, old_status, new_status):
		if cls.order_dict[new_status] > cls.order_dict[old_status]:
			return new_status
		else:
			return old_status


def parse_status(status_log):
	"""\
	Parse the status from a single status log.
	Do not use with status logs from multi-machine tests.
	"""
	parser = Machine()
	for line in file(status_log):
		parser.process_line(line)
	result = {
	    "status": parser.details,
	    "test_on": parser.test_name,
	    "test_num_complete": parser.test_count
	    }
	return result


def _file_iterator(filename):
	"""\
	Return an iterator over file(filename), or an empty iterator
	if the file does not exist.
	"""
	if os.path.exists(filename):
		return iter(file(filename))
	else:
		return ()


def parse_machine_status(root_path, name):
	"""Parse the status for one machine (of a multi-machine test)"""
	general_log = _file_iterator(os.path.join(root_path, "status.log"))
	machine_log = _file_iterator(os.path.join(root_path, name, "status.log"))
	timestamp_regex = re.compile("\ttimestamp=(\d+)")
	# collect all the lines from both the root & machine-specific log
	lines = []
	timestamp = 0
	for line in itertools.chain(general_log, machine_log):
		timestamp_match = timestamp_regex.search(line)
		# if the log line has a timestamp, use it
		# otherwise, just use the timestamp from the previous line
		if timestamp_match:
			timestamp = int(timestamp_match.group(1))
		lines.append((timestamp, line))
	lines.sort()  # this will sort the lines by timestamp
	# now actually run the lines through the parser
	parser = Machine()
	for timestamp, line in lines:
		parser.process_line(line)
	return {
	    "status": parser.details,
	    "test_on": parser.test_name,
	    "test_num_complete": parser.test_count
	    }


def parse_multimachine_status(root_path, machine_names):
	"""Parse the status for a set of machines."""
	results = {}
	for name in machine_names:
		results[name] = parse_machine_status(root_path, name)
	return results


if __name__ == "__main__":
 	args = sys.argv[1:]
 	if len(args) != 1:
 		print "USAGE: status.py status_log"
 		sys.exit(1)
	print parse_status(args[0])