Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 2 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Send system monitoring data to the timeseries monitoring API.""" |
| 7 | |
| 8 | from __future__ import absolute_import |
| 9 | from __future__ import print_function |
| 10 | |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 11 | import time |
| 12 | |
| 13 | from chromite.lib import commandline |
| 14 | from chromite.lib import cros_logging as logging |
| 15 | from chromite.lib import metrics |
| 16 | from chromite.lib import ts_mon_config |
| 17 | from infra_libs.ts_mon.common import interface |
| 18 | |
| 19 | from chromite.scripts.sysmon import git_metrics |
| 20 | from chromite.scripts.sysmon import loop |
| 21 | from chromite.scripts.sysmon import net_metrics |
| 22 | from chromite.scripts.sysmon import osinfo_metrics |
| 23 | from chromite.scripts.sysmon import proc_metrics |
| 24 | from chromite.scripts.sysmon import prod_metrics |
| 25 | from chromite.scripts.sysmon import puppet_metrics |
| 26 | from chromite.scripts.sysmon import system_metrics |
| 27 | |
| 28 | logger = logging.getLogger(__name__) |
| 29 | |
| 30 | |
| 31 | class _MetricCollector(object): |
| 32 | """Metric collector class.""" |
| 33 | |
| 34 | def __init__(self, collect_prod_hosts=False): |
| 35 | self._collect_osinfo = _TimedCallback( |
| 36 | callback=osinfo_metrics.collect_os_info, |
| 37 | interval=60 * 60) |
| 38 | if collect_prod_hosts: |
| 39 | logger.info(u'Enabling prod host metric collection.') |
| 40 | self._collect_prod_hosts = _TimedCallback( |
| 41 | callback=prod_metrics.collect_prod_hosts, |
| 42 | interval=10 * 60) |
| 43 | else: |
| 44 | self._collect_prod_hosts = lambda: None |
| 45 | |
| 46 | def __call__(self): |
| 47 | """Collect metrics.""" |
| 48 | system_metrics.collect_uptime() |
| 49 | system_metrics.collect_cpu_info() |
| 50 | system_metrics.collect_disk_info() |
| 51 | system_metrics.collect_mem_info() |
| 52 | net_metrics.collect_net_info() |
| 53 | proc_metrics.collect_proc_info() |
| 54 | system_metrics.collect_load_avg() |
| 55 | puppet_metrics.collect_puppet_summary() |
| 56 | git_metrics.collect_git_metrics() |
| 57 | self._collect_prod_hosts() |
| 58 | self._collect_osinfo() |
| 59 | system_metrics.collect_unix_time() # must be just before flush |
| 60 | metrics.Flush() |
| 61 | |
| 62 | |
| 63 | class _TimedCallback(object): |
| 64 | """Limits callback to one call in a given interval.""" |
| 65 | |
| 66 | def __init__(self, callback, interval): |
| 67 | """Initialize instance. |
| 68 | |
| 69 | Args: |
| 70 | callback: function to call |
| 71 | interval: Number of seconds between allowed calls |
| 72 | """ |
| 73 | self._callback = callback |
| 74 | self._interval = interval |
Allen Li | ddba434 | 2017-11-21 18:27:08 -0800 | [diff] [blame] | 75 | self._last_called = float('-inf') |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 76 | |
| 77 | def __call__(self): |
| 78 | if time.time() >= self._next_call: |
| 79 | self._callback() |
| 80 | self._last_called = time.time() |
| 81 | |
| 82 | @property |
| 83 | def _next_call(self): |
| 84 | return self._last_called + self._interval |
| 85 | |
| 86 | |
| 87 | def main(): |
| 88 | parser = commandline.ArgumentParser( |
| 89 | description=__doc__, |
| 90 | default_log_level='DEBUG') |
| 91 | parser.add_argument( |
| 92 | '--interval', |
| 93 | default=60, |
| 94 | type=int, |
| 95 | help='time (in seconds) between sampling system metrics') |
| 96 | parser.add_argument( |
| 97 | '--collect-prod-hosts', |
| 98 | action='store_true', |
Prathmesh Prabhu | 118e74e | 2018-02-06 10:11:46 -0800 | [diff] [blame] | 99 | help='[DEPRECATED. Use --collect-host-manifest instead.] ' |
| 100 | 'Enable collection of prod host metrics, like roles') |
| 101 | parser.add_argument( |
| 102 | '--collect-host-manifest', |
| 103 | default=None, |
| 104 | choices=['prod', 'staging'], |
| 105 | help='Enable collection of server metrics (e.g. roles) for servers in ' |
| 106 | 'the given lab environment.') |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 107 | opts = parser.parse_args() |
| 108 | opts.Freeze() |
| 109 | |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 110 | # This call returns a context manager that doesn't do anything, so we |
| 111 | # ignore the return value. |
| 112 | ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False) |
| 113 | # The default prefix is '/chrome/infra/'. |
| 114 | interface.state.metric_name_prefix = (interface.state.metric_name_prefix |
| 115 | + 'chromeos/sysmon/') |
| 116 | |
Prathmesh Prabhu | 118e74e | 2018-02-06 10:11:46 -0800 | [diff] [blame] | 117 | # Transitional, while we migrate users off of |collect_prod_hosts| |
| 118 | if opts.collect_host_manifest is not None: |
| 119 | opts.collect_prod_hosts = True |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 120 | collector = _MetricCollector(collect_prod_hosts=opts.collect_prod_hosts) |
| 121 | loop.SleepLoop(callback=collector, |
| 122 | interval=opts.interval).loop_forever() |