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 | |
|
dsansome
2016/06/29 06:21:29
Wrap these comments to 80 characters?
chrishall
2016/07/01 04:27:16
Done.
| |
| 28 # we want to collect some metrics (e.g. os_info) | |
| 29 # only once per hour, so here we count the minutes | |
| 30 # within the hour | |
| 31 self._call_count = -1 | |
|
dsansome
2016/06/29 06:21:29
I'd start at 0 and move the += 1 into the finally
chrishall
2016/07/01 04:27:17
Done.
| |
| 32 | |
| 33 # should be called everytime self.task is called | |
| 34 def count_call(self): | |
| 35 # mark that we were called | |
| 36 self._call_count += 1 | |
| 37 | |
| 38 # roll over at 60, only care about minutes within hour | |
| 39 self._call_count %= 60 | |
|
dsansome
2016/06/29 06:21:29
If this is named call_count I think it makes sense
chrishall
2016/07/01 04:27:17
Done.
| |
| 40 | |
| 41 # check if this call is on the hour | |
| 42 def is_hour(self): | |
| 43 return self._call_count == 0 | |
|
ddoman
2016/06/29 07:57:54
Doesn't self.opts.interval determine how often tas
dsansome
2016/06/30 04:25:46
You should add a comment here explaining why we're
chrishall
2016/07/01 04:27:17
Done.
| |
| 44 | |
| 23 def add_argparse_options(self, parser): | 45 def add_argparse_options(self, parser): |
| 24 super(SysMon, self).add_argparse_options(parser) | 46 super(SysMon, self).add_argparse_options(parser) |
| 25 | 47 |
| 26 parser.add_argument( | 48 parser.add_argument( |
| 27 '--interval', | 49 '--interval', |
| 28 default=10, type=int, | 50 default=10, type=int, |
| 29 help='time (in seconds) between sampling system metrics') | 51 help='time (in seconds) between sampling system metrics') |
| 30 parser.add_argument( | 52 parser.add_argument( |
| 31 '--root-setup', | 53 '--root-setup', |
| 32 action='store_true', | 54 action='store_true', |
| 33 help='if this is set sysmon will run once to initialise configs in ' | 55 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 ' | 56 '/etc and then exit immediately. Used on GCE bots to bootstrap ' |
| 35 'sysmon') | 57 'sysmon') |
| 36 | 58 |
| 37 parser.set_defaults( | 59 parser.set_defaults( |
| 38 ts_mon_flush='manual', | 60 ts_mon_flush='manual', |
| 39 ) | 61 ) |
| 40 | 62 |
| 41 def task(self): | 63 def task(self): |
| 64 self.count_call() | |
|
dsansome
2016/06/29 06:21:29
I'm not sure hiding the implementations of these i
chrishall
2016/07/01 04:27:17
I disagree, I wanted to give them names rather tha
| |
| 42 try: | 65 try: |
| 43 system_metrics.get_uptime() | 66 system_metrics.get_uptime() |
| 44 system_metrics.get_cpu_info() | 67 system_metrics.get_cpu_info() |
| 45 system_metrics.get_disk_info() | 68 system_metrics.get_disk_info() |
| 46 system_metrics.get_mem_info() | 69 system_metrics.get_mem_info() |
| 47 system_metrics.get_net_info() | 70 system_metrics.get_net_info() |
| 48 system_metrics.get_proc_info() | 71 system_metrics.get_proc_info() |
| 72 if self.is_hour(): | |
| 73 # collect once per hour | |
| 74 system_metrics.get_os_info() | |
| 75 else: | |
| 76 # clear on all other minutes | |
| 77 system_metrics.clear_os_info() | |
| 49 puppet_metrics.get_puppet_summary() | 78 puppet_metrics.get_puppet_summary() |
| 50 cipd_metrics.get_cipd_summary() | 79 cipd_metrics.get_cipd_summary() |
| 51 android_device_metrics.get_device_statuses() | 80 android_device_metrics.get_device_statuses() |
| 52 system_metrics.get_unix_time() # must be the last in the list | 81 system_metrics.get_unix_time() # must be the last in the list |
| 82 | |
| 53 finally: | 83 finally: |
| 54 ts_mon.flush() | 84 ts_mon.flush() |
| 55 return True | 85 return True |
| 56 | 86 |
| 57 def sleep_timeout(self): | 87 def sleep_timeout(self): |
| 58 return self.opts.interval | 88 return self.opts.interval |
| 59 | 89 |
| 60 def main(self, opts): | 90 def main(self, opts): |
| 61 if opts.root_setup: | 91 if opts.root_setup: |
| 62 return root_setup.root_setup() | 92 return root_setup.root_setup() |
| 63 | 93 |
| 64 # This returns a 0 value the first time it's called. Call it now and | 94 # This returns a 0 value the first time it's called. Call it now and |
| 65 # discard the return value. | 95 # discard the return value. |
| 66 psutil.cpu_times_percent() | 96 psutil.cpu_times_percent() |
| 67 | 97 |
| 68 # Wait a random amount of time before starting the loop in case sysmon is | 98 # Wait a random amount of time before starting the loop in case sysmon is |
| 69 # started at exactly the same time on all machines. | 99 # started at exactly the same time on all machines. |
| 70 time.sleep(random.uniform(0, opts.interval)) | 100 time.sleep(random.uniform(0, opts.interval)) |
| 71 | 101 |
| 72 return super(SysMon, self).main(opts) | 102 return super(SysMon, self).main(opts) |
| 73 | 103 |
| 74 | 104 |
| 75 if __name__ == '__main__': | 105 if __name__ == '__main__': |
| 76 SysMon().run() | 106 SysMon().run() |
| OLD | NEW |