blob: 9ce08820781b36a29ef2bec42e412ff78137b435 [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
7from __future__ import print_function
8
9import time
10
11import yaml
12
Allen Liec5beb32016-09-08 15:31:41 -070013from chromite.lib import cros_logging as logging
Allen Lifdfa6612016-10-25 16:10:37 -070014from infra_libs import ts_mon
Allen Liec5beb32016-09-08 15:31:41 -070015
Allen Li79317bb2016-12-16 18:25:07 -080016logger = logging.getLogger(__name__)
17
Allen Lifdfa6612016-10-25 16:10:37 -070018LAST_RUN_FILE = '/var/lib/puppet/state/last_run_summary.yaml'
Allen Liec5beb32016-09-08 15:31:41 -070019
Allen Lifdfa6612016-10-25 16:10:37 -070020_config_version_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070021 'puppet/version/config',
22 description='The version of the puppet configuration.'
23 ' By default this is the time that the configuration was parsed')
Allen Lifdfa6612016-10-25 16:10:37 -070024_puppet_version_metric = ts_mon.StringMetric(
Allen Liec5beb32016-09-08 15:31:41 -070025 'puppet/version/puppet',
26 description='Version of puppet client installed.')
Allen Lifdfa6612016-10-25 16:10:37 -070027_events_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070028 'puppet/events',
29 description='Number of changes the puppet client made to the system in its'
30 ' last run, by success or failure')
Allen Lifdfa6612016-10-25 16:10:37 -070031_resources_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070032 'puppet/resources',
33 description='Number of resources known by the puppet client in its last'
34 ' run')
Allen Lifdfa6612016-10-25 16:10:37 -070035_times_metric = ts_mon.FloatMetric(
Allen Liec5beb32016-09-08 15:31:41 -070036 'puppet/times',
37 description='Time taken to perform various parts of the last puppet run',
38 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Lifdfa6612016-10-25 16:10:37 -070039_age_metric = ts_mon.FloatMetric(
40 'puppet/age',
41 description='Time since last run',
42 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Liec5beb32016-09-08 15:31:41 -070043
44
Allen Lia07466d2016-12-16 16:57:50 -080045class _PuppetRunSummary(object):
Allen Lifdfa6612016-10-25 16:10:37 -070046 """Puppet run summary information."""
47
48 def __init__(self, summary_file):
49 self.filename = summary_file
50 with open(self.filename) as file_:
51 self._data = yaml.safe_load(file_)
52
53 @property
54 def versions(self):
55 """Return mapping of version information."""
56 return self._data.get('version', {})
57
58 @property
59 def config_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070060 """Return config version as int."""
61 return self.versions.get('config', -1)
Allen Lifdfa6612016-10-25 16:10:37 -070062
63 @property
64 def puppet_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070065 """Return Puppet version as string."""
66 return self.versions.get('puppet', '')
Allen Lifdfa6612016-10-25 16:10:37 -070067
68 @property
69 def events(self):
70 """Return mapping of events information."""
71 return self._data.get('events', {})
72
73 @property
74 def resources(self):
75 """Return mapping of resources information."""
76 return self._data.get('resources', {})
77
78 @property
79 def times(self):
80 """Return mapping of time information."""
81 return self._data.get(time, {})
82
83 @property
84 def last_run_time(self):
85 """Return last run time as UNIX seconds or None."""
86 return self.times.get('last_run')
Allen Liec5beb32016-09-08 15:31:41 -070087
88
Allen Lifdfa6612016-10-25 16:10:37 -070089def get_puppet_summary():
90 """Send Puppet run summary metrics."""
Allen Liec5beb32016-09-08 15:31:41 -070091 try:
Allen Lia07466d2016-12-16 16:57:50 -080092 summary = _PuppetRunSummary(LAST_RUN_FILE)
Allen Lifdfa6612016-10-25 16:10:37 -070093 except Exception as e:
Allen Li79317bb2016-12-16 18:25:07 -080094 logger.warning('Error loading Puppet run summary: %s', e)
Allen Lifdfa6612016-10-25 16:10:37 -070095 else:
Allen Li2b3a93c2016-11-01 12:27:21 -070096 _config_version_metric.set(summary.config_version)
Allen Lifdfa6612016-10-25 16:10:37 -070097 _puppet_version_metric.set(str(summary.puppet_version))
Allen Liec5beb32016-09-08 15:31:41 -070098
Allen Lifdfa6612016-10-25 16:10:37 -070099 for key, value in summary.events.iteritems():
100 _events_metric.set(value, {'result': key})
Allen Liec5beb32016-09-08 15:31:41 -0700101
Allen Lifdfa6612016-10-25 16:10:37 -0700102 for key, value in summary.resources.iteritems():
103 _resources_metric.set(value, {'action': key})
Allen Liec5beb32016-09-08 15:31:41 -0700104
Allen Lifdfa6612016-10-25 16:10:37 -0700105 for key, value in summary.times.iteritems():
106 _times_metric.set(value, {'step': key})
Allen Liec5beb32016-09-08 15:31:41 -0700107
Allen Lifdfa6612016-10-25 16:10:37 -0700108 if summary.last_run_time is not None:
109 _age_metric.set(time.time() - summary.last_run_time)