blob: a607dc3a0af15976d4de048000eddfa8d38ba63f [file] [log] [blame]
Allen Liec5beb32016-09-08 15:31:41 -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
Allen Li788d1672016-12-16 15:58:23 -08005"""Send system monitoring data to the timeseries monitoring API."""
Allen Liec5beb32016-09-08 15:31:41 -07006
7from __future__ import print_function
8
Allen Li788d1672016-12-16 15:58:23 -08009import random
10import time
Allen Liec5beb32016-09-08 15:31:41 -070011
Allen Li788d1672016-12-16 15:58:23 -080012import psutil
13
14from chromite.lib import commandline
Allen Liec5beb32016-09-08 15:31:41 -070015from chromite.lib import cros_logging as logging
Allen Li788d1672016-12-16 15:58:23 -080016from chromite.lib import metrics
17from chromite.lib import ts_mon_config
Allen Libaa422d2016-12-16 18:21:10 -080018from chromite.scripts.sysmon import loop
19from chromite.scripts.sysmon import osinfo_metrics
Allen Li788d1672016-12-16 15:58:23 -080020from chromite.scripts.sysmon import puppet_metrics
21from chromite.scripts.sysmon import system_metrics
22from infra_libs.ts_mon.common import interface
Allen Liec5beb32016-09-08 15:31:41 -070023
Allen Li788d1672016-12-16 15:58:23 -080024logger = logging.getLogger(__name__)
25
26
Allen Libaa422d2016-12-16 18:21:10 -080027class _MetricCollector(object):
Allen Li38de6412016-12-16 16:52:45 -080028 """Metric collector class."""
29
30 def __init__(self):
Allen Libaa422d2016-12-16 18:21:10 -080031 self._get_osinfo = _TimedCallback(
32 callback=osinfo_metrics.get_os_info,
33 interval=60 * 60)
Allen Li38de6412016-12-16 16:52:45 -080034
35 def __call__(self):
36 """Collect metrics."""
37 system_metrics.get_uptime()
38 system_metrics.get_cpu_info()
39 system_metrics.get_disk_info()
40 system_metrics.get_mem_info()
41 system_metrics.get_net_info()
42 system_metrics.get_proc_info()
43 system_metrics.get_load_avg()
44 puppet_metrics.get_puppet_summary()
Allen Libaa422d2016-12-16 18:21:10 -080045 self._get_osinfo()
Allen Li38de6412016-12-16 16:52:45 -080046 system_metrics.get_unix_time() # must be just before flush
47 metrics.Flush()
48
49 @property
50 def _next_osinfo_collection(self):
51 return self._last_osinfo_collection + (60 * 60)
Allen Li788d1672016-12-16 15:58:23 -080052
53
Allen Libaa422d2016-12-16 18:21:10 -080054class _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
66 self._last_called = time.time() - interval
67
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
Allen Li788d1672016-12-16 15:58:23 -080078def main():
79 parser = commandline.ArgumentParser(
80 description=__doc__,
Allen Li337729d2016-12-16 16:45:49 -080081 default_log_level='DEBUG')
Allen Li788d1672016-12-16 15:58:23 -080082 parser.add_argument(
83 '--interval',
Allen Li337729d2016-12-16 16:45:49 -080084 default=60,
85 type=int,
Allen Li788d1672016-12-16 15:58:23 -080086 help='time (in seconds) between sampling system metrics')
87 opts = parser.parse_args()
88 opts.Freeze()
89
90 # This returns a 0 value the first time it's called. Call it now and
91 # discard the return value.
92 psutil.cpu_times_percent()
93
94 # Wait a random amount of time before starting the loop in case sysmon
95 # is started at exactly the same time on all machines.
96 time.sleep(random.uniform(0, opts.interval))
97
98 # This call returns a context manager that doesn't do anything, so we
99 # ignore the return value.
100 ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False)
101 # The default prefix is '/chrome/infra/'.
102 interface.state.metric_name_prefix = (interface.state.metric_name_prefix
103 + 'chromeos/sysmon/')
104
Allen Libaa422d2016-12-16 18:21:10 -0800105 loop.SleepLoop(callback=_MetricCollector(),
Allen Li26d10082016-12-16 16:31:02 -0800106 interval=opts.interval).loop_forever()
Allen Li788d1672016-12-16 15:58:23 -0800107
108
109if __name__ == '__main__':
110 main()