Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Send system monitoring data to the timeseries monitoring API.""" | 6 """Send system monitoring data to the timeseries monitoring API.""" |
| 7 | 7 |
| 8 import random | 8 import random |
| 9 import time | 9 import time |
| 10 | 10 |
| 11 import psutil | 11 import psutil |
| 12 | 12 |
| 13 from infra.libs.service_utils import outer_loop | 13 from infra.libs.service_utils import outer_loop |
| 14 from infra.services.sysmon import android_device_metrics | 14 from infra.services.sysmon import android_device_metrics |
| 15 from infra.services.sysmon import cipd_metrics | 15 from infra.services.sysmon import cipd_metrics |
| 16 from infra.services.sysmon import puppet_metrics | 16 from infra.services.sysmon import puppet_metrics |
| 17 from infra.services.sysmon import root_setup | 17 from infra.services.sysmon import root_setup |
| 18 from infra.services.sysmon import system_metrics | 18 from infra.services.sysmon import system_metrics |
| 19 from infra_libs import ts_mon | 19 from infra_libs import ts_mon |
| 20 | 20 |
| 21 | 21 |
| 22 class SysMon(outer_loop.Application): | 22 class SysMon(outer_loop.Application): |
| 23 def __init__(self): | |
| 24 # make sure we call our super's init | |
| 25 super(SysMon, self).__init__() | |
| 26 | |
| 27 # SysMon.task is called every minute we want to collect some metrics | |
| 28 # (e.g. os_info) only once per hour, so here we count the minutes within | |
| 29 # the hour | |
| 30 self._minute_count = 0 | |
| 31 | |
| 32 # should be called at the end of each call to self.task | |
|
dsansome
2016/07/01 04:30:56
Make this a docstring?
chrishall
2016/07/01 04:53:56
Done.
| |
| 33 def count_minute(self): | |
| 34 # mark that we were called | |
| 35 self._minute_count += 1 | |
| 36 | |
| 37 # roll over each day-ish, 60 minutes * 24 hours | |
| 38 self._minute_count %= 60 * 24 | |
| 39 | |
| 40 # check if this call is on the hour | |
| 41 def is_hour(self): | |
| 42 return self._minute_count % 60 == 0 | |
| 43 | |
| 23 def add_argparse_options(self, parser): | 44 def add_argparse_options(self, parser): |
| 24 super(SysMon, self).add_argparse_options(parser) | 45 super(SysMon, self).add_argparse_options(parser) |
| 25 | 46 |
| 26 parser.add_argument( | 47 parser.add_argument( |
| 27 '--interval', | 48 '--interval', |
| 28 default=10, type=int, | 49 default=10, type=int, |
| 29 help='time (in seconds) between sampling system metrics') | 50 help='time (in seconds) between sampling system metrics') |
| 30 parser.add_argument( | 51 parser.add_argument( |
| 31 '--root-setup', | 52 '--root-setup', |
| 32 action='store_true', | 53 action='store_true', |
| 33 help='if this is set sysmon will run once to initialise configs in ' | 54 help='if this is set sysmon will run once to initialise configs in ' |
| 34 '/etc and then exit immediately. Used on GCE bots to bootstrap ' | 55 '/etc and then exit immediately. Used on GCE bots to bootstrap ' |
| 35 'sysmon') | 56 'sysmon') |
| 36 | 57 |
| 37 parser.set_defaults( | 58 parser.set_defaults( |
| 38 ts_mon_flush='manual', | 59 ts_mon_flush='manual', |
| 39 ) | 60 ) |
| 40 | 61 |
| 41 def task(self): | 62 def task(self): |
| 42 try: | 63 try: |
| 43 system_metrics.get_uptime() | 64 system_metrics.get_uptime() |
| 44 system_metrics.get_cpu_info() | 65 system_metrics.get_cpu_info() |
| 45 system_metrics.get_disk_info() | 66 system_metrics.get_disk_info() |
| 46 system_metrics.get_mem_info() | 67 system_metrics.get_mem_info() |
| 47 system_metrics.get_net_info() | 68 system_metrics.get_net_info() |
| 48 system_metrics.get_proc_info() | 69 system_metrics.get_proc_info() |
| 70 if self.is_hour(): | |
| 71 # collect once per hour | |
| 72 system_metrics.get_os_info() | |
| 73 else: | |
| 74 # clear on all other minutes | |
| 75 system_metrics.clear_os_info() | |
| 49 puppet_metrics.get_puppet_summary() | 76 puppet_metrics.get_puppet_summary() |
| 50 cipd_metrics.get_cipd_summary() | 77 cipd_metrics.get_cipd_summary() |
| 51 android_device_metrics.get_device_statuses() | 78 android_device_metrics.get_device_statuses() |
| 52 system_metrics.get_unix_time() # must be the last in the list | 79 system_metrics.get_unix_time() # must be the last in the list |
| 80 | |
| 53 finally: | 81 finally: |
| 54 ts_mon.flush() | 82 ts_mon.flush() |
| 83 self.count_minute() | |
| 55 return True | 84 return True |
| 56 | 85 |
| 57 def sleep_timeout(self): | 86 def sleep_timeout(self): |
| 58 return self.opts.interval | 87 return self.opts.interval |
| 59 | 88 |
| 60 def main(self, opts): | 89 def main(self, opts): |
| 61 if opts.root_setup: | 90 if opts.root_setup: |
| 62 return root_setup.root_setup() | 91 return root_setup.root_setup() |
| 63 | 92 |
| 64 # This returns a 0 value the first time it's called. Call it now and | 93 # This returns a 0 value the first time it's called. Call it now and |
| 65 # discard the return value. | 94 # discard the return value. |
| 66 psutil.cpu_times_percent() | 95 psutil.cpu_times_percent() |
| 67 | 96 |
| 68 # Wait a random amount of time before starting the loop in case sysmon is | 97 # Wait a random amount of time before starting the loop in case sysmon is |
| 69 # started at exactly the same time on all machines. | 98 # started at exactly the same time on all machines. |
| 70 time.sleep(random.uniform(0, opts.interval)) | 99 time.sleep(random.uniform(0, opts.interval)) |
| 71 | 100 |
| 72 return super(SysMon, self).main(opts) | 101 return super(SysMon, self).main(opts) |
| 73 | 102 |
| 74 | 103 |
| 75 if __name__ == '__main__': | 104 if __name__ == '__main__': |
| 76 SysMon().run() | 105 SysMon().run() |
| OLD | NEW |