Class: Puppet::Transaction::Report

Inherits:
Object
  • Object
show all
Extended by:
Indirector
Defined in:
lib/puppet/transaction/report.rb

Overview

A class for reporting what happens on each client. Reports consist of two types of data: Logs and Metrics. Logs are the output that each change produces, and Metrics are all of the numerical data involved in the transaction.

Defined Under Namespace

Classes: Processor, Rest, Yaml

Constant Summary

Constants included from Indirector

Indirector::BadNameRegexp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Indirector

configure_routes, indirects

Constructor Details

#initialize(kind, configuration_version = nil, environment = nil) ⇒ Report

Returns a new instance of Report.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/puppet/transaction/report.rb', line 77

def initialize(kind, configuration_version=nil, environment=nil)
  @metrics = {}
  @logs = []
  @resource_statuses = {}
  @external_times ||= {}
  @host = Puppet[:node_name_value]
  @time = Time.now
  @kind = kind
  @report_format = 3
  @puppet_version = Puppet.version
  @configuration_version = configuration_version
  @environment = environment
  @status = 'failed' # assume failed until the report is finalized
end

Instance Attribute Details

#configuration_versionObject

Returns the value of attribute configuration_version.



13
14
15
# File 'lib/puppet/transaction/report.rb', line 13

def configuration_version
  @configuration_version
end

#environmentObject

Returns the value of attribute environment.



13
14
15
# File 'lib/puppet/transaction/report.rb', line 13

def environment
  @environment
end

#hostObject

Returns the value of attribute host.



13
14
15
# File 'lib/puppet/transaction/report.rb', line 13

def host
  @host
end

#kindObject (readonly)

Returns the value of attribute kind.



14
15
16
# File 'lib/puppet/transaction/report.rb', line 14

def kind
  @kind
end

#logsObject (readonly)

Returns the value of attribute logs.



14
15
16
# File 'lib/puppet/transaction/report.rb', line 14

def logs
  @logs
end

#metricsObject (readonly)

Returns the value of attribute metrics.



14
15
16
# File 'lib/puppet/transaction/report.rb', line 14

def metrics
  @metrics
end

#resource_statusesObject (readonly)

Returns the value of attribute resource_statuses.



14
15
16
# File 'lib/puppet/transaction/report.rb', line 14

def resource_statuses
  @resource_statuses
end

#statusObject (readonly)

Returns the value of attribute status.



14
15
16
# File 'lib/puppet/transaction/report.rb', line 14

def status
  @status
end

#timeObject (readonly)

Returns the value of attribute time.



14
15
16
# File 'lib/puppet/transaction/report.rb', line 14

def time
  @time
end

Class Method Details

.default_formatObject

This is necessary since Marshall doesn’t know how to dump hash with default proc (see below @records)



18
19
20
# File 'lib/puppet/transaction/report.rb', line 18

def self.default_format
  :yaml
end

.from_pson(data) ⇒ Object



22
23
24
25
26
# File 'lib/puppet/transaction/report.rb', line 22

def self.from_pson(data)
  obj = self.allocate
  obj.initialize_from_hash(data)
  obj
end

Instance Method Details

#<<(msg) ⇒ Object



28
29
30
31
# File 'lib/puppet/transaction/report.rb', line 28

def <<(msg)
  @logs << msg
  self
end

#add_metric(name, hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/puppet/transaction/report.rb', line 37

def add_metric(name, hash)
  metric = Puppet::Util::Metric.new(name)

  hash.each do |name, value|
    metric.newvalue(name, value)
  end

  @metrics[metric.name] = metric
  metric
end

#add_resource_status(status) ⇒ Object



48
49
50
# File 'lib/puppet/transaction/report.rb', line 48

def add_resource_status(status)
  @resource_statuses[status.resource] = status
end

#add_times(name, value) ⇒ Object



33
34
35
# File 'lib/puppet/transaction/report.rb', line 33

