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 |
Allen Li | baa422d | 2016-12-16 18:21:10 -0800 | [diff] [blame^] | 18 | from chromite.scripts.sysmon import loop |
| 19 | from chromite.scripts.sysmon import osinfo_metrics |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 20 | from chromite.scripts.sysmon import puppet_metrics |
| 21 | from chromite.scripts.sysmon import system_metrics |
| 22 | from infra_libs.ts_mon.common import interface |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 23 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 24 | logger = logging.getLogger(__name__) |
| 25 | |
| 26 | |
Allen Li | baa422d | 2016-12-16 18:21:10 -0800 | [diff] [blame^] | 27 | class _MetricCollector(object): |
Allen Li | 38de641 | 2016-12-16 16:52:45 -0800 | [diff] [blame] | 28 | """Metric collector class.""" |
| 29 | |
| 30 | def __init__(self): |
Allen Li | baa422d | 2016-12-16 18:21:10 -0800 | [diff] [blame^] | 31 | self._get_osinfo = _TimedCallback( |
| 32 | callback=osinfo_metrics.get_os_info, |
| 33 | interval=60 * 60) |
Allen Li | 38de641 | 2016-12-16 16:52:45 -0800 | [diff] [blame] | 34 | |
| 35 | def __call__(self): |
| 36 | """Collect metrics.""" |
| 37 | system_metrics.get_uptime() |
| 38 | system_metrics.get_cpu_info() |
| 39 | system_metrics.get_disk_info() |
| 40 | system_metrics.get_mem_info() |
| 41 | system_metrics.get_net_info() |
| 42 | system_metrics.get_proc_info() |
| 43 | system_metrics.get_load_avg() |
| 44 | puppet_metrics.get_puppet_summary() |
Allen Li | baa422d | 2016-12-16 18:21:10 -0800 | [diff] [blame^] | 45 | self._get_osinfo() |
Allen Li | 38de641 | 2016-12-16 16:52:45 -0800 | [diff] [blame] | 46 | system_metrics.get_unix_time() # must be just before flush |
| 47 | metrics.Flush() |
| 48 | |
| 49 | @property |
| 50 | def _next_osinfo_collection(self): |
| 51 | return self._last_osinfo_collection + (60 * 60) |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 52 | |
| 53 | |
Allen Li | baa422d | 2016-12-16 18:21:10 -0800 | [diff] [blame^] | 54 | class _TimedCallback(object): |
| 55 | """Limits callback to one call in a given interval.""" |
| 56 | |
| 57 | def __init__(self, callback, interval): |
| 58 | """Initialize instance. |
| 59 | |
| 60 | Args: |
| 61 | callback: function to call |
| 62 | interval: Number of seconds between allowed calls |
| 63 | """ |
| 64 | self._callback = callback |
| 65 | self._interval = interval |
| 66 | self._last_called = time.time() - interval |
| 67 | |
| 68 | def __call__(self): |
| 69 | if time.time() > self._next_call: |
| 70 | self._callback() |
| 71 | self._last_called = time.time() |
| 72 | |
| 73 | @property |
| 74 | def _next_call(self): |
| 75 | return self._last_called + self._interval |
| 76 | |
| 77 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 78 | def main(): |
| 79 | parser = commandline.ArgumentParser( |
| 80 | description=__doc__, |
Allen Li | 337729d | 2016-12-16 16:45:49 -0800 | [diff] [blame] | 81 | default_log_level='DEBUG') |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 82 | parser.add_argument( |
| 83 | '--interval', |
Allen Li | 337729d | 2016-12-16 16:45:49 -0800 | [diff] [blame] | 84 | default=60, |
| 85 | type=int, |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 86 | help='time (in seconds) between sampling system metrics') |
| 87 | opts = parser.parse_args() |
| 88 | opts.Freeze() |
| 89 | |
| 90 | # This returns a 0 value the first time it's called. Call it now and |
| 91 | # discard the return value. |
| 92 | psutil.cpu_times_percent() |
| 93 | |
| 94 | # Wait a random amount of time before starting the loop in case sysmon |
| 95 | # is started at exactly the same time on all machines. |
| 96 | time.sleep(random.uniform(0, opts.interval)) |
| 97 | |
| 98 | # This call returns a context manager that doesn't do anything, so we |
| 99 | # ignore the return value. |
| 100 | ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False) |
| 101 | # The default prefix is '/chrome/infra/'. |
| 102 | interface.state.metric_name_prefix = (interface.state.metric_name_prefix |
| 103 | + 'chromeos/sysmon/') |
| 104 | |
Allen Li | baa422d | 2016-12-16 18:21:10 -0800 | [diff] [blame^] | 105 | loop.SleepLoop(callback=_MetricCollector(), |
Allen Li | 26d1008 | 2016-12-16 16:31:02 -0800 | [diff] [blame] | 106 | interval=opts.interval).loop_forever() |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame] | 107 | |
| 108 | |
| 109 | if __name__ == '__main__': |
| 110 | main() |