blob: f98318e5edca647d4df1f88778ef3cef060a063f [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Allen Li02915572017-07-12 18:05:45 -07002# 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
8from __future__ import absolute_import
9from __future__ import print_function
10
Allen Li02915572017-07-12 18:05:45 -070011import time
12
13from chromite.lib import commandline
14from chromite.lib import cros_logging as logging
15from chromite.lib import metrics
16from chromite.lib import ts_mon_config
17from infra_libs.ts_mon.common import interface
18
19from chromite.scripts.sysmon import git_metrics
20from chromite.scripts.sysmon import loop
21from chromite.scripts.sysmon import net_metrics
22from chromite.scripts.sysmon import osinfo_metrics
23from chromite.scripts.sysmon import proc_metrics
24from chromite.scripts.sysmon import prod_metrics
25from chromite.scripts.sysmon import puppet_metrics
26from chromite.scripts.sysmon import system_metrics
27
28logger = logging.getLogger(__name__)
29
30
31class _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
63class _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 Liddba4342017-11-21 18:27:08 -080075 self._last_called = float('-inf')
Allen Li02915572017-07-12 18:05:45 -070076
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
87def 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 Prabhu118e74e2018-02-06 10:11:46 -080099 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 Li02915572017-07-12 18:05:45 -0700107 opts = parser.parse_args()
108 opts.Freeze()
109
Allen Li02915572017-07-12 18:05:45 -0700110 # 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 Prabhu118e74e2018-02-06 10:11:46 -0800117 # 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 Li02915572017-07-12 18:05:45 -0700120 collector = _MetricCollector(collect_prod_hosts=opts.collect_prod_hosts)
121 loop.SleepLoop(callback=collector,
122 interval=opts.interval).loop_forever()