def add_times(name, value)
  @external_times[name] = value
end

#compute_status(resource_metrics, change_metric) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/puppet/transaction/report.rb', line 52

def compute_status(resource_metrics, change_metric)
  if (resource_metrics["failed"] || 0) > 0
    'failed'
  elsif change_metric > 0
    'changed'
  else
    'unchanged'
  end
end

#exit_statusObject

Based on the contents of this report’s metrics, compute a single number that represents the report. The resulting number is a bitmask where individual bits represent the presence of different metrics.



175
176
177
178
179
180
# File 'lib/puppet/transaction/report.rb', line 175

def exit_status
  status = 0
  status |= 2 if @metrics["changes"]["total"] > 0
  status |= 4 if @metrics["resources"]["failed"] > 0
  status
end

#finalize_reportObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/transaction/report.rb', line 66

def finalize_report
  prune_internal_data

  resource_metrics = add_metric(:resources, calculate_resource_metrics)
  add_metric(:time, calculate_time_metrics)
  change_metric = calculate_change_metric
  add_metric(:changes, {"total" => change_metric})
  add_metric(:events, calculate_event_metrics)
  @status = compute_status(resource_metrics, change_metric)
end

#initialize_from_hash(data) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet/transaction/report.rb', line 92

def initialize_from_hash(data)
  @puppet_version = data['puppet_version']
  @report_format = data['report_format']
  @configuration_version = data['configuration_version']
  @environment = data['environment']
  @status = data['status']
  @host = data['host']
  @time = data['time']
  if @time.is_a? String
    @time = Time.parse(@time)
  end
  @kind = data['kind']

  @metrics = {}
  data['metrics'].each do |name, hash|
    @metrics[name] = Puppet::Util::Metric.from_pson(hash)
  end

  @logs = data['logs'].map do |record|
    Puppet::Util::Log.from_pson(record)
  end

  @resource_statuses = {}
  data['resource_statuses'].map do |record|
    if record[1] == {}
      status = nil
    else
      status = Puppet::Resource::Status.from_pson(record[1])
    end
    @resource_statuses[record[0]] = status
  end
end

#nameObject



125
126
127
# File 'lib/puppet/transaction/report.rb', line 125

def name
  host
end

#prune_internal_dataObject



62
63
64
# File 'lib/puppet/transaction/report.rb', line 62

def prune_internal_data
  resource_statuses.delete_if {|name,res| res.resource_type == 'Whit'}
end

#raw_summaryObject

Provide a raw hash summary of this report.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/puppet/transaction/report.rb', line 157

def raw_summary
  report = { "version" => { "config" => configuration_version, "puppet" => Puppet.version  } }

  @metrics.each do |name, metric|
    key = metric.name.to_s
    report[key] = {}
    metric.values.each do |name, label, value|
      report[key][name.to_s] = value
    end
    report[key]["total"] = 0 unless key == "time" or report[key].include?("total")
  end
  (report["time"] ||= {})["last_run"] = Time.now.tv_sec
  report
end

#summaryObject

Provide a human readable textual summary of this report.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/puppet/transaction/report.rb', line 130

def summary
  report = raw_summary

  ret = ""
  report.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |key|
    ret += "#{Puppet::Util::Metric.labelize(key)}:\n"

    report[key].keys.sort { |a,b|
      # sort by label
      if a == :total
        1
      elsif b == :total
        -1
      else
        report[key][a].to_s <=> report[key][b].to_s
      end
    }.each do |label|
      value = report[key][label]
      next if value == 0
      value = "%0.2f" % value if value.is_a?(Float)
      ret += "   %15s %s\n" % [Puppet::Util::Metric.labelize(label) + ":", value]
    end
  end
  ret
end

#to_yaml_propertiesObject



182
183
184
# File 'lib/puppet/transaction/report.rb', line 182

def to_yaml_properties
  (instance_variables - ["@external_times"])
end