[sysmon] Move osinfo cycles logic
Having this logic split across two different places is unnecessary;
its more sane to put all of the logic right next to where the osinfo
metric is collected.
Since this metric isnt time sensitive (we dont care if we sometimes
collect it every 60 or 59 or 61 minutes), we use a very simple check.
BUG=chromium:655796
TEST=Run sysmon
Change-Id: Ic8fa07e5e04934bf22331a9cf20d5f4b38c87a64
Reviewed-on: https://chromium-review.googlesource.com/421571
Commit-Ready: Allen Li <ayatane@chromium.org>
Tested-by: Allen Li <ayatane@chromium.org>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/scripts/sysmon/__main__.py b/scripts/sysmon/__main__.py
index 19fab64..c054dca 100644
--- a/scripts/sysmon/__main__.py
+++ b/scripts/sysmon/__main__.py
@@ -23,19 +23,31 @@
logger = logging.getLogger(__name__)
-def collect_metrics(cycles):
- system_metrics.get_uptime()
- system_metrics.get_cpu_info()
- system_metrics.get_disk_info()
- system_metrics.get_mem_info()
- system_metrics.get_net_info()
- system_metrics.get_proc_info()
- system_metrics.get_load_avg()
- puppet_metrics.get_puppet_summary()
- if cycles == 0:
- system_metrics.get_os_info()
- system_metrics.get_unix_time() # must be just before flush
- metrics.Flush()
+class MetricCollector(object):
+ """Metric collector class."""
+
+ def __init__(self):
+ self._last_osinfo_collection = time.time()
+
+ def __call__(self):
+ """Collect metrics."""
+ system_metrics.get_uptime()
+ system_metrics.get_cpu_info()
+ system_metrics.get_disk_info()
+ system_metrics.get_mem_info()
+ system_metrics.get_net_info()
+ system_metrics.get_proc_info()
+ system_metrics.get_load_avg()
+ puppet_metrics.get_puppet_summary()
+ if time.time() > self._next_osinfo_collection:
+ system_metrics.get_os_info()
+ self._last_osinfo_collection = time.time()
+ system_metrics.get_unix_time() # must be just before flush
+ metrics.Flush()
+
+ @property
+ def _next_osinfo_collection(self):
+ return self._last_osinfo_collection + (60 * 60)
def main():
@@ -65,7 +77,7 @@
interface.state.metric_name_prefix = (interface.state.metric_name_prefix
+ 'chromeos/sysmon/')
- loop.SleepLoop(callback=collect_metrics,
+ loop.SleepLoop(callback=MetricCollector(),
interval=opts.interval).loop_forever()