blob: 7aaa6f0a7a2d34e446e054791fb67268ed2b8d27 [file] [log] [blame]
Michael Liang5952fbe2014-07-24 13:51:26 -07001#!/usr/bin/env python
2
3# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# This file defines script for getting host_history for DUTs in Autotest.
8
9"""Script for checking host history for a selected group of hosts.
10
11Currently only supports aggregating stats for each host.
Michael Liang5952fbe2014-07-24 13:51:26 -070012
13Example usage:
Dan Shi17ecbbf2014-10-06 13:56:34 -070014 python host_history.py -n 10000 -l 24 --board=daisy
Michael Liang5952fbe2014-07-24 13:51:26 -070015
16Output:
17
18 trying to get all duts...
19 making the query...
20 found all duts. Time to get host_history.
21 usage stats for host: chromeos2-row5-rack1-host6
Dan Shidfea3682014-08-10 23:38:40 -070022 2014-07-24 10:24:07 - 2014-07-25 10:24:07
23 Verifying: 0.00 %
24 Running: 0.00 %
25 Ready: 100.00 %
26 Repairing: 0.00 %
27 Repair Failed: 0.00 %
28 Cleaning: 0.00 %
29 Pending: 0.00 %
30 Resetting: 0.00 %
31 Provisioning: 0.00 %
32 Locked: 0.00 %
Michael Liang5952fbe2014-07-24 13:51:26 -070033 - -- --- ---- ----- ---- --- -- -
34
35Example usage2: more than one host:
Dan Shi17ecbbf2014-10-06 13:56:34 -070036 python host_history.py -n 1000 -l 2 \
Michael Liang5952fbe2014-07-24 13:51:26 -070037 --hosts chromeos2-row5-rack4-host6 chromeos4-row12-rack11-host2
38
39 ['chromeos2-row5-rack4-host6', 'chromeos4-row12-rack11-host2']
40 found all duts. Time to get host_history.
41 usage stats for host: chromeos2-row5-rack4-host6
42 2014-07-25 13:02:22 - 2014-07-25 15:02:22
43 Num entries found in this interval: 0
Dan Shidfea3682014-08-10 23:38:40 -070044 Verifying: 0.00 %
45 Running: 0.00 %
46 Ready: 100.00 %
47 Repairing: 0.00 %
48 Repair Failed: 0.00 %
49 Cleaning: 0.00 %
50 Pending: 0.00 %
51 Resetting: 0.00 %
52 Provisioning: 0.00 %
53 Locked: 0.00 %
Michael Liang5952fbe2014-07-24 13:51:26 -070054 - -- --- ---- ----- ---- --- -- -
55
56 usage stats for host: chromeos4-row12-rack11-host2
57 2014-07-25 13:02:22 - 2014-07-25 15:02:22
58 Num entries found in this interval: 138
Dan Shidfea3682014-08-10 23:38:40 -070059 Verifying: 0.00 %
60 Running: 70.45 %
61 Ready: 17.79 %
62 Repairing: 0.00 %
63 Repair Failed: 0.00 %
64 Cleaning: 0.00 %
65 Pending: 1.24 %
66 Resetting: 10.78 %
67 Provisioning: 0.00 %
68 Locked: 0.00 %
Michael Liang5952fbe2014-07-24 13:51:26 -070069 - -- --- ---- ----- ---- --- -- -
70"""
71
Michael Liang93b4ba42014-07-30 15:09:44 -070072import argparse
Michael Liang5952fbe2014-07-24 13:51:26 -070073import time
Michael Liang93b4ba42014-07-30 15:09:44 -070074import traceback
Michael Liang5952fbe2014-07-24 13:51:26 -070075
76import common
Dan Shidfea3682014-08-10 23:38:40 -070077from autotest_lib.client.common_lib import time_utils
Michael Liang9e0be9d2014-08-07 09:48:42 -070078from autotest_lib.site_utils import host_history_utils
Michael Liang5952fbe2014-07-24 13:51:26 -070079
80
Michael Liang93b4ba42014-07-30 15:09:44 -070081def print_all_stats(results, labels, t_start, t_end):
82 """Prints overall stats followed by stats for each host.
83
Dan Shidfea3682014-08-10 23:38:40 -070084 @param results: A list of tuples of three elements.
85 1st element: String representing report for individual host.
86 2nd element: An ordered dictionary with
87 key as (t_start, t_end) and value as (status, metadata)
88 status = status of the host. e.g. 'Repair Failed'
89 t_start is the beginning of the interval where the DUT's has
90 that status
91 t_end is the end of the interval where the DUT has that
92 status
93 metadata: A dictionary of other metadata, e.g.,
94 {'task_id':123, 'task_name':'Reset'}
95 3rd element: hostname of the dut.
Michael Liang93b4ba42014-07-30 15:09:44 -070096 @param labels: A list of labels useful for describing the group
97 of hosts these overall stats represent.
98 @param t_start: beginning of time period we are interested in.
99 @param t_end: end of time period we are interested in.
100 """
Dan Shidfea3682014-08-10 23:38:40 -0700101 result_strs, stat_intervals_lst, hostname = zip(*results)
Michael Liang93b4ba42014-07-30 15:09:44 -0700102 overall_report_str = host_history_utils.get_overall_report(
103 labels, t_start, t_end, stat_intervals_lst)
104 # Print the overall stats
105 print overall_report_str
106 # Print the stats for each individual host.
107 for result_str in result_strs:
108 print result_str
109
110
111def get_host_history(input):
112 """Gets the host history.
113
114 @param input: A dictionary of input arguments to
115 host_history_utils.host_history_stats.
116 Must contain these keys:
Dan Shidfea3682014-08-10 23:38:40 -0700117 't_start',
118 't_end',
119 'hostname',
120 'size,'
121 'print_each_interval'
Michael Liang93b4ba42014-07-30 15:09:44 -0700122 @returns:
Dan Shidfea3682014-08-10 23:38:40 -0700123 result_str: String reporting history for specific host.
124 stat_intervals: A ordered dictionary with
125 key as (t_start, t_end) and value as (status, metadata)
126 status = status of the host. e.g. 'Repair Failed'
127 t_start is the beginning of the interval where the DUT's has
128 that status
129 t_end is the end of the interval where the DUT has that
130 status
131 metadata: A dictionary of other metadata, e.g.,
132 {'task_id':123, 'task_name':'Reset'}
Michael Liang93b4ba42014-07-30 15:09:44 -0700133 """
134 try:
135 result_str, stat_intervals = host_history_utils.get_report_for_host(
Dan Shidfea3682014-08-10 23:38:40 -0700136 **input)
137 return result_str, stat_intervals, input['hostname']
Michael Liang93b4ba42014-07-30 15:09:44 -0700138 except Exception as e:
Dan Shidfea3682014-08-10 23:38:40 -0700139 # In case any process throws an Exception, we want to see it.
Michael Liang93b4ba42014-07-30 15:09:44 -0700140 print traceback.print_exc()
Dan Shidfea3682014-08-10 23:38:40 -0700141 return None, None, None
142
143
Dan Shi17ecbbf2014-10-06 13:56:34 -0700144def get_results(start_time, end_time, hosts=None, board=None, pool=None,
145 verbose=False):
146 """Get history results of specified hosts or board/pool.
Dan Shidfea3682014-08-10 23:38:40 -0700147
148 If hosts is set to None, all hosts are used, filtered by the board and pool
149 constraints. If board is not provided, all boards are included. If pool is
150 not provided, all pools are included.
151 If a list of hosts is provided, the board and pool constraints are ignored.
152
Dan Shidfea3682014-08-10 23:38:40 -0700153 @param hosts: A list of hosts to search for history. Default is None.
154 @param board: board type of hosts. Default is None.
155 @param pool: pool type of hosts. Default is None.
156 @param start_time: start time to search for history, can be string value or
157 epoch time.
158 @param end_time: end time to search for history, can be string value or
159 epoch time.
Dan Shidfea3682014-08-10 23:38:40 -0700160 @param verbose: True to print out detail intervals of host history.
Dan Shi17ecbbf2014-10-06 13:56:34 -0700161
162 @returns: A dictionary of host history.
Dan Shidfea3682014-08-10 23:38:40 -0700163 """
164 assert start_time and end_time
165 start_time = time_utils.to_epoch_time(start_time)
166 end_time = time_utils.to_epoch_time(end_time)
167 assert start_time < end_time
168
Dan Shi17ecbbf2014-10-06 13:56:34 -0700169 return host_history_utils.get_report(t_start=start_time, t_end=end_time,
170 hosts=hosts, board=board, pool=pool,
171 print_each_interval=verbose)
Dan Shidfea3682014-08-10 23:38:40 -0700172
173
174def get_history_details(start_time, end_time, hosts=None, board=None,
Dan Shi17ecbbf2014-10-06 13:56:34 -0700175 pool=None):
Dan Shidfea3682014-08-10 23:38:40 -0700176 """Get the details of host history.
177
178 The return is a dictionary of host history for each host, for example,
179 {'172.22.33.51': [{'status': 'Resetting'
180 'start_time': '2014-08-07 10:02:16',
181 'end_time': '2014-08-07 10:03:16',
182 'log_url': 'http://autotest/reset-546546/debug',
183 'task_id': 546546},
184 {'status': 'Running'
185 'start_time': '2014-08-07 10:03:18',
186 'end_time': '2014-08-07 10:13:00',
187 'log_url': ('http://%s/tko/retrieve_logs.cgi?job=/'
188 'results/16853-debug/172.22.33.51'),
189 'job_id': 16853}
190 ]
191 }
192 @param start_time: start time to search for history, can be string value or
193 epoch time.
194 @param end_time: end time to search for history, can be string value or
195 epoch time.
196 @param hosts: A list of hosts to search for history. Default is None.
197 @param board: board type of hosts. Default is None.
198 @param pool: pool type of hosts. Default is None.
Dan Shidfea3682014-08-10 23:38:40 -0700199 @returns: A dictionary of the host history details.
200 """
Dan Shi17ecbbf2014-10-06 13:56:34 -0700201 results = get_results(start_time=start_time, end_time=end_time, hosts=hosts,
202 board=board, pool=pool)
Dan Shi1ccb6522014-09-19 17:15:30 -0700203 if not results:
204 # No host found.
205 return None
Dan Shidfea3682014-08-10 23:38:40 -0700206 all_history = {}
207 for result_str, status_intervals, hostname in results:
208 if hostname:
Dan Shi1ccb6522014-09-19 17:15:30 -0700209 all_history[hostname] = host_history_utils.build_history(
210 hostname, status_intervals)
Dan Shidfea3682014-08-10 23:38:40 -0700211 return all_history
Michael Liang93b4ba42014-07-30 15:09:44 -0700212
213
Michael Liang5952fbe2014-07-24 13:51:26 -0700214def main():
215 """main script. """
Michael Liang93b4ba42014-07-30 15:09:44 -0700216 t_now = time.time()
217 t_now_minus_one_day = t_now - 3600 * 24
Michael Liang5952fbe2014-07-24 13:51:26 -0700218 parser = argparse.ArgumentParser()
Michael Liang5952fbe2014-07-24 13:51:26 -0700219 parser.add_argument('-v', action='store_true', dest='verbose',
220 default=False,
Michael Liang9e0be9d2014-08-07 09:48:42 -0700221 help='-v to print out ALL entries.')
Michael Liang5952fbe2014-07-24 13:51:26 -0700222 parser.add_argument('-l', type=float, dest='last',
223 help='last hours to search results across',
Michael Liang93b4ba42014-07-30 15:09:44 -0700224 default=None)
Michael Liang5952fbe2014-07-24 13:51:26 -0700225 parser.add_argument('--board', type=str, dest='board',
226 help='restrict query by board, not implemented yet',
227 default=None)
228 parser.add_argument('--pool', type=str, dest='pool',
229 help='restrict query by pool, not implemented yet',
230 default=None)
231 parser.add_argument('--hosts', nargs='+', dest='hosts',
232 help='Enter space deliminated hostnames',
233 default=[])
Michael Liang93b4ba42014-07-30 15:09:44 -0700234 parser.add_argument('--start', type=str, dest='start',
Dan Shidfea3682014-08-10 23:38:40 -0700235 help=('Enter start time as: yyyy-mm-dd hh:mm:ss,'
Michael Liang93b4ba42014-07-30 15:09:44 -0700236 'defualts to 24h ago.'),
Dan Shidfea3682014-08-10 23:38:40 -0700237 default=time_utils.epoch_time_to_date_string(
Michael Liang329153d2014-08-06 15:31:25 -0700238 t_now_minus_one_day))
Michael Liang93b4ba42014-07-30 15:09:44 -0700239 parser.add_argument('--end', type=str, dest='end',
Dan Shidfea3682014-08-10 23:38:40 -0700240 help=('Enter end time in as: yyyy-mm-dd hh:mm:ss,'
Michael Liang93b4ba42014-07-30 15:09:44 -0700241 'defualts to current time.'),
Dan Shidfea3682014-08-10 23:38:40 -0700242 default=time_utils.epoch_time_to_date_string(t_now))
Michael Liang5952fbe2014-07-24 13:51:26 -0700243 options = parser.parse_args()
244
Michael Liang93b4ba42014-07-30 15:09:44 -0700245 if options.last:
Dan Shidfea3682014-08-10 23:38:40 -0700246 start_time = t_now - 3600 * options.last
247 end_time = t_now
Michael Liang93b4ba42014-07-30 15:09:44 -0700248 else:
Dan Shidfea3682014-08-10 23:38:40 -0700249 start_time = time_utils.to_epoch_time(options.start)
250 end_time = time_utils.to_epoch_time(options.end)
Michael Liang5952fbe2014-07-24 13:51:26 -0700251
Dan Shi17ecbbf2014-10-06 13:56:34 -0700252 results = get_results(hosts=options.hosts,
253 board=options.board,
254 pool=options.pool,
255 start_time=start_time,
256 end_time=end_time,
257 verbose=options.verbose)
Michael Liang93b4ba42014-07-30 15:09:44 -0700258 time.sleep(3)
259 labels = []
260 if options.board:
261 labels.append('board:%s' % (options.board))
262 if options.pool:
263 labels.append('pool:%s' % (options.pool))
Dan Shidfea3682014-08-10 23:38:40 -0700264 print_all_stats(results, labels, start_time, end_time)
Michael Liang5952fbe2014-07-24 13:51:26 -0700265
266
267if __name__ == '__main__':
Dan Shidfea3682014-08-10 23:38:40 -0700268 main()