blob: c054dcad441147e3646cf05d69a51e36b46cde8c [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 Li38de6412016-12-16 16:52:45 -080026class MetricCollector(object):
27 """Metric collector class."""
28
29 def __init__(self):
30 self._last_osinfo_collection = time.time()
31
32 def __call__(self):
33 """Collect metrics."""
34 system_metrics.get_uptime()
35 system_metrics.get_cpu_info()
36 system_metrics.get_disk_info()
37 system_metrics.get_mem_info()
38 system_metrics.get_net_info()
39 system_metrics.get_proc_info()
40 system_metrics.get_load_avg()
41 puppet_metrics.get_puppet_summary()
42 if time.time() > self._next_osinfo_collection:
43 system_metrics.get_os_info()
44 self._last_osinfo_collection = time.time()
45 system_metrics.get_unix_time() # must be just before flush
46 metrics.Flush()
47
48 @property
49 def _next_osinfo_collection(self):
50 return self._last_osinfo_collection + (60 * 60)
Allen Li788d1672016-12-16 15:58:23 -080051
52
53def main():
54 parser = commandline.ArgumentParser(
55 description=__doc__,
Allen Li337729d2016-12-16 16:45:49 -080056 default_log_level='DEBUG')
Allen Li788d1672016-12-16 15:58:23 -080057 parser.add_argument(
58 '--interval',
Allen Li337729d2016-12-16 16:45:49 -080059 default=60,
60 type=int,
Allen Li788d1672016-12-16 15:58:23 -080061 help='time (in seconds) between sampling system metrics')
62 opts = parser.parse_args()
63 opts.Freeze()
64
65 # This returns a 0 value the first time it's called. Call it now and
66 # discard the return value.
67 psutil.cpu_times_percent()
68
69 # Wait a random amount of time before starting the loop in case sysmon
70 # is started at exactly the same time on all machines.
71 time.sleep(random.uniform(0, opts.interval))
72
73 # This call returns a context manager that doesn't do anything, so we
74 # ignore the return value.
75 ts_mon_config.SetupTsMonGlobalState('sysmon', auto_flush=False)
76 # The default prefix is '/chrome/infra/'.
77 interface.state.metric_name_prefix = (interface.state.metric_name_prefix
78 + 'chromeos/sysmon/')
79
Allen Li38de6412016-12-16 16:52:45 -080080 loop.SleepLoop(callback=MetricCollector(),
Allen Li26d10082016-12-16 16:31:02 -080081 interval=opts.interval).loop_forever()
Allen Li788d1672016-12-16 15:58:23 -080082
83
84if __name__ == '__main__':
85 main()