analyze_reboot_time.py revision dfea368e5c830b1d7950ced5ee7b191e3b141ca3
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    """
59b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    query = es_utils.create_range_eq_query_multiple(
60b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        fields_returned=['hostname', 'time_recorded', 'value'],
61b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        equality_constraints=[('_type', 'reboot_total'),
62b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                              ('hostname', hostname)],
63b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        range_constraints=[('time_recorded', time_start, time_end),
64b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                           ('value', gte, lte)],
65b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        size=size,
66b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        sort_specs=[{'hostname': 'asc'}, {'value': 'desc'}])
67b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    results = es_utils.execute_query(query, index=index,
68b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                                     host=es_utils.METADATA_ES_SERVER,
69b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                                     port=es_utils.ES_PORT)
70b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    return results
71b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang
72b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang
73b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangdef get_results_string(hostname, time_start, time_end, results):
74b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    """Prints entries from esdb in a readable fashion.
75b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang
76b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    @param hostname: Hostname of DUT we are printing result for.
77b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    @param time_start: Earliest time entry was recorded
78b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    @param time_end: Latest time entry was recorded
79b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    @param gte: Lowest reboot_time to return
80b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    @param lte: Highest reboot_time to return
81b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    @param size: Max number of entries to return
82b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    @returns: String reporting reboot times for this host.
83b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    """
84b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    num_entries = results['hits']['total']
85b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    return_string = ' Host: %s \n   Number of entries: %s \n' % (
86b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang            hostname, results['hits']['total'])
87b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    return_string += ' %s - %s \n' % (
88dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi            time_utils.epoch_time_to_date_string(time_start),
89dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi            time_utils.epoch_time_to_date_string(time_end))
90b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    if num_entries <= 0:
91b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        return return_string
92b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    for result in results['hits']['hits']:
93b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        fields = result['fields']
94b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        time_recorded = fields['time_recorded'][0]
95dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi        time_string = time_utils.epoch_time_to_date_string(
96b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                time_recorded)
97b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        reboot_total = fields['value'][0]
98b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        spaces = (15 - len(str(time_string))) * ' '
99b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        return_string += '    %s  Reboot_time:  %.3fs\n' % (
100b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                time_string, reboot_total)
101b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    return return_string
102b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang
103b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang
104b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liangif __name__ == '__main__':
105b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    """main script"""
106b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    t_now = time.time()
107b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    t_now_minus_one_day = t_now - 3600 * 24
108b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser = argparse.ArgumentParser()
109b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('-l', type=float, dest='last',
110b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help='last hours to search results across',
111b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=24)
112b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--server', type=str, dest='autotest_server',
113b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        required=True,
114b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help='Enter Autotest instance name, e.g. "cautotest".')
115b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--board', type=str, dest='board',
116b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help='restrict query by board, not implemented yet',
117b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=None)
118b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--pool', type=str, dest='pool',
119b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help='restrict query by pool, not implemented yet',
120b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=None)
121b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--start', type=str, dest='start',
122b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help=('Enter start time as: yyyy-mm-dd hh-mm-ss,'
123b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                              'defualts to 24h ago.'),
124b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=t_now_minus_one_day)
125b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--end', type=str, dest='end',
126b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help=('Enter end time as: yyyy-mm-dd hh-mm-ss,'
127b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                              'defualts to current time.'),
128b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=t_now)
129b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--gte', type=float, dest='gte',
130b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help=('Enter lower bound on reboot time '
131b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                              'for entries to return.'),
132b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=0)
133b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--lte', type=float, dest='lte',
134b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help=('Enter upper bound on reboot time '
135b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                              'for entries to return.'),
136b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=None)
137b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('-n', type=int, dest='size',
138b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help='Maximum number of entries to return.',
139b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=10000)
140b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    parser.add_argument('--hosts', nargs='+', dest='hosts',
141b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        help='Enter space deliminated hostnames',
142b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                        default=[])
143b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    options = parser.parse_args()
144b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang
145b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    if options.last:
146b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        t_start = t_now - 3600 * options.last
147b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        t_end = t_now
148b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    else:
149dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi        t_start = time_utils.to_epoch_time(options.start)
150dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi        t_end = time_utils.to_epoch_time(options.end)
151b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    if options.hosts:
152b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        hosts = options.hosts
153b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    else:
154dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi        hosts = host_history.get_matched_hosts(options.autotest_server,
155dfea368e5c830b1d7950ced5ee7b191e3b141ca3Dan Shi                                               options.board, options.pool)
156b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang
157b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang    for hostname in hosts:
158b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        results = get_entries(
159b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                t_start, t_end, options.gte, options.lte, options.size,
160b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang                options.autotest_server, hostname)
161b1d6c73d8cbc45d04591453f8b80cb744017ddf9Michael Liang        print get_results_string(hostname, t_start, t_end, results)
162