class Object

Public Instance Methods

do_nothing() click to toggle source

Literally, do nothing.

# File reportreader.rb, line 15
def do_nothing() 
end
extract_logs(logs) click to toggle source

Simpler extract method which, given Puppet::Transaction::Report.logs output, returns strings if within a certain log level

projects.puppetlabs.com/projects/puppet/wiki/Report_Format_3

# File reportreader.rb, line 248
def extract_logs(logs)
        @@string = ""

        logs.each { |v|
                case v.level
                        when (:info or :notice or :warning or :err or :alert or :emerg or :crit)
                                @@string << "\n\t" + v.time.to_s + ": " + v.message 
                        else # /:debug/ or /:info/ 
                                do_nothing
                end #case v.level

        } # logs.each { |v|

        return @@string

end
extract_resource_statuses( resObj, tagFilter=nil, tagReverseFilter=nil ) click to toggle source

Puppet::Resource::Status is the output from a Puppet::Transaction::Report.resource_statuses[1]. For each resource,

  • compare tags against tagFilter and tagReverseFilter

  • eval against $customMethods

Return @@string of resource attributes if above queries are true.

rubydoc.info/github/puppetlabs/puppet/master/Puppet/Resource/Status

projects.puppetlabs.com/projects/puppet/wiki/Report_Format_3

# File reportreader.rb, line 130
def extract_resource_statuses( resObj, tagFilter=nil, tagReverseFilter=nil )

        @@string = ""

        resObj.each { |v| 
                @@skip = false

                if $customMethods != []
                        @@skip = true

                        $customMethods.each { |method|
                                if method[1]
                                        @@skip = false if eval("v[1].#{method[0]} #{method[1]}")
                                else
                                        @@skip = false if v[1].send(method[0]) 
                                end # if method[1]

                        } # $customMethods.each { |method|

                end # if $customMethods != []

                ##
                # Not included attributes: 
                # title
                # resource_type
                 
                if $verbose
                        resources = %W[
                                resource
                                failed
                                changed
        
                                time
                                evaluation_time
                                file
                                line
        
                                status
                                current_values
                                default_log_level
                                node
                                source_description
                                change_count
                                out_of_sync_count
                                pending_count
                                compliant_count
                                failed_count
        
                                skipped
                                events
                        ]
                else 
                        resources = %W[
                                resource
                                failed
                                skipped
                                events
                        ]
                end # if $verbose

                @@tagString = ""

                if tagFilter 
                        @@tagMatch = false

                        v[1].tags.each { |tag| 
                                if Regexp.new(tagFilter).match(tag)
                                        @@tagMatch = true
                                        @@tagString << "\n\t\t\t#{tag}"
                                end # if Regexp.new(tagFilter).match(tag)

                        } # v[1].tags.each { |tag| 

                elsif tagReverseFilter
                        @@tagMatch = true

                        v[1].tags.each { |tag| 
                                if Regexp.new(tagReverseFilter).match(tag)
                                        @@tagMatch = false
                                end # unless Regexp.new(tagReverseFilter).match(tag)

                        } # v[1].tags

                else
                        @@tagMatch  = true # if neither are defined, print it
                end # if tagFilter
                        
                @@resourceString = ""
                if @@tagMatch
                        resources.each { |x|
                                begin
                                        @@resourceString << %Q[\n\t\t#{x}: #{v[1].send( x.to_sym) }] if v[1].send( x.to_sym) 
                                rescue NoMethodError # basically, do nothing
                                end
                        } # resources.each { |x|
                end # if @@tagMatch

                if @@resourceString != "" and @@skip == false
                        @@string << %Q[\n\t#{v[0]} ] 
                        @@string << @@resourceString
                        @@string << "\n\t\tMatching tags: #{@@tagString.to_s}" if $verbose
                end # if @@resourceString != "" and @@skip == false

        } # aResource.each

        return @@string 

end
filter_by_time( fileList, hours=12 ) click to toggle source

Given array of filenames (fileList), and float of hours, Return: [ [files within timescope], [files outside of timescope] ]

# File reportreader.rb, line 70
def filter_by_time( fileList, hours=12 )
        arr = []
        olderarr = []

        fileList.each { |f|
                if (Time.now - (hours*60*60)).gmtime.strftime("%Y%m%d%H%M") <  f[%r[0-9]{12}.yaml/]
                        arr.push(f)
                else 
                        olderarr.push(f)
                end # if (Time.now - (hours*60*60)).gmtime.strftime("%Y%m%d%H%M") <  f[/[0-9]{12}.yaml/]
        } #fileList.each { |f|

        arr2 = arr.sort { |x,y| y <=> x } # reverse sort puts present on top of past, though this means hostnames are backwards as well. 

        return arr2,olderarr

end
parse_custom_methods(cMParam) click to toggle source

Given customMethods argument, return array of arrays: [ [method_name, method_arguments] ]

# File reportreader.rb, line 373
def parse_custom_methods(cMParam) 
        @arr = cMParam.split(',')

        @arr.each { |method|
                @meth = []

                if %r\s/.match(method)
                        @meth[0] = $`
                        @meth[1] = $'
                else 
                        @meth[0] = method
                end # if /\s/.match(method)

                $customMethods.delete(@meth)
                $customMethods.push(@meth)

        } # @arr.each { |method|

end
print_host_changes( anArr, tagFilter=nil, tagReverseFilter=nil) click to toggle source

Given filelist, read reports, then print matching reports and resources to STDOUT

rubydoc.info/github/puppetlabs/puppet/master/Puppet/Transaction/Report

print_host_logs( anArr, tagFilter=nil, tagReverseFilter=nil ) click to toggle source

Given filelist, read reports, then print matching logs and resources to STDOUT

process_files( aDir ) click to toggle source

Recurse through aDir for yaml files, unless it is “zz_archive”. Return: [ [filelist], [[directory,last report in directory]] ]

# File reportreader.rb, line 26
def process_files( aDir )
        unless FileTest.directory?(aDir)
                puts "Error. Invalid input for report directory: #{aDir}."
                exit
        end # unless

        @@files = []
        @@directories = []

        ##
        # The actual bit that does the recursing through the dir tree

        def process_files_recurse(aDir) 
                @@tempFiles = []

                Dir.foreach( aDir ) { |f|
                        myPath = "#{aDir}\/#{f}"
                        if FileTest.directory?(myPath) and f != '.' and f != '..' and f != 'zz_archive'
                                 process_files_recurse(myPath)
                        else
                                @@tempFiles.push(myPath) if f.match(%r\.yaml\Z/)
                        end
                } # Find.find( aDir ) { |f|

                sortedTempFiles = @@tempFiles.sort 
                
                @@files.push(sortedTempFiles)
                @@directories.push([aDir[%r[^\/]+$/],sortedTempFiles[sortedTempFiles.length - 1]])

        end # def process_files_recurse (aDir) 

        process_files_recurse(aDir)

        files = @@files.sort.flatten
        directories = @@directories[0..(@@directories.length - 2)].sort_by { |e| e.nil? ? 'z' : e[0] } 

        return files,directories

end
process_files_recurse(aDir) click to toggle source

The actual bit that does the recursing through the dir tree

# File reportreader.rb, line 38
def process_files_recurse(aDir) 
        @@tempFiles = []

        Dir.foreach( aDir ) { |f|
                myPath = "#{aDir}\/#{f}"
                if FileTest.directory?(myPath) and f != '.' and f != '..' and f != 'zz_archive'
                         process_files_recurse(myPath)
                else
                        @@tempFiles.push(myPath) if f.match(%r\.yaml\Z/)
                end
        } # Find.find( aDir ) { |f|

        sortedTempFiles = @@tempFiles.sort 
        
        @@files.push(sortedTempFiles)
        @@directories.push([aDir[%r[^\/]+$/],sortedTempFiles[sortedTempFiles.length - 1]])

end
read_report( fileList ) click to toggle source

Given an array of files (fileList), Return {report attributes => value}

# File reportreader.rb, line 93
def read_report( fileList )
        File.open(fileList) { |g|
                if b = YAML.load(g) then
                                strings = %W[host time logs metrics resource_statuses configuration_version
                                        report_format puppet_version kind status pending_count complaint_count
                                        failed_count]
                                @@hsh = {}

                                strings.each { |string|
                                        begin # strings.each rescue block
                                        @@hsh[string.to_sym] =  b.send(string.to_sym) 

                                        rescue NoMethodError # basically, do_nothing.
                                        end # strings.each rescue block
                                } # strings.each { |string|

                end # if b = YAML.load(g) then

        } # File.open(f) { |g|

        return @@hsh

end