Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 2 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 6 | """Puppet metrics""" |
| 7 | |
Allen Li | 13bdf0c | 2017-03-02 15:18:16 -0800 | [diff] [blame] | 8 | from __future__ import absolute_import |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
| 11 | import time |
| 12 | |
| 13 | import yaml |
| 14 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_logging as logging |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 16 | from chromite.lib import metrics |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 17 | |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 18 | |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 19 | logger = logging.getLogger(__name__) |
| 20 | |
Allen Li | d467872 | 2017-05-22 12:25:20 -0700 | [diff] [blame] | 21 | LAST_RUN_FILE = '/var/lib/cros_puppet/state/last_run_summary.yaml' |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 22 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 23 | _config_version_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 24 | 'puppet/version/config', |
| 25 | description='The version of the puppet configuration.' |
| 26 | ' By default this is the time that the configuration was parsed') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 27 | _puppet_version_metric = metrics.StringMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 28 | 'puppet/version/puppet', |
| 29 | description='Version of puppet client installed.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 30 | _events_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 31 | 'puppet/events', |
| 32 | description='Number of changes the puppet client made to the system in its' |
| 33 | ' last run, by success or failure') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 34 | _resources_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 35 | 'puppet/resources', |
| 36 | description='Number of resources known by the puppet client in its last' |
| 37 | ' run') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 38 | _times_metric = metrics.FloatMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 39 | 'puppet/times', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 40 | description='Time taken to perform various parts of the last puppet run') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 41 | _age_metric = metrics.FloatMetric( |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 42 | 'puppet/age', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 43 | description='Time since last run') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 44 | |
| 45 | |
Allen Li | a07466d | 2016-12-16 16:57:50 -0800 | [diff] [blame] | 46 | class _PuppetRunSummary(object): |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 47 | """Puppet run summary information.""" |
| 48 | |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 49 | def __init__(self, f): |
| 50 | """Instantiate instance. |
| 51 | |
| 52 | Args: |
| 53 | f: file object to read summary from |
| 54 | """ |
| 55 | self._data = yaml.safe_load(f) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 56 | |
| 57 | @property |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 58 | def _versions(self): |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 59 | """Return mapping of version information.""" |
| 60 | return self._data.get('version', {}) |
| 61 | |
| 62 | @property |
| 63 | def config_version(self): |
Allen Li | 2b3a93c | 2016-11-01 12:27:21 -0700 | [diff] [blame] | 64 | """Return config version as int.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 65 | return self._versions.get('config', -1) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 66 | |
| 67 | @property |
| 68 | def puppet_version(self): |
Allen Li | 2b3a93c | 2016-11-01 12:27:21 -0700 | [diff] [blame] | 69 | """Return Puppet version as string.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 70 | return self._versions.get('puppet', '') |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 71 | |
| 72 | @property |
| 73 | def events(self): |
| 74 | """Return mapping of events information.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 75 | events = self._data.get('events', {}) |
| 76 | events.pop('total', None) |
| 77 | return events |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 78 | |
| 79 | @property |
| 80 | def resources(self): |
| 81 | """Return mapping of resources information.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 82 | resources = self._data.get('resources', {}) |
| 83 | total = resources.pop('total', 0) |
| 84 | resources['other'] = max(0, total - sum(resources.itervalues())) |
| 85 | return resources |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 86 | |
| 87 | @property |
| 88 | def times(self): |
| 89 | """Return mapping of time information.""" |
Allen Li | f8d6798 | 2017-10-09 18:45:59 -0700 | [diff] [blame] | 90 | times = self._data.get('time', {}).copy() |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 91 | times.pop('last_run', None) |
| 92 | total = times.pop('total', None) |
| 93 | times['other'] = max(0, total - sum(times.itervalues())) |
| 94 | return times |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 95 | |
| 96 | @property |
| 97 | def last_run_time(self): |
| 98 | """Return last run time as UNIX seconds or None.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 99 | times = self._data.get('time', {}) |
| 100 | return times.get('last_run') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 101 | |
| 102 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 103 | def collect_puppet_summary(): |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 104 | """Send Puppet run summary metrics.""" |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 105 | try: |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 106 | with open(LAST_RUN_FILE) as f: |
| 107 | summary = _PuppetRunSummary(f) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 108 | except Exception as e: |
Allen Li | 867d458 | 2017-05-24 18:00:43 -0700 | [diff] [blame] | 109 | logger.warning(u'Error loading Puppet run summary: %s', e) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 110 | else: |
Allen Li | 2b3a93c | 2016-11-01 12:27:21 -0700 | [diff] [blame] | 111 | _config_version_metric.set(summary.config_version) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 112 | _puppet_version_metric.set(str(summary.puppet_version)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 113 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 114 | for key, value in summary.events.iteritems(): |
| 115 | _events_metric.set(value, {'result': key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 116 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 117 | for key, value in summary.resources.iteritems(): |
| 118 | _resources_metric.set(value, {'action': key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 119 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 120 | for key, value in summary.times.iteritems(): |
| 121 | _times_metric.set(value, {'step': key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 122 | |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 123 | if summary.last_run_time is not None: |
| 124 | _age_metric.set(time.time() - summary.last_run_time) |