This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathinflux.rb
57 lines (48 loc) · 1.71 KB
/
influx.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'influxdb'
require 'timeout'
module Sensu::Extension
class Influx < Handler
def name
'influx'
end
def description
'outputs metrics to InfluxDB'
end
def post_init
@influxdb = InfluxDB::Client.new settings['influx']['database'], :host => settings['influx']['host'], :port => settings['influx']['port'], :username => settings['influx']['user'], :password => settings['influx']['password']
@timeout = @settings['influx']['timeout'] || 15
end
def run(event)
begin
event = Oj.load(event)
host = event[:client][:name]
series = event[:check][:name]
timestamp = event[:check][:issued]
duration = event[:check][:duration]
output = event[:check][:output]
rescue => e
@logger.error("InfluxDB: Error setting up event object - #{e.backtrace.to_s}")
end
begin
points = []
output.split(/\n/).each do |line|
@logger.debug("Parsing line: #{line}")
k,v,t = line.split(/\s+/)
v = v.match('\.').nil? ? Integer(v) : Float(v) rescue v.to_s
k.gsub!(/^.*#{@settings['influx']['strip_metric']}\.(.*$)/, '\1') if @settings['influx']['strip_metric']
points << {:time => t.to_f, :host => host, :metric => k, :value => v}
end
rescue => e
@logger.error("InfluxDB: Error parsing output lines - #{e.backtrace.to_s}")
@logger.error("InfluxDB: #{output}")
end
begin
@influxdb.write_point(series, points, true)
rescue => e
@logger.error("InfluxDB: Error posting event - #{e.backtrace.to_s}")
end
yield("InfluxDB: Handler finished", 0)
end
end
end