analyze_reboot_time.py revision 55bfe14a6c2cc2710593ecf4d461af64181915c0
1b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang#!/usr/bin/env python 2b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 3b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 4b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang# Use of this source code is governed by a BSD-style license that can be 5b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang# found in the LICENSE file. 6b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 7b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang# This file defines script for getting entries from ES concerning reboot time. 8b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 9b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang""" 10b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael LiangExample usage: 11b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang python analyze_reboot_time.py -l 12 --server cautotest --board daisy_spring 12b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 13b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael LiangUsage: analyze_reboot_time.py [-h] [-l LAST] --server AUTOTEST_SERVER 14b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang [--board BOARD] [--pool POOL] [--start START] 15b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang [--end END] [--gte GTE] [--lte LTE] [-n SIZE] 16b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang [--hosts HOSTS [HOSTS ...]] 17b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 18b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangoptional arguments: 19b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang -h, --help show this help message and exit 20b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang -l LAST last hours to search results across 21b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --server AUTOTEST_SERVER 22b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang Enter Autotest instance name, e.g. "cautotest". 23b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --board BOARD restrict query by board, not implemented yet 24b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --pool POOL restrict query by pool, not implemented yet 25b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --start START Enter start time as: yyyy-mm-dd hh-mm-ss,defualts to 26b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 24h ago. 27b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --end END Enter end time as: yyyy-mm-dd hh-mm-ss,defualts to 28b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang current time. 29b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --gte GTE Enter lower bound on reboot time for entries to 30b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return. 31b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --lte LTE Enter upper bound on reboot time for entries to 32b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return. 33b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang -n SIZE Maximum number of entries to return. 34b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang --hosts HOSTS [HOSTS ...] 35b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang Enter space deliminated hostnames 36b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang""" 37b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 38b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangimport argparse 39b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangimport time 40b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 41b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangimport common 42dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shiimport host_history 43dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shifrom autotest_lib.client.common_lib import time_utils 44b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangfrom autotest_lib.client.common_lib.cros.graphite import es_utils 45b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 46b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 47b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangdef get_entries(time_start, time_end, gte, lte, size, index, hostname): 48b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang """Gets all entries from es db with the given constraints. 49b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 50b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param time_start: Earliest time entry was recorded 51b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param time_end: Latest time entry was recorded 52b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param gte: Lowest reboot_time to return 53b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param lte: Highest reboot_time to return 54b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param size: Max number of entries to return 55b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param index: es db index to get entries for, i.e. 'cautotest' 56b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param hostname: string representing hostname to query for 57b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @returns: Entries from esdb. 58b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang """ 5955bfe14a6c2cc2710593ecf4d461af64181915c0Gabe Black time_start_epoch = time_utils.to_epoch_time(time_start) 6055bfe14a6c2cc2710593ecf4d461af64181915c0Gabe Black time_end_epoch = time_utils.to_epoch_time(time_end) 6155bfe14a6c2cc2710593ecf4d461af64181915c0Gabe Black gte_epoch = time_utils.to_epoch_time(gte) 6255bfe14a6c2cc2710593ecf4d461af64181915c0Gabe Black lte_epoch = time_utils.to_epoch_time(lte) 63b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang query = es_utils.create_range_eq_query_multiple( 64b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang fields_returned=['hostname', 'time_recorded', 'value'], 65b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang equality_constraints=[('_type', 'reboot_total'), 66b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang ('hostname', hostname)], 6755bfe14a6c2cc2710593ecf4d461af64181915c0Gabe Black range_constraints=[('time_recorded', time_start_epoch, time_end_epoch), 6855bfe14a6c2cc2710593ecf4d461af64181915c0Gabe Black ('value', gte_epoch, lte_epoch)], 69b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang size=size, 70b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang sort_specs=[{'hostname': 'asc'}, {'value': 'desc'}]) 71b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang results = es_utils.execute_query(query, index=index, 72b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang host=es_utils.METADATA_ES_SERVER, 73b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang port=es_utils.ES_PORT) 74b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return results 75b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 76b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 77b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangdef get_results_string(hostname, time_start, time_end, results): 78b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang """Prints entries from esdb in a readable fashion. 79b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 80b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param hostname: Hostname of DUT we are printing result for. 81b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param time_start: Earliest time entry was recorded 82b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param time_end: Latest time entry was recorded 83b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param gte: Lowest reboot_time to return 84b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param lte: Highest reboot_time to return 85b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @param size: Max number of entries to return 86b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang @returns: String reporting reboot times for this host. 87b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang """ 88b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang num_entries = results['hits']['total'] 89b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return_string = ' Host: %s \n Number of entries: %s \n' % ( 90b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang hostname, results['hits']['total']) 91b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return_string += ' %s - %s \n' % ( 92dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi time_utils.epoch_time_to_date_string(time_start), 93dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi time_utils.epoch_time_to_date_string(time_end)) 94b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang if num_entries <= 0: 95b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return return_string 96b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang for result in results['hits']['hits']: 97b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang fields = result['fields'] 98b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang time_recorded = fields['time_recorded'][0] 99dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi time_string = time_utils.epoch_time_to_date_string( 100b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang time_recorded) 101b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang reboot_total = fields['value'][0] 102b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang spaces = (15 - len(str(time_string))) * ' ' 103b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return_string += ' %s Reboot_time: %.3fs\n' % ( 104b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang time_string, reboot_total) 105b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang return return_string 106b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 107b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 108b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangif __name__ == '__main__': 109b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang """main script""" 110b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang t_now = time.time() 111b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang t_now_minus_one_day = t_now - 3600 * 24 112b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser = argparse.ArgumentParser() 113b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('-l', type=float, dest='last', 114b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help='last hours to search results across', 115b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=24) 116b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--server', type=str, dest='autotest_server', 117b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang required=True, 118b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help='Enter Autotest instance name, e.g. "cautotest".') 119b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--board', type=str, dest='board', 120b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help='restrict query by board, not implemented yet', 121b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=None) 122b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--pool', type=str, dest='pool', 123b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help='restrict query by pool, not implemented yet', 124b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=None) 125b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--start', type=str, dest='start', 126b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help=('Enter start time as: yyyy-mm-dd hh-mm-ss,' 127b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 'defualts to 24h ago.'), 128b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=t_now_minus_one_day) 129b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--end', type=str, dest='end', 130b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help=('Enter end time as: yyyy-mm-dd hh-mm-ss,' 131b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 'defualts to current time.'), 132b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=t_now) 133b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--gte', type=float, dest='gte', 134b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help=('Enter lower bound on reboot time ' 135b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 'for entries to return.'), 136b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=0) 137b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--lte', type=float, dest='lte', 138b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help=('Enter upper bound on reboot time ' 139b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 'for entries to return.'), 140b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=None) 141b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('-n', type=int, dest='size', 142b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help='Maximum number of entries to return.', 143b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=10000) 144b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang parser.add_argument('--hosts', nargs='+', dest='hosts', 145b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang help='Enter space deliminated hostnames', 146b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang default=[]) 147b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang options = parser.parse_args() 148b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 149b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang if options.last: 150b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang t_start = t_now - 3600 * options.last 151b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang t_end = t_now 152b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang else: 153dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi t_start = time_utils.to_epoch_time(options.start) 154dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi t_end = time_utils.to_epoch_time(options.end) 155b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang if options.hosts: 156b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang hosts = options.hosts 157b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang else: 158dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi hosts = host_history.get_matched_hosts(options.autotest_server, 159dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi options.board, options.pool) 160b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang 161b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang for hostname in hosts: 162b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang results = get_entries( 163b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang t_start, t_end, options.gte, options.lte, options.size, 164b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang options.autotest_server, hostname) 165b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang print get_results_string(hostname, t_start, t_end, results) 166