blob: 163d408049ace961a2fb0e399debd7a1ba0c9791 [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 Lifdfa6612016-10-25 16:10:37 -070016LAST_RUN_FILE = '/var/lib/puppet/state/last_run_summary.yaml'
Allen Liec5beb32016-09-08 15:31:41 -070017
Allen Lifdfa6612016-10-25 16:10:37 -070018_config_version_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070019 'puppet/version/config',
20 description='The version of the puppet configuration.'
21 ' By default this is the time that the configuration was parsed')
Allen Lifdfa6612016-10-25 16:10:37 -070022_puppet_version_metric = ts_mon.StringMetric(
Allen Liec5beb32016-09-08 15:31:41 -070023 'puppet/version/puppet',
24 description='Version of puppet client installed.')
Allen Lifdfa6612016-10-25 16:10:37 -070025_events_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070026 'puppet/events',
27 description='Number of changes the puppet client made to the system in its'
28 ' last run, by success or failure')
Allen Lifdfa6612016-10-25 16:10:37 -070029_resources_metric = ts_mon.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070030 'puppet/resources',
31 description='Number of resources known by the puppet client in its last'
32 ' run')
Allen Lifdfa6612016-10-25 16:10:37 -070033_times_metric = ts_mon.FloatMetric(
Allen Liec5beb32016-09-08 15:31:41 -070034 'puppet/times',
35 description='Time taken to perform various parts of the last puppet run',
36 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Lifdfa6612016-10-25 16:10:37 -070037_age_metric = ts_mon.FloatMetric(
38 'puppet/age',
39 description='Time since last run',
40 units=ts_mon.MetricsDataUnits.SECONDS)
Allen Liec5beb32016-09-08 15:31:41 -070041
42
Allen Lifdfa6612016-10-25 16:10:37 -070043class PuppetRunSummary(object):
44 """Puppet run summary information."""
45
46 def __init__(self, summary_file):
47 self.filename = summary_file
48 with open(self.filename) as file_:
49 self._data = yaml.safe_load(file_)
50
51 @property
52 def versions(self):
53 """Return mapping of version information."""
54 return self._data.get('version', {})
55
56 @property
57 def config_version(self):
58 """Return config version as int or None."""
59 return self.versions.get('config', None)
60
61 @property
62 def puppet_version(self):
63 """Return Puppet version as string or None."""
64 return self.versions.get('puppet', None)
65
66 @property
67 def events(self):
68 """Return mapping of events information."""
69 return self._data.get('events', {})
70
71 @property
72 def resources(self):
73 """Return mapping of resources information."""
74 return self._data.get('resources', {})
75
76 @property
77 def times(self):
78 """Return mapping of time information."""
79 return self._data.get(time, {})
80
81 @property
82 def last_run_time(self):
83 """Return last run time as UNIX seconds or None."""
84 return self.times.get('last_run')
Allen Liec5beb32016-09-08 15:31:41 -070085
86
Allen Lifdfa6612016-10-25 16:10:37 -070087def get_puppet_summary():
88 """Send Puppet run summary metrics."""
Allen Liec5beb32016-09-08 15:31:41 -070089 try:
Allen Lifdfa6612016-10-25 16:10:37 -070090 summary = PuppetRunSummary(LAST_RUN_FILE)
91 except Exception as e:
92 logging.warning('Error loading Puppet run summary: %s', e)
93 else:
94 _config_version_metric.set(str(summary.config_version))
95 _puppet_version_metric.set(str(summary.puppet_version))
Allen Liec5beb32016-09-08 15:31:41 -070096
Allen Lifdfa6612016-10-25 16:10:37 -070097 for key, value in summary.events.iteritems():
98 _events_metric.set(value, {'result': key})
Allen Liec5beb32016-09-08 15:31:41 -070099
Allen Lifdfa6612016-10-25 16:10:37 -0700100 for key, value in summary.resources.iteritems():
101 _resources_metric.set(value, {'action': key})
Allen Liec5beb32016-09-08 15:31:41 -0700102
Allen Lifdfa6612016-10-25 16:10:37 -0700103 for key, value in summary.times.iteritems():
104 _times_metric.set(value, {'step': key})
Allen Liec5beb32016-09-08 15:31:41 -0700105
Allen Lifdfa6612016-10-25 16:10:37 -0700106 if summary.last_run_time is not None:
107 _age_metric.set(time.time() - summary.last_run_time)