Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2016 The ChromiumOS Authors |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 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 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 9 | import logging |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 10 | import time |
| 11 | |
Mike Frysinger | cb56b64 | 2019-08-25 15:33:08 -0400 | [diff] [blame] | 12 | import yaml # pylint: disable=import-error |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 13 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 14 | from chromite.lib import metrics |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 15 | |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 16 | |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 17 | logger = logging.getLogger(__name__) |
| 18 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | LAST_RUN_FILE = "/var/lib/cros_puppet/state/last_run_summary.yaml" |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 20 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 21 | _config_version_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | "puppet/version/config", |
| 23 | description="The version of the puppet configuration." |
| 24 | " By default this is the time that the configuration was parsed", |
| 25 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 26 | _puppet_version_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | "puppet/version/puppet", description="Version of puppet client installed." |
| 28 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 29 | _events_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [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", |
| 33 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 34 | _resources_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | "puppet/resources", |
| 36 | description="Number of resources known by the puppet client in its last" |
| 37 | " run", |
| 38 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 39 | _times_metric = metrics.FloatMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | "puppet/times", |
| 41 | description="Time taken to perform various parts of the last puppet run", |
| 42 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 43 | _age_metric = metrics.FloatMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | "puppet/age", description="Time since last run" |
| 45 | ) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 46 | |
| 47 | |
Allen Li | a07466d | 2016-12-16 16:57:50 -0800 | [diff] [blame] | 48 | class _PuppetRunSummary(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | """Puppet run summary information.""" |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 50 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | def __init__(self, f): |
| 52 | """Instantiate instance. |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | Args: |
| 55 | f: file object to read summary from |
| 56 | """ |
| 57 | self._data = yaml.safe_load(f) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | @property |
| 60 | def _versions(self): |
| 61 | """Return mapping of version information.""" |
| 62 | return self._data.get("version", {}) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | @property |
| 65 | def config_version(self): |
| 66 | """Return config version as int.""" |
| 67 | return self._versions.get("config", -1) |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | @property |
| 70 | def puppet_version(self): |
| 71 | """Return Puppet version as string.""" |
| 72 | return self._versions.get("puppet", "") |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | @property |
| 75 | def events(self): |
| 76 | """Return mapping of events information.""" |
| 77 | events = self._data.get("events", {}) |
| 78 | events.pop("total", None) |
| 79 | return events |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | @property |
| 82 | def resources(self): |
| 83 | """Return mapping of resources information.""" |
| 84 | resources = self._data.get("resources", {}) |
| 85 | total = resources.pop("total", 0) |
| 86 | resources["other"] = max(0, total - sum(resources.values())) |
| 87 | return resources |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 88 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | @property |
| 90 | def times(self): |
| 91 | """Return mapping of time information.""" |
| 92 | times = self._data.get("time", {}).copy() |
| 93 | times.pop("last_run", None) |
| 94 | total = times.pop("total", 0) |
| 95 | times["other"] = max(0, total - sum(times.values())) |
| 96 | return times |
Allen Li | fdfa661 | 2016-10-25 16:10:37 -0700 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | @property |
| 99 | def last_run_time(self): |
| 100 | """Return last run time as UNIX seconds or None.""" |
| 101 | times = self._data.get("time", {}) |
| 102 | return times.get("last_run") |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 103 | |
| 104 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 105 | def collect_puppet_summary(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | """Send Puppet run summary metrics.""" |
| 107 | try: |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame^] | 108 | with open(LAST_RUN_FILE, encoding="utf-8") as f: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | summary = _PuppetRunSummary(f) |
| 110 | except Exception as e: |
| 111 | logger.warning("Error loading Puppet run summary: %s", e) |
| 112 | else: |
| 113 | _config_version_metric.set(summary.config_version) |
| 114 | _puppet_version_metric.set(str(summary.puppet_version)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | for key, value in summary.events.items(): |
| 117 | _events_metric.set(value, {"result": key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 118 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | for key, value in summary.resources.items(): |
| 120 | _resources_metric.set(value, {"action": key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 121 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | for key, value in summary.times.items(): |
| 123 | _times_metric.set(value, {"step": key}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | if summary.last_run_time is not None: |
| 126 | _age_metric.set(time.time() - summary.last_run_time) |