safe_dump_simple_hash_array

in source/code/plugins/oms_common.rb [806:906]


      def safe_dump_simple_hash_array(records)
        msg = nil

        begin
          msg = Yajl.dump(records)
        rescue => error
          OMS::Log.warn_once("Unable to dump to JSON string. #{error}")
          begin
            
            
            records.each do | hash |
              
              hash.each do | key, value |
                
                
                if value.instance_of? String
                  hash[key] = value.encode('utf-8', 'iso-8859-1')
                end
              end
            end

            msg = Yajl.dump(records)
          rescue => error
            
            
            OMS::Log.warn_once("Skipping due to failed encoding for #{records}: #{error}")
          end
        end

        return msg
      end 

      
      
      
      
      
      
      
      
      def start_request(req, secure_http, ignore404 = false, return_entire_response = false)
        
        
        
        begin
          res = nil
          res = secure_http.start { |http|  http.request(req) }
        rescue => e 
          
          raise RetryRequestException, "Net::HTTP.#{req.method.capitalize} raises exception: #{e.class}, '#{e.message}'"
        else
          if res.nil?
            raise RetryRequestException, "Failed to #{req.method} at #{req.to_s} (res=nil)"
          end

          if res.is_a?(Net::HTTPSuccess)
            if return_entire_response
              return res
            else
              return res.body
            end
          end

          if ignore404 and res.code == "404"
            return ''
          end

          if res.code != "200"
            
            res_summary = "(request-id=#{req["X-Request-ID"]}; class=#{res.class.name}; code=#{res.code}; message=#{res.message}; body=#{res.body};)"
            OMS::Log.error_once("HTTP Error: #{res_summary}")
            raise RetryRequestException, "HTTP error: #{res_summary}"
          end

        end 
      end 
    end 

  end 

  class IPcache
    
    def initialize(refresh_interval_seconds)
      @cache = {}
      @cache_lock = Mutex.new
      @refresh_interval_seconds = refresh_interval_seconds
      @condition = ConditionVariable.new
      @thread = Thread.new(&method(:refresh_cache))
    end

    def get_ip(hostname)
      if @cache.has_key?(hostname)
        return @cache[hostname]
      else
        ip = get_ip_from_socket(hostname)
        @cache_lock.synchronize {
          @cache[hostname] = ip
        }
        return ip
      end