blob: b36526ab5ed854de5c34c9a5809d3a128daa2426 [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 Liec5beb32016-09-08 15:31:41 -07005"""Puppet metrics"""
6
Allen Li13bdf0c2017-03-02 15:18:16 -08007from __future__ import absolute_import
Allen Liec5beb32016-09-08 15:31:41 -07008from __future__ import print_function
9
10import time
11
12import yaml
13
Allen Liec5beb32016-09-08 15:31:41 -070014from chromite.lib import cros_logging as logging
Allen Lifdfa6612016-10-25 16:10:37 -070015from infra_libs import ts_mon
Allen Liec5beb32016-09-08 15:31:41 -070016
Allen Li79317bb2016-12-16 18:25:07 -080017logger = logging.getLogger(__name__)
18
Allen Lid4678722017-05-22 12:25:20 -070019LAST_RUN_FILE = '/var/lib/cros_puppet/state/last_run_summary.yaml'
Allen Liec5beb32016-09-08 15:31:41 -070020
Allen Lifdfa6612016-10-25 16:10:37 -070021_config_version_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070022 'puppet/version/config',
23 description='The version of the puppet configuration.'
24 ' By default this is the time that the configuration was parsed')
Allen Lifdfa6612016-10-25 16:10:37 -070025_puppet_version_metric = ts_mon.StringMetric(
Allen Liec5beb32016-09-08 15:31:41 -070026 'puppet/version/puppet',
27 description='Version of puppet client installed.')
Allen Lifdfa6612016-10-25 16:10:37 -070028_events_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070029 'puppet/events',
30 description='Number of changes the puppet client made to the system in its'
31 ' last run, by success or failure')
Allen Lifdfa6612016-10-25 16:10:37 -070032_resources_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070033 'puppet/resources',
34 description='Number of resources known by the puppet client in its last'
35 ' run')
Allen Lifdfa6612016-10-25 16:10:37 -070036_times_metric = ts_mon.FloatMetric(
Allen Liec5beb32016-09-08 15:31:41 -070037 'puppet/times',
38 description='Time taken to perform various parts of the last puppet run',
39 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Lifdfa6612016-10-25 16:10:37 -070040_age_metric = ts_mon.FloatMetric(
41 'puppet/age',
42 description='Time since last run',
43 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Liec5beb32016-09-08 15:31:41 -070044
45
Allen Lia07466d2016-12-16 16:57:50 -080046class _PuppetRunSummary(object):
Allen Lifdfa6612016-10-25 16:10:37 -070047 """Puppet run summary information."""
48
49 def __init__(self, summary_file):
50 self.filename = summary_file
51 with open(self.filename) as file_:
52 self._data = yaml.safe_load(file_)
53
54 @property
55 def versions(self):
56 """Return mapping of version information."""
57 return self._data.get('version', {})
58
59 @property
60 def config_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070061 """Return config version as int."""
62 return self.versions.get('config', -1)
Allen Lifdfa6612016-10-25 16:10:37 -070063
64 @property
65 def puppet_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070066 """Return Puppet version as string."""
67 return self.versions.get('puppet', '')
Allen Lifdfa6612016-10-25 16:10:37 -070068
69 @property
70 def events(self):
71 """Return mapping of events information."""
72 return self._data.get('events', {})
73
74 @property
75 def resources(self):
76 """Return mapping of resources information."""
77 return self._data.get('resources', {})
78
79 @property
80 def times(self):
81 """Return mapping of time information."""
82 return self._data.get(time, {})
83
84 @property
85 def last_run_time(self):
86 """Return last run time as UNIX seconds or None."""
87 return self.times.get('last_run')
Allen Liec5beb32016-09-08 15:31:41 -070088
89
Allen Li45ae8392017-03-02 14:19:35 -080090def collect_puppet_summary():
Allen Lifdfa6612016-10-25 16:10:37 -070091 """Send Puppet run summary metrics."""
Allen Liec5beb32016-09-08 15:31:41 -070092 try:
Allen Lia07466d2016-12-16 16:57:50 -080093 summary = _PuppetRunSummary(LAST_RUN_FILE)
Allen Lifdfa6612016-10-25 16:10:37 -070094 except Exception as e:
Allen Li867d4582017-05-24 18:00:43 -070095 logger.warning(u'Error loading Puppet run summary: %s', e)
Allen Lifdfa6612016-10-25 16:10:37 -070096 else:
Allen Li2b3a93c2016-11-01 12:27:21 -070097 _config_version_metric.set(summary.config_version)
Allen Lifdfa6612016-10-25 16:10:37 -070098 _puppet_version_metric.set(str(summary.puppet_version))
Allen Liec5beb32016-09-08 15:31:41 -070099
Allen Lifdfa6612016-10-25 16:10:37 -0700100 for key, value in summary.events.iteritems():
101 _events_metric.set(value, {'result': key})
Allen Liec5beb32016-09-08 15:31:41 -0700102
Allen Lifdfa6612016-10-25 16:10:37 -0700103 for key, value in summary.resources.iteritems():
104 _resources_metric.set(value, {'action': key})
Allen Liec5beb32016-09-08 15:31:41 -0700105
Allen Lifdfa6612016-10-25 16:10:37 -0700106 for key, value in summary.times.iteritems():
107 _times_metric.set(value, {'step': key})
Allen Liec5beb32016-09-08 15:31:41 -0700108
Allen Lifdfa6612016-10-25 16:10:37 -0700109 if summary.last_run_time is not None:
110 _age_metric.set(time.time() - summary.last_run_time)