forked from lusis/sensu_influxdb_handler
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinfluxdb.rb
159 lines (130 loc) · 4.87 KB
/
influxdb.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'em-http-request'
require 'eventmachine'
require 'multi_json'
module Sensu::Extension
class InfluxDB < Handler
def name
definition[:name]
end
def definition
{
type: 'extension',
name: 'influxdb'
}
end
def description
'Outputs metrics to InfluxDB'
end
def post_init()
@influx_conf = parse_settings
logger.info("InfluxDB extension initialiazed using #{@influx_conf['protocol'] }://#{ @influx_conf['host'] }:#{ @influx_conf['port'] } - Defaults : db=#{@influx_conf['database']} precision=#{@influx_conf['time_precision']}")
@buffer = {}
@flush_timer = EventMachine::PeriodicTimer.new(@influx_conf['buffer_max_age'].to_i) do
unless buffer_size == 0
logger.debug("InfluxDB cache age > #{@influx_conf['buffer_max_age']} : forcing flush")
flush_buffer
end
end
logger.info("InfluxDB write buffer initiliazed : buffer flushed every #{@influx_conf['buffer_max_size']} points OR every #{@influx_conf['buffer_max_age']} seconds) ")
end
def run(event_data)
event = parse_event(event_data)
# init event and check data
body = []
client = event[:client][:name]
event[:check][:influxdb][:database] ||= @influx_conf['database']
event[:check][:time_precision] ||= @influx_conf['time_precision']
event[:check][:output].split(/\n/).each do |line|
key, value, time = line.split(/\s+/)
values = "value=#{value.to_f}"
if event[:check][:duration]
values += ",duration=#{event[:check][:duration].to_f}"
end
if @influx_conf['strip_metric'] == 'host'
key = slice_host(key, client)
elsif @influx_conf['strip_metric']
key.gsub!(/^.*#{@influx_conf['strip_metric']}\.(.*$)/, '\1')
end
# Avoid things break down due to comma in key name
key.gsub!(',', '\,')
key.gsub!(/\s/, '\ ')
key.gsub!('"', '\"')
key.gsub!("\\"){ "\\\\" }
# This will merge : default conf tags < check embedded tags < sensu client/host tag
tags = @influx_conf['tags'].merge(event[:check][:influxdb][:tags]).merge({'host' => client})
tags.each do |tag, val|
key += ",#{tag}=#{val}"
end
@buffer[event[:check][:influxdb][:database]] ||= {}
@buffer[event[:check][:influxdb][:database]][event[:check][:time_precision]] ||= []
@buffer[event[:check][:influxdb][:database]][event[:check][:time_precision]].push([key, values, time.to_i].join(' '))
flush_buffer if buffer_size >= @influx_conf['buffer_max_size']
end
yield('', 0)
end
def stop
logger.info('Flushing InfluxDB buffer before exiting')
flush_buffer
true
end
private
def flush_buffer
@flush_timer.cancel
logger.debug('Flushing InfluxDB buffer')
@buffer.each do |db, tp|
tp.each do |p, points|
logger.debug("Sending #{ points.length } points to #{ db } InfluxDB database with precision=#{ p }")
EventMachine::HttpRequest.new("#{ @influx_conf['protocol'] }://#{ @influx_conf['host'] }:#{ @influx_conf['port'] }/write?db=#{ db }&precision=#{ p }&u=#{ @influx_conf['username'] }&p=#{ @influx_conf['password'] }").post :body => points.join("\n")
end
logger.debug("Cleaning buffer for db #{ db }")
@buffer[db] = {}
end
end
def buffer_size
sum = @buffer.map { |_db, tp| tp.map { |_p, points| points.length}.inject(:+) }.inject(:+)
return sum || 0
end
def parse_event(event_data)
begin
event = MultiJson.load(event_data)
# default values
# n, u, ms, s, m, and h (default community plugins use standard epoch date)
event[:check][:time_precision] ||= nil
event[:check][:influxdb] ||= {}
event[:check][:influxdb][:tags] ||= {}
event[:check][:influxdb][:database] ||= nil
rescue => e
logger.error("Failed to parse event data: #{e}")
end
return event
end
def parse_settings()
begin
settings = @settings['influxdb']
# default values
settings['tags'] ||= {}
settings['use_ssl'] ||= false
settings['time_precision'] ||= 's'
settings['protocol'] = settings['use_ssl'] ? 'https' : 'http'
settings['buffer_max_size'] ||= 500
settings['buffer_max_age'] ||= 6 # seconds
settings['port'] ||= 8086
rescue => e
logger.error("Failed to parse InfluxDB settings #{e}")
end
return settings
end
def slice_host(slice, prefix)
prefix.chars.zip(slice.chars).each do |char1, char2|
break if char1 != char2
slice.slice!(char1)
end
slice.slice!('.') if slice.chars.first == '.'
return slice
end
def logger
Sensu::Logger.get
end
end
end