Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 1 | # 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 Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 5 | """Puppet metrics""" |
| 6 | |
Allen Li | 13bdf0c | 2017-03-02 15:18:16 -0800 | [diff] [blame] | 7 | from __future__ import absolute_import |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
| 10 | import time |
| 11 | |
| 12 | import yaml |
| 13 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_logging as logging |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 15 | from chromite.lib import metrics |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 16 | |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame^] | 17 | |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 18 | logger = logging.getLogger(__name__) |
| 19 | |
Allen Li | d467872 | 2017-05-22 12:25:20 -0700 | [diff] [blame] | 20 | LAST_RUN_FILE = '/var/lib/cros_puppet/state/last_run_summary.yaml' |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 21 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 22 | _config_version_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 23 | 'puppet/version/config', |
| 24 | description='The version of the puppet configuration.' |
| 25 | ' By default this is the time that the configuration was parsed') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 26 | _puppet_version_metric = metrics.StringMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 27 | 'puppet/version/puppet', |
| 28 | description='Version of puppet client installed.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 29 | _events_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 30 | 'puppet/events', |
| 31 | description='Number of changes the puppet client made to the system in its' |
| 32 | ' last run, by success or failure') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 33 | _resources_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 34 | 'puppet/resources', |
| 35 | description='Number of resources known by the puppet client in its last' |
| 36 | ' run') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 37 | _times_metric = metrics.FloatMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 38 | 'puppet/times', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame^] | 39 | description='Time taken to perform various parts of the last puppet run') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 40 | _age_metric = metrics.FloatMetric( |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 41 | 'puppet/age', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame^] | 42 | description='Time since last run') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 43 | |
| 44 | |
Allen Li | a07466d | 2016-12-16 16:57:50 -0800 | [diff] [blame] | 45 | class _PuppetRunSummary(object): |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 46 | """Puppet run summary information.""" |
| 47 | |
| 48 | def __init__(self, summary_file): |
| 49 | self.filename = summary_file |
| 50 | with open(self.filename) as file_: |
| 51 | self._data = yaml.safe_load(file_) |
| 52 | |
| 53 | @property |
| 54 | def versions(self): |
| 55 | """Return mapping of version information.""" |
| 56 | return self._data.get('version', {}) |
| 57 | |
| 58 | @property |
| 59 | def config_version(self): |
Allen Li | 2b3a93c | 2016-11-01 12:27:21 -0700 | [diff] [blame] | 60 | """Return config version as int.""" |
| 61 | return self.versions.get('config', -1) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 62 | |
| 63 | @property |
| 64 | def puppet_version(self): |
Allen Li | 2b3a93c | 2016-11-01 12:27:21 -0700 | [diff] [blame] | 65 | """Return Puppet version as string.""" |
| 66 | return self.versions.get('puppet', '') |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 67 | |
| 68 | @property |
| 69 | def events(self): |
| 70 | """Return mapping of events information.""" |
| 71 | return self._data.get('events', {}) |
| 72 | |
| 73 | @property |
| 74 | def resources(self): |
| 75 | """Return mapping of resources information.""" |
| 76 | return self._data.get('resources', {}) |
| 77 | |
| 78 | @property |
| 79 | def times(self): |
| 80 | """Return mapping of time information.""" |
| 81 | return self._data.get(time, {}) |
| 82 | |
| 83 | @property |
| 84 | def last_run_time(self): |
| 85 | """Return last run time as UNIX seconds or None.""" |
| 86 | return self.times.get('last_run') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 87 | |
| 88 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 89 | def collect_puppet_summary(): |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 90 | """Send Puppet run summary metrics.""" |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 91 | try: |
Allen Li | a07466d | 2016-12-16 16:57:50 -0800 | [diff] [blame] | 92 | summary = _PuppetRunSummary(LAST_RUN_FILE) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 93 | except Exception as e: |
Allen Li | 867d458 | 2017-05-24 18:00:43 -0700 | [diff] [blame] | 94 | logger.warning(u'Error loading Puppet run summary: %s', e) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 95 | else: |
Allen Li | 2b3a93c | 2016-11-01 12:27:21 -0700 | [diff] [blame] | 96 | _config_version_metric.set(summary.config_version) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 97 | _puppet_version_metric.set(str(summary.puppet_version)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 98 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 99 | for key, value in summary.events.iteritems(): |
| 100 | _events_metric.set(value, {'result': key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 101 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 102 | for key, value in summary.resources.iteritems(): |
| 103 | _resources_metric.set(value, {'action': key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 104 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 105 | for key, value in summary.times.iteritems(): |
| 106 | _times_metric.set(value, {'step': key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 107 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 108 | if summary.last_run_time is not None: |
| 109 | _age_metric.set(time.time() - summary.last_run_time) |