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