bin/metrics-influxdb.rb in sensu-plugins-influxdb-0.0.5 vs bin/metrics-influxdb.rb in sensu-plugins-influxdb-0.0.7

- old
+ new

@@ -31,39 +31,61 @@ # # Sensu To Influxdb # class SensuToInfluxDB < Sensu::Handler + option :config, + description: 'Configuration information to use', + short: '-c CONFIG', + long: '--config CONFIG', + default: 'influxdb' + def filter; end - def handle - opts = settings['influxdb'].each_with_object({}) do |(k, v), sym| - sym[k.to_sym] = v - end + def create_point(series, value, time) + point = { series: series, + tags: { host: @event['client']['name'], metric: @event['check']['name'] }, + values: { value: value }, + timestamp: time + } + point[:tags].merge!(@event['check']['tags']) unless @event['check']['tags'].nil? + point + end - 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'] - + def parse_output data = [] + metric_raw = @event['check']['output'] metric_raw.split("\n").each do |metric| m = metric.split next unless m.count == 3 key = m[0].split('.', 2)[1] - key.gsub!('.', '_') + key.tr!('.', '_') value = m[1].to_f time = m[2] - point = { series: key, - tags: { host: client_name, metric: metric_name }, - values: { value: value }, - timestamp: time - } + point = create_point(key, value, time) data.push(point) end + data + end + + def check_status + data = [] + data.push(create_point(@event['check']['name'], @event['check']['status'], @event['client']['timestamp'])) + end + + def handle + opts = settings[config[:config]].each_with_object({}) do |(k, v), sym| + sym[k.to_sym] = v + end + database = opts[:database] + + influxdb_data = InfluxDB::Client.new database, opts + influxdb_data.create_database(database) # Ensure the database exists + + data = if opts[:status] == false || opts[:status].nil? + parse_output + else + check_status + end influxdb_data.write_points(data) end end