blob: d5166f3c371dfaafac42880662d1cec01841d98d [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
18from chromite.scripts.sysmon import puppet_metrics
19from chromite.scripts.sysmon import system_metrics
Allen Li142dc6d2016-12-16 17:03:45 -080020from chromite.scripts.sysmon import osinfo_metrics
Allen Li26d10082016-12-16 16:31:02 -080021from chromite.scripts.sysmon import loop
Allen Li788d1672016-12-16 15:58:23 -080022from 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 Li38de6412016-12-16 16:52:45 -080027class MetricCollector(object):
28 """Metric collector class."""
29
30 def __init__(self):
31 self._last_osinfo_collection = time.time()
32
33 def __call__(self):
34 """Collect metrics."""
35 system_metrics.get_uptime()
36 system_metrics.get_cpu_info()
37 system_metrics.get_disk_info()
38 system_metrics.get_mem_info()
39 system_metrics.get_net_info()
40 system_metrics.get_proc_info()
41 system_metrics.get_load_avg()
42 puppet_metrics.get_puppet_summary()
43 if time.time() > self._next_osinfo_collection:
Allen Li142dc6d2016-12-16 17:03:45 -080044 osinfo_metrics.get_os_info()
Allen Li38de6412016-12-16 16:52:45 -080045 self._last_osinfo_collection = time.time()
46 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
54def main():
55 parser = commandline.ArgumentParser(
56 description=__doc__,
Allen Li337729d2016-12-16 16:45:49 -080057 default_log_level='DEBUG')
Allen Li788d1672016-12-16 15:58:23 -080058 parser.add_argument(
59 '--interval',
Allen Li337729d2016-12-16 16:45:49 -080060 default=60,
61 type=int,
Allen Li788d1672016-12-16 15:58:23 -080062 help='time (in seconds) between sampling system metrics')
63 opts = parser.parse_args()
64 opts.Freeze()
65
66 # This returns a 0 value the first time it's called. Call it now and
67 # discard the return value.
68 psutil.cpu_times_percent()
69
70 # Wait a random amount of time before starting the loop in case sysmon
71 # is started at exactly the same time on all machines.
72 time.sleep(random.uniform(0, opts.interval))
73
74 # This call returns a context manager that doesn't do anything, so we
75 # ignore the return value.
76 ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False)
77 # The default prefix is '/chrome/infra/'.
78 interface.state.metric_name_prefix = (interface.state.metric_name_prefix
79 + 'chromeos/sysmon/')
80
Allen Li38de6412016-12-16 16:52:45 -080081 loop.SleepLoop(callback=MetricCollector(),
Allen Li26d10082016-12-16 16:31:02 -080082 interval=opts.interval).loop_forever()
Allen Li788d1672016-12-16 15:58:23 -080083
84
85if __name__ == '__main__':
86 main()