blob: ba7a7ac11ac4a7db138dd8ad9b76c437c28dd153 [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
Allen Li13bdf0c2017-03-02 15:18:16 -08007from __future__ import absolute_import
Allen Liec5beb32016-09-08 15:31:41 -07008from __future__ import print_function
Allen Li13bdf0c2017-03-02 15:18:16 -08009from __future__ import unicode_literals
Allen Liec5beb32016-09-08 15:31:41 -070010
11import time
12
13import yaml
14
Allen Liec5beb32016-09-08 15:31:41 -070015from chromite.lib import cros_logging as logging
Allen Lifdfa6612016-10-25 16:10:37 -070016from infra_libs import ts_mon
Allen Liec5beb32016-09-08 15:31:41 -070017
Allen Li79317bb2016-12-16 18:25:07 -080018logger = logging.getLogger(__name__)
19
Allen Lifdfa6612016-10-25 16:10:37 -070020LAST_RUN_FILE = '/var/lib/puppet/state/last_run_summary.yaml'
Allen Liec5beb32016-09-08 15:31:41 -070021
Allen Lifdfa6612016-10-25 16:10:37 -070022_config_version_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070023 'puppet/version/config',
24 description='The version of the puppet configuration.'
25 ' By default this is the time that the configuration was parsed')
Allen Lifdfa6612016-10-25 16:10:37 -070026_puppet_version_metric = ts_mon.StringMetric(
Allen Liec5beb32016-09-08 15:31:41 -070027 'puppet/version/puppet',
28 description='Version of puppet client installed.')
Allen Lifdfa6612016-10-25 16:10:37 -070029_events_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070030 'puppet/events',
31 description='Number of changes the puppet client made to the system in its'
32 ' last run, by success or failure')
Allen Lifdfa6612016-10-25 16:10:37 -070033_resources_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070034 'puppet/resources',
35 description='Number of resources known by the puppet client in its last'
36 ' run')
Allen Lifdfa6612016-10-25 16:10:37 -070037_times_metric = ts_mon.FloatMetric(
Allen Liec5beb32016-09-08 15:31:41 -070038 'puppet/times',
39 description='Time taken to perform various parts of the last puppet run',
40 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Lifdfa6612016-10-25 16:10:37 -070041_age_metric = ts_mon.FloatMetric(
42 'puppet/age',
43 description='Time since last run',
44 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Liec5beb32016-09-08 15:31:41 -070045
46
Allen Lia07466d2016-12-16 16:57:50 -080047class _PuppetRunSummary(object):
Allen Lifdfa6612016-10-25 16:10:37 -070048 """Puppet run summary information."""
49
50 def __init__(self, summary_file):
51 self.filename = summary_file
52 with open(self.filename) as file_:
53 self._data = yaml.safe_load(file_)
54
55 @property
56 def versions(self):
57 """Return mapping of version information."""
58 return self._data.get('version', {})
59
60 @property
61 def config_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070062 """Return config version as int."""
63 return self.versions.get('config', -1)
Allen Lifdfa6612016-10-25 16:10:37 -070064
65 @property
66 def puppet_version(self):
Allen Li2b3a93c2016-11-01 12:27:21 -070067 """Return Puppet version as string."""
68 return self.versions.get('puppet', '')
Allen Lifdfa6612016-10-25 16:10:37 -070069
70 @property
71 def events(self):
72 """Return mapping of events information."""
73 return self._data.get('events', {})
74
75 @property
76 def resources(self):
77 """Return mapping of resources information."""
78 return self._data.get('resources', {})
79
80 @property
81 def times(self):
82 """Return mapping of time information."""
83 return self._data.get(time, {})
84
85 @property
86 def last_run_time(self):
87 """Return last run time as UNIX seconds or None."""
88 return self.times.get('last_run')
Allen Liec5beb32016-09-08 15:31:41 -070089
90
Allen Li45ae8392017-03-02 14:19:35 -080091def collect_puppet_summary():
Allen Lifdfa6612016-10-25 16:10:37 -070092 """Send Puppet run summary metrics."""
Allen Liec5beb32016-09-08 15:31:41 -070093 try:
Allen Lia07466d2016-12-16 16:57:50 -080094 summary = _PuppetRunSummary(LAST_RUN_FILE)
Allen Lifdfa6612016-10-25 16:10:37 -070095 except Exception as e:
Allen Li79317bb2016-12-16 18:25:07 -080096 logger.warning('Error loading Puppet run summary: %s', e)
Allen Lifdfa6612016-10-25 16:10:37 -070097 else:
Allen Li2b3a93c2016-11-01 12:27:21 -070098 _config_version_metric.set(summary.config_version)
Allen Lifdfa6612016-10-25 16:10:37 -070099 _puppet_version_metric.set(str(summary.puppet_version))
Allen Liec5beb32016-09-08 15:31:41 -0700100
Allen Lifdfa6612016-10-25 16:10:37 -0700101 for key, value in summary.events.iteritems():
102 _events_metric.set(value, {'result': key})
Allen Liec5beb32016-09-08 15:31:41 -0700103
Allen Lifdfa6612016-10-25 16:10:37 -0700104 for key, value in summary.resources.iteritems():
105 _resources_metric.set(value, {'action': key})
Allen Liec5beb32016-09-08 15:31:41 -0700106
Allen Lifdfa6612016-10-25 16:10:37 -0700107 for key, value in summary.times.iteritems():
108 _times_metric.set(value, {'step': key})
Allen Liec5beb32016-09-08 15:31:41 -0700109
Allen Lifdfa6612016-10-25 16:10:37 -0700110 if summary.last_run_time is not None:
111 _age_metric.set(time.time() - summary.last_run_time)