blob: da5a691a943b2dbe23690a22cf05b853bfc2bacd [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
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 -040024from infra_libs.ts_mon.common import interface
25
Allen Li02915572017-07-12 18:05:45 -070026
27logger = logging.getLogger(__name__)
28
29
30class _MetricCollector(object):
31 """Metric collector class."""
32
Allen Lifccfbbc2018-09-05 15:39:51 -070033 def __init__(self):
Allen Li02915572017-07-12 18:05:45 -070034 self._collect_osinfo = _TimedCallback(
35 callback=osinfo_metrics.collect_os_info,
36 interval=60 * 60)
Allen Li02915572017-07-12 18:05:45 -070037
38 def __call__(self):
39 """Collect metrics."""
40 system_metrics.collect_uptime()
41 system_metrics.collect_cpu_info()
42 system_metrics.collect_disk_info()
43 system_metrics.collect_mem_info()
44 net_metrics.collect_net_info()
45 proc_metrics.collect_proc_info()
46 system_metrics.collect_load_avg()
47 puppet_metrics.collect_puppet_summary()
48 git_metrics.collect_git_metrics()
Allen Li02915572017-07-12 18:05:45 -070049 self._collect_osinfo()
50 system_metrics.collect_unix_time() # must be just before flush
51 metrics.Flush()
52
53
54class _TimedCallback(object):
55 """Limits callback to one call in a given interval."""
56
57 def __init__(self, callback, interval):
58 """Initialize instance.
59
60 Args:
61 callback: function to call
62 interval: Number of seconds between allowed calls
63 """
64 self._callback = callback
65 self._interval = interval
Allen Liddba4342017-11-21 18:27:08 -080066 self._last_called = float('-inf')
Allen Li02915572017-07-12 18:05:45 -070067
68 def __call__(self):
69 if time.time() >= self._next_call:
70 self._callback()
71 self._last_called = time.time()
72
73 @property
74 def _next_call(self):
75 return self._last_called + self._interval
76
77
78def main():
79 parser = commandline.ArgumentParser(
80 description=__doc__,
81 default_log_level='DEBUG')
82 parser.add_argument(
83 '--interval',
84 default=60,
85 type=int,
86 help='time (in seconds) between sampling system metrics')
Allen Li02915572017-07-12 18:05:45 -070087 opts = parser.parse_args()
88 opts.Freeze()
89
Allen Li02915572017-07-12 18:05:45 -070090 # 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 = (interface.state.metric_name_prefix
95 + 'chromeos/sysmon/')
96
Allen Lifccfbbc2018-09-05 15:39:51 -070097 collector = _MetricCollector()
Allen Li02915572017-07-12 18:05:45 -070098 loop.SleepLoop(callback=collector,
99 interval=opts.interval).loop_forever()