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 |
| 20 | from infra_libs.ts_mon.common import interface |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 21 | |
Allen Li | 788d167 | 2016-12-16 15:58:23 -0800 | [diff] [blame^] | 22 | DEFAULT_LOG_LEVEL = 'DEBUG' |
| 23 | DEFAULT_INTERVAL = 60 # 1 minute |
| 24 | |
| 25 | logger = logging.getLogger(__name__) |
| 26 | |
| 27 | |
| 28 | class _MainLoop(object): |
| 29 | """Main loop for sysmon.""" |
| 30 | |
| 31 | def __init__(self, loop_func, interval=60): |
| 32 | """Initialize instance. |
| 33 | |
| 34 | Args: |
| 35 | loop_func: Function to call on each loop. |
| 36 | interval: Time between loops in seconds. |
| 37 | """ |
| 38 | self._loop_func = loop_func |
| 39 | self._interval = interval |
| 40 | self._cycles = 0 |
| 41 | |
| 42 | def loop_once(self): |
| 43 | """Do actions for a single loop.""" |
| 44 | try: |
| 45 | self._loop_func(self._cycles) |
| 46 | except Exception: |
| 47 | logger.exception('Error during loop.') |
| 48 | |
| 49 | def loop_forever(self): |
| 50 | while True: |
| 51 | self.loop_once() |
| 52 | _force_sleep(self._interval) |
| 53 | self._cycles = (self._cycles + 1) % 60 |
| 54 | |
| 55 | |
| 56 | def _force_sleep(secs): |
| 57 | """Force sleep for at least the given number of seconds.""" |
| 58 | now = time.time() |
| 59 | finished_time = now + secs |
| 60 | while now < finished_time: |
| 61 | remaining = finished_time - now |
| 62 | logger.debug('Sleeping for %d, %d remaining', secs, remaining) |
| 63 | time.sleep(remaining) |
| 64 | now = time.time() |
| 65 | |
| 66 | |
| 67 | def collect_metrics(cycles): |
| 68 | system_metrics.get_uptime() |
| 69 | system_metrics.get_cpu_info() |
| 70 | system_metrics.get_disk_info() |
| 71 | system_metrics.get_mem_info() |
| 72 | system_metrics.get_net_info() |
| 73 | system_metrics.get_proc_info() |
| 74 | system_metrics.get_load_avg() |
| 75 | puppet_metrics.get_puppet_summary() |
| 76 | if cycles == 0: |
| 77 | system_metrics.get_os_info() |
| 78 | system_metrics.get_unix_time() # must be just before flush |
| 79 | metrics.Flush() |
| 80 | |
| 81 | |
| 82 | def main(): |
| 83 | parser = commandline.ArgumentParser( |
| 84 | description=__doc__, |
| 85 | default_log_level=DEFAULT_LOG_LEVEL) |
| 86 | parser.add_argument( |
| 87 | '--interval', |
| 88 | default=DEFAULT_INTERVAL, type=int, |
| 89 | help='time (in seconds) between sampling system metrics') |
| 90 | opts = parser.parse_args() |
| 91 | opts.Freeze() |
| 92 | |
| 93 | # This returns a 0 value the first time it's called. Call it now and |
| 94 | # discard the return value. |
| 95 | psutil.cpu_times_percent() |
| 96 | |
| 97 | # Wait a random amount of time before starting the loop in case sysmon |
| 98 | # is started at exactly the same time on all machines. |
| 99 | time.sleep(random.uniform(0, opts.interval)) |
| 100 | |
| 101 | # This call returns a context manager that doesn't do anything, so we |
| 102 | # ignore the return value. |
| 103 | ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False) |
| 104 | # The default prefix is '/chrome/infra/'. |
| 105 | interface.state.metric_name_prefix = (interface.state.metric_name_prefix |
| 106 | + 'chromeos/sysmon/') |
| 107 | |
| 108 | mainloop = _MainLoop(loop_func=collect_metrics, |
| 109 | interval=opts.interval) |
| 110 | mainloop.loop_forever() |
| 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | main() |