summaryrefslogtreecommitdiffstats
path: root/lib/puppet/application/inspect.rb
blob: 6737128aac41533a2eabfa1827aa78f9a7fd6bb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
124
125
126
127
128
129
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'puppet/application'

class Puppet::Application::Inspect < Puppet::Application

  should_parse_config
  run_mode :agent

  option("--debug","-d")
  option("--verbose","-v")

  option("--logdest LOGDEST", "-l") do |arg|
    begin
      Puppet::Util::Log.newdestination(arg)
      options[:logset] = true
    rescue => detail
      $stderr.puts detail.to_s
    end
  end

  def help
    <<-HELP

puppet-inspect(8) -- Send an inspection report
========

SYNOPSIS
--------

Prepares and submits an inspection report to the puppet master.


USAGE
-----
puppet inspect [--archive_files] [--archive_file_server]


DESCRIPTION
-----------

This command uses the cached catalog from the previous run of 'puppet
agent' to determine which attributes of which resources have been
marked as auditable with the 'audit' metaparameter. It then examines
the current state of the system, writes the state of the specified
resource attributes to a report, and submits the report to the puppet
master.

Puppet inspect does not run as a daemon, and must be run manually or
from cron.


OPTIONS
-------

Any configuration setting which is valid in the configuration file is
also a valid long argument, e.g. '--server=master.domain.com'. See the
configuration file documentation at
http://docs.puppetlabs.com/references/latest/configuration.html for
the full list of acceptable settings.

* --archive_files:
  During an inspect run, whether to archive files whose contents are audited to
  a file bucket.

* --archive_file_server:
  During an inspect run, the file bucket server to archive files to if
  archive_files is set.  The default value is '$server'.


AUTHOR
------

Puppet Labs


COPYRIGHT
---------
Copyright (c) 2011 Puppet Labs, LLC Licensed under the Apache 2.0 License

    HELP
  end

  def setup
    exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?

    raise "Inspect requires reporting to be enabled. Set report=true in puppet.conf to enable reporting." unless Puppet[:report]

    @report = Puppet::Transaction::Report.new("inspect")

    Puppet::Util::Log.newdestination(@report)
    Puppet::Util::Log.newdestination(:console) unless options[:logset]

    Signal.trap(:INT) do
      $stderr.puts "Exiting"
      exit(1)
    end

    if options[:debug]
      Puppet::Util::Log.level = :debug
    elsif options[:verbose]
      Puppet::Util::Log.level = :info
    end

    Puppet::Transaction::Report.indirection.terminus_class = :rest
    Puppet::Resource::Catalog.indirection.terminus_class = :yaml
  end

  def preinit
    require 'puppet'
    require 'puppet/file_bucket/dipper'
  end

  def run_command
    benchmark(:notice, "Finished inspection") do
      retrieval_starttime = Time.now

      unless catalog = Puppet::Resource::Catalog.indirection.find(Puppet[:certname])
        raise "Could not find catalog for #{Puppet[:certname]}"
      end

      @report.configuration_version = catalog.version

      inspect_starttime = Time.now
      @report.add_times("config_retrieval", inspect_starttime - retrieval_starttime)

      if Puppet[:archive_files]
        dipper = Puppet::FileBucket::Dipper.new(:Server => Puppet[:archive_file_server])
      end

      catalog.to_ral.resources.each do |ral_resource|
        audited_attributes = ral_resource[:audit]
        next unless audited_attributes

        status = Puppet::Resource::Status.new(ral_resource)

        begin
          audited_resource = ral_resource.to_resource
        rescue StandardError => detail
          puts detail.backtrace if Puppet[:trace]
          ral_resource.err "Could not inspect #{ral_resource}; skipping: #{detail}"
          audited_attributes.each do |name|
            event = ral_resource.event(
                                       :property => name,
                                       :status   => "failure",
                                       :audited  => true,
                                       :message  => "failed to inspect #{name}"
                                       )
            status.add_event(event)
          end
        else
          audited_attributes.each do |name|
            next if audited_resource[name].nil?
            # Skip :absent properties of :absent resources. Really, it would be nicer if the RAL returned nil for those, but it doesn't. ~JW
            if name == :ensure or audited_resource[:ensure] != :absent or audited_resource[name] != :absent
              event = ral_resource.event(
                                         :previous_value => audited_resource[name],
                                         :property       => name,
                                         :status         => "audit",
                                         :audited        => true,
                                         :message        => "inspected value is #{audited_resource[name].inspect}"
                                         )
              status.add_event(event)
            end
          end
        end
        if Puppet[:archive_files] and ral_resource.type == :file and audited_attributes.include?(:content)
          path = ral_resource[:path]
          if File.readable?(path)
            begin
              dipper.backup(path)
            rescue StandardError => detail
              Puppet.warning detail
            end
          end
        end
        @report.add_resource_status(status)
      end

      finishtime = Time.now
      @report.add_times("inspect", finishtime - inspect_starttime)
      @report.finalize_report

      begin
        Puppet::Transaction::Report.indirection.save(@report)
      rescue => detail
        puts detail.backtrace if Puppet[:trace]
        Puppet.err "Could not send report: #{detail}"
      end
    end
  end
end