site_sysinfo.py revision c60b981b0a2e02035db7184948800c1176336e85
1# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7import re
8
9from autotest_lib.client.common_lib import log
10from autotest_lib.client.common_lib import error, utils, global_config
11from autotest_lib.client.bin import base_sysinfo
12from autotest_lib.client.cros import constants
13
14get_value = global_config.global_config.get_config_value
15collect_corefiles = get_value('CLIENT', 'collect_corefiles',
16                              type=bool, default=True)
17
18
19logfile = base_sysinfo.logfile
20command = base_sysinfo.command
21
22
23class logdir(base_sysinfo.loggable):
24    """Represents a log directory."""
25    def __init__(self, directory, additional_exclude=None):
26        super(logdir, self).__init__(directory, log_in_keyval=False)
27        self.dir = directory
28        self.additional_exclude = additional_exclude
29
30
31    def __repr__(self):
32        return "site_sysinfo.logdir(%r, %s)" % (self.dir,
33                                                self.additional_exclude)
34
35
36    def __eq__(self, other):
37        if isinstance(other, logdir):
38            return (self.dir == other.dir and
39                    self.additional_exclude == other.additional_exclude)
40        elif isinstance(other, base_sysinfo.loggable):
41            return False
42        return NotImplemented
43
44
45    def __ne__(self, other):
46        result = self.__eq__(other)
47        if result is NotImplemented:
48            return result
49        return not result
50
51
52    def __hash__(self):
53        return hash(self.dir) + hash(self.additional_exclude)
54
55
56    def run(self, log_dir):
57        """Copies this log directory to the specified directory.
58
59        @param log_dir: The destination log directory.
60        """
61        if os.path.exists(self.dir):
62            parent_dir = os.path.dirname(self.dir)
63            utils.system("mkdir -p %s%s" % (log_dir, parent_dir))
64            # Take source permissions and add ugo+r so files are accessible via
65            # archive server.
66            additional_exclude_str = ""
67            if self.additional_exclude:
68                additional_exclude_str = "--exclude=" + self.additional_exclude
69
70            utils.system("rsync --no-perms --chmod=ugo+r -a --exclude=autoserv*"
71                         " %s %s %s%s" % (additional_exclude_str, self.dir,
72                                          log_dir, parent_dir))
73
74
75class file_stat(object):
76    """Store the file size and inode, used for retrieving new data in file."""
77    def __init__(self, file_path):
78        """Collect the size and inode information of a file.
79
80        @param file_path: full path to the file.
81
82        """
83        stat = os.stat(file_path)
84        # Start size of the file, skip that amount of bytes when do diff.
85        self.st_size = stat.st_size
86        # inode of the file. If inode is changed, treat this as a new file and
87        # copy the whole file.
88        self.st_ino = stat.st_ino
89
90
91class diffable_logdir(logdir):
92    """Represents a log directory that only new content will be copied.
93
94    An instance of this class should be added in both
95    before_iteration_loggables and after_iteration_loggables. This is to
96    guarantee the file status information is collected when run method is
97    called in before_iteration_loggables, and diff is executed when run
98    method is called in after_iteration_loggables.
99
100    """
101    def __init__(self, directory, additional_exclude=None,
102                 keep_file_hierarchy=True, append_diff_in_name=True):
103        """
104        Constructor of a diffable_logdir instance.
105
106        @param directory: directory to be diffed after an iteration finished.
107        @param additional_exclude: additional dir to be excluded, not used.
108        @param keep_file_hierarchy: True if need to preserve full path, e.g.,
109            sysinfo/var/log/sysstat, v.s. sysinfo/sysstat if it's False.
110        @param append_diff_in_name: True if you want to append '_diff' to the
111            folder name to indicate it's a diff, e.g., var/log_diff. Option
112            keep_file_hierarchy must be True for this to take effect.
113
114        """
115        super(diffable_logdir, self).__init__(directory, additional_exclude)
116        self.additional_exclude = additional_exclude
117        self.keep_file_hierarchy = keep_file_hierarchy
118        self.append_diff_in_name = append_diff_in_name
119        # Init dictionary to store all file status for files in the directory.
120        self._log_stats = {}
121
122
123    def _get_init_status_of_src_dir(self, src_dir):
124        """Get initial status of files in src_dir folder.
125
126        @param src_dir: directory to be diff-ed.
127
128        """
129        # Dictionary used to store the initial status of files in src_dir.
130        for file_path in self._get_all_files(src_dir):
131            self._log_stats[file_path] = file_stat(file_path)
132        self.file_stats_collected = True
133
134
135    def _get_all_files(self, path):
136        """Iterate through files in given path including subdirectories.
137
138        @param path: root directory.
139        @return: an iterator that iterates through all files in given path
140            including subdirectories.
141
142        """
143        if not os.path.exists(path):
144            yield []
145        for root, dirs, files in os.walk(path):
146            for f in files:
147                if f.startswith('autoserv'):
148                    continue
149                yield os.path.join(root, f)
150
151
152    def _copy_new_data_in_file(self, file_path, src_dir, dest_dir):
153        """Copy all new data in a file to target directory.
154
155        @param file_path: full path to the file to be copied.
156        @param src_dir: source directory to do the diff.
157        @param dest_dir: target directory to store new data of src_dir.
158
159        """
160        bytes_to_skip = 0
161        if self._log_stats.has_key(file_path):
162            prev_stat = self._log_stats[file_path]
163            new_stat = os.stat(file_path)
164            if new_stat.st_ino == prev_stat.st_ino:
165                bytes_to_skip = prev_stat.st_size
166            if new_stat.st_size == bytes_to_skip:
167                return
168            elif new_stat.st_size < prev_stat.st_size:
169                # File is modified to a smaller size, copy whole file.
170                bytes_to_skip = 0
171        try:
172            with open(file_path, 'r') as in_log:
173                if bytes_to_skip > 0:
174                    in_log.seek(bytes_to_skip)
175                # Skip src_dir in path, e.g., src_dir/[sub_dir]/file_name.
176                target_path = os.path.join(dest_dir,
177                                           os.path.relpath(file_path, src_dir))
178                target_dir = os.path.dirname(target_path)
179                if not os.path.exists(target_dir):
180                    os.makedirs(target_dir)
181                with open(target_path, "w") as out_log:
182                    out_log.write(in_log.read())
183        except IOError as e:
184            logging.error('Diff %s failed with error: %s', file_path, e)
185
186
187    def _log_diff(self, src_dir, dest_dir):
188        """Log all of the new data in src_dir to dest_dir.
189
190        @param src_dir: source directory to do the diff.
191        @param dest_dir: target directory to store new data of src_dir.
192
193        """
194        if self.keep_file_hierarchy:
195            dir = src_dir.lstrip('/')
196            if self.append_diff_in_name:
197                dir = dir.rstrip('/') + '_diff'
198            dest_dir = os.path.join(dest_dir, dir)
199
200        if not os.path.exists(dest_dir):
201            os.makedirs(dest_dir)
202
203        for src_file in self._get_all_files(src_dir):
204            self._copy_new_data_in_file(src_file, src_dir, dest_dir)
205
206
207    def run(self, log_dir, collect_init_status=True):
208        """Copies new content from self.dir to the destination log_dir.
209
210        @param log_dir: The destination log directory.
211        @param collect_init_status: Set to True if run method is called to
212            collect the initial status of files.
213
214        """
215        if collect_init_status:
216            self._get_init_status_of_src_dir(self.dir)
217        elif os.path.exists(self.dir):
218            self._log_diff(self.dir, log_dir)
219
220
221class purgeable_logdir(logdir):
222    """Represents a log directory that will be purged."""
223    def __init__(self, directory, additional_exclude=None):
224        super(purgeable_logdir, self).__init__(directory, additional_exclude)
225        self.additional_exclude = additional_exclude
226
227    def run(self, log_dir):
228        """Copies this log dir to the destination dir, then purges the source.
229
230        @param log_dir: The destination log directory.
231        """
232        super(purgeable_logdir, self).run(log_dir)
233
234        if os.path.exists(self.dir):
235            utils.system("rm -rf %s/*" % (self.dir))
236
237
238class site_sysinfo(base_sysinfo.base_sysinfo):
239    """Represents site system info."""
240    _CHROME_VERSION_COMMAND = constants.BROWSER_EXE + " --version"
241
242
243    def __init__(self, job_resultsdir):
244        super(site_sysinfo, self).__init__(job_resultsdir)
245        crash_exclude_string = None
246        if not collect_corefiles:
247            crash_exclude_string = "*.core"
248
249        # This is added in before and after_iteration_loggables. When run is
250        # called in before_iteration_loggables, it collects file status in
251        # the directory. When run is called in after_iteration_loggables, diff
252        # is executed.
253        diffable_log = diffable_logdir(constants.LOG_DIR)
254        self.diffable_loggables = set()
255        self.diffable_loggables.add(diffable_log)
256
257        # add in some extra command logging
258        self.boot_loggables.add(command("ls -l /boot",
259                                        "boot_file_list"))
260        self.before_iteration_loggables.add(
261            command(self._CHROME_VERSION_COMMAND, "chrome_version"))
262        self.test_loggables.add(
263            purgeable_logdir(
264                os.path.join(constants.CRYPTOHOME_MOUNT_PT, "log")))
265        # We only want to gather and purge crash reports after the client test
266        # runs in case a client test is checking that a crash found at boot
267        # (such as a kernel crash) is handled.
268        self.after_iteration_loggables.add(
269            purgeable_logdir(
270                os.path.join(constants.CRYPTOHOME_MOUNT_PT, "crash"),
271                additional_exclude=crash_exclude_string))
272        self.after_iteration_loggables.add(
273            purgeable_logdir(constants.CRASH_DIR,
274                             additional_exclude=crash_exclude_string))
275        self.test_loggables.add(
276            logfile(os.path.join(constants.USER_DATA_DIR,
277                                 ".Google/Google Talk Plugin/gtbplugin.log")))
278        self.test_loggables.add(purgeable_logdir(
279                constants.CRASH_DIR,
280                additional_exclude=crash_exclude_string))
281        # Collect files under /tmp/crash_reporter, which contain the procfs
282        # copy of those crashed processes whose core file didn't get converted
283        # into minidump. We need these additional files for post-mortem analysis
284        # of the conversion failure.
285        self.test_loggables.add(
286            purgeable_logdir(constants.CRASH_REPORTER_RESIDUE_DIR))
287
288
289    @log.log_and_ignore_errors("pre-test sysinfo error:")
290    def log_before_each_test(self, test):
291        """Logging hook called before a test starts.
292
293        @param test: A test object.
294        """
295        super(site_sysinfo, self).log_before_each_test(test)
296
297        for log in self.diffable_loggables:
298            log.run(log_dir=None, collect_init_status=True)
299
300
301    @log.log_and_ignore_errors("post-test sysinfo error:")
302    def log_after_each_test(self, test):
303        """Logging hook called after a test finishs.
304
305        @param test: A test object.
306        """
307        super(site_sysinfo, self).log_after_each_test(test)
308
309        test_sysinfodir = self._get_sysinfodir(test.outputdir)
310        for log in self.diffable_loggables:
311            log.run(log_dir=test_sysinfodir, collect_init_status=False)
312
313
314    def _get_chrome_version(self):
315        """Gets the Chrome version number and milestone as strings.
316
317        Invokes "chrome --version" to get the version number and milestone.
318
319        @return A tuple (chrome_ver, milestone) where "chrome_ver" is the
320            current Chrome version number as a string (in the form "W.X.Y.Z")
321            and "milestone" is the first component of the version number
322            (the "W" from "W.X.Y.Z").  If the version number cannot be parsed
323            in the "W.X.Y.Z" format, the "chrome_ver" will be the full output
324            of "chrome --version" and the milestone will be the empty string.
325
326        """
327        version_string = utils.system_output(self._CHROME_VERSION_COMMAND)
328        match = re.search('(\d+)\.\d+\.\d+\.\d+', version_string)
329        ver = match.group(0) if match else version_string
330        milestone = match.group(1) if match else ''
331        return ver, milestone
332
333
334    def log_test_keyvals(self, test_sysinfodir):
335        keyval = super(site_sysinfo, self).log_test_keyvals(test_sysinfodir)
336
337        lsb_lines = utils.system_output(
338            "cat /etc/lsb-release",
339            ignore_status=True).splitlines()
340        lsb_dict = dict(item.split("=") for item in lsb_lines)
341
342        for lsb_key in lsb_dict.keys():
343            # Special handling for build number
344            if lsb_key == "CHROMEOS_RELEASE_DESCRIPTION":
345                keyval["CHROMEOS_BUILD"] = (
346                    lsb_dict[lsb_key].rstrip(")").split(" ")[3])
347            keyval[lsb_key] = lsb_dict[lsb_key]
348
349        # Get the hwid (hardware ID), if applicable.
350        try:
351            keyval["hwid"] = utils.system_output('crossystem hwid')
352        except error.CmdError:
353            # The hwid may not be available (e.g, when running on a VM).
354            # If the output of 'crossystem mainfw_type' is 'nonchrome', then
355            # we expect the hwid to not be avilable, and we can proceed in this
356            # case.  Otherwise, the hwid is missing unexpectedly.
357            mainfw_type = utils.system_output('crossystem mainfw_type')
358            if mainfw_type == 'nonchrome':
359                logging.info(
360                    'HWID not available; not logging it as a test keyval.')
361            else:
362                logging.exception('HWID expected but could not be identified; '
363                                  'output of "crossystem mainfw_type" is "%s"',
364                                  mainfw_type)
365                raise
366
367        # Get the chrome version and milestone numbers.
368        keyval["CHROME_VERSION"], keyval["MILESTONE"] = (
369                self._get_chrome_version())
370
371        # Return the updated keyvals.
372        return keyval
373