lib/one2influx/influx.rb
# Checks whether InfluxDB connection is possible, database @db exists
# and @retention_policy exists.
# @return [boolean]
Checks whether InfluxDB connection is possible, database @db exists
and @retention_policy exists.
Checks whether InfluxDB connection is possible database @db exists and @retention_policy exists .
def db_exists?
    uri = URI('/query')
    query = {:q => "SHOW RETENTION POLICIES ON #{@db}"}
    uri.query = URI.encode_www_form(query)
    req = Net::HTTP::Get.new(uri.to_s)
    response =  make_request(req)
    if response.nil?
      return false
    end
    if response.code.to_i == 401
      $LOG.error "Unauthorized user '#{@user}', unable to verify connection" +
                     ' to InfluxDB.'
      return false
    elsif response.code.to_i != 200
      $LOG.error 'Failed to store data to InfluxDB. Received HTTP code ' +
                     "#{response.code}, body: #{response.body}"
      return false
    end
    begin
      response = JSON.parse(response.body)
    rescue JSON::ParserError => e
      $LOG.error 'Unable to parse InfluxDB response, while verifying ' +
                     "connection to database '#{@db}': #{e}. " +
                     "Received: #{response.body}"
      return false
    end
    begin
      if response['results'][0].has_key? 'error'
        $LOG.error "Unable to verify connection to InfluxDB database '#{@db}'" +
                       " with message: #{response['results'][0]['error']}"
        return false
      end
      response['results'][0]['series'][0]['values'].each do |policy|
        if policy[0] == @retention_policy
          $LOG.info "Connection to InfluxDB database '#{@db}' with retention " +
                        "policy '#{@retention_policy}' verified."
          return true
        end
      end
    rescue Exception
      $LOG.error "Unable to verify connection to InfluxDB database '#{@db}'. " +
                     'Invalid InfluxDB response format.'
      return false
    end
    $LOG.error "InfluxDB database '#{@db}' does not contain supplied " +
                   "retention policy '#{@retention_policy}'."
    return false
  end
def db_exists? uri = URI ( '/query' ) query = { :q => "SHOW RETENTION POLICIES ON #{@db}" } uri . query = URI . encode_www_form ( query ) req = Net :: HTTP :: Get . new ( uri . to_s ) response = make_request ( req ) if response . nil? return false end if response . code . to_i == 401 $LOG . error "Unauthorized user '#{@user}', unable to verify connection" + ' to InfluxDB.' return false elsif response . code . to_i != 200 $LOG . error 'Failed to store data to InfluxDB. Received HTTP code ' + "#{response.code}, body: #{response.body}" return false end begin response = JSON . parse ( response . body ) rescue JSON :: ParserError => e $LOG . error 'Unable to parse InfluxDB response, while verifying ' + "connection to database '#{@db}': #{e}. " + "Received: #{response.body}" return false end begin if response [ 'results' ] [ 0 ] . has_key? 'error' $LOG . error "Unable to verify connection to InfluxDB database '#{@db}'" + " with message: #{response['results'][0]['error']}" return false end response [ 'results' ] [ 0 ] [ 'series' ] [ 0 ] [ 'values' ] . each do | policy | if policy [ 0 ] == @retention_policy $LOG . info "Connection to InfluxDB database '#{@db}' with retention " + "policy '#{@retention_policy}' verified." return true end end rescue Exception $LOG . error "Unable to verify connection to InfluxDB database '#{@db}'. " + 'Invalid InfluxDB response format.' return false end $LOG . error "InfluxDB database '#{@db}' does not contain supplied " + "retention policy '#{@retention_policy}'." return false end
Checks whether InfluxDB connection is possible, database @db exists
and @retention_policy exists.
Checks whether InfluxDB connection is possible database @db exists and @retention_policy exists .
# Was request successful? # Check for invalid HTTP response codes # Parse response # Handle index out of bounds etc. exceptions
None
boolean