Sha256: 9a120b5b2829c184a8d65c74cb63d53901bcec49650ba7a649d32c3a1ce6f618

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

module Munin
  module Parser  
    # Parse a version request
    #
    def parse_version(line)
      if line =~  /^munins node on/
        line.split.last
      else
        raise Munin::InvalidResponse, "Invalid version response"
      end
    end
    
    # Process response
    #
    def process_data(lines)  
      data = {}
      lines.each do |line|
        line = line.split
        key = line.first.split('.value').first
        data[key] = line.last
      end
      data
    end
    
    # Parse 'config' request
    #
    def parse_config(data)
      config = {'graph' => {}, 'metrics' => {}}
      data.each do |l|
        if l =~ /^graph_/
          key_name, value = l.scan(/^graph_([\w]+)\s(.*)/).flatten
          config['graph'][key_name] = value
        elsif l =~ /^[a-z]+\./
          matches = l.scan(/^([a-z\d\-\_]+)\.([a-z\d\-\_]+)\s(.*)/).flatten
          config['metrics'][matches[0]] ||= {}
          config['metrics'][matches[0]][matches[1]] = matches[2]
        end
      end
      
      # Now, lets process the args hash
      if config['graph'].key?('args')
        config['graph']['args'] = parse_config_args(config['graph']['args'])   
      end
      
      config
    end
    
    # Parse 'fetch' request
    #
    def parse_fetch(data)
      process_data(data)
    end
    
    # Detect error from output
    #
    def parse_error(lines)
      if lines.size == 1
        case lines.first
          when '# Unknown service' then raise UnknownService
          when '# Bad exit'        then raise BadExit
        end
      end
    end
    
    # Parse configuration arguments
    #
    def parse_config_args(args)
      result = {}
      args.scan(/--?([a-z\-\_]+)\s([\d]+)\s?/).each do |arg|
        result[arg.first] = arg.last
      end
      {'raw' => args, 'parsed' => result}
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
munin-ruby-0.2.4 lib/munin-ruby/parser.rb
munin-ruby-0.2.3 lib/munin-ruby/parser.rb
munin-ruby-0.2.2 lib/munin-ruby/parser.rb
munin-ruby-0.2.1 lib/munin-ruby/parser.rb