• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2#
3# Copyright 2016 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# Collect network stats from the DUT.
8# For now just collect the byte count from the default interface.
9
10from autotest_lib.client.common_lib import utils
11
12def get_network_stats(machine):
13    try:
14        dut = hosts.create_target_machine(machine)
15
16        # The information is not critical, so ping the DUT first
17        # and if it doesn't reply quickly, give up.
18        if utils.ping(dut.hostname, tries=1, timeout=3) != 0:
19            logging.info('ping failed: not collecting network stats')
20            return
21
22        # In a single ssh call, get list of network interfaces
23        # and their byte counts.
24        result = dut.run('route; echo SEPARATOR; cat /proc/net/dev')
25
26        # Split output
27        lines = result.stdout.splitlines()
28        separator_index = lines.index('SEPARATOR')
29        route_lines = lines[:separator_index]
30        proc_lines = lines[separator_index+1:]
31
32        for line in route_lines:
33            fields = line.split()
34            # look for default network interface
35            if fields[0] == 'default':
36                iface = fields[7]
37                iface_prefix = iface + ':'
38                break
39        else:
40            logging.info('get_network_stats: no default interface')
41            return
42
43        for line in proc_lines:
44            fields = line.split()
45            # Look for the interface in /proc/net/dev.
46            if fields[0] == iface_prefix:
47                logging.info('get_network_stats: %s RXbytes %s TXbytes %s',
48                             network_stats_label, fields[1], fields[9])
49                break
50        else:
51            logging.info('get_network_stats: iface %s not in /proc/net/dev',
52                         iface)
53    except Exception as e:
54        logging.info('get_network_stats: ignoring exception: %s', e)
55
56
57job.parallel_simple(get_network_stats, machines)
58