blob: d2fd32259734305e4aeb682327f03d4c90b56dfa [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2016 The ChromiumOS Authors
Allen Liec5beb32016-09-08 15:31:41 -07002# 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
Chris McDonald59650c32021-07-20 15:29:28 -06009import logging
Allen Liec5beb32016-09-08 15:31:41 -070010import time
11
Mike Frysingercb56b642019-08-25 15:33:08 -040012import yaml # pylint: disable=import-error
Allen Liec5beb32016-09-08 15:31:41 -070013
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
Alex Klein1699fab2022-09-08 08:46:06 -060019LAST_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(
Alex Klein1699fab2022-09-08 08:46:06 -060022 "puppet/version/config",
Trent Apted66736d82023-05-25 10:38:28 +100023 description=(
24 "The version of the puppet configuration."
25 " By default this is the time that the configuration was parsed"
26 ),
Alex Klein1699fab2022-09-08 08:46:06 -060027)
Allen Lia9c6e802017-07-11 15:42:47 -070028_puppet_version_metric = metrics.StringMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060029 "puppet/version/puppet", description="Version of puppet client installed."
30)
Allen Lia9c6e802017-07-11 15:42:47 -070031_events_metric = metrics.GaugeMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060032 "puppet/events",
Trent Apted66736d82023-05-25 10:38:28 +100033 description=(
34 "Number of changes the puppet client made to the system in its"
35 " last run, by success or failure"
36 ),
Alex Klein1699fab2022-09-08 08:46:06 -060037)
Allen Lia9c6e802017-07-11 15:42:47 -070038_resources_metric = metrics.GaugeMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060039 "puppet/resources",
Trent Apted66736d82023-05-25 10:38:28 +100040 description=(
41 "Number of resources known by the puppet client in its last run"
42 ),
Alex Klein1699fab2022-09-08 08:46:06 -060043)
Allen Lia9c6e802017-07-11 15:42:47 -070044_times_metric = metrics.FloatMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060045 "puppet/times",
46 description="Time taken to perform various parts of the last puppet run",
47)
Allen Lia9c6e802017-07-11 15:42:47 -070048_age_metric = metrics.FloatMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060049 "puppet/age", description="Time since last run"
50)
Allen Liec5beb32016-09-08 15:31:41 -070051
52
Allen Lia07466d2016-12-16 16:57:50 -080053class _PuppetRunSummary(object):
Alex Klein1699fab2022-09-08 08:46:06 -060054 """Puppet run summary information."""
Allen Lifdfa6612016-10-25 16:10:37 -070055
Alex Klein1699fab2022-09-08 08:46:06 -060056 def __init__(self, f):
57 """Instantiate instance.
Allen Lid7a679c2017-07-13 15:16:08 -070058
Alex Klein1699fab2022-09-08 08:46:06 -060059 Args:
Trent Apted66736d82023-05-25 10:38:28 +100060 f: file object to read summary from
Alex Klein1699fab2022-09-08 08:46:06 -060061 """
62 self._data = yaml.safe_load(f)
Allen Lifdfa6612016-10-25 16:10:37 -070063
Alex Klein1699fab2022-09-08 08:46:06 -060064 @property
65 def _versions(self):
66 """Return mapping of version information."""
67 return self._data.get("version", {})
Allen Lifdfa6612016-10-25 16:10:37 -070068
Alex Klein1699fab2022-09-08 08:46:06 -060069 @property
70 def config_version(self):
71 """Return config version as int."""
72 return self._versions.get("config", -1)
Allen Lifdfa6612016-10-25 16:10:37 -070073
Alex Klein1699fab2022-09-08 08:46:06 -060074 @property
75 def puppet_version(self):
76 """Return Puppet version as string."""
77 return self._versions.get("puppet", "")
Allen Lifdfa6612016-10-25 16:10:37 -070078
Alex Klein1699fab2022-09-08 08:46:06 -060079 @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 Lifdfa6612016-10-25 16:10:37 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 @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 Lifdfa6612016-10-25 16:10:37 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 @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 Lifdfa6612016-10-25 16:10:37 -0700102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 @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 Liec5beb32016-09-08 15:31:41 -0700108
109
Allen Li45ae8392017-03-02 14:19:35 -0800110def collect_puppet_summary():
Alex Klein1699fab2022-09-08 08:46:06 -0600111 """Send Puppet run summary metrics."""
112 try:
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500113 with open(LAST_RUN_FILE, encoding="utf-8") as f:
Alex Klein1699fab2022-09-08 08:46:06 -0600114 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 Liec5beb32016-09-08 15:31:41 -0700120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 for key, value in summary.events.items():
122 _events_metric.set(value, {"result": key})
Allen Liec5beb32016-09-08 15:31:41 -0700123
Alex Klein1699fab2022-09-08 08:46:06 -0600124 for key, value in summary.resources.items():
125 _resources_metric.set(value, {"action": key})
Allen Liec5beb32016-09-08 15:31:41 -0700126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 for key, value in summary.times.items():
128 _times_metric.set(value, {"step": key})
Allen Liec5beb32016-09-08 15:31:41 -0700129
Alex Klein1699fab2022-09-08 08:46:06 -0600130 if summary.last_run_time is not None:
131 _age_metric.set(time.time() - summary.last_run_time)