blob: 47614c099fe44cd86b0eb58d8b35a280b68c4083 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Allen Liec5beb32016-09-08 15:31:41 -07002# Copyright 2016 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Allen Liec5beb32016-09-08 15:31:41 -07006"""Puppet metrics"""
7
Allen Li13bdf0c2017-03-02 15:18:16 -08008from __future__ import absolute_import
Allen Liec5beb32016-09-08 15:31:41 -07009from __future__ import print_function
10
11import time
12
13import yaml
14
Allen Liec5beb32016-09-08 15:31:41 -070015from chromite.lib import cros_logging as logging
Allen Lia9c6e802017-07-11 15:42:47 -070016from chromite.lib import metrics
Allen Liec5beb32016-09-08 15:31:41 -070017
Allen Li22989bd2017-07-12 10:34:37 -070018
Allen Li79317bb2016-12-16 18:25:07 -080019logger = logging.getLogger(__name__)
20
Allen Lid4678722017-05-22 12:25:20 -070021LAST_RUN_FILE = '/var/lib/cros_puppet/state/last_run_summary.yaml'
Allen Liec5beb32016-09-08 15:31:41 -070022
Allen Lia9c6e802017-07-11 15:42:47 -070023_config_version_metric = metrics.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070024 'puppet/version/config',
25 description='The version of the puppet configuration.'
26 ' By default this is the time that the configuration was parsed')
Allen Lia9c6e802017-07-11 15:42:47 -070027_puppet_version_metric = metrics.StringMetric(
Allen Liec5beb32016-09-08 15:31:41 -070028 'puppet/version/puppet',
29 description='Version of puppet client installed.')
Allen Lia9c6e802017-07-11 15:42:47 -070030_events_metric = metrics.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070031 'puppet/events',
32 description='Number of changes the puppet client made to the system in its'
33 ' last run, by success or failure')
Allen Lia9c6e802017-07-11 15:42:47 -070034_resources_metric = metrics.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070035 'puppet/resources',
36 description='Number of resources known by the puppet client in its last'
37 ' run')
Allen Lia9c6e802017-07-11 15:42:47 -070038_times_metric = metrics.FloatMetric(
Allen Liec5beb32016-09-08 15:31:41 -070039 'puppet/times',
Allen Li22989bd2017-07-12 10:34:37 -070040 description='Time taken to perform various parts of the last puppet run')
Allen Lia9c6e802017-07-11 15:42:47 -070041_age_metric = metrics.FloatMetric(
Allen Lifdfa6612016-10-25 16:10:37 -070042 'puppet/age',
Allen Li22989bd2017-07-12 10:34:37 -070043 description='Time since last run')
Allen Liec5beb32016-09-08 15:31:41 -070044
45
Allen Lia07466d2016-12-16 16:57:50 -080046class _PuppetRunSummary(object):
Allen Lifdfa6612016-10-25 16:10:37 -070047 """Puppet run summary information."""
48
Allen Lid7a679c2017-07-13 15:16:08 -070049 def __init__(self, f):
50 """Instantiate instance.
51
52 Args:
53 f: file object to read summary from
54 """
55 self._data = yaml.safe_load(f)
Allen Lifdfa6612016-10-25 16:10:37 -070056
57 @property
Allen Lid7a679c2017-07-13 15:16:08 -070058 def _versions(self):
Allen Lifdfa6612016-10-25 16:10:37 -070059 """Return mapping of version information."""
60 return self._data.get('version', {})
61
62 @property
63 def config_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070064 """Return config version as int."""
Allen Lid7a679c2017-07-13 15:16:08 -070065 return self._versions.get('config', -1)
Allen Lifdfa6612016-10-25 16:10:37 -070066
67 @property
68 def puppet_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070069 """Return Puppet version as string."""
Allen Lid7a679c2017-07-13 15:16:08 -070070 return self._versions.get('puppet', '')
Allen Lifdfa6612016-10-25 16:10:37 -070071
72 @property
73 def events(self):
74 """Return mapping of events information."""
Allen Lid7a679c2017-07-13 15:16:08 -070075 events = self._data.get('events', {})
76 events.pop('total', None)
77 return events
Allen Lifdfa6612016-10-25 16:10:37 -070078
79 @property
80 def resources(self):
81 """Return mapping of resources information."""
Allen Lid7a679c2017-07-13 15:16:08 -070082 resources = self._data.get('resources', {})
83 total = resources.pop('total', 0)
Mike Frysinger0bdbc102019-06-13 15:27:29 -040084 resources['other'] = max(0, total - sum(resources.values()))
Allen Lid7a679c2017-07-13 15:16:08 -070085 return resources
Allen Lifdfa6612016-10-25 16:10:37 -070086
87 @property
88 def times(self):
89 """Return mapping of time information."""
Allen Lif8d67982017-10-09 18:45:59 -070090 times = self._data.get('time', {}).copy()
Allen Lid7a679c2017-07-13 15:16:08 -070091 times.pop('last_run', None)
Allen Lid0ab2ca2017-11-22 11:25:37 -080092 total = times.pop('total', 0)
Mike Frysinger0bdbc102019-06-13 15:27:29 -040093 times['other'] = max(0, total - sum(times.values()))
Allen Lid7a679c2017-07-13 15:16:08 -070094 return times
Allen Lifdfa6612016-10-25 16:10:37 -070095
96 @property
97 def last_run_time(self):
98 """Return last run time as UNIX seconds or None."""
Allen Lid7a679c2017-07-13 15:16:08 -070099 times = self._data.get('time', {})
100 return times.get('last_run')
Allen Liec5beb32016-09-08 15:31:41 -0700101
102
Allen Li45ae8392017-03-02 14:19:35 -0800103def collect_puppet_summary():
Allen Lifdfa6612016-10-25 16:10:37 -0700104 """Send Puppet run summary metrics."""
Allen Liec5beb32016-09-08 15:31:41 -0700105 try:
Allen Lid7a679c2017-07-13 15:16:08 -0700106 with open(LAST_RUN_FILE) as f:
107 summary = _PuppetRunSummary(f)
Allen Lifdfa6612016-10-25 16:10:37 -0700108 except Exception as e:
Allen Li867d4582017-05-24 18:00:43 -0700109 logger.warning(u'Error loading Puppet run summary: %s', e)
Allen Lifdfa6612016-10-25 16:10:37 -0700110 else:
Allen Li2b3a93c2016-11-01 12:27:21 -0700111 _config_version_metric.set(summary.config_version)
Allen Lifdfa6612016-10-25 16:10:37 -0700112 _puppet_version_metric.set(str(summary.puppet_version))
Allen Liec5beb32016-09-08 15:31:41 -0700113
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400114 for key, value in summary.events.items():
Allen Lifdfa6612016-10-25 16:10:37 -0700115 _events_metric.set(value, {'result': key})
Allen Liec5beb32016-09-08 15:31:41 -0700116
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400117 for key, value in summary.resources.items():
Allen Lifdfa6612016-10-25 16:10:37 -0700118 _resources_metric.set(value, {'action': key})
Allen Liec5beb32016-09-08 15:31:41 -0700119
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400120 for key, value in summary.times.items():
Allen Lifdfa6612016-10-25 16:10:37 -0700121 _times_metric.set(value, {'step': key})
Allen Liec5beb32016-09-08 15:31:41 -0700122
Allen Lifdfa6612016-10-25 16:10:37 -0700123 if summary.last_run_time is not None:
124 _age_metric.set(time.time() - summary.last_run_time)