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 | logger = logging.getLogger(__name__) |
| 24 | |
| 25 | |
Allen Li | 38de641 | 2016-12-16 16:52:45 -0800 | [diff] [blame] | 26 | class MetricCollector(object): |
| 27 | """Metric collector class.""" |
| 28 | |
| 29 | def __init__(self): |
| 30 | self._last_osinfo_collection = time.time() |
| 31 | |
| 32 | def __call__(self): |
| 33 | """Collect metrics.""" |
| 34 | system_metrics.get_uptime() |
| 35 | system_metrics.get_cpu_info() |
| 36 | system_metrics.get_disk_info() |
| 37 | system_metrics.get_mem_info() |
| 38 | system_metrics.get_net_info() |
| 39 | system_metrics.get_proc_info() |
| 40 | system_metrics.get_load_avg() |
| 41 | puppet_metrics.get_puppet_summary() |
| 42 | if time.time() > self._next_osinfo_collection: |
| 43 | system_metrics.get_os_info() |
| 44 | self._last_osinfo_collection = time.time() |
| 45 | system_metrics.get_unix_time() # must be just before flush |
| 46 | metrics.Flush() |
| 47 | |
| 48 | @property |
| 49 | def _next_osinfo_collection(self): |
| 50 | return self._last_osinfo_collection + (60 * 60) |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def main(): |
| 54 | parser = commandline.ArgumentParser( |
| 55 | description=__doc__, |
Allen Li | 337729d | 2016-12-16 16:45:49 -0800 | [diff] [blame] | 56 | default_log_level='DEBUG') |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 57 | parser.add_argument( |
| 58 | '--interval', |
Allen Li | 337729d | 2016-12-16 16:45:49 -0800 | [diff] [blame] | 59 | default=60, |
| 60 | type=int, |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 61 | help='time (in seconds) between sampling system metrics') |
| 62 | opts = parser.parse_args() |
| 63 | opts.Freeze() |
| 64 | |
| 65 | # This returns a 0 value the first time it's called. Call it now and |
| 66 | # discard the return value. |
| 67 | psutil.cpu_times_percent() |
| 68 | |
| 69 | # Wait a random amount of time before starting the loop in case sysmon |
| 70 | # is started at exactly the same time on all machines. |
| 71 | time.sleep(random.uniform(0, opts.interval)) |
| 72 | |
| 73 | # This call returns a context manager that doesn't do anything, so we |
| 74 | # ignore the return value. |
| 75 | ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False) |
| 76 | # The default prefix is '/chrome/infra/'. |
| 77 | interface.state.metric_name_prefix = (interface.state.metric_name_prefix |
| 78 | + 'chromeos/sysmon/') |
| 79 | |
Allen Li | 38de641 | 2016-12-16 16:52:45 -0800 | [diff] [blame] | 80 | loop.SleepLoop(callback=MetricCollector(), |
Allen Li | 26d1008 | 2016-12-16 16:31:02 -0800 | [diff] [blame] | 81 | interval=opts.interval).loop_forever() |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 82 | |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | main() |