blob: 498a3d6d16240f273516e2745c1f47ee18b16f86 [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
Chris McDonald59650c32021-07-20 15:29:28 -06009import logging
Allen Li02915572017-07-12 18:05:45 -070010import time
11
Chris McDonald59650c32021-07-20 15:29:28 -060012from chromite.third_party.infra_libs.ts_mon.common import interface
13
Allen Li02915572017-07-12 18:05:45 -070014from chromite.lib import commandline
Allen Li02915572017-07-12 18:05:45 -070015from chromite.lib import metrics
16from chromite.lib import ts_mon_config
Allen Li02915572017-07-12 18:05:45 -070017from chromite.scripts.sysmon import git_metrics
18from chromite.scripts.sysmon import loop
19from chromite.scripts.sysmon import net_metrics
20from chromite.scripts.sysmon import osinfo_metrics
21from chromite.scripts.sysmon import proc_metrics
Allen Li02915572017-07-12 18:05:45 -070022from chromite.scripts.sysmon import puppet_metrics
23from chromite.scripts.sysmon import system_metrics
Mike Frysinger144dce02018-07-14 00:43:45 -040024
Allen Li02915572017-07-12 18:05:45 -070025
26logger = logging.getLogger(__name__)
27
28
29class _MetricCollector(object):
30 """Metric collector class."""
31
Allen Lifccfbbc2018-09-05 15:39:51 -070032 def __init__(self):
Allen Li02915572017-07-12 18:05:45 -070033 self._collect_osinfo = _TimedCallback(
34 callback=osinfo_metrics.collect_os_info,
35 interval=60 * 60)
Allen Li02915572017-07-12 18:05:45 -070036
37 def __call__(self):
38 """Collect metrics."""
39 system_metrics.collect_uptime()
40 system_metrics.collect_cpu_info()
41 system_metrics.collect_disk_info()
42 system_metrics.collect_mem_info()
43 net_metrics.collect_net_info()
44 proc_metrics.collect_proc_info()
45 system_metrics.collect_load_avg()
46 puppet_metrics.collect_puppet_summary()
47 git_metrics.collect_git_metrics()
Allen Li02915572017-07-12 18:05:45 -070048 self._collect_osinfo()
49 system_metrics.collect_unix_time() # must be just before flush
50 metrics.Flush()
51
52
53class _TimedCallback(object):
54 """Limits callback to one call in a given interval."""
55
56 def __init__(self, callback, interval):
57 """Initialize instance.
58
59 Args:
60 callback: function to call
61 interval: Number of seconds between allowed calls
62 """
63 self._callback = callback
64 self._interval = interval
Allen Liddba4342017-11-21 18:27:08 -080065 self._last_called = float('-inf')
Allen Li02915572017-07-12 18:05:45 -070066
67 def __call__(self):
68 if time.time() >= self._next_call:
69 self._callback()
70 self._last_called = time.time()
71
72 @property
73 def _next_call(self):
74 return self._last_called + self._interval
75
76
77def main():
78 parser = commandline.ArgumentParser(
79 description=__doc__,
80 default_log_level='DEBUG')
81 parser.add_argument(
82 '--interval',
83 default=60,
84 type=int,
85 help='time (in seconds) between sampling system metrics')
Allen Li02915572017-07-12 18:05:45 -070086 opts = parser.parse_args()
87 opts.Freeze()
88
Allen Li02915572017-07-12 18:05:45 -070089 # This call returns a context manager that doesn't do anything, so we
90 # ignore the return value.
91 ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False)
92 # The default prefix is '/chrome/infra/'.
93 interface.state.metric_name_prefix = (interface.state.metric_name_prefix
94 + 'chromeos/sysmon/')
95
Allen Lifccfbbc2018-09-05 15:39:51 -070096 collector = _MetricCollector()
Allen Li02915572017-07-12 18:05:45 -070097 loop.SleepLoop(callback=collector,
98 interval=opts.interval).loop_forever()