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