Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 1 | # Copyright 2016 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 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 5 | """Send system monitoring data to the timeseries monitoring API.""" |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 6 | |
| 7 | from __future__ import print_function |
| 8 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 9 | import random |
| 10 | import time |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 11 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 12 | import psutil |
| 13 | |
| 14 | from chromite.lib import commandline |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_logging as logging |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 16 | from chromite.lib import metrics |
| 17 | from chromite.lib import ts_mon_config |
| 18 | from chromite.scripts.sysmon import puppet_metrics |
| 19 | from chromite.scripts.sysmon import system_metrics |
Allen Li | 26d1008 | 2016-12-16 16:31:02 -0800 | [diff] [blame] | 20 | from chromite.scripts.sysmon import loop |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 21 | from infra_libs.ts_mon.common import interface |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 22 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 23 | DEFAULT_LOG_LEVEL = 'DEBUG' |
| 24 | DEFAULT_INTERVAL = 60 # 1 minute |
| 25 | |
| 26 | logger = logging.getLogger(__name__) |
| 27 | |
| 28 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 29 | def collect_metrics(cycles): |
| 30 | system_metrics.get_uptime() |
| 31 | system_metrics.get_cpu_info() |
| 32 | system_metrics.get_disk_info() |
| 33 | system_metrics.get_mem_info() |
| 34 | system_metrics.get_net_info() |
| 35 | system_metrics.get_proc_info() |
| 36 | system_metrics.get_load_avg() |
| 37 | puppet_metrics.get_puppet_summary() |
| 38 | if cycles == 0: |
| 39 | system_metrics.get_os_info() |
| 40 | system_metrics.get_unix_time() # must be just before flush |
| 41 | metrics.Flush() |
| 42 | |
| 43 | |
| 44 | def main(): |
| 45 | parser = commandline.ArgumentParser( |
| 46 | description=__doc__, |
| 47 | default_log_level=DEFAULT_LOG_LEVEL) |
| 48 | parser.add_argument( |
| 49 | '--interval', |
| 50 | default=DEFAULT_INTERVAL, type=int, |
| 51 | help='time (in seconds) between sampling system metrics') |
| 52 | opts = parser.parse_args() |
| 53 | opts.Freeze() |
| 54 | |
| 55 | # This returns a 0 value the first time it's called. Call it now and |
| 56 | # discard the return value. |
| 57 | psutil.cpu_times_percent() |
| 58 | |
| 59 | # Wait a random amount of time before starting the loop in case sysmon |
| 60 | # is started at exactly the same time on all machines. |
| 61 | time.sleep(random.uniform(0, opts.interval)) |
| 62 | |
| 63 | # This call returns a context manager that doesn't do anything, so we |
| 64 | # ignore the return value. |
| 65 | ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False) |
| 66 | # The default prefix is '/chrome/infra/'. |
| 67 | interface.state.metric_name_prefix = (interface.state.metric_name_prefix |
| 68 | + 'chromeos/sysmon/') |
| 69 | |
Allen Li | 26d1008 | 2016-12-16 16:31:02 -0800 | [diff] [blame] | 70 | loop.SleepLoop(callback=collect_metrics, |
| 71 | interval=opts.interval).loop_forever() |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | main() |