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