blob: c1d4f2212245e622560da0daa180141c29248771 [file] [log] [blame]
Allen Li02915572017-07-12 18:05:45 -07001# 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
5"""Send system monitoring data to the timeseries monitoring API."""
6
7from __future__ import absolute_import
Allen Li02915572017-07-12 18:05:45 -07008
Allen Li02915572017-07-12 18:05:45 -07009import time
10
11from chromite.lib import commandline
12from chromite.lib import cros_logging as logging
13from chromite.lib import metrics
14from chromite.lib import ts_mon_config
Allen Li02915572017-07-12 18:05:45 -070015from chromite.scripts.sysmon import git_metrics
16from chromite.scripts.sysmon import loop
17from chromite.scripts.sysmon import net_metrics
18from chromite.scripts.sysmon import osinfo_metrics
19from chromite.scripts.sysmon import proc_metrics
Allen Li02915572017-07-12 18:05:45 -070020from chromite.scripts.sysmon import puppet_metrics
21from chromite.scripts.sysmon import system_metrics
Mike Frysinger8d6a51d2021-02-12 07:40:03 -050022from chromite.third_party.infra_libs.ts_mon.common import interface
Mike Frysinger144dce02018-07-14 00:43:45 -040023
Allen Li02915572017-07-12 18:05:45 -070024
25logger = logging.getLogger(__name__)
26
27
28class _MetricCollector(object):
29 """Metric collector class."""
30
Allen Lifccfbbc2018-09-05 15:39:51 -070031 def __init__(self):
Allen Li02915572017-07-12 18:05:45 -070032 self._collect_osinfo = _TimedCallback(
33 callback=osinfo_metrics.collect_os_info,
34 interval=60 * 60)
Allen Li02915572017-07-12 18:05:45 -070035
36 def __call__(self):
37 """Collect metrics."""
38 system_metrics.collect_uptime()
39 system_metrics.collect_cpu_info()
40 system_metrics.collect_disk_info()
41 system_metrics.collect_mem_info()
42 net_metrics.collect_net_info()
43 proc_metrics.collect_proc_info()
44 system_metrics.collect_load_avg()
45 puppet_metrics.collect_puppet_summary()
46 git_metrics.collect_git_metrics()
Allen Li02915572017-07-12 18:05:45 -070047 self._collect_osinfo()
48 system_metrics.collect_unix_time() # must be just before flush
49 metrics.Flush()
50
51
52class _TimedCallback(object):
53 """Limits callback to one call in a given interval."""
54
55 def __init__(self, callback, interval):
56 """Initialize instance.
57
58 Args:
59 callback: function to call
60 interval: Number of seconds between allowed calls
61 """
62 self._callback = callback
63 self._interval = interval
Allen Liddba4342017-11-21 18:27:08 -080064 self._last_called = float('-inf')
Allen Li02915572017-07-12 18:05:45 -070065
66 def __call__(self):
67 if time.time() >= self._next_call:
68 self._callback()
69 self._last_called = time.time()
70
71 @property
72 def _next_call(self):
73 return self._last_called + self._interval
74
75
76def main():
77 parser = commandline.ArgumentParser(
78 description=__doc__,
79 default_log_level='DEBUG')
80 parser.add_argument(
81 '--interval',
82 default=60,
83 type=int,
84 help='time (in seconds) between sampling system metrics')
Allen Li02915572017-07-12 18:05:45 -070085 opts = parser.parse_args()
86 opts.Freeze()
87
Allen Li02915572017-07-12 18:05:45 -070088 # This call returns a context manager that doesn't do anything, so we
89 # ignore the return value.
90 ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False)
91 # The default prefix is '/chrome/infra/'.
92 interface.state.metric_name_prefix = (interface.state.metric_name_prefix
93 + 'chromeos/sysmon/')
94
Allen Lifccfbbc2018-09-05 15:39:51 -070095 collector = _MetricCollector()
Allen Li02915572017-07-12 18:05:45 -070096 loop.SleepLoop(callback=collector,
97 interval=opts.interval).loop_forever()