blob: 8b7118e4bddbbddfd62f30109a5821eeff52f7f3 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2016 The ChromiumOS Authors
Allen Li02915572017-07-12 18:05:45 -07002# 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):
Alex Klein1699fab2022-09-08 08:46:06 -060030 """Metric collector class."""
Allen Li02915572017-07-12 18:05:45 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 def __init__(self):
33 self._collect_osinfo = _TimedCallback(
34 callback=osinfo_metrics.collect_os_info, interval=60 * 60
35 )
Allen Li02915572017-07-12 18:05:45 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 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()
48 self._collect_osinfo()
49 system_metrics.collect_unix_time() # must be just before flush
50 metrics.Flush()
Allen Li02915572017-07-12 18:05:45 -070051
52
53class _TimedCallback(object):
Alex Klein1699fab2022-09-08 08:46:06 -060054 """Limits callback to one call in a given interval."""
Allen Li02915572017-07-12 18:05:45 -070055
Alex Klein1699fab2022-09-08 08:46:06 -060056 def __init__(self, callback, interval):
57 """Initialize instance.
Allen Li02915572017-07-12 18:05:45 -070058
Alex Klein1699fab2022-09-08 08:46:06 -060059 Args:
60 callback: function to call
61 interval: Number of seconds between allowed calls
62 """
63 self._callback = callback
64 self._interval = interval
65 self._last_called = float("-inf")
Allen Li02915572017-07-12 18:05:45 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 def __call__(self):
68 if time.time() >= self._next_call:
69 self._callback()
70 self._last_called = time.time()
Allen Li02915572017-07-12 18:05:45 -070071
Alex Klein1699fab2022-09-08 08:46:06 -060072 @property
73 def _next_call(self):
74 return self._last_called + self._interval
Allen Li02915572017-07-12 18:05:45 -070075
76
77def main():
Alex Klein1699fab2022-09-08 08:46:06 -060078 parser = commandline.ArgumentParser(
79 description=__doc__, default_log_level="DEBUG"
80 )
81 parser.add_argument(
82 "--interval",
83 default=60,
84 type=int,
85 help="time (in seconds) between sampling system metrics",
86 )
87 opts = parser.parse_args()
88 opts.Freeze()
Allen Li02915572017-07-12 18:05:45 -070089
Alex Klein1699fab2022-09-08 08:46:06 -060090 # This call returns a context manager that doesn't do anything, so we
91 # ignore the return value.
92 ts_mon_config.SetupTsMonGlobalState("sysmon", auto_flush=False)
93 # The default prefix is '/chrome/infra/'.
94 interface.state.metric_name_prefix = (
95 interface.state.metric_name_prefix + "chromeos/sysmon/"
96 )
Allen Li02915572017-07-12 18:05:45 -070097
Alex Klein1699fab2022-09-08 08:46:06 -060098 collector = _MetricCollector()
99 loop.SleepLoop(callback=collector, interval=opts.interval).loop_forever()