bin/metrics-influxdb.rb in sensu-plugins-influxdb-0.0.3 vs bin/metrics-influxdb.rb in sensu-plugins-influxdb-0.0.4

- old
+ new

@@ -24,39 +24,46 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. # require 'sensu-handler' +gem 'influxdb', '>=0.2.0' require 'influxdb' # # Sensu To Influxdb # class SensuToInfluxDB < Sensu::Handler def filter; end def handle - influxdb_server = settings['influxdb']['server'] - influxdb_port = settings['influxdb']['port'] - influxdb_user = settings['influxdb']['username'] - influxdb_pass = settings['influxdb']['password'] - influxdb_db = settings['influxdb']['database'] + opts = settings['influxdb'].each_with_object({}) do |(k, v), sym| + sym[k.to_sym] = v + end - influxdb_data = InfluxDB::Client.new influxdb_db, host: influxdb_server, - username: influxdb_user, - password: influxdb_pass, - port: influxdb_port, - server: influxdb_server - mydata = [] - @event['check']['output'].each do |metric| + database = opts[:database] + + influxdb_data = InfluxDB::Client.new database, opts + + client_name = @event['client']['name'] + metric_name = @event['check']['name'] + + metric_raw = @event['check']['output'] + + data = [] + metric_raw.split("\n").each do |metric| m = metric.split next unless m.count == 3 key = m[0].split('.', 2)[1] key.gsub!('.', '_') value = m[1].to_f - mydata = { host: @event['client']['name'], value: value, - ip: @event['client']['address'] - } - influxdb_data.write_point(key, mydata) + time = m[2] + point = { series: key, + tags: { host: client_name, metric: metric_name }, + values: { value: value }, + timestamp: time + } + data.push(point) end + influxdb_data.write_points(data) end end