blob: 307347669aca1b0f0741bade371e4d8562eea14b [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 -07008
9import time
10
Mike Frysingercb56b642019-08-25 15:33:08 -040011import yaml # pylint: disable=import-error
Allen Liec5beb32016-09-08 15:31:41 -070012
Allen Liec5beb32016-09-08 15:31:41 -070013from chromite.lib import cros_logging as logging
Allen Lia9c6e802017-07-11 15:42:47 -070014from chromite.lib import metrics
Allen Liec5beb32016-09-08 15:31:41 -070015
Allen Li22989bd2017-07-12 10:34:37 -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 Lia9c6e802017-07-11 15:42:47 -070021_config_version_metric = metrics.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 Lia9c6e802017-07-11 15:42:47 -070025_puppet_version_metric = metrics.StringMetric(
Allen Liec5beb32016-09-08 15:31:41 -070026 'puppet/version/puppet',
27 description='Version of puppet client installed.')
Allen Lia9c6e802017-07-11 15:42:47 -070028_events_metric = metrics.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 Lia9c6e802017-07-11 15:42:47 -070032_resources_metric = metrics.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 Lia9c6e802017-07-11 15:42:47 -070036_times_metric = metrics.FloatMetric(
Allen Liec5beb32016-09-08 15:31:41 -070037 'puppet/times',
Allen Li22989bd2017-07-12 10:34:37 -070038 description='Time taken to perform various parts of the last puppet run')
Allen Lia9c6e802017-07-11 15:42:47 -070039_age_metric = metrics.FloatMetric(
Allen Lifdfa6612016-10-25 16:10:37 -070040 'puppet/age',
Allen Li22989bd2017-07-12 10:34:37 -070041 description='Time since last run')
Allen Liec5beb32016-09-08 15:31:41 -070042
43
Allen Lia07466d2016-12-16 16:57:50 -080044class _PuppetRunSummary(object):
Allen Lifdfa6612016-10-25 16:10:37 -070045 """Puppet run summary information."""
46
Allen Lid7a679c2017-07-13 15:16:08 -070047 def __init__(self, f):
48 """Instantiate instance.
49
50 Args:
51 f: file object to read summary from
52 """
53 self._data = yaml.safe_load(f)
Allen Lifdfa6612016-10-25 16:10:37 -070054
55 @property
Allen Lid7a679c2017-07-13 15:16:08 -070056 def _versions(self):
Allen Lifdfa6612016-10-25 16:10:37 -070057 """Return mapping of version information."""
58 return self._data.get('version', {})
59
60 @property
61 def config_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070062 """Return config version as int."""
Allen Lid7a679c2017-07-13 15:16:08 -070063 return self._versions.get('config', -1)
Allen Lifdfa6612016-10-25 16:10:37 -070064
65 @property
66 def puppet_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070067 """Return Puppet version as string."""
Allen Lid7a679c2017-07-13 15:16:08 -070068 return self._versions.get('puppet', '')
Allen Lifdfa6612016-10-25 16:10:37 -070069
70 @property
71 def events(self):
72 """Return mapping of events information."""
Allen Lid7a679c2017-07-13 15:16:08 -070073 events = self._data.get('events', {})
74 events.pop('total', None)
75 return events
Allen Lifdfa6612016-10-25 16:10:37 -070076
77 @property
78 def resources(self):
79 """Return mapping of resources information."""
Allen Lid7a679c2017-07-13 15:16:08 -070080 resources = self._data.get('resources', {})
81 total = resources.pop('total', 0)
Mike Frysinger0bdbc102019-06-13 15:27:29 -040082 resources['other'] = max(0, total - sum(resources.values()))
Allen Lid7a679c2017-07-13 15:16:08 -070083 return resources
Allen Lifdfa6612016-10-25 16:10:37 -070084
85 @property
86 def times(self):
87 """Return mapping of time information."""
Allen Lif8d67982017-10-09 18:45:59 -070088 times = self._data.get('time', {}).copy()
Allen Lid7a679c2017-07-13 15:16:08 -070089 times.pop('last_run', None)
Allen Lid0ab2ca2017-11-22 11:25:37 -080090 total = times.pop('total', 0)
Mike Frysinger0bdbc102019-06-13 15:27:29 -040091 times['other'] = max(0, total - sum(times.values()))
Allen Lid7a679c2017-07-13 15:16:08 -070092 return times
Allen Lifdfa6612016-10-25 16:10:37 -070093
94 @property
95 def last_run_time(self):
96 """Return last run time as UNIX seconds or None."""
Allen Lid7a679c2017-07-13 15:16:08 -070097 times = self._data.get('time', {})
98 return times.get('last_run')
Allen Liec5beb32016-09-08 15:31:41 -070099
100
Allen Li45ae8392017-03-02 14:19:35 -0800101def collect_puppet_summary():
Allen Lifdfa6612016-10-25 16:10:37 -0700102 """Send Puppet run summary metrics."""
Allen Liec5beb32016-09-08 15:31:41 -0700103 try:
Allen Lid7a679c2017-07-13 15:16:08 -0700104 with open(LAST_RUN_FILE) as f:
105 summary = _PuppetRunSummary(f)
Allen Lifdfa6612016-10-25 16:10:37 -0700106 except Exception as e:
Allen Li867d4582017-05-24 18:00:43 -0700107 logger.warning(u'Error loading Puppet run summary: %s', e)
Allen Lifdfa6612016-10-25 16:10:37 -0700108 else:
Allen Li2b3a93c2016-11-01 12:27:21 -0700109 _config_version_metric.set(summary.config_version)
Allen Lifdfa6612016-10-25 16:10:37 -0700110 _puppet_version_metric.set(str(summary.puppet_version))
Allen Liec5beb32016-09-08 15:31:41 -0700111
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400112 for key, value in summary.events.items():
Allen Lifdfa6612016-10-25 16:10:37 -0700113 _events_metric.set(value, {'result': key})
Allen Liec5beb32016-09-08 15:31:41 -0700114
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400115 for key, value in summary.resources.items():
Allen Lifdfa6612016-10-25 16:10:37 -0700116 _resources_metric.set(value, {'action': key})
Allen Liec5beb32016-09-08 15:31:41 -0700117
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400118 for key, value in summary.times.items():
Allen Lifdfa6612016-10-25 16:10:37 -0700119 _times_metric.set(value, {'step': key})
Allen Liec5beb32016-09-08 15:31:41 -0700120
Allen Lifdfa6612016-10-25 16:10:37 -0700121 if summary.last_run_time is not None:
122 _age_metric.set(time.time() - summary.last_run_time)