blob: a1095a9cb861d28d8472c01529f5548d4280469a [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 Li26d10082016-12-16 16:31:02 -080020from chromite.scripts.sysmon import loop
Allen Li788d1672016-12-16 15:58:23 -080021from infra_libs.ts_mon.common import interface
Allen Liec5beb32016-09-08 15:31:41 -070022
Allen Li788d1672016-12-16 15:58:23 -080023DEFAULT_LOG_LEVEL = 'DEBUG'
24DEFAULT_INTERVAL = 60 # 1 minute
25
26logger = logging.getLogger(__name__)
27
28
Allen Li788d1672016-12-16 15:58:23 -080029def collect_metrics(cycles):
30 system_metrics.get_uptime()
31 system_metrics.get_cpu_info()
32 system_metrics.get_disk_info()
33 system_metrics.get_mem_info()
34 system_metrics.get_net_info()
35 system_metrics.get_proc_info()
36 system_metrics.get_load_avg()
37 puppet_metrics.get_puppet_summary()
38 if cycles == 0:
39 system_metrics.get_os_info()
40 system_metrics.get_unix_time() # must be just before flush
41 metrics.Flush()
42
43
44def main():
45 parser = commandline.ArgumentParser(
46 description=__doc__,
47 default_log_level=DEFAULT_LOG_LEVEL)
48 parser.add_argument(
49 '--interval',
50 default=DEFAULT_INTERVAL, type=int,
51 help='time (in seconds) between sampling system metrics')
52 opts = parser.parse_args()
53 opts.Freeze()
54
55 # This returns a 0 value the first time it's called. Call it now and
56 # discard the return value.
57 psutil.cpu_times_percent()
58
59 # Wait a random amount of time before starting the loop in case sysmon
60 # is started at exactly the same time on all machines.
61 time.sleep(random.uniform(0, opts.interval))
62
63 # This call returns a context manager that doesn't do anything, so we
64 # ignore the return value.
65 ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False)
66 # The default prefix is '/chrome/infra/'.
67 interface.state.metric_name_prefix = (interface.state.metric_name_prefix
68 + 'chromeos/sysmon/')
69
Allen Li26d10082016-12-16 16:31:02 -080070 loop.SleepLoop(callback=collect_metrics,
71 interval=opts.interval).loop_forever()
Allen Li788d1672016-12-16 15:58:23 -080072
73
74if __name__ == '__main__':
75 main()