Class: Mysqlcollector::Influxdb

Inherits:
Object
  • Object
show all
Defined in:
lib/mysqlcollector/influxdb.rb

Instance Method Summary collapse

Constructor Details

#initializeInfluxdb

Returns a new instance of Influxdb.



5
6
7
8
9
10
11
# File 'lib/mysqlcollector/influxdb.rb', line 5

def initialize
  @influx = InfluxDB::Client.new(host:     $config[:influx_host],
                                 database: $config[:influx_database],
                                 username: $config[:influx_user],
                                 password: $config[:influx_password],
                                 retry:    10)
end

Instance Method Details

#data(type, metrics, instance) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mysqlcollector/influxdb.rb', line 22

def data(type, metrics, instance)
  # Validate a variable metrics has values on array?
  return nil if type.nil?
  return nil if instance.nil?
  return nil if metrics.nil?
  return nil if metrics.count == 0
  return nil unless metrics.is_a?(Array)

  # Send value:
  values = []
  metrics.each do |metric, value|
    if value.is_number?
      values << {
        series: type,
        values: { value: Float(value) },
        tags: { metric: metric,
                instance: instance }
      }

      Mysqlcollector::Log.new.debug("InfluxDB Values: #{values.last}");
    end
  end
  values
end

#send(type, metrics) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/mysqlcollector/influxdb.rb', line 47

def send(type, metrics)
  metrics = vacuum(metrics)
  metrics = data(type, metrics, $config[:alias])

  return nil unless metrics.is_a?(Array)

  @influx.write_points(metrics)
rescue Exception => error
  Mysqlcollector::Log.new.error(error.message)
end

#vacuum(values) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/mysqlcollector/influxdb.rb', line 13

def vacuum(values)
  # Validate it is array and have values?
  return nil if values.nil?
  return nil unless values.is_a?(Array)

  # Remove empty variable on array of array:
  values.reject{ |a| a.all? {|e| e.nil? || e.strip.empty? }}
end