blob: 1b8ce214a1531ef20c86502ae1be289fb264f67e [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
5# Copyright (c) 2015 The Chromium Authors. All rights reserved.
6# Use of this source code is governed by a BSD-style license that can be
7# found in the LICENSE file.
8
9"""Puppet metrics"""
10
11from __future__ import print_function
12
13import time
14
15import yaml
16
17from infra_libs import ts_mon
18from chromite.lib import cros_logging as logging
19
20
21config_version = ts_mon.GaugeMetric(
22 'puppet/version/config',
23 description='The version of the puppet configuration.'
24 ' By default this is the time that the configuration was parsed')
25puppet_version = ts_mon.StringMetric(
26 'puppet/version/puppet',
27 description='Version of puppet client installed.')
28events = ts_mon.GaugeMetric(
29 'puppet/events',
30 description='Number of changes the puppet client made to the system in its'
31 ' last run, by success or failure')
32resources = ts_mon.GaugeMetric(
33 'puppet/resources',
34 description='Number of resources known by the puppet client in its last'
35 ' run')
36times = ts_mon.FloatMetric(
37 'puppet/times',
38 description='Time taken to perform various parts of the last puppet run',
39 units=ts_mon.MetricsDataUnits.SECONDS)
40age = ts_mon.FloatMetric('puppet/age',
41 description='Time since last run',
42 units=ts_mon.MetricsDataUnits.SECONDS)
43
44
45_LAST_RUN_FILE = '/var/lib/puppet_last_run_summary.yaml'
46
47
48def get_puppet_summary(time_fn=time.time):
49 path = _LAST_RUN_FILE
50
51 try:
52 with open(path) as fh:
53 data = yaml.safe_load(fh)
54 except IOError:
55 # This is fine - the system probably isn't managed by puppet.
56 return
57 except yaml.YAMLError:
58 # This is less fine - the file exists but is invalid.
59 logging.exception('Failed to read puppet lastrunfile %s', path)
60 return
61
62 if not isinstance(data, dict):
63 return
64
65 try:
66 config_version.set(data['version']['config'])
67 except ts_mon.MonitoringInvalidValueTypeError:
68 # https://crbug.com/581749
69 logging.exception('lastrunfile contains invalid "config" value. '
70 'Please fix Puppet.')
71 except KeyError:
72 logging.warning('version/config not found in %s', path)
73
74 try:
75 puppet_version.set(data['version']['puppet'])
76 except ts_mon.MonitoringInvalidValueTypeError:
77 # https://crbug.com/581749
78 logging.exception('lastrunfile contains invalid puppet version. '
79 'Please fix Puppet.')
80 except KeyError:
81 logging.warning('version/puppet not found in %s', path)
82
83 try:
84 for key, value in data['events'].iteritems():
85 if key != 'total':
86 events.set(value, {'result': key})
87 except KeyError:
88 logging.warning('events not found in %s', path)
89
90 try:
91 for key, value in data['resources'].iteritems():
92 resources.set(value, {'action': key})
93 except KeyError:
94 logging.warning('resources not found in %s', path)
95
96 try:
97 for key, value in data['time'].iteritems():
98 if key == 'last_run':
99 age.set(time_fn() - value)
100 elif key != 'total':
101 times.set(value, {'step': key})
102 except KeyError:
103 logging.warning('time not found in %s', path